From 54d1b481e1d69b115d68d0b9f55f49aec860a193 Mon Sep 17 00:00:00 2001 From: Justintime50 <39606064+Justintime50@users.noreply.github.com> Date: Fri, 11 Apr 2025 11:15:14 -0600 Subject: [PATCH] feat: route Amazon account on create --- CHANGELOG.md | 1 + src/constants.js | 3 + src/services/carrier_account_service.js | 9 +- .../recording.har | 303 ++++++++++++++++++ test/services/carrier_account.test.js | 15 +- 5 files changed, 328 insertions(+), 3 deletions(-) create mode 100644 test/cassettes/CarrierAccount-Service_2076435725/creates-an-Amazon-carrier-account_772644036/recording.har diff --git a/CHANGELOG.md b/CHANGELOG.md index bfffe0421..ee8058ff5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - `betaReferralCustomer.createBankAccountClientSecret` - `referralCustomer.addCreditCardFromStripe` - `referralCustomer.addBankAccountFromStripe` +- Routes `AmazonShippingAccount` to the correct endpoint on create - Fixes all references to the docs - Properly returns the response body of the following functions: `addPaymentMethod`, `refundByAmount`, `refundByPaymentLog` - `findMatchingMockRequest` mocking function made private diff --git a/src/constants.js b/src/constants.js index bbeef9386..6d05f6a43 100644 --- a/src/constants.js +++ b/src/constants.js @@ -10,6 +10,9 @@ export default class Constants { static get UPS_OAUTH_CARRIER_TYPES() { return ['UpsAccount', 'UpsMailInnovationsAccount', 'UpsSurepostAccount']; } + static get CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_OAUTH() { + return ['AmazonShippingAccount']; + } static EXTERNAL_API_CALL_FAILED = 'Communication with %s failed, please try again later'; static INVALID_API_KEY_TYPE = 'Invalid API key type.'; static INVALID_PARAMETER = 'Invalid parameter: %s.'; diff --git a/src/services/carrier_account_service.js b/src/services/carrier_account_service.js index 1237bd0ef..df9879094 100644 --- a/src/services/carrier_account_service.js +++ b/src/services/carrier_account_service.js @@ -82,10 +82,12 @@ export default (easypostClient) => static _selectCarrierAccountCreationEndpoint(carrierAccountType) { if (Constants.CARRIER_ACCOUNTS_WITH_CUSTOM_CREATE_WORKFLOWS.includes(carrierAccountType)) { return 'carrier_accounts/register'; - } - if (Constants.UPS_OAUTH_CARRIER_TYPES.includes(carrierAccountType)) { + } else if (Constants.UPS_OAUTH_CARRIER_TYPES.includes(carrierAccountType)) { return 'ups_oauth_registrations'; + } else if (Constants.CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_OAUTH.includes(carrierAccountType)) { + return 'carrier_accounts/register_oauth'; } + return 'carrier_accounts'; } @@ -113,7 +115,10 @@ export default (easypostClient) => static _wrapCarrierAccountParams(carrierAccountType, params) { if (Constants.UPS_OAUTH_CARRIER_TYPES.includes(carrierAccountType)) { return { ups_oauth_registrations: params }; + } else if (Constants.CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_OAUTH.includes(carrierAccountType)) { + return { carrier_account_oauth_registrations: params }; } + return { carrier_account: params }; } diff --git a/test/cassettes/CarrierAccount-Service_2076435725/creates-an-Amazon-carrier-account_772644036/recording.har b/test/cassettes/CarrierAccount-Service_2076435725/creates-an-Amazon-carrier-account_772644036/recording.har new file mode 100644 index 000000000..2f20c2041 --- /dev/null +++ b/test/cassettes/CarrierAccount-Service_2076435725/creates-an-Amazon-carrier-account_772644036/recording.har @@ -0,0 +1,303 @@ +{ + "log": { + "_recordingName": "CarrierAccount Service/creates an Amazon carrier account", + "creator": { + "comment": "persister:fs", + "name": "Polly.JS", + "version": "6.0.6" + }, + "entries": [ + { + "_id": "d55a133132db5170164145dac80f93b8", + "_order": 0, + "cache": {}, + "request": { + "bodySize": 72, + "cookies": [], + "headers": [ + { + "name": "accept-encoding", + "value": "gzip, deflate" + }, + { + "name": "accept", + "value": "application/json" + }, + { + "name": "content-type", + "value": "application/json" + }, + { + "name": "content-length", + "value": 72 + }, + { + "name": "host", + "value": "api.easypost.com" + } + ], + "headersSize": 413, + "httpVersion": "HTTP/1.1", + "method": "POST", + "postData": { + "mimeType": "application/json", + "params": [], + "text": "{\"carrier_account_oauth_registrations\":{\"type\":\"AmazonShippingAccount\"}}" + }, + "queryString": [], + "url": "https://api.easypost.com/v2/carrier_accounts/register_oauth" + }, + "response": { + "bodySize": 904, + "content": { + "encoding": "base64", + "mimeType": "application/json; charset=utf-8", + "size": 904, + "text": "{\"id\":\"ca_9c3a0be772744f33bfd6ec719740f76e\",\"object\":\"CarrierAccount\",\"type\":\"AmazonShippingAccount\",\"clone\":false,\"created_at\":\"2025-04-11T17:14:34Z\",\"updated_at\":\"2025-04-11T17:14:34Z\",\"description\":null,\"reference\":null,\"billing_type\":\"carrier\",\"readable\":\"Amazon Shipping\",\"logo\":null,\"fields\":[],\"credentials\":{},\"test_credentials\":null}" + }, + "cookies": [], + "headers": [ + { + "name": "x-frame-options", + "value": "SAMEORIGIN" + }, + { + "name": "x-xss-protection", + "value": "1; mode=block" + }, + { + "name": "x-content-type-options", + "value": "nosniff" + }, + { + "name": "x-download-options", + "value": "noopen" + }, + { + "name": "x-permitted-cross-domain-policies", + "value": "none" + }, + { + "name": "referrer-policy", + "value": "strict-origin-when-cross-origin" + }, + { + "name": "x-ep-request-uuid", + "value": "3275da7467f94dfae2ca7b33005cedb7" + }, + { + "name": "cache-control", + "value": "private, no-cache, no-store" + }, + { + "name": "pragma", + "value": "no-cache" + }, + { + "name": "expires", + "value": "0" + }, + { + "name": "content-type", + "value": "application/json; charset=utf-8" + }, + { + "name": "x-runtime", + "value": "0.075866" + }, + { + "name": "content-encoding", + "value": "gzip" + }, + { + "name": "transfer-encoding", + "value": "chunked" + }, + { + "name": "x-node", + "value": "bigweb38nuq" + }, + { + "name": "x-version-label", + "value": "easypost-202504102051-85835c857e-master" + }, + { + "name": "x-backend", + "value": "easypost" + }, + { + "name": "x-proxied", + "value": "intlb3nuq 284c5d344a, extlb1nuq 99aac35317" + }, + { + "name": "strict-transport-security", + "value": "max-age=31536000; includeSubDomains; preload" + }, + { + "name": "connection", + "value": "close" + } + ], + "headersSize": 710, + "httpVersion": "HTTP/1.1", + "redirectURL": "", + "status": 201, + "statusText": "Created" + }, + "startedDateTime": "2025-04-11T17:14:34.602Z", + "time": 263, + "timings": { + "blocked": -1, + "connect": -1, + "dns": -1, + "receive": 0, + "send": 0, + "ssl": -1, + "wait": 263 + } + }, + { + "_id": "5c679e434ae156b7dbf7285198163122", + "_order": 0, + "cache": {}, + "request": { + "bodySize": 0, + "cookies": [], + "headers": [ + { + "name": "accept-encoding", + "value": "gzip, deflate" + }, + { + "name": "accept", + "value": "application/json" + }, + { + "name": "content-type", + "value": "application/json" + }, + { + "name": "host", + "value": "api.easypost.com" + } + ], + "headersSize": 416, + "httpVersion": "HTTP/1.1", + "method": "DELETE", + "queryString": [], + "url": "https://api.easypost.com/v2/carrier_accounts/ca_9c3a0be772744f33bfd6ec719740f76e" + }, + "response": { + "bodySize": 44, + "content": { + "encoding": "base64", + "mimeType": "application/json; charset=utf-8", + "size": 44, + "text": "{}" + }, + "cookies": [], + "headers": [ + { + "name": "x-frame-options", + "value": "SAMEORIGIN" + }, + { + "name": "x-xss-protection", + "value": "1; mode=block" + }, + { + "name": "x-content-type-options", + "value": "nosniff" + }, + { + "name": "x-download-options", + "value": "noopen" + }, + { + "name": "x-permitted-cross-domain-policies", + "value": "none" + }, + { + "name": "referrer-policy", + "value": "strict-origin-when-cross-origin" + }, + { + "name": "x-ep-request-uuid", + "value": "3275da7367f94dfbe2ca7b34005ceded" + }, + { + "name": "cache-control", + "value": "private, no-cache, no-store" + }, + { + "name": "pragma", + "value": "no-cache" + }, + { + "name": "expires", + "value": "0" + }, + { + "name": "content-type", + "value": "application/json; charset=utf-8" + }, + { + "name": "x-runtime", + "value": "0.098200" + }, + { + "name": "content-encoding", + "value": "gzip" + }, + { + "name": "transfer-encoding", + "value": "chunked" + }, + { + "name": "x-node", + "value": "bigweb34nuq" + }, + { + "name": "x-version-label", + "value": "easypost-202504102051-85835c857e-master" + }, + { + "name": "x-backend", + "value": "easypost" + }, + { + "name": "x-proxied", + "value": "intlb3nuq 284c5d344a, extlb1nuq 99aac35317" + }, + { + "name": "strict-transport-security", + "value": "max-age=31536000; includeSubDomains; preload" + }, + { + "name": "connection", + "value": "close" + } + ], + "headersSize": 710, + "httpVersion": "HTTP/1.1", + "redirectURL": "", + "status": 200, + "statusText": "OK" + }, + "startedDateTime": "2025-04-11T17:14:34.869Z", + "time": 204, + "timings": { + "blocked": -1, + "connect": -1, + "dns": -1, + "receive": 0, + "send": 0, + "ssl": -1, + "wait": 204 + } + } + ], + "pages": [], + "version": "1.2" + } +} diff --git a/test/services/carrier_account.test.js b/test/services/carrier_account.test.js index a109f7668..b00e62f06 100644 --- a/test/services/carrier_account.test.js +++ b/test/services/carrier_account.test.js @@ -72,7 +72,20 @@ describe('CarrierAccount Service', function () { expect(carrierAccount).to.be.an.instanceOf(CarrierAccount); expect(carrierAccount.id).to.match(/^ca_/); expect(carrierAccount.type).to.equal(type); - // account number not returned in API response, can't assert + + await client.CarrierAccount.delete(carrierAccount.id); + }); + + it('creates an Amazon carrier account', async function () { + const type = 'AmazonShippingAccount'; + + const data = { type: type }; + + const carrierAccount = await client.CarrierAccount.create(data); + + expect(carrierAccount).to.be.an.instanceOf(CarrierAccount); + expect(carrierAccount.id).to.match(/^ca_/); + expect(carrierAccount.type).to.equal(type); await client.CarrierAccount.delete(carrierAccount.id); });