diff options
Diffstat (limited to 'spec/models/concerns/spammable_spec.rb')
-rw-r--r-- | spec/models/concerns/spammable_spec.rb | 111 |
1 files changed, 100 insertions, 11 deletions
diff --git a/spec/models/concerns/spammable_spec.rb b/spec/models/concerns/spammable_spec.rb index 44cf87aa1c1..8a2fa6675e5 100644 --- a/spec/models/concerns/spammable_spec.rb +++ b/spec/models/concerns/spammable_spec.rb @@ -2,7 +2,23 @@ require 'spec_helper' -RSpec.describe Spammable do +RSpec.describe Spammable, feature_category: :instance_resiliency do + before do + stub_const('SpammableModel', Class.new(ActiveRecord::Base)) + + SpammableModel.class_eval do + self.table_name = 'issues' + + include Spammable + + attr_accessor :other_attr + + attr_spammable :title, spam_title: true + attr_spammable :description, spam_description: true + end + end + + let(:spammable_model) { SpammableModel.new } let(:issue) { create(:issue, description: 'Test Desc.') } describe 'Associations' do @@ -25,6 +41,30 @@ RSpec.describe Spammable do end describe '#check_for_spam?' do + context 'when not overriden' do + subject { spammable_model.check_for_spam? } + + context 'when spammable attributes have changed' do + where(attr: [:title, :description]) + + with_them do + before do + spammable_model.assign_attributes(attr => 'x') + end + + it { is_expected.to eq(true) } + end + end + + context 'when other attributes have changed' do + before do + spammable_model.other_attr = true + end + + it { is_expected.to eq(false) } + end + end + it 'returns true for public project' do issue.project.update_attribute(:visibility_level, Gitlab::VisibilityLevel::PUBLIC) @@ -37,17 +77,39 @@ RSpec.describe Spammable do end describe '#invalidate_if_spam' do - using RSpec::Parameterized::TableSyntax - before do stub_application_setting(recaptcha_enabled: true) end context 'when the model is spam' do - subject { invalidate_if_spam(is_spam: true) } + where(model: [:issue, :merge_request, :snippet, :spammable_model]) - it 'has an error related to spam on the model' do - expect(subject.errors.messages[:base]).to match_array /has been discarded/ + with_them do + subject do + model.to_s.classify.constantize.new.tap do |m| + m.spam! + m.invalidate_if_spam + end + end + + it 'has an error related to spam on the model' do + expect(subject.errors.messages[:base]) + .to match_array /Your #{subject.class.model_name.human.downcase} has been recognized as spam./ + end + end + + context 'when the spammable model is a Note' do + subject do + Note.new.tap do |m| + m.spam! + m.invalidate_if_spam + end + end + + it 'has an error related to spam on the model' do + expect(subject.errors.messages[:base]) + .to match_array /Your comment has been recognized as spam./ + end end end @@ -59,6 +121,18 @@ RSpec.describe Spammable do end end + context 'when the model needs recaptcha but does not support it' do + subject { invalidate_if_spam(needs_recaptcha: true) } + + before do + allow(issue).to receive(:supports_recaptcha?).and_return(false) + end + + it 'has an error that discards the spammable' do + expect(subject.errors.messages[:base]).to match_array /has been recognized as spam/ + end + end + context 'if the model is spam and also needs recaptcha' do subject { invalidate_if_spam(is_spam: true, needs_recaptcha: true) } @@ -83,7 +157,7 @@ RSpec.describe Spammable do subject { invalidate_if_spam(needs_recaptcha: true) } it 'has no errors' do - expect(subject.errors.messages[:base]).to match_array /has been discarded/ + expect(subject.errors.messages[:base]).to match_array /has been recognized as spam/ end end @@ -112,11 +186,26 @@ RSpec.describe Spammable do end describe '#needs_recaptcha!' do - it 'adds `needs_recaptcha` flag' do - issue.needs_recaptcha! + context 'when recaptcha is supported' do + it 'adds `needs_recaptcha` flag' do + issue.needs_recaptcha! - expect(issue.spam).to be_falsey - expect(issue.needs_recaptcha).to be_truthy + expect(issue.spam).to be_falsey + expect(issue.needs_recaptcha).to be_truthy + end + end + + context 'when recaptcha is not supported' do + before do + allow(issue).to receive(:supports_recaptcha?).and_return(false) + end + + it 'marks the object as spam' do + issue.needs_recaptcha! + + expect(issue.spam).to be_truthy + expect(issue.needs_recaptcha).to be_falsey + end end end |