spammable_spec.rb 1.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
require 'spec_helper'

describe Issue, 'Spammable' do
  let(:issue) { create(:issue, description: 'Test Desc.') }

  describe 'Associations' do
    it { is_expected.to have_one(:user_agent_detail).dependent(:destroy) }
  end

  describe 'ClassMethods' do
    it 'should return correct attr_spammable' do
      expect(issue.send(:spammable_text)).to eq("#{issue.title}\n#{issue.description}")
    end
  end

  describe 'InstanceMethods' do
    it 'should return the correct creator' do
18
      expect(issue.send(:owner).id).to eq(issue.author_id)
19 20 21 22 23 24 25 26 27 28 29
    end

    it 'should be invalid if spam' do
      issue.spam = true
      expect(issue.valid?).to be_truthy
    end

    it 'should be submittable' do
      create(:user_agent_detail, subject_id: issue.id, subject_type: issue.class.to_s)
      expect(issue.can_be_submitted?).to be_truthy
    end
30 31 32 33 34 35 36 37 38 39 40 41 42 43

    describe '#check_for_spam?' do
      before do
        allow_any_instance_of(Gitlab::AkismetHelper).to receive(:akismet_enabled?).and_return(true)
      end
      it 'returns true for public project' do
        issue.project.update_attribute(:visibility_level, Gitlab::VisibilityLevel::PUBLIC)
        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
44 45 46 47
  end

  describe 'AkismetMethods' do
    before do
48
      allow_any_instance_of(Gitlab::AkismetHelper).to receive_messages(is_spam?: true, spam!: nil)
49 50
    end

51
    it { expect(issue.spam_detected?(:mock_env)).to be_truthy }
52 53 54
    it { expect(issue.submit_spam).to be_nil }
  end
end