2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-07-28 20:02:56 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Spammable do
|
2016-07-28 20:02:56 -04:00
|
|
|
let(:issue) { create(:issue, description: 'Test Desc.') }
|
|
|
|
|
|
|
|
describe 'Associations' do
|
2017-04-25 08:28:55 -04:00
|
|
|
subject { build(:issue) }
|
|
|
|
|
2016-07-28 20:02:56 -04:00
|
|
|
it { is_expected.to have_one(:user_agent_detail).dependent(:destroy) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'ClassMethods' do
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'returns correct attr_spammable' do
|
2016-08-09 13:43:47 -04:00
|
|
|
expect(issue.spammable_text).to eq("#{issue.title}\n#{issue.description}")
|
2016-07-28 20:02:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'InstanceMethods' do
|
2017-02-10 05:41:22 -05:00
|
|
|
let(:issue) { build(:issue, spam: true) }
|
|
|
|
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'is invalid if spam' do
|
2016-08-05 18:10:08 -04:00
|
|
|
expect(issue.valid?).to be_falsey
|
2016-07-28 20:02:56 -04:00
|
|
|
end
|
|
|
|
|
2016-07-30 00:18:32 -04:00
|
|
|
describe '#check_for_spam?' do
|
|
|
|
it 'returns true for public project' do
|
|
|
|
issue.project.update_attribute(:visibility_level, Gitlab::VisibilityLevel::PUBLIC)
|
2017-02-14 14:07:11 -05:00
|
|
|
|
2021-07-20 02:08:37 -04:00
|
|
|
expect(issue.check_for_spam?(user: issue.author)).to eq(true)
|
2016-07-30 00:18:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false for other visibility levels' do
|
2021-07-20 02:08:37 -04:00
|
|
|
expect(issue.check_for_spam?(user: issue.author)).to eq(false)
|
2016-07-30 00:18:32 -04:00
|
|
|
end
|
|
|
|
end
|
2017-02-10 05:41:22 -05:00
|
|
|
|
2020-02-26 04:08:47 -05:00
|
|
|
describe '#invalidate_if_spam' do
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
before do
|
|
|
|
stub_application_setting(recaptcha_enabled: true)
|
|
|
|
end
|
|
|
|
|
2020-02-26 04:08:47 -05:00
|
|
|
context 'when the model is spam' do
|
2020-04-21 11:21:10 -04:00
|
|
|
subject { invalidate_if_spam(is_spam: true) }
|
|
|
|
|
|
|
|
it 'has an error related to spam on the model' do
|
|
|
|
expect(subject.errors.messages[:base]).to match_array /has been discarded/
|
2020-02-26 04:08:47 -05:00
|
|
|
end
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
2020-02-26 04:08:47 -05:00
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
context 'when the model needs recaptcha' do
|
|
|
|
subject { invalidate_if_spam(needs_recaptcha: true) }
|
2020-02-26 04:08:47 -05:00
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
it 'has an error related to spam on the model' do
|
2022-03-03 07:14:02 -05:00
|
|
|
expect(subject.errors.messages[:base]).to match_array /content or solve the/
|
2020-02-26 04:08:47 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
context 'if the model is spam and also needs recaptcha' do
|
|
|
|
subject { invalidate_if_spam(is_spam: true, needs_recaptcha: true) }
|
|
|
|
|
|
|
|
it 'has an error related to spam on the model' do
|
2022-03-03 07:14:02 -05:00
|
|
|
expect(subject.errors.messages[:base]).to match_array /content or solve the/
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
|
|
|
end
|
2020-02-26 04:08:47 -05:00
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
context 'when the model is not spam nor needs recaptcha' do
|
|
|
|
subject { invalidate_if_spam }
|
2020-02-26 04:08:47 -05:00
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
it 'returns no error' do
|
|
|
|
expect(subject.errors.messages[:base]).to be_empty
|
2020-02-26 04:08:47 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
context 'if recaptcha is not enabled and the model needs recaptcha' do
|
|
|
|
before do
|
|
|
|
stub_application_setting(recaptcha_enabled: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { invalidate_if_spam(needs_recaptcha: true) }
|
2020-02-26 04:08:47 -05:00
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
it 'has no errors' do
|
|
|
|
expect(subject.errors.messages[:base]).to match_array /has been discarded/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def invalidate_if_spam(is_spam: false, needs_recaptcha: false)
|
2020-02-26 04:08:47 -05:00
|
|
|
issue.tap do |i|
|
|
|
|
i.spam = is_spam
|
2020-04-21 11:21:10 -04:00
|
|
|
i.needs_recaptcha = needs_recaptcha
|
2020-02-26 04:08:47 -05:00
|
|
|
i.invalidate_if_spam
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
describe 'spam flags' do
|
|
|
|
before do
|
|
|
|
issue.spam = false
|
|
|
|
issue.needs_recaptcha = false
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#spam!' do
|
|
|
|
it 'adds only `spam` flag' do
|
|
|
|
issue.spam!
|
|
|
|
|
|
|
|
expect(issue.spam).to be_truthy
|
|
|
|
expect(issue.needs_recaptcha).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#needs_recaptcha!' do
|
|
|
|
it 'adds `needs_recaptcha` flag' do
|
|
|
|
issue.needs_recaptcha!
|
|
|
|
|
|
|
|
expect(issue.spam).to be_falsey
|
|
|
|
expect(issue.needs_recaptcha).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-28 19:09:17 -05:00
|
|
|
describe '#render_recaptcha?' do
|
|
|
|
before do
|
|
|
|
allow(Gitlab::Recaptcha).to receive(:enabled?) { recaptcha_enabled }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when recaptcha is not enabled' do
|
|
|
|
let(:recaptcha_enabled) { false }
|
|
|
|
|
|
|
|
it 'returns false' do
|
|
|
|
expect(issue.render_recaptcha?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when recaptcha is enabled' do
|
|
|
|
let(:recaptcha_enabled) { true }
|
|
|
|
|
|
|
|
context 'when there are two or more errors' do
|
|
|
|
before do
|
|
|
|
issue.errors.add(:base, 'a spam error')
|
|
|
|
issue.errors.add(:base, 'some other error')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false' do
|
|
|
|
expect(issue.render_recaptcha?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are less than two errors' do
|
|
|
|
before do
|
|
|
|
issue.errors.add(:base, 'a spam error')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when spammable does not need recaptcha' do
|
|
|
|
before do
|
|
|
|
issue.needs_recaptcha = false
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false' do
|
|
|
|
expect(issue.render_recaptcha?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when spammable needs recaptcha' do
|
|
|
|
before do
|
|
|
|
issue.needs_recaptcha!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false' do
|
|
|
|
expect(issue.render_recaptcha?).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
describe '#clear_spam_flags!' do
|
|
|
|
it 'clears spam and recaptcha flags' do
|
|
|
|
issue.spam = true
|
|
|
|
issue.needs_recaptcha = true
|
|
|
|
|
|
|
|
issue.clear_spam_flags!
|
|
|
|
|
|
|
|
expect(issue).not_to be_spam
|
|
|
|
expect(issue.needs_recaptcha).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-10 05:41:22 -05:00
|
|
|
describe '#submittable_as_spam_by?' do
|
|
|
|
let(:admin) { build(:admin) }
|
|
|
|
let(:user) { build(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(issue).to receive(:submittable_as_spam?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'tests if the user can submit spam' do
|
|
|
|
expect(issue.submittable_as_spam_by?(admin)).to be(true)
|
|
|
|
expect(issue.submittable_as_spam_by?(user)).to be(false)
|
|
|
|
expect(issue.submittable_as_spam_by?(nil)).to be_nil
|
|
|
|
end
|
|
|
|
end
|
2016-07-28 20:02:56 -04:00
|
|
|
end
|
|
|
|
end
|