summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/graphql/mutations/spammable_mutation_fields_examples.rb
blob: 54b3f84a6e6837cb23c610bf5f3c8acfbe6f4c7f (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.shared_examples 'spam flag is present' do
  specify :aggregate_failures do
    subject

    expect(mutation_response).to have_key('spam')
    expect(mutation_response['spam']).to be_falsey
  end
end

RSpec.shared_examples 'can raise spam flag' do
  it 'spam parameters are passed to the service' do
    expect(service).to receive(:new).with(anything, anything, hash_including(api: true, request: instance_of(ActionDispatch::Request)))

    subject
  end

  context 'when the snippet is detected as spam' do
    it 'raises spam flag' do
      allow_next_instance_of(service) do |instance|
        allow(instance).to receive(:spam_check) do |snippet, user, _|
          snippet.spam!
        end
      end

      subject

      expect(mutation_response['spam']).to be true
      expect(mutation_response['errors']).to include("Your snippet has been recognized as spam and has been discarded.")
    end
  end

  context 'when :snippet_spam flag is disabled' do
    before do
      stub_feature_flags(snippet_spam: false)
    end

    it 'request parameter is not passed to the service' do
      expect(service).to receive(:new).with(anything, anything, hash_not_including(request: instance_of(ActionDispatch::Request)))

      subject
    end
  end
end