2020-04-21 15:21:10 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 15:08:50 +00:00
|
|
|
RSpec.describe Spam::SpamVerdictService do
|
2020-04-21 15:21:10 +00:00
|
|
|
include_context 'includes Spam constants'
|
|
|
|
|
|
|
|
let(:fake_ip) { '1.2.3.4' }
|
|
|
|
let(:fake_user_agent) { 'fake-user-agent' }
|
2021-05-01 00:10:00 +00:00
|
|
|
let(:fake_referer) { 'fake-http-referer' }
|
2020-04-21 15:21:10 +00:00
|
|
|
let(:env) do
|
|
|
|
{ 'action_dispatch.remote_ip' => fake_ip,
|
|
|
|
'HTTP_USER_AGENT' => fake_user_agent,
|
2021-05-01 00:10:00 +00:00
|
|
|
'HTTP_REFERER' => fake_referer }
|
2020-04-21 15:21:10 +00:00
|
|
|
end
|
2020-08-11 03:11:00 +00:00
|
|
|
|
2020-04-21 15:21:10 +00:00
|
|
|
let(:check_for_spam) { true }
|
2020-06-08 06:08:19 +00:00
|
|
|
let_it_be(:user) { create(:user) }
|
2021-05-01 00:10:00 +00:00
|
|
|
let_it_be(:issue) { create(:issue, author: user) }
|
2021-06-29 03:07:32 +00:00
|
|
|
|
2020-04-21 15:21:10 +00:00
|
|
|
let(:service) do
|
2021-06-21 12:07:45 +00:00
|
|
|
described_class.new(user: user, target: issue, options: {})
|
2020-04-21 15:21:10 +00:00
|
|
|
end
|
|
|
|
|
2021-05-07 21:10:34 +00:00
|
|
|
let(:attribs) do
|
|
|
|
extra_attributes = { "monitorMode" => "false" }
|
|
|
|
extra_attributes
|
|
|
|
end
|
|
|
|
|
2022-03-02 00:13:45 +00:00
|
|
|
before do
|
|
|
|
stub_feature_flags(allow_possible_spam: false)
|
|
|
|
end
|
|
|
|
|
2020-04-21 15:21:10 +00:00
|
|
|
describe '#execute' do
|
|
|
|
subject { service.execute }
|
|
|
|
|
|
|
|
before do
|
2020-05-22 09:08:09 +00:00
|
|
|
allow(service).to receive(:akismet_verdict).and_return(nil)
|
2021-05-07 21:10:34 +00:00
|
|
|
allow(service).to receive(:spamcheck_verdict).and_return([nil, attribs])
|
2020-05-22 09:08:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'if all services return nil' do
|
|
|
|
it 'renders ALLOW verdict' do
|
|
|
|
expect(subject).to eq ALLOW
|
2020-04-21 15:21:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-22 09:08:09 +00:00
|
|
|
context 'if only one service returns a verdict' do
|
|
|
|
context 'and it is supported' do
|
|
|
|
before do
|
|
|
|
allow(service).to receive(:akismet_verdict).and_return(DISALLOW)
|
|
|
|
end
|
2020-04-21 15:21:10 +00:00
|
|
|
|
2020-05-22 09:08:09 +00:00
|
|
|
it 'renders that verdict' do
|
|
|
|
expect(subject).to eq DISALLOW
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and it is unexpected' do
|
2020-04-21 15:21:10 +00:00
|
|
|
before do
|
2020-05-22 09:08:09 +00:00
|
|
|
allow(service).to receive(:akismet_verdict).and_return("unexpected")
|
2020-04-21 15:21:10 +00:00
|
|
|
end
|
|
|
|
|
2020-05-22 09:08:09 +00:00
|
|
|
it 'allows' do
|
|
|
|
expect(subject).to eq ALLOW
|
2020-04-21 15:21:10 +00:00
|
|
|
end
|
|
|
|
end
|
2020-05-22 09:08:09 +00:00
|
|
|
end
|
2020-04-21 15:21:10 +00:00
|
|
|
|
2020-05-22 09:08:09 +00:00
|
|
|
context 'if more than one service returns a verdict' do
|
|
|
|
context 'and they are supported' do
|
2020-04-21 15:21:10 +00:00
|
|
|
before do
|
2020-05-22 09:08:09 +00:00
|
|
|
allow(service).to receive(:akismet_verdict).and_return(DISALLOW)
|
2021-05-07 21:10:34 +00:00
|
|
|
allow(service).to receive(:spamcheck_verdict).and_return([BLOCK_USER, attribs])
|
2020-04-21 15:21:10 +00:00
|
|
|
end
|
|
|
|
|
2020-05-22 09:08:09 +00:00
|
|
|
it 'renders the more restrictive verdict' do
|
|
|
|
expect(subject).to eq BLOCK_USER
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and one is supported' do
|
|
|
|
before do
|
|
|
|
allow(service).to receive(:akismet_verdict).and_return('nonsense')
|
2021-05-07 21:10:34 +00:00
|
|
|
allow(service).to receive(:spamcheck_verdict).and_return([BLOCK_USER, attribs])
|
2020-05-22 09:08:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders the more restrictive verdict' do
|
|
|
|
expect(subject).to eq BLOCK_USER
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and none are supported' do
|
|
|
|
before do
|
|
|
|
allow(service).to receive(:akismet_verdict).and_return('nonsense')
|
2021-05-07 21:10:34 +00:00
|
|
|
allow(service).to receive(:spamcheck_verdict).and_return(['rubbish', attribs])
|
2020-05-22 09:08:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders the more restrictive verdict' do
|
|
|
|
expect(subject).to eq ALLOW
|
|
|
|
end
|
|
|
|
end
|
2021-05-07 21:10:34 +00:00
|
|
|
|
|
|
|
context 'and attribs - monitorMode is true' do
|
|
|
|
let(:attribs) do
|
|
|
|
extra_attributes = { "monitorMode" => "true" }
|
|
|
|
extra_attributes
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(service).to receive(:akismet_verdict).and_return(DISALLOW)
|
|
|
|
allow(service).to receive(:spamcheck_verdict).and_return([BLOCK_USER, attribs])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders the more restrictive verdict' do
|
|
|
|
expect(subject).to eq(DISALLOW)
|
|
|
|
end
|
|
|
|
end
|
2020-05-22 09:08:09 +00:00
|
|
|
end
|
2021-05-17 15:10:15 +00:00
|
|
|
|
2022-03-02 00:13:45 +00:00
|
|
|
context 'if allow_possible_spam flag is true' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(allow_possible_spam: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and a service returns a verdict that should be overridden' do
|
|
|
|
before do
|
|
|
|
allow(service).to receive(:spamcheck_verdict).and_return([BLOCK_USER, attribs])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'overrides and renders the override verdict' do
|
|
|
|
expect(subject).to eq OVERRIDE_VIA_ALLOW_POSSIBLE_SPAM
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and a service returns a verdict that does not need to be overridden' do
|
|
|
|
before do
|
|
|
|
allow(service).to receive(:spamcheck_verdict).and_return([ALLOW, attribs])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not override and renders the original verdict' do
|
|
|
|
expect(subject).to eq ALLOW
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-17 15:10:15 +00:00
|
|
|
context 'records metrics' do
|
|
|
|
let(:histogram) { instance_double(Prometheus::Client::Histogram) }
|
|
|
|
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
where(:verdict, :error, :label) do
|
|
|
|
Spam::SpamConstants::ALLOW | false | 'ALLOW'
|
|
|
|
Spam::SpamConstants::ALLOW | true | 'ERROR'
|
|
|
|
Spam::SpamConstants::CONDITIONAL_ALLOW | false | 'CONDITIONAL_ALLOW'
|
|
|
|
Spam::SpamConstants::BLOCK_USER | false | 'BLOCK'
|
|
|
|
Spam::SpamConstants::DISALLOW | false | 'DISALLOW'
|
|
|
|
Spam::SpamConstants::NOOP | false | 'NOOP'
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
before do
|
|
|
|
allow(Gitlab::Metrics).to receive(:histogram).with(:gitlab_spamcheck_request_duration_seconds, anything).and_return(histogram)
|
|
|
|
allow(service).to receive(:spamcheck_verdict).and_return([verdict, attribs, error])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'records duration with labels' do
|
|
|
|
expect(histogram).to receive(:observe).with(a_hash_including(result: label), anything)
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-05-22 09:08:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#akismet_verdict' do
|
|
|
|
subject { service.send(:akismet_verdict) }
|
|
|
|
|
|
|
|
context 'if Akismet is enabled' do
|
|
|
|
before do
|
|
|
|
stub_application_setting(akismet_enabled: true)
|
|
|
|
allow_next_instance_of(Spam::AkismetService) do |service|
|
|
|
|
allow(service).to receive(:spam?).and_return(akismet_result)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'if Akismet considers it spam' do
|
|
|
|
let(:akismet_result) { true }
|
|
|
|
|
|
|
|
context 'if reCAPTCHA is enabled' do
|
|
|
|
before do
|
|
|
|
stub_application_setting(recaptcha_enabled: true)
|
|
|
|
end
|
|
|
|
|
2020-05-26 03:08:02 +00:00
|
|
|
it 'returns conditionally allow verdict' do
|
|
|
|
expect(subject).to eq CONDITIONAL_ALLOW
|
2020-05-22 09:08:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'if reCAPTCHA is not enabled' do
|
|
|
|
before do
|
|
|
|
stub_application_setting(recaptcha_enabled: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders disallow verdict' do
|
|
|
|
expect(subject).to eq DISALLOW
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'if Akismet does not consider it spam' do
|
|
|
|
let(:akismet_result) { false }
|
|
|
|
|
|
|
|
it 'renders allow verdict' do
|
|
|
|
expect(subject).to eq ALLOW
|
2020-04-21 15:21:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-22 09:08:09 +00:00
|
|
|
context 'if Akismet is not enabled' do
|
|
|
|
before do
|
|
|
|
stub_application_setting(akismet_enabled: false)
|
|
|
|
end
|
2020-04-21 15:21:10 +00:00
|
|
|
|
2020-05-22 09:08:09 +00:00
|
|
|
it 'renders allow verdict' do
|
2020-04-21 15:21:10 +00:00
|
|
|
expect(subject).to eq ALLOW
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-05-22 09:08:09 +00:00
|
|
|
|
2021-05-01 00:10:00 +00:00
|
|
|
describe '#spamcheck_verdict' do
|
|
|
|
subject { service.send(:spamcheck_verdict) }
|
2020-05-22 09:08:09 +00:00
|
|
|
|
|
|
|
context 'if a Spam Check endpoint enabled and set to a URL' do
|
|
|
|
let(:spam_check_body) { {} }
|
2021-05-01 00:10:00 +00:00
|
|
|
let(:endpoint_url) { "grpc://www.spamcheckurl.com/spam_check" }
|
|
|
|
|
|
|
|
let(:spam_client) do
|
|
|
|
Gitlab::Spamcheck::Client.new
|
|
|
|
end
|
2020-05-22 09:08:09 +00:00
|
|
|
|
|
|
|
before do
|
|
|
|
stub_application_setting(spam_check_endpoint_enabled: true)
|
2021-05-01 00:10:00 +00:00
|
|
|
stub_application_setting(spam_check_endpoint_url: endpoint_url)
|
2020-05-22 09:08:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'if the endpoint is accessible' do
|
2021-05-01 00:10:00 +00:00
|
|
|
let(:error) { '' }
|
2020-05-22 09:08:09 +00:00
|
|
|
let(:verdict) { nil }
|
2021-05-01 00:10:00 +00:00
|
|
|
|
2021-05-07 21:10:34 +00:00
|
|
|
let(:attribs) do
|
|
|
|
extra_attributes = { "monitorMode" => "false" }
|
|
|
|
extra_attributes
|
|
|
|
end
|
|
|
|
|
2021-05-01 00:10:00 +00:00
|
|
|
before do
|
|
|
|
allow(service).to receive(:spamcheck_client).and_return(spam_client)
|
2021-05-07 21:10:34 +00:00
|
|
|
allow(spam_client).to receive(:issue_spam?).and_return([verdict, attribs, error])
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'if the result is a NOOP verdict' do
|
|
|
|
let(:verdict) { NOOP }
|
|
|
|
|
|
|
|
it 'returns the verdict' do
|
|
|
|
expect(subject).to eq([NOOP, attribs])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'if attribs - monitorMode is true' do
|
|
|
|
let(:attribs) do
|
|
|
|
extra_attributes = { "monitorMode" => "true" }
|
|
|
|
extra_attributes
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:verdict) { ALLOW }
|
|
|
|
|
|
|
|
it 'returns the verdict' do
|
|
|
|
expect(subject).to eq([ALLOW, attribs])
|
|
|
|
end
|
2020-05-22 09:08:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'the result is a valid verdict' do
|
2021-05-01 00:10:00 +00:00
|
|
|
let(:verdict) { ALLOW }
|
2020-05-22 09:08:09 +00:00
|
|
|
|
|
|
|
it 'returns the verdict' do
|
2021-05-07 21:10:34 +00:00
|
|
|
expect(subject).to eq([ALLOW, attribs])
|
2020-05-22 09:08:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-01 00:10:00 +00:00
|
|
|
context 'when recaptcha is enabled' do
|
|
|
|
before do
|
|
|
|
allow(Gitlab::Recaptcha).to receive(:enabled?).and_return(true)
|
|
|
|
end
|
2020-05-22 09:08:09 +00:00
|
|
|
|
2021-05-01 00:10:00 +00:00
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
# rubocop: disable Lint/BinaryOperatorWithIdenticalOperands
|
|
|
|
where(:verdict_value, :expected) do
|
|
|
|
::Spam::SpamConstants::ALLOW | ::Spam::SpamConstants::ALLOW
|
|
|
|
::Spam::SpamConstants::CONDITIONAL_ALLOW | ::Spam::SpamConstants::CONDITIONAL_ALLOW
|
2021-10-22 12:09:12 +00:00
|
|
|
::Spam::SpamConstants::DISALLOW | ::Spam::SpamConstants::DISALLOW
|
|
|
|
::Spam::SpamConstants::BLOCK_USER | ::Spam::SpamConstants::BLOCK_USER
|
2021-05-01 00:10:00 +00:00
|
|
|
end
|
|
|
|
# rubocop: enable Lint/BinaryOperatorWithIdenticalOperands
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
let(:verdict) { verdict_value }
|
|
|
|
|
|
|
|
it "returns expected spam constant" do
|
2021-05-07 21:10:34 +00:00
|
|
|
expect(subject).to eq([expected, attribs])
|
2021-05-01 00:10:00 +00:00
|
|
|
end
|
2020-05-22 09:08:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-01 00:10:00 +00:00
|
|
|
context 'when recaptcha is disabled' do
|
|
|
|
before do
|
|
|
|
allow(Gitlab::Recaptcha).to receive(:enabled?).and_return(false)
|
|
|
|
end
|
2020-05-22 09:08:09 +00:00
|
|
|
|
2021-05-01 00:10:00 +00:00
|
|
|
[::Spam::SpamConstants::ALLOW,
|
|
|
|
::Spam::SpamConstants::CONDITIONAL_ALLOW,
|
|
|
|
::Spam::SpamConstants::DISALLOW,
|
|
|
|
::Spam::SpamConstants::BLOCK_USER].each do |verdict_value|
|
|
|
|
let(:verdict) { verdict_value }
|
2021-05-07 21:10:34 +00:00
|
|
|
let(:expected) { [verdict_value, attribs] }
|
2021-05-01 00:10:00 +00:00
|
|
|
|
|
|
|
it "returns expected spam constant" do
|
|
|
|
expect(subject).to eq(expected)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'the verdict is an unexpected value' do
|
|
|
|
let(:verdict) { :this_is_fine }
|
|
|
|
|
|
|
|
it 'returns the string' do
|
2021-05-07 21:10:34 +00:00
|
|
|
expect(subject).to eq([verdict, attribs])
|
2020-05-22 09:08:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'the verdict is an empty string' do
|
|
|
|
let(:verdict) { '' }
|
|
|
|
|
|
|
|
it 'returns nil' do
|
2021-05-07 21:10:34 +00:00
|
|
|
expect(subject).to eq([verdict, attribs])
|
2020-05-22 09:08:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'the verdict is nil' do
|
|
|
|
let(:verdict) { nil }
|
|
|
|
|
|
|
|
it 'returns nil' do
|
2021-05-07 21:10:34 +00:00
|
|
|
expect(subject).to eq([nil, attribs])
|
2020-05-22 09:08:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'there is an error' do
|
|
|
|
let(:error) { "Sorry Dave, I can't do that" }
|
|
|
|
|
|
|
|
it 'returns nil' do
|
2021-05-07 21:10:34 +00:00
|
|
|
expect(subject).to eq([nil, attribs])
|
2020-05-22 09:08:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-01 00:10:00 +00:00
|
|
|
context 'the requested is aborted' do
|
2021-05-07 21:10:34 +00:00
|
|
|
let(:attribs) { nil }
|
|
|
|
|
2021-05-01 00:10:00 +00:00
|
|
|
before do
|
|
|
|
allow(spam_client).to receive(:issue_spam?).and_raise(GRPC::Aborted)
|
|
|
|
end
|
2020-05-22 09:08:09 +00:00
|
|
|
|
|
|
|
it 'returns nil' do
|
2021-05-17 15:10:15 +00:00
|
|
|
expect(subject).to eq([ALLOW, attribs, true])
|
2020-05-22 09:08:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'the confused API endpoint returns both an error and a verdict' do
|
|
|
|
let(:verdict) { 'disallow' }
|
|
|
|
let(:error) { 'oh noes!' }
|
|
|
|
|
|
|
|
it 'renders the verdict' do
|
2021-05-07 21:10:34 +00:00
|
|
|
expect(subject).to eq [DISALLOW, attribs]
|
2020-05-22 09:08:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'if the endpoint times out' do
|
2021-05-07 21:10:34 +00:00
|
|
|
let(:attribs) { nil }
|
|
|
|
|
2020-05-22 09:08:09 +00:00
|
|
|
before do
|
2021-05-01 00:10:00 +00:00
|
|
|
allow(spam_client).to receive(:issue_spam?).and_raise(GRPC::DeadlineExceeded)
|
2020-05-22 09:08:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil' do
|
2021-05-17 15:10:15 +00:00
|
|
|
expect(subject).to eq([ALLOW, attribs, true])
|
2020-05-22 09:08:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'if a Spam Check endpoint is not set' do
|
|
|
|
before do
|
|
|
|
stub_application_setting(spam_check_endpoint_url: nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil' do
|
|
|
|
expect(subject).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'if Spam Check endpoint is not enabled' do
|
|
|
|
before do
|
|
|
|
stub_application_setting(spam_check_endpoint_enabled: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil' do
|
|
|
|
expect(subject).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-04-21 15:21:10 +00:00
|
|
|
end
|