Skip to content

Commit 20f94a5

Browse files
authored
Merge pull request #323 from EasyPost/amazon_account
feat: Amazon carrier account creation
2 parents e65653d + 62b2f3d commit 20f94a5

6 files changed

+256
-27
lines changed

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: 21 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,28 @@
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(
37+
error.errors.any? do |err|
38+
err['field'] == 'account_number' && err['message'] == 'must be present and a string'
39+
end,
40+
).to be true
41+
}
42+
end
3643

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

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

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.
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)