-
Notifications
You must be signed in to change notification settings - Fork 5
Add turnstile token to subscription #810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cocomarine
wants to merge
8
commits into
main
Choose a base branch
from
1353-add-turnstile-token-to-subscription
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+160
−4
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3c87df7
Config turnstile screte and add test
cocomarine 35fe29d
linting
cocomarine d7426c4
update controller
cocomarine dc09c57
Merge branch 'main' into 1353-add-turnstile-token-to-subscription
cocomarine 04866c6
add turnstile key to env
cocomarine e7a835f
update errors and test
cocomarine e2e4784
update params and add timeouts to faraday
cocomarine d6044f9
fix faraday connection
cocomarine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,8 @@ | |
| subscription: { | ||
| email: '[email protected]', | ||
| test_opt_in: true, | ||
| privacy_policy: true | ||
| privacy_policy: true, | ||
| turnstile_token: 'test-token' | ||
| } | ||
| } | ||
| end | ||
|
|
@@ -37,8 +38,10 @@ | |
| let(:submitter) { instance_double(Subscriptions::PardotFormHandlerSubmitter) } | ||
|
|
||
| before do | ||
| allow(Rails.configuration.x.cloudflare_turnstile).to receive(:enabled).and_return(false) | ||
| allow(Subscriptions::PardotFormHandlerSubmitter).to receive(:new).and_return(submitter) | ||
| allow(submitter).to receive(:call).and_return(submitter_result_success) | ||
| allow(Sentry).to receive(:capture_exception) | ||
| end | ||
|
|
||
| it 'returns success for a valid payload' do | ||
|
|
@@ -110,5 +113,98 @@ | |
| 'message' => 'Subscription provider rejected the request.' | ||
| ) | ||
| end | ||
|
|
||
| describe 'Cloudflare Turnstile integration' do | ||
| let(:request_url) { Api::SubscriptionsController::API_URL } | ||
| let(:turnstile_request_body) { { 'secret' => 'test-secret', 'response' => 'test-token', 'remoteip' => '127.0.0.1' } } | ||
| let(:post_params) { payload } | ||
|
|
||
| before do | ||
| allow(Rails.configuration.x.cloudflare_turnstile).to receive_messages( | ||
| enabled: true, | ||
| secret_key: 'test-secret' | ||
| ) | ||
| end | ||
|
|
||
| shared_examples 'turnstile verification failure' do | ||
| it 'returns 422 with turnstile_verification_failed error code' do | ||
| post(path, params: post_params, as: :json) | ||
|
|
||
| expect(response).to have_http_status(:unprocessable_content) | ||
| expect(response.parsed_body['error_code']).to eq('turnstile_verification_failed') | ||
| end | ||
| end | ||
|
|
||
| shared_examples 'fail-open turnstile response' do | ||
| it 'allows the request through' do | ||
| post(path, params: payload, as: :json) | ||
|
|
||
| expect(response).to have_http_status(:ok) | ||
| expect(response.parsed_body['ok']).to be(true) | ||
| end | ||
| end | ||
|
|
||
| context 'when turnstile token is missing' do | ||
| let(:post_params) { payload.deep_merge(subscription: { turnstile_token: '' }) } | ||
|
|
||
| it_behaves_like 'turnstile verification failure' | ||
| end | ||
|
|
||
| context 'when turnstile verification fails' do | ||
| before do | ||
| stub_request(:post, request_url) | ||
| .with(body: turnstile_request_body) | ||
| .to_return(status: 200, body: { success: false }.to_json) | ||
| end | ||
|
|
||
| it_behaves_like 'turnstile verification failure' | ||
| end | ||
|
|
||
| context 'when turnstile verification times out' do | ||
| before do | ||
| stub_request(:post, request_url) | ||
| .with(body: turnstile_request_body) | ||
| .to_timeout | ||
| end | ||
|
|
||
| it 'allows the request through and reports to Sentry' do | ||
| post(path, params: payload, as: :json) | ||
|
|
||
| expect(response).to have_http_status(:ok) | ||
| expect(response.parsed_body['ok']).to be(true) | ||
| expect(Sentry).to have_received(:capture_exception).with(be_a(Faraday::Error)) | ||
| end | ||
| end | ||
|
|
||
| context 'when Cloudflare returns a server error' do | ||
| before do | ||
| stub_request(:post, request_url) | ||
| .with(body: turnstile_request_body) | ||
| .to_return(status: 500, body: 'Internal Server Error') | ||
| end | ||
|
|
||
| it_behaves_like 'fail-open turnstile response' | ||
| end | ||
|
|
||
| context 'when Cloudflare returns malformed JSON' do | ||
| before do | ||
| stub_request(:post, request_url) | ||
| .with(body: turnstile_request_body) | ||
| .to_return(status: 200, body: 'not-json') | ||
| end | ||
|
|
||
| it_behaves_like 'fail-open turnstile response' | ||
| end | ||
|
|
||
| context 'when turnstile token is valid' do | ||
| before do | ||
| stub_request(:post, request_url) | ||
| .with(body: turnstile_request_body) | ||
| .to_return(status: 200, body: { success: true }.to_json) | ||
| end | ||
|
|
||
| it_behaves_like 'fail-open turnstile response' | ||
| end | ||
| end | ||
| end | ||
| end | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.