Skip to content

Commit afa51dc

Browse files
committed
Add test coverage for #call_adyen_api
1 parent 7c6bda1 commit afa51dc

File tree

1 file changed

+119
-7
lines changed

1 file changed

+119
-7
lines changed

spec/client_spec.rb

Lines changed: 119 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@
118118
skip "Only runs on Ruby >= 3.2" unless RUBY_VERSION >= '3.2'
119119
connection_options = Faraday::ConnectionOptions.new(
120120
request: {
121-
open_timeout: 5,
122-
timeout: 10
121+
open_timeout: 5,
122+
timeout: 10
123123
}
124124
)
125125
expect(Faraday::ConnectionOptions).not_to receive(:new)
@@ -278,7 +278,7 @@
278278
.to eq('https://terminal-api-test.adyen.com/connectedTerminals')
279279

280280
end
281-
281+
282282
it 'checks the initialization of the terminal region' do
283283
client = Adyen::Client.new(api_key: 'api_key', env: :test, terminal_region: 'eu')
284284
expect(client.service_url('TerminalCloudAPI', 'connectedTerminals', nil))
@@ -301,7 +301,7 @@
301301
client = Adyen::Client.new(env: :test)
302302
expect(client.service_url_base('Disputes'))
303303
.to eq('https://ca-test.adyen.com/ca/services/DisputesService')
304-
end
304+
end
305305

306306
it 'checks the creation of SessionAuthentication url for the test env' do
307307
client = Adyen::Client.new(env: :test)
@@ -375,7 +375,7 @@
375375
message: "Return URL is missing.",
376376
errorType: "validation",
377377
pspReference: "8816118280275544"
378-
}
378+
}
379379
mock_response = Faraday::Response.new(status: 422, body: error_body)
380380

381381
allow(Faraday).to receive(:new).and_return(mock_faraday_connection)
@@ -435,7 +435,7 @@
435435
it 'raises NotFoundError on 404 response with an invalid JSON body' do
436436
client = Adyen::Client.new(api_key: 'api_key', env: :test)
437437
mock_faraday_connection = double(Faraday::Connection)
438-
error_body = "this is an error message"
438+
error_body = "this is an error message"
439439
mock_response = Faraday::Response.new(status: 404, body: error_body)
440440

