summaryrefslogtreecommitdiff
path: root/spec/services/spam/spam_check_service_spec.rb
blob: 732b64b52a03a685801a418e67dcb9c7a5b10dd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# frozen_string_literal: true

require 'spec_helper'

describe Spam::SpamCheckService do
  let(:fake_ip) { '1.2.3.4' }
  let(:fake_user_agent) { 'fake-user-agent' }
  let(:fake_referrer) { 'fake-http-referrer' }
  let(:env) do
    { 'action_dispatch.remote_ip' => fake_ip,
      'HTTP_USER_AGENT' => fake_user_agent,
      'HTTP_REFERRER' => fake_referrer }
  end
  let(:request) { double(:request, env: env) }

  let_it_be(:project) { create(:project, :public) }
  let_it_be(:user) { create(:user) }
  let_it_be(:issue) { create(:issue, project: project, author: user) }

  before do
    issue.spam = false
  end

  describe '#initialize' do
    subject { described_class.new(spammable: issue, request: request) }

    context 'when the request is nil' do
      let(:request) { nil }

      it 'assembles the options with information from the spammable' do
        aggregate_failures do
          expect(subject.options[:ip_address]).to eq(issue.ip_address)
          expect(subject.options[:user_agent]).to eq(issue.user_agent)
          expect(subject.options.key?(:referrer)).to be_falsey
        end
      end
    end

    context 'when the request is present' do
      let(:request) { double(:request, env: env) }

      it 'assembles the options with information from the spammable' do
        aggregate_failures do
          expect(subject.options[:ip_address]).to eq(fake_ip)
          expect(subject.options[:user_agent]).to eq(fake_user_agent)
          expect(subject.options[:referrer]).to eq(fake_referrer)
        end
      end
    end
  end

  describe '#execute' do
    let(:request) { double(:request, env: env) }

    let_it_be(:existing_spam_log) { create(:spam_log, user: user, recaptcha_verified: false) }

    subject do
      described_service = described_class.new(spammable: issue, request: request)
      described_service.execute(user_id: user.id, api: nil, recaptcha_verified: recaptcha_verified, spam_log_id: existing_spam_log.id)
    end

    context 'when recaptcha was already verified' do
      let(:recaptcha_verified) { true }

      it "updates spam log and doesn't check Akismet" do
        aggregate_failures do
          expect(SpamLog).not_to receive(:create!)
          expect(an_instance_of(described_class)).not_to receive(:check)
        end

        subject
      end

      it 'updates spam log' do
        subject

        expect(existing_spam_log.reload.recaptcha_verified).to be_truthy
      end
    end

    context 'when recaptcha was not verified' do
      let(:recaptcha_verified) { false }

      context 'when spammable attributes have not changed' do
        before do
          issue.closed_at = Time.zone.now

          allow(Spam::AkismetService).to receive(:new).and_return(double(spam?: true))
        end

        it 'returns false' do
          expect(subject).to be_falsey
        end

        it 'does not create a spam log' do
          expect { subject }
            .not_to change { SpamLog.count }
        end
      end

      context 'when spammable attributes have changed' do
        before do
          issue.description = 'SPAM!'
        end

        context 'when indicated as spam by akismet' do
          before do
            allow(Spam::AkismetService).to receive(:new).and_return(double(spam?: true))
          end

          context 'when allow_possible_spam feature flag is false' do
            before do
              stub_feature_flags(allow_possible_spam: false)
            end

            it_behaves_like 'akismet spam'

            it 'checks as spam' do
              subject

              expect(issue.reload.spam).to be_truthy
            end
          end

          context 'when allow_possible_spam feature flag is true' do
            it_behaves_like 'akismet spam'

            it 'does not check as spam' do
              subject

              expect(issue.spam).to be_falsey
            end
          end
        end

        context 'when not indicated as spam by akismet' do
          before do
            allow(Spam::AkismetService).to receive(:new).and_return(double(spam?: false))
          end

          it 'returns false' do
            expect(subject).to be_falsey
          end

          it 'does not create a spam log' do
            expect { subject }
              .not_to change { SpamLog.count }
          end
        end
      end
    end
  end
end