From 482fecf51b99dba016d9b27f54795212560fe37e Mon Sep 17 00:00:00 2001 From: Lucas Gomes Date: Sat, 24 Aug 2024 19:32:38 -0300 Subject: [PATCH] Specify the desired token for evaluating risk assessments and improvement tests --- Gemfile.lock | 2 +- lib/incognia_api/api.rb | 9 ++- spec/incognia_spec.rb | 118 ++++++++++++++++++++++++---------------- 3 files changed, 78 insertions(+), 51 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 2b3346c..92282f0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - incognia_api (1.1.0) + incognia_api (1.2.0) faraday (~> 1.10) faraday_middleware (~> 1.2) diff --git a/lib/incognia_api/api.rb b/lib/incognia_api/api.rb index 3273b1f..21fbdcc 100644 --- a/lib/incognia_api/api.rb +++ b/lib/incognia_api/api.rb @@ -15,8 +15,9 @@ def initialize(client_id:, client_secret:) host: "https://api.incognia.com/api") end - def register_signup(address: nil, **opts) + def register_signup(request_token: nil, address: nil, **opts) params = {} + params.merge!({ request_token: request_token }) if request_token params.merge!(opts) params.merge!(address&.to_hash) if address @@ -29,11 +30,12 @@ def register_signup(address: nil, **opts) SignupAssessment.from_hash(response.body) if response.success? end - def register_login(account_id:, **opts) + def register_login(account_id:, request_token: nil, **opts) params = { type: :login, account_id: account_id } + params.merge!({ request_token: request_token }) if request_token params.merge!(opts) response = connection.request( @@ -66,8 +68,9 @@ def register_feedback(event:, occurred_at: nil, expires_at: nil, timestamp: nil, response.success? end - def register_payment(account_id:, **opts) + def register_payment(account_id:, request_token: nil, **opts) params = { account_id: account_id, type: :payment } + params.merge!({ request_token: request_token }) if request_token params.merge!(opts) response = connection.request( diff --git a/spec/incognia_spec.rb b/spec/incognia_spec.rb index 0154ebc..3305d58 100644 --- a/spec/incognia_spec.rb +++ b/spec/incognia_spec.rb @@ -58,27 +58,34 @@ module Incognia end context "HTTP request" do - it "hits the endpoint with request_token" do - stub_token_request + shared_examples_for 'receiving one of the required tokens' do |token_name| + let(:token_value) { SecureRandom.uuid } - stub = stub_signup_request - stub.with( - body: { request_token: request_token }, - headers: { - 'Content-Type' => 'application/json', 'Authorization' => /Bearer.*/ - } - ) + it "hits the endpoint with #{token_name}" do + stub_token_request - api.register_signup(request_token: request_token) + stub = stub_signup_request + stub.with( + body: { token_name => token_value }, + headers: { + 'Content-Type' => 'application/json', 'Authorization' => /Bearer.*/ + } + ) - expect(stub).to have_been_made.once + api.register_signup(token_name => token_value) + + expect(stub).to have_been_made.once + end end + it_behaves_like 'receiving one of the required tokens', :request_token + it_behaves_like 'receiving one of the required tokens', :installation_id + it_behaves_like 'receiving one of the required tokens', :session_token + it "hits the endpoint with request_token and address_line" do stub_token_request - stub = stub_signup_request - stub.with( + stub = stub_signup_request.with( body: { request_token: request_token, address_line: line_format }, headers: { 'Content-Type' => 'application/json', 'Authorization' => /Bearer.*/ @@ -154,7 +161,6 @@ module Incognia end end end - end describe "#register_login" do @@ -177,27 +183,36 @@ module Incognia end context "HTTP request" do - it "hits the endpoint with request_token and account_id" do - stub_token_request + shared_examples_for 'receiving one of the required tokens with account_id' do |token_name| + let(:token_value) { SecureRandom.uuid } - stub = stub_login_request.with( - body: { - type: 'login', - request_token: request_token, account_id: account_id - }, - headers: { - 'Content-Type' => 'application/json', 'Authorization' => /Bearer.*/ - } - ) + it "hits the endpoint with #{token_name} and account_id" do + stub_token_request - api.register_login( - request_token: request_token, - account_id: account_id - ) + stub = stub_login_request.with( + body: { + type: 'login', + account_id: account_id, + token_name => token_value + }, + headers: { + 'Content-Type' => 'application/json', 'Authorization' => /Bearer.*/ + } + ) - expect(stub).to have_been_made.once + api.register_login( + account_id: account_id, + token_name => token_value + ) + + expect(stub).to have_been_made.once + end end + it_behaves_like 'receiving one of the required tokens with account_id', :request_token + it_behaves_like 'receiving one of the required tokens with account_id', :installation_id + it_behaves_like 'receiving one of the required tokens with account_id', :session_token + context 'when receiving any other optional arguments' do shared_examples_for 'receiving optional args' do |optional_arguments| it "hits the endpoint also with #{optional_arguments}" do @@ -258,27 +273,36 @@ module Incognia end context "HTTP request" do - it "hits the endpoint with request_token and account_id" do - stub_token_request + shared_examples_for 'receiving one of the required tokens with account_id' do |token_name| + let(:token_value) { SecureRandom.uuid } - stub = stub_payment_request.with( - body: { - type: 'payment', - request_token: request_token, account_id: account_id - }, - headers: { - 'Content-Type' => 'application/json', 'Authorization' => /Bearer.*/ - } - ) + it "hits the endpoint with #{token_name} and account_id" do + stub_token_request - api.register_payment( - request_token: request_token, - account_id: account_id - ) + stub = stub_payment_request.with( + body: { + type: 'payment', + account_id: account_id, + token_name => token_value + }, + headers: { + 'Content-Type' => 'application/json', 'Authorization' => /Bearer.*/ + } + ) - expect(stub).to have_been_made.once + api.register_payment( + account_id: account_id, + token_name => token_value + ) + + expect(stub).to have_been_made.once + end end + it_behaves_like 'receiving one of the required tokens with account_id', :request_token + it_behaves_like 'receiving one of the required tokens with account_id', :installation_id + it_behaves_like 'receiving one of the required tokens with account_id', :session_token + context 'when receiving any other optional arguments' do shared_examples_for 'receiving optional args' do |optional_arguments| it "hits the endpoint also with #{optional_arguments}" do @@ -305,7 +329,7 @@ module Incognia end end - it_behaves_like 'receiving optional args', 'external_id', 'payment request', 'aaa' do + it_behaves_like 'receiving optional args', 'external_id' do let(:opts) { { external_id: 'external-id' } } end it_behaves_like 'receiving optional args', 'payment_value' do