|
118 | 118 | skip "Only runs on Ruby >= 3.2" unless RUBY_VERSION >= '3.2' |
119 | 119 | connection_options = Faraday::ConnectionOptions.new( |
120 | 120 | request: { |
121 | | - open_timeout: 5, |
122 | | - timeout: 10 |
| 121 | + open_timeout: 5, |
| 122 | + timeout: 10 |
123 | 123 | } |
124 | 124 | ) |
125 | 125 | expect(Faraday::ConnectionOptions).not_to receive(:new) |
|
278 | 278 | .to eq('https://terminal-api-test.adyen.com/connectedTerminals') |
279 | 279 |
|
280 | 280 | end |
281 | | - |
| 281 | + |
282 | 282 | it 'checks the initialization of the terminal region' do |
283 | 283 | client = Adyen::Client.new(api_key: 'api_key', env: :test, terminal_region: 'eu') |
284 | 284 | expect(client.service_url('TerminalCloudAPI', 'connectedTerminals', nil)) |
|
301 | 301 | client = Adyen::Client.new(env: :test) |
302 | 302 | expect(client.service_url_base('Disputes')) |
303 | 303 | .to eq('https://ca-test.adyen.com/ca/services/DisputesService') |
304 | | - end |
| 304 | + end |
305 | 305 |
|
306 | 306 | it 'checks the creation of SessionAuthentication url for the test env' do |
307 | 307 | client = Adyen::Client.new(env: :test) |
|
375 | 375 | message: "Return URL is missing.", |
376 | 376 | errorType: "validation", |
377 | 377 | pspReference: "8816118280275544" |
378 | | - } |
| 378 | + } |
379 | 379 | mock_response = Faraday::Response.new(status: 422, body: error_body) |
380 | 380 |
|
381 | 381 | allow(Faraday).to receive(:new).and_return(mock_faraday_connection) |
|
435 | 435 | it 'raises NotFoundError on 404 response with an invalid JSON body' do |
436 | 436 | client = Adyen::Client.new(api_key: 'api_key', env: :test) |
437 | 437 | mock_faraday_connection = double(Faraday::Connection) |
438 | | - error_body = "this is an error message" |
| 438 | + error_body = "this is an error message" |
439 | 439 | mock_response = Faraday::Response.new(status: 404, body: error_body) |
440 | 440 |
|
441 | 441 | allow(Faraday).to receive(:new).and_return(mock_faraday_connection) |
|
449 | 449 | expect(error.msg).to eq('Not found error') |
450 | 450 | end |
451 | 451 | 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 |
453 | 565 | end |
0 commit comments