Skip to content

Commit 1c31a43

Browse files
committed
feat: Amazon carrier account creation
1 parent e65653d commit 1c31a43

6 files changed

Lines changed: 254 additions & 27 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- `beta_referral_customer.create_bank_account_client_secret`
99
- `referral_customer.add_credit_card_from_stripe`
1010
- `referral_customer.add_bank_account_from_stripe`
11+
- Routes `AmazonShippingAccount` to the proper create endpoint
1112
- Fixes error parsing
1213
- Allows for alternative format of `errors` field
1314
- Corrects available properties of an `EasyPostError` and `ApiError` (`code` and `field` removed from `EasyPostError`, `message` unfurled and explicitly added to `ApiError`)

lib/easypost/services/carrier_account.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
class EasyPost::Services::CarrierAccount < EasyPost::Services::Service
44
CUSTOM_WORKFLOW_CARRIER_TYPES = %w[FedexAccount FedexSmartpostAccount].freeze
55
UPS_OAUTH_CARRIER_ACCOUNT_TYPES = %w[UpsAccount UpsMailInnovationsAccount UpsSurepostAccount].freeze
6+
CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_OAUTH = %w[AmazonShippingAccount].freeze
67
MODEL_CLASS = EasyPost::Models::CarrierAccount # :nodoc:
78

89
# Create a carrier account
@@ -15,6 +16,8 @@ def create(params = {})
1516
'carrier_accounts/register'
1617
elsif UPS_OAUTH_CARRIER_ACCOUNT_TYPES.include?(carrier_account_type)
1718
'ups_oauth_registrations'
19+
elsif CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_OAUTH.include?(carrier_account_type)
20+
'carrier_accounts/register_oauth'
1821
else
1922
'carrier_accounts'
2023
end
@@ -63,6 +66,8 @@ def delete(id)
6366
def select_top_layer_key(carrier_account_type)
6467
if UPS_OAUTH_CARRIER_ACCOUNT_TYPES.include?(carrier_account_type)
6568
'ups_oauth_registrations'
69+
elsif CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_OAUTH.include?(carrier_account_type)
70+
'carrier_account_oauth_registrations'
6671
else
6772
'carrier_account'
6873
end

spec/carrier_account_spec.rb

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
client.carrier_account.delete(carrier_account.id)
1818
end
1919

20-
it 'creates an UPS account' do
20+
it 'creates a UPS account' do
2121
carrier_account = client.carrier_account.create({ type: 'UpsAccount', account_number: '123456789' })
2222

2323
expect(carrier_account).to be_an_instance_of(EasyPost::Models::CarrierAccount)
@@ -28,15 +28,26 @@
2828
client.carrier_account.delete(carrier_account.id)
2929
end
3030

31-
it 'sends FedexAccount to the correct endpoint' do
32-
allow(client).to receive(:make_request).with(
33-
:post, 'carrier_accounts/register',
34-
{ carrier_account: { type: 'FedexAccount' } },
35-
).and_return({ 'id' => 'ca_123' })
31+
it 'creates a FedEx account' do
32+
expect {
33+
client.carrier_account.create({ type: 'FedexAccount', registration_data: {} })
34+
}.to raise_error(EasyPost::Errors::ApiError) { |error|
35+
expect(error.status_code).to eq(422)
36+
expect(error.errors.any? do |err|
37+
err['field'] == 'account_number' && err['message'] == 'must be present and a string'
38+
end).to be true
39+
}
40+
end
3641

37-
response = client.carrier_account.create(type: 'FedexAccount')
42+
it 'creates an Amazon account' do
43+
carrier_account = client.carrier_account.create({ type: 'AmazonShippingAccount', account_number: '123456789' })
3844

39-
expect(response['id']).to eq('ca_123')
45+
expect(carrier_account).to be_an_instance_of(EasyPost::Models::CarrierAccount)
46+
expect(carrier_account.id).to match('ca_')
47+
expect(carrier_account.type).to eq('AmazonShippingAccount')
48+
49+
# Remove the carrier account once we have tested it so we don't pollute the account with test accounts
50+
client.carrier_account.delete(carrier_account.id)
4051
end
4152
end
4253

spec/cassettes/carrier_account/EasyPost_Services_CarrierAccount_create_creates_a_FedEx_account.yml

Lines changed: 82 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/cassettes/carrier_account/EasyPost_Services_CarrierAccount_create_creates_an_UPS_account.yml renamed to spec/cassettes/carrier_account/EasyPost_Services_CarrierAccount_create_creates_a_UPS_account.yml

Lines changed: 17 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/cassettes/carrier_account/EasyPost_Services_CarrierAccount_create_creates_an_Amazon_account.yml

Lines changed: 130 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)