441441
allow(Faraday).to receive(:new).and_return(mock_faraday_connection)
@@ -449,5 +449,117 @@
449449
expect(error.msg).to eq('Not found error')
450450
end
451451
end
452-
452+
453+
454+
describe '#call_adyen_api' do
455+
it 'successfully makes a POST request and returns AdyenResult' do
456+
client = Adyen::Client.new(api_key: 'test_key', env: :test)
457+
mock_faraday_connection = double(Faraday::Connection)
458+
response_body = { pspReference: '123456789', resultCode: 'Authorised' }.to_json
459+
mock_response = Faraday::Response.new(
460+
status: 200,
461+
body: response_body,
462+
response_headers: { 'content-type' => 'application/json' }
463+
)
464+
465+
expect(Faraday).to receive(:new)
466+
.with('https://checkout-test.adyen.com/v71/payments', anything)
467+
.and_return(mock_faraday_connection)
468+
allow(mock_faraday_connection).to receive_message_chain(:headers, :[]=)
469+
allow(mock_faraday_connection).to receive(:post).and_return(mock_response)
470+
471+
result = client.call_adyen_api('Checkout', 'payments', { amount: { value: 1000 } }, {}, '71')
472+
473+
expect(result).to be_a(Adyen::AdyenResult)
474+
expect(result.status).to eq(200)
475+
expect(result.response["pspReference"]).to eq('123456789')
476+
end
477+
478+
it 'successfully makes a GET request' do
479+
client = Adyen::Client.new(api_key: 'test_key', env: :test)
480+
mock_faraday_connection = double(Faraday::Connection)
481+
response_body = { data: [{ id: '1' }] }.to_json
482+
mock_response = Faraday::Response.new(status: 200, body: response_body, response_headers: {})
483+
484+
expect(Faraday).to receive(:new)
485+
.with('https://management-test.adyen.com/v1/companies', anything)
486+
.and_return(mock_faraday_connection)
487+
.and_yield(mock_faraday_connection)
488+
allow(mock_faraday_connection).to receive(:adapter)
489+
allow(mock_faraday_connection).to receive_message_chain(:headers, :[]=)
490+
allow(mock_faraday_connection).to receive(:get).and_return(mock_response)
491+
492+
result = client.call_adyen_api('Management', { url: 'companies', method: 'get' }, {}, {}, '1')
493+
494+
expect(result).to be_a(Adyen::AdyenResult)
495+
expect(result.status).to eq(200)
496+
end
497+
498+
it 'sets correct headers including custom headers' do
499+
client = Adyen::Client.new(api_key: 'test_key', env: :test)
500+
mock_faraday_connection = double(Faraday::Connection)
501+
mock_response = Faraday::Response.new(status: 200, body: '{}', response_headers: {})
502+
503+
headers_spy = {}
504+
expect(Faraday).to receive(:new)
505+
.with('https://checkout-test.adyen.com/v71/payments', anything)
506+
.and_return(mock_faraday_connection)
507+
.and_yield(mock_faraday_connection)
508+
allow(mock_faraday_connection).to receive(:adapter)
509+
allow(mock_faraday_connection).to receive_message_chain(:headers, :[]=) do |key, value|
510+
headers_spy[key] = value
511+
end
512+
allow(mock_faraday_connection).to receive(:post).and_return(mock_response)
513+
514+
custom_headers = { 'Idempotency-Key' => 'test-123' }
515+
client.call_adyen_api('Checkout', 'payments', {}, custom_headers, '71')
516+
517+
expect(headers_spy['Content-Type']).to eq('application/json')
518+
expect(headers_spy['x-api-key']).to eq('test_key')
519+
expect(headers_spy['Idempotency-Key']).to eq('test-123')
520+
end
521+
522+
it 'handles connection failures' do
523+
client = Adyen::Client.new(api_key: 'test_key', env: :test)
524+
mock_faraday_connection = double(Faraday::Connection)
525+
526+
expect(Faraday).to receive(:new)
527+
.with('https://checkout-test.adyen.com/v71/payments', anything)
528+
.and_return(mock_faraday_connection)
529+
.and_yield(mock_faraday_connection)
530+
allow(mock_faraday_connection).to receive_message_chain(:headers, :[]=)
531+
allow(mock_faraday_connection).to receive(:adapter)
532+
allow(mock_faraday_connection).to receive(:post).and_raise(Faraday::ConnectionFailed.new('Connection failed'))
533+
534+
expect {
535+
client.call_adyen_api('Checkout', 'payments', {}, {}, '71')
536+
}.to raise_error(Faraday::ConnectionFailed, /Connection to .* failed/)
537+
end
538+
539+
it 'converts request data to JSON' do
540+
client = Adyen::Client.new(api_key: 'test_key', env: :test)
541+
mock_faraday_connection = double(Faraday::Connection)
542+
mock_response = Faraday::Response.new(status: 200, body: '{}', response_headers: {})
543+
544+
expect(Faraday).to receive(:new)
545+
.with('https://checkout-test.adyen.com/v71/payments', anything)
546+
.and_return(mock_faraday_connection)
547+
.and_yield(mock_faraday_connection)
548+
allow(mock_faraday_connection).to receive(:adapter)
549+
allow(mock_faraday_connection).to receive_message_chain(:headers, :[]=)
550+
551+
request_body_sent = nil
552+
allow(mock_faraday_connection).to receive(:post) do |&block|
553+
req = double('request')
554+
allow(req).to receive(:body=) { |body| request_body_sent = body }
555+
block.call(req)
556+
mock_response
557+
end
558+
559+
hash_data = { amount: { value: 1000, currency: 'EUR' } }
560+
client.call_adyen_api('Checkout', 'payments', hash_data, {}, '71')
561+
562+
expect(request_body_sent).to eq(hash_data.to_json)
563+
end
564+
end
453565
end

0 commit comments

Comments
 (0)