spammable_spec.rb 2.5 KB
Newer Older
1 2
# frozen_string_literal: true

3 4
require 'spec_helper'

S
Sean McGivern 已提交
5
describe Spammable do
6 7 8
  let(:issue) { create(:issue, description: 'Test Desc.') }

  describe 'Associations' do
S
Sean McGivern 已提交
9 10
    subject { build(:issue) }

11 12 13 14
    it { is_expected.to have_one(:user_agent_detail).dependent(:destroy) }
  end

  describe 'ClassMethods' do
15
    it 'returns correct attr_spammable' do
16
      expect(issue.spammable_text).to eq("#{issue.title}\n#{issue.description}")
17 18 19 20
    end
  end

  describe 'InstanceMethods' do
21 22
    let(:issue) { build(:issue, spam: true) }

23
    it 'is invalid if spam' do
24
      expect(issue.valid?).to be_falsey
25 26
    end

27 28 29
    describe '#check_for_spam?' do
      it 'returns true for public project' do
        issue.project.update_attribute(:visibility_level, Gitlab::VisibilityLevel::PUBLIC)
30

31 32 33 34 35 36 37
        expect(issue.check_for_spam?).to eq(true)
      end

      it 'returns false for other visibility levels' do
        expect(issue.check_for_spam?).to eq(false)
      end
    end
38

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    describe '#invalidate_if_spam' do
      using RSpec::Parameterized::TableSyntax

      context 'when the model is spam' do
        where(:recaptcha_enabled, :error) do
          true  | /solve the reCAPTCHA to proceed/
          false | /has been discarded/
        end

        with_them do
          subject { invalidate_if_spam(true, recaptcha_enabled) }

          it 'has an error related to spam on the model' do
            expect(subject.errors.messages[:base]).to match_array error
          end
        end
      end

      context 'when the model is not spam' do
        [true, false].each do |enabled|
          let(:recaptcha_enabled) { enabled }

          subject { invalidate_if_spam(false, recaptcha_enabled) }

          it 'returns no error' do
            expect(subject.errors.messages[:base]).to be_empty
          end
        end
      end

      def invalidate_if_spam(is_spam, recaptcha_enabled)
        stub_application_setting(recaptcha_enabled: recaptcha_enabled)

        issue.tap do |i|
          i.spam = is_spam
          i.invalidate_if_spam
        end
      end
    end

79 80 81 82 83 84 85 86 87 88 89 90 91 92
    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
93 94
  end
end