summaryrefslogtreecommitdiff
path: root/spec/controllers/concerns/spammable_actions_spec.rb
blob: 25d5398c9da3f4f2a09ace0982cc086dce57190d (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
# frozen_string_literal: true

require 'spec_helper'

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

    # #update is used here to test #recaptcha_check_with_fallback, but it could be invoked
    # from #create or any other action which mutates a spammable via a controller.
    def update
      should_redirect = params[:should_redirect] == 'true'

      recaptcha_check_with_fallback(should_redirect) { render json: :ok }
    end

    private

    def spammable_path
      '/fake_spammable_path'
    end
  end

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

  describe '#recaptcha_check_with_fallback' do
    shared_examples 'yields to block' do
      it do
        subject

        expect(json_response).to eq({ json: 'ok' })
      end
    end

    let(:format) { :html }

    subject { post :update, format: format, params: params }

    let(:spammable) { double(:spammable) }
    let(:should_redirect) { nil }
    let(:params) do
      {
        should_redirect: should_redirect
      }
    end

    before do
      routes.draw { get 'update' => 'anonymous#update' }
      allow(controller).to receive(:spammable) { spammable }
    end

    context 'when should_redirect is true and spammable is valid' do
      let(:should_redirect) { true }

      before do
        allow(spammable).to receive(:valid?) { true }
      end

      it 'redirects to spammable_path' do
        expect(subject).to redirect_to('/fake_spammable_path')
      end
    end

    context 'when should_redirect is false or spammable is not valid' do
      before do
        allow(spammable).to receive(:valid?) { false }
      end

      context 'when spammable.render_recaptcha? is true' do
        before do
          expect(spammable).to receive(:render_recaptcha?) { true }
        end

        context 'when format is :html' do
          it 'renders :verify' do
            expect(controller).to receive(:render).with(:verify)

            subject
          end
        end

        context 'when format is :json' do
          let(:format) { :json }
          let(:recaptcha_html) { '<recaptcha-html/>' }

          it 'renders json with recaptcha_html' do
            expect(controller).to receive(:render_to_string).with(
              {
                partial: 'shared/recaptcha_form',
                formats: :html,
                locals: {
                  spammable: spammable,
                  script: false,
                  has_submit: false
                }
              }
            ) { recaptcha_html }

            subject

            expect(json_response).to eq({ 'recaptcha_html' => recaptcha_html })
          end
        end
      end
    end
  end
end