summaryrefslogtreecommitdiff
path: root/spec/controllers/concerns/spammable_actions_spec.rb
blob: 3b5b4d11a9b126ee90f602102430a43fe153f8f7 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# frozen_string_literal: true

require 'spec_helper'

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

    # #create is used to test spammable_params
    # for testing purposes
    def create
      spam_params = spammable_params

      # replace the actual request with a string in the JSON response, all we care is that it got set
      spam_params[:request] = 'this is the request' if spam_params[:request]

      # just return the params in the response so they can be verified in this fake controller spec.
      # Normally, they are processed further by the controller action
      render json: spam_params.to_json, status: :ok
    end

    # #update is used to test recaptcha_check_with_fallback
    # for testing purposes
    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
    # Ordinarily we would not stub a method on the class under test, but :ensure_spam_config_loaded!
    # returns false in the test environment, and is also strong_memoized, so we need to stub it
    allow(controller).to receive(:ensure_spam_config_loaded!) { true }
  end

  describe '#spammable_params' do
    subject { post :create, format: :json, params: params }

    shared_examples 'expects request param only' do
      it do
        subject

        expect(response).to be_successful
        expect(json_response).to eq({ 'request' => 'this is the request' })
      end
    end

    shared_examples 'expects all spammable params' do
      it do
        subject

        expect(response).to be_successful
        expect(json_response['request']).to eq('this is the request')
        expect(json_response['recaptcha_verified']).to eq(true)
        expect(json_response['spam_log_id']).to eq('1')
      end
    end

    let(:recaptcha_response) { nil }
    let(:spam_log_id) { nil }

    context 'when recaptcha response is not present' do
      let(:params) do
        {
          spam_log_id: spam_log_id
        }
      end

      it_behaves_like 'expects request param only'
    end

    context 'when recaptcha response is present' do
      let(:recaptcha_response) { 'abd123' }
      let(:params) do
        {
          'g-recaptcha-response': recaptcha_response,
          spam_log_id: spam_log_id
        }
      end

      context 'when verify_recaptcha returns falsey' do
        before do
          expect(controller).to receive(:verify_recaptcha).with(
            {
              response: recaptcha_response
            }) { false }
        end

        it_behaves_like 'expects request param only'
      end

      context 'when verify_recaptcha returns truthy' do
        let(:spam_log_id) { 1 }

        before do
          expect(controller).to receive(:verify_recaptcha).with(
            {
              response: recaptcha_response
            }) { true }
        end

        it_behaves_like 'expects all spammable params'
      end
    end
  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

      # NOTE: Not adding coverage of details of render_recaptcha?, the plan is to refactor it out
      # of this module anyway as part of adding support for the GraphQL reCAPTCHA flow.

      context 'when render_recaptcha? is true' do
        before do
          expect(controller).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