Skip to content

Commit 09fec20

Browse files
authored
Merge pull request #321 from Adyen/add-tests-exception-handling
Test exception handling
2 parents 62dfda8 + 68f8d23 commit 09fec20

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

spec/client_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,4 +431,23 @@
431431
expect(error.msg).to eq('Not found error')
432432
end
433433
end
434+
435+
it 'raises NotFoundError on 404 response with an invalid JSON body' do
436+
client = Adyen::Client.new(api_key: 'api_key', env: :test)
437+
mock_faraday_connection = double(Faraday::Connection)
438+
error_body = "this is an error message"
439+
mock_response = Faraday::Response.new(status: 404, body: error_body)
440+
441+
allow(Faraday).to receive(:new).and_return(mock_faraday_connection)
442+
allow(mock_faraday_connection).to receive_message_chain(:headers, :[]=)
443+
allow(mock_faraday_connection).to receive(:post).and_return(mock_response)
444+
445+
expect {
446+
client.payment.payments_api.authorise({})
447+
}.to raise_error(Adyen::NotFoundError) do |error|
448+
expect(error.code).to eq(404)
449+
expect(error.msg).to eq('Not found error')
450+
end
451+
end
452+
434453
end

spec/lem_spec.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,58 @@
6060
expect(result.status)
6161
.to eq(200)
6262
end
63+
64+
it 'makes a legal_entities GET call' do
65+
legal_entity_id = 'LE322JV223222D5F4K62J7465'
66+
response_body = json_from_file('mocks/responses/LegalEntityManagement/get_legal_entity.json')
67+
68+
url = @shared_values[:client].service_url(@shared_values[:service], "legalEntities/#{legal_entity_id}",
69+
@shared_values[:client].legal_entity_management.version)
70+
WebMock.stub_request(:get, url)
71+
.with(
72+
headers: {
73+
'x-api-key' => @shared_values[:client].api_key
74+
}
75+
)
76+
.to_return(
77+
body: response_body
78+
)
79+
80+
result = @shared_values[:client].legal_entity_management.legal_entities_api.get_legal_entity(legal_entity_id)
81+
response_hash = result.response
82+
83+
expect(result.status)
84+
.to eq(200)
85+
expect(response_hash)
86+
.to eq(JSON.parse(response_body))
87+
expect(response_hash)
88+
.to be_a Adyen::HashWithAccessors
89+
expect(response_hash)
90+
.to be_a_kind_of Hash
91+
end
92+
93+
it 'raises an error when calling legal_entities GET call' do
94+
invalid_legal_entity_id = 'NON_EXISTENT_ID'
95+
error_response_body = { status: 404, errorCode: '100', message: 'Legal entity not found', errorType: 'validation' }.to_json
96+
97+
url = @shared_values[:client].service_url(@shared_values[:service], "legalEntities/#{invalid_legal_entity_id}",
98+
@shared_values[:client].legal_entity_management.version)
99+
WebMock.stub_request(:get, url)
100+
.with(
101+
headers: {
102+
'x-api-key' => @shared_values[:client].api_key
103+
}
104+
)
105+
.to_return(
106+
status: 404,
107+
body: error_response_body
108+
)
109+
110+
expect do
111+
@shared_values[:client].legal_entity_management.legal_entities_api.get_legal_entity(invalid_legal_entity_id)
112+
end.to raise_error(Adyen::NotFoundError) do |error|
113+
expect(error.code).to eq(404)
114+
expect(error.msg).to eq('Legal entity not found ErrorCode: 100')
115+
end
116+
end
63117
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"id": "LE322JV223222D5F4K62J7465",
3+
"type": "Organization",
4+
"status": "Active",
5+
"legalEntityCode": "LE322JV223222D5F4K62J7465",
6+
"reference": "MyCompany",
7+
"capabilities": [
8+
{
9+
"id": "capability_1",
10+
"requested": true,
11+
"allowed": true
12+
}
13+
]
14+
}

0 commit comments

Comments
 (0)