-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BotDetectController specs, with re-usable turnstile mocks
- Loading branch information
Showing
3 changed files
with
80 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe BotDetectController, type: :controller do | ||
include WebmockTurnstileHelperMethods | ||
|
||
describe "#verify_challenge" do | ||
it "handles turnstile success" do | ||
turnstile_response = stub_turnstile_success | ||
|
||
post :verify_challenge, params: { cf_turnstile_response: "XXXX.DUMMY.TOKEN.XXXX" } | ||
expect(response.status).to be 200 | ||
expect(response.body).to eq turnstile_response.to_json | ||
|
||
expect(session[BotDetectController.session_passed_key]).to be_present | ||
expect(Time.new(session[BotDetectController.session_passed_key])).to be_within(60).of(Time.now.utc) | ||
end | ||
|
||
it "handles turnstile failure" do | ||
turnstile_response = stub_turnstile_failure | ||
|
||
post :verify_challenge, params: { cf_turnstile_response: "XXXX.DUMMY.TOKEN.XXXX" } | ||
expect(response.status).to be 200 | ||
expect(response.body).to eq turnstile_response.to_json | ||
|
||
expect(session[BotDetectController.session_passed_key]).not_to be_present | ||
end | ||
end | ||
end |
38 changes: 38 additions & 0 deletions
38
spec/test_support/helper_ruby/webmock_turnstile_helper_methods.rb
This file contains 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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module WebmockTurnstileHelperMethods | ||
def stub_turnstile_success(turnstile_response: {}, | ||
request_body: {"secret"=>BotDetectController.cf_turnstile_secret_key, "response"=>"XXXX.DUMMY.TOKEN.XXXX", "remoteip"=>"0.0.0.0"}) | ||
|
||
turnstile_response.reverse_merge!( | ||
{"success"=>true, "error-codes"=>[], "challenge_ts"=>Time.now.utc.iso8601(3), "hostname"=>"example.com", "metadata"=>{"result_with_testing_key"=>true}} | ||
) | ||
|
||
stub_request(:post, BotDetectController.cf_turnstile_validation_url). | ||
with( | ||
body: request_body.to_json | ||
).to_return(status: 200, | ||
body: turnstile_response.to_json, | ||
headers: { 'Content-Type'=>'application/json; charset=utf-8' } | ||
) | ||
|
||
return turnstile_response | ||
end | ||
|
||
def stub_turnstile_failure(turnstile_response: {}, | ||
request_body: {"secret"=>BotDetectController.cf_turnstile_secret_key, "response"=>"XXXX.DUMMY.TOKEN.XXXX", "remoteip"=>"0.0.0.0"}) | ||
|
||
turnstile_response.reverse_merge!( | ||
{"success"=>false, "error-codes"=>["invalid-input-response"], "messages"=>[], "metadata"=>{"result_with_testing_key"=>true}} | ||
) | ||
|
||
stub_request(:post, BotDetectController.cf_turnstile_validation_url). | ||
with( | ||
body: request_body.to_json | ||
).to_return(status: 200, | ||
body: turnstile_response.to_json, | ||
headers: { 'Content-Type'=>'application/json; charset=utf-8' } | ||
) | ||
|
||
return turnstile_response | ||
end | ||
|
||
end |