|
10 | 10 | subscription: { |
11 | 11 | email: 'teacher@example.com', |
12 | 12 | test_opt_in: true, |
13 | | - privacy_policy: true |
| 13 | + privacy_policy: true, |
| 14 | + turnstile_token: 'test-token' |
14 | 15 | } |
15 | 16 | } |
16 | 17 | end |
|
110 | 111 | 'message' => 'Subscription provider rejected the request.' |
111 | 112 | ) |
112 | 113 | end |
| 114 | + |
| 115 | + describe 'Cloudflare Turnstile integration' do |
| 116 | + let(:request_url) { 'https://challenges.cloudflare.com/turnstile/v0/siteverify' } |
| 117 | + |
| 118 | + before do |
| 119 | + allow(Rails.configuration.x.cloudflare_turnstile).to receive(:enabled).and_return(true) |
| 120 | + allow(Rails.configuration.x.cloudflare_turnstile).to receive(:secret_key).and_return('test-secret') |
| 121 | + end |
| 122 | + |
| 123 | + it 'returns 422 when turnstile token is missing' do |
| 124 | + post(path, params: payload.deep_merge(subscription: { turnstile_token: '' }), as: :json) |
| 125 | + |
| 126 | + expect(response).to have_http_status(:unprocessable_content) |
| 127 | + expect(response.parsed_body['error_code']).to eq('turnstile_verification_failed') |
| 128 | + end |
| 129 | + |
| 130 | + it 'returns 422 when turnstile verification fails' do |
| 131 | + stub_request(:post, request_url) |
| 132 | + .with( |
| 133 | + body: hash_including( |
| 134 | + secret: 'test-secret', |
| 135 | + response: 'test-token' |
| 136 | + ) |
| 137 | + ) |
| 138 | + .to_return(status: 200, body: { success: false }.to_json) |
| 139 | + |
| 140 | + post(path, params: payload, as: :json) |
| 141 | + |
| 142 | + expect(response).to have_http_status(:unprocessable_content) |
| 143 | + expect(response.parsed_body['error_code']).to eq('turnstile_verification_failed') |
| 144 | + end |
| 145 | + |
| 146 | + it 'allows request through if turnstile verification is unavailable' do |
| 147 | + stub_request(:post, request_url) |
| 148 | + .with( |
| 149 | + body: hash_including( |
| 150 | + secret: 'test-secret', |
| 151 | + response: 'test-token' |
| 152 | + ) |
| 153 | + ) |
| 154 | + .to_timeout |
| 155 | + |
| 156 | + post(path, params: payload, as: :json) |
| 157 | + |
| 158 | + expect(response).to have_http_status(:ok) |
| 159 | + expect(response.parsed_body['ok']).to eq(true) |
| 160 | + end |
| 161 | + end |
113 | 162 | end |
114 | 163 | end |
0 commit comments