|
302 | 302 | expect(client.service_url_base('Disputes')) |
303 | 303 | .to eq('https://ca-test.adyen.com/ca/services/DisputesService') |
304 | 304 | end |
| 305 | + |
| 306 | + it 'raises FormatError on 400 response and checks content' do |
| 307 | + client = Adyen::Client.new(api_key: 'api_key', env: :test) |
| 308 | + mock_faraday_connection = double(Faraday::Connection) |
| 309 | + error_body = { |
| 310 | + status: 400, |
| 311 | + errorCode: "702", |
| 312 | + message: "Structure of CreateCheckoutSessionRequest contains the following unknown fields: [paymentMethod]", |
| 313 | + errorType: "validation" |
| 314 | + } |
| 315 | + mock_response = Faraday::Response.new(status: 400, body: error_body) |
| 316 | + |
| 317 | + allow(Faraday).to receive(:new).and_return(mock_faraday_connection) |
| 318 | + allow(mock_faraday_connection).to receive_message_chain(:headers, :[]=) |
| 319 | + allow(mock_faraday_connection).to receive(:post).and_return(mock_response) |
| 320 | + |
| 321 | + expect { |
| 322 | + client.checkout.payments_api.payments({}) |
| 323 | + }.to raise_error(Adyen::FormatError) do |error| |
| 324 | + expect(error.code).to eq(400) |
| 325 | + expect(error.msg).to eq('Structure of CreateCheckoutSessionRequest contains the following unknown fields: [paymentMethod] ErrorCode: 702') |
| 326 | + expect(error.response[:errorCode]).to eq('702') |
| 327 | + end |
| 328 | + end |
| 329 | + |
| 330 | + it 'raises ValidationError on 422 response with RestServiceError (based on RFC 7807)' do |
| 331 | + client = Adyen::Client.new(api_key: 'api_key', env: :test) |
| 332 | + mock_faraday_connection = double(Faraday::Connection) |
| 333 | + error_body = { |
| 334 | + type: "https://docs.adyen.com/errors/validation", |
| 335 | + title: "The request is missing required fields or contains invalid data.", |
| 336 | + status: 422, |
| 337 | + detail: "It is mandatory to specify a legalEntityId when creating a new account holder.", |
| 338 | + invalidFields: [{ "name" => "legalEntityId", "message" => "legalEntityId is not provided" }], |
| 339 | + errorCode: "30_011" |
| 340 | + } |
| 341 | + mock_response = Faraday::Response.new(status: 422, body: error_body) |
| 342 | + |
| 343 | + allow(Faraday).to receive(:new).and_return(mock_faraday_connection) |
| 344 | + allow(mock_faraday_connection).to receive_message_chain(:headers, :[]=) |
| 345 | + allow(mock_faraday_connection).to receive(:post).and_return(mock_response) |
| 346 | + |
| 347 | + expect { |
| 348 | + client.checkout.payments_api.payments({}) |
| 349 | + }.to raise_error(Adyen::ValidationError) do |error| |
| 350 | + expect(error.code).to eq(422) |
| 351 | + expect(error.msg).to eq('It is mandatory to specify a legalEntityId when creating a new account holder. ErrorCode: 30_011') |
| 352 | + expect(error.response[:errorCode]).to eq('30_011') |
| 353 | + expect(error.response[:invalidFields]).to have_attributes(size: 1) |
| 354 | + end |
| 355 | + end |
| 356 | + |
| 357 | + it 'raises ValidationError on 422 response with ServiceError (legacy)' do |
| 358 | + client = Adyen::Client.new(api_key: 'api_key', env: :test) |
| 359 | + mock_faraday_connection = double(Faraday::Connection) |
| 360 | + error_body = { |
| 361 | + status: 422, |
| 362 | + errorCode: "14_030", |
| 363 | + message: "Return URL is missing.", |
| 364 | + errorType: "validation", |
| 365 | + pspReference: "8816118280275544" |
| 366 | + } |
| 367 | + mock_response = Faraday::Response.new(status: 422, body: error_body) |
| 368 | + |
| 369 | + allow(Faraday).to receive(:new).and_return(mock_faraday_connection) |
| 370 | + allow(mock_faraday_connection).to receive_message_chain(:headers, :[]=) |
| 371 | + allow(mock_faraday_connection).to receive(:post).and_return(mock_response) |
| 372 | + |
| 373 | + expect { |
| 374 | + client.checkout.payments_api.payments({}) |
| 375 | + }.to raise_error(Adyen::ValidationError) do |error| |
| 376 | + expect(error.code).to eq(422) |
| 377 | + expect(error.msg).to eq('Return URL is missing. ErrorCode: 14_030') |
| 378 | + expect(error.response[:errorCode]).to eq('14_030') |
| 379 | + end |
| 380 | + end |
| 381 | + |
| 382 | + it 'raises ServerError on 500 response and checks content' do |
| 383 | + client = Adyen::Client.new(api_key: 'api_key', env: :test) |
| 384 | + mock_faraday_connection = double(Faraday::Connection) |
| 385 | + error_body = { |
| 386 | + status: 500, |
| 387 | + errorCode: "999", |
| 388 | + message: "Unexpected error.", |
| 389 | + errorType: "server error" |
| 390 | + } |
| 391 | + mock_response = Faraday::Response.new(status: 500, body: error_body) |
| 392 | + |
| 393 | + allow(Faraday).to receive(:new).and_return(mock_faraday_connection) |
| 394 | + allow(mock_faraday_connection).to receive_message_chain(:headers, :[]=) |
| 395 | + allow(mock_faraday_connection).to receive(:post).and_return(mock_response) |
| 396 | + |
| 397 | + expect { |
| 398 | + client.checkout.payments_api.payments({}) |
| 399 | + }.to raise_error(Adyen::ServerError) do |error| |
| 400 | + expect(error.code).to eq(500) |
| 401 | + expect(error.msg).to eq('Unexpected error. ErrorCode: 999') |
| 402 | + end |
| 403 | + end |
305 | 404 | end |
0 commit comments