summaryrefslogtreecommitdiff
path: root/spec/controllers/concerns/spammable_actions/captcha_check/json_format_actions_support_spec.rb
blob: d7a44351ad8bb4ac791f95dfd9893ef8aa635b7e (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe SpammableActions::CaptchaCheck::JsonFormatActionsSupport do
  controller(ActionController::Base) do
    include SpammableActions::CaptchaCheck::JsonFormatActionsSupport

    def some_action
      with_captcha_check_json_format { render :some_rendered_view }
    end
  end

  before do
    allow(Gitlab::Recaptcha).to receive(:load_configurations!) { true }
  end

  describe '#with_captcha_check_json_format' do
    subject { post :some_action }

    let(:spammable) { double(:spammable) }

    before do
      routes.draw { get 'some_action' => 'anonymous#some_action' }
      allow(controller).to receive(:spammable) { spammable }
      expect(spammable).to receive(:render_recaptcha?).at_least(:once) { render_recaptcha }
    end

    context 'when spammable.render_recaptcha? is true' do
      let(:render_recaptcha) { true }
      let(:spam_log) { double(:spam_log, id: 1) }
      let(:spammable) { double(:spammable, spam?: true, render_recaptcha?: render_recaptcha, spam_log: spam_log) }
      let(:recaptcha_site_key) { 'abc123' }
      let(:spam_action_response_fields) do
        {
          spam: true,
          needs_captcha_response: render_recaptcha,
          spam_log_id: 1,
          captcha_site_key: recaptcha_site_key
        }
      end

      it 'renders json containing spam_action_response_fields' do
        expect(controller).to receive(:render).with(json: spam_action_response_fields, status: :conflict)
        allow(Gitlab::CurrentSettings).to receive(:recaptcha_site_key) { recaptcha_site_key }
        subject
      end
    end

    context 'when spammable.render_recaptcha? is false' do
      let(:render_recaptcha) { false }

      it 'yields to block' do
        expect(controller).to receive(:render).with(:some_rendered_view)

        subject
      end
    end
  end
end