summaryrefslogtreecommitdiff
path: root/spec/features/snippets/spam_snippets_spec.rb
blob: 1483ba4bf8f2c8bac0f945fe1a3f247b6743b499 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.shared_examples_for 'snippet editor' do
  include_context 'includes Spam constants'

  def description_field
    find('.js-description-input').find('input,textarea')
  end

  before do
    stub_feature_flags(allow_possible_spam: false)
    stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')

    Gitlab::CurrentSettings.update!(
      akismet_enabled: true,
      akismet_api_key: 'testkey',
      recaptcha_enabled: true,
      recaptcha_site_key: 'test site key',
      recaptcha_private_key: 'test private key'
    )

    sign_in(user)
    visit new_snippet_path

    fill_in 'personal_snippet_title', with: 'My Snippet Title'

    # Click placeholder first to expand full description field
    description_field.click
    fill_in 'personal_snippet_description', with: 'My Snippet **Description**'

    find('#personal_snippet_visibility_level_20').set(true)
    page.within('.file-editor') do
      el = find('.inputarea')
      el.send_keys 'Hello World!'
    end
  end

  shared_examples 'solve reCAPTCHA' do
    it 'creates a snippet after solving reCAPTCHA' do
      click_button('Create snippet')
      wait_for_requests

      # it is impossible to test reCAPTCHA automatically and there is no possibility to fill in recaptcha
      # reCAPTCHA verification is skipped in test environment and it always returns true
      expect(page).not_to have_content('My Snippet Title')
      expect(page).to have_css('.recaptcha')
      click_button('Submit personal snippet')

      expect(page).to have_content('My Snippet Title')
    end
  end

  shared_examples 'does not allow creation' do
    it 'rejects creation of the snippet' do
      click_button('Create snippet')
      wait_for_requests

      expect(page).to have_content('discarded')
      expect(page).not_to have_content('My Snippet Title')
      expect(page).not_to have_css('.recaptcha')
    end
  end

  context 'when SpamVerdictService requires recaptcha' do
    before do
      expect_next_instance_of(Spam::SpamVerdictService) do |verdict_service|
        expect(verdict_service).to receive(:execute).and_return(CONDITIONAL_ALLOW)
      end
    end

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

      it_behaves_like 'does not allow creation'
    end

    context 'when allow_possible_spam feature flag is true' do
      it_behaves_like 'solve reCAPTCHA'
    end
  end

  context 'when SpamVerdictService disallows' do
    before do
      expect_next_instance_of(Spam::SpamVerdictService) do |verdict_service|
        expect(verdict_service).to receive(:execute).and_return(DISALLOW)
      end
    end

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

      it_behaves_like 'does not allow creation'
    end

    context 'when allow_possible_spam feature flag is true' do
      it_behaves_like 'does not allow creation'
    end
  end

  context 'when SpamVerdictService allows' do
    before do
      expect_next_instance_of(Spam::SpamVerdictService) do |verdict_service|
        expect(verdict_service).to receive(:execute).and_return(ALLOW)
      end
    end

    it 'creates a snippet' do
      click_button('Create snippet')
      wait_for_requests

      expect(page).not_to have_css('.recaptcha')
      expect(page).to have_content('My Snippet Title')
    end
  end
end

RSpec.describe 'User creates snippet', :js do
  let_it_be(:user) { create(:user) }

  context 'Vue application' do
    before do
      stub_feature_flags(snippets_edit_vue: false)
    end

    it_behaves_like "snippet editor"
  end

  context 'non-Vue application' do
    before do
      stub_feature_flags(snippets_vue: false)
      stub_feature_flags(snippets_edit_vue: false)
    end

    it_behaves_like "snippet editor"
  end
end