summaryrefslogtreecommitdiff
path: root/spec/features/issues/spam_issues_spec.rb
blob: b59cd2d632a06ffd9df1bb3173236bfdd6a06d71 (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
# frozen_string_literal: true

require 'spec_helper'

describe 'New issue', :js do
  include StubENV

  let(:project) { create(:project, :public) }
  let(:user)    { create(:user)}

  before do
    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'
    )

    project.add_maintainer(user)
    sign_in(user)
  end

  context 'when identified as spam' do
    before do
      WebMock.stub_request(:any, /.*akismet.com.*/).to_return(body: "true", status: 200)

      visit new_project_issue_path(project)
    end

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

      it 'creates an issue after solving reCaptcha' do
        fill_in 'issue_title', with: 'issue title'
        fill_in 'issue_description', with: 'issue description'

        click_button 'Submit issue'

        # 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('issue title')
        expect(page).to have_css('.recaptcha')

        click_button 'Submit issue'

        expect(page.find('.issue-details h2.title')).to have_content('issue title')
        expect(page.find('.issue-details .description')).to have_content('issue description')
      end
    end

    context 'when allow_possible_spam feature flag is true' do
      before do
        fill_in 'issue_title', with: 'issue title'
        fill_in 'issue_description', with: 'issue description'
      end

      it 'creates an issue without a need to solve reCaptcha' do
        click_button 'Submit issue'

        expect(page).not_to have_css('.recaptcha')
        expect(page.find('.issue-details h2.title')).to have_content('issue title')
        expect(page.find('.issue-details .description')).to have_content('issue description')
      end

      it 'creates a spam log record' do
        expect { click_button 'Submit issue' }
          .to log_spam(title: 'issue title', description: 'issue description', user_id: user.id, noteable_type: 'Issue')
      end
    end
  end

  context 'when not identified as spam' do
    before do
      WebMock.stub_request(:any, /.*akismet.com.*/).to_return(body: 'false', status: 200)

      visit new_project_issue_path(project)
    end

    it 'creates an issue' do
      fill_in 'issue_title', with: 'issue title'
      fill_in 'issue_description', with: 'issue description'

      click_button 'Submit issue'

      expect(page.find('.issue-details h2.title')).to have_content('issue title')
      expect(page.find('.issue-details .description')).to have_content('issue description')
    end
  end
end