2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-01-27 11:25:39 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe SpamService do
|
2017-02-14 14:07:11 -05:00
|
|
|
describe '#when_recaptcha_verified' do
|
|
|
|
def check_spam(issue, request, recaptcha_verified)
|
|
|
|
described_class.new(issue, request).when_recaptcha_verified(recaptcha_verified) do
|
|
|
|
'yielded'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'yields block when recaptcha was already verified' do
|
|
|
|
issue = build_stubbed(:issue)
|
2017-01-27 11:25:39 -05:00
|
|
|
|
2017-02-14 14:07:11 -05:00
|
|
|
expect(check_spam(issue, nil, true)).to eql('yielded')
|
2017-01-27 11:25:39 -05:00
|
|
|
end
|
|
|
|
|
2017-02-14 14:07:11 -05:00
|
|
|
context 'when recaptcha was not verified' do
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create(:project, :public) }
|
2017-02-14 14:07:11 -05:00
|
|
|
let(:issue) { create(:issue, project: project) }
|
|
|
|
let(:request) { double(:request, env: {}) }
|
2017-01-27 11:25:39 -05:00
|
|
|
|
2017-03-20 22:37:29 -04:00
|
|
|
context 'when spammable attributes have not changed' do
|
|
|
|
before do
|
|
|
|
issue.closed_at = Time.zone.now
|
2017-01-27 11:25:39 -05:00
|
|
|
|
2017-08-24 13:05:02 -04:00
|
|
|
allow(AkismetService).to receive(:new).and_return(double(spam?: true))
|
2017-02-14 14:07:11 -05:00
|
|
|
end
|
2017-01-27 11:25:39 -05:00
|
|
|
|
2017-03-20 22:37:29 -04:00
|
|
|
it 'returns false' do
|
|
|
|
expect(check_spam(issue, request, false)).to be_falsey
|
2017-02-14 14:07:11 -05:00
|
|
|
end
|
2017-01-27 11:25:39 -05:00
|
|
|
|
2017-03-20 22:37:29 -04:00
|
|
|
it 'does not create a spam log' do
|
2017-02-14 14:07:11 -05:00
|
|
|
expect { check_spam(issue, request, false) }
|
2017-03-20 22:37:29 -04:00
|
|
|
.not_to change { SpamLog.count }
|
2017-02-14 14:07:11 -05:00
|
|
|
end
|
2017-03-20 22:37:29 -04:00
|
|
|
end
|
2017-01-27 11:25:39 -05:00
|
|
|
|
2017-03-20 22:37:29 -04:00
|
|
|
context 'when spammable attributes have changed' do
|
|
|
|
before do
|
|
|
|
issue.description = 'SPAM!'
|
2017-02-14 14:07:11 -05:00
|
|
|
end
|
2017-01-27 11:25:39 -05:00
|
|
|
|
2017-03-20 22:37:29 -04:00
|
|
|
context 'when indicated as spam by akismet' do
|
2019-09-24 23:06:21 -04:00
|
|
|
shared_examples 'akismet spam' do
|
|
|
|
it 'doesnt check as spam when request is missing' do
|
|
|
|
check_spam(issue, nil, false)
|
|
|
|
|
|
|
|
expect(issue).not_to be_spam
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a spam log' do
|
|
|
|
expect { check_spam(issue, request, false) }
|
|
|
|
.to log_spam(title: issue.title, description: issue.description, noteable_type: 'Issue')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not yield to the block' do
|
|
|
|
expect(check_spam(issue, request, false))
|
|
|
|
.to eql(SpamLog.last)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-20 22:37:29 -04:00
|
|
|
before do
|
2017-08-24 13:05:02 -04:00
|
|
|
allow(AkismetService).to receive(:new).and_return(double(spam?: true))
|
2017-03-20 22:37:29 -04:00
|
|
|
end
|
2017-02-14 14:07:11 -05:00
|
|
|
|
2019-09-24 23:06:21 -04:00
|
|
|
context 'when allow_possible_spam feature flag is false' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(allow_possible_spam: false)
|
|
|
|
end
|
2017-03-20 22:37:29 -04:00
|
|
|
|
2019-09-24 23:06:21 -04:00
|
|
|
it_behaves_like 'akismet spam'
|
2017-03-20 22:37:29 -04:00
|
|
|
|
2019-09-24 23:06:21 -04:00
|
|
|
it 'checks as spam' do
|
|
|
|
check_spam(issue, request, false)
|
2017-03-20 22:37:29 -04:00
|
|
|
|
2019-09-24 23:06:21 -04:00
|
|
|
expect(issue.spam).to be_truthy
|
|
|
|
end
|
2017-03-20 22:37:29 -04:00
|
|
|
end
|
|
|
|
|
2019-09-24 23:06:21 -04:00
|
|
|
context 'when allow_possible_spam feature flag is true' do
|
|
|
|
it_behaves_like 'akismet spam'
|
|
|
|
|
|
|
|
it 'does not check as spam' do
|
|
|
|
check_spam(issue, request, false)
|
2017-03-20 22:37:29 -04:00
|
|
|
|
2019-09-24 23:06:21 -04:00
|
|
|
expect(issue.spam).to be_nil
|
|
|
|
end
|
2017-03-20 22:37:29 -04:00
|
|
|
end
|
2017-02-14 14:07:11 -05:00
|
|
|
end
|
|
|
|
|
2017-03-20 22:37:29 -04:00
|
|
|
context 'when not indicated as spam by akismet' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
2017-08-24 13:05:02 -04:00
|
|
|
allow(AkismetService).to receive(:new).and_return(double(spam?: false))
|
2017-06-14 14:18:56 -04:00
|
|
|
end
|
2017-03-20 22:37:29 -04:00
|
|
|
|
|
|
|
it 'returns false' do
|
|
|
|
expect(check_spam(issue, request, false)).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a spam log' do
|
|
|
|
expect { check_spam(issue, request, false) }
|
|
|
|
.not_to change { SpamLog.count }
|
|
|
|
end
|
2017-02-14 14:07:11 -05:00
|
|
|
end
|
2017-01-27 11:25:39 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|