Skip to content

Commit b08262d

Browse files
committed
Add test coverage for #call_adyen_api
1 parent 62dfda8 commit b08262d

File tree

1 file changed

+117
-5
lines changed

1 file changed

+117
-5
lines changed

spec/client_spec.rb

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

0 commit comments

Comments
 (0)