Skip to content

Commit 76d547c

Browse files
authored
Fix API key bug when adding credit card to referral user (#361)
- Fix bug where API key switch during referral credit card add does not switch back after request - Add unit test to verify API key switching is correct - Add Mock key to TestUtils
1 parent 6b64d76 commit 76d547c

File tree

5 files changed

+27
-7
lines changed

5 files changed

+27
-7
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## Next Release
4+
- Fix bug where the temporary internal API key switch when adding a credit card to a referral user was not reverted after the request.
5+
- After adding a credit card to a referral user, the existing Client would be misconfigured for following requests.
6+
37
## v4.0.1 (2022-10-24)
48

59
- `myInsurance.Refresh()` function HTTP method fixed from `PATCH` to `GET`

EasyPost.Tests/ServicesTests/PartnerServiceTest.cs

+3
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ public async Task TestAddCreditCardToUser()
8888
Assert.IsType<PaymentMethod>(paymentMethod);
8989
Assert.NotNull(paymentMethod.Id);
9090
Assert.EndsWith(paymentMethod.Last4, card.Number);
91+
92+
// Assert that the original API key was restored to the client properly after the request
93+
Assert.Equal(TestUtils.GetApiKey(TestUtils.ApiKey.Mock), Client.Configuration.ApiKey);
9194
}
9295

9396
[Fact]

EasyPost.Tests/TestUtils.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace EasyPost.Tests._Utilities
1717
{
1818
public class TestUtils
1919
{
20-
private const string ApiKeyFailedToPull = "couldnotpullapikey";
20+
internal const string ApiKeyFailedToPull = "couldnotpullapikey";
2121

2222
private static readonly List<string> BodyCensors = new()
2323
{
@@ -50,7 +50,8 @@ public enum ApiKey
5050
Test,
5151
Production,
5252
Partner,
53-
Referral
53+
Referral,
54+
Mock
5455
}
5556

5657
public static string GetSourceFileDirectory([CallerFilePath] string sourceFilePath = "") => Path.GetDirectoryName(sourceFilePath);
@@ -72,6 +73,9 @@ internal static string GetApiKey(ApiKey apiKey)
7273
case ApiKey.Referral:
7374
keyName = "REFERRAL_USER_PROD_API_KEY";
7475
break;
76+
case ApiKey.Mock:
77+
keyName = "EASYPOST_MOCK_API_KEY"; // does not exist, will trigger to use ApiKeyFailedToPull
78+
break;
7579
default:
7680
throw new Exception(Constants.ErrorMessages.InvalidApiKeyType);
7781
}

EasyPost.Tests/_Utilities/UnitTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ protected virtual IEnumerable<TestUtils.MockRequest> MockRequests
9898
protected void UseMockClient(IEnumerable<TestUtils.MockRequest>? mockRequestsOverride = null)
9999
{
100100
// set up the mock client
101-
Client = new TestUtils.MockClient(new Client("mock_api_key")); // API key doesn't matter for mock client, since no real requests are made);
101+
Client = new TestUtils.MockClient(new Client(TestUtils.GetApiKey(TestUtils.ApiKey.Mock))); // API key doesn't matter for mock client, since no real requests are made);
102102

103103
// add the mock requests to the mock client
104104
((TestUtils.MockClient)Client).AddMockRequests(mockRequestsOverride ?? MockRequests);

EasyPost/Services/PartnerService.cs

+13-4
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,19 @@ private async Task<PaymentMethod> CreateEasypostCreditCard(string referralApiKey
117117
}
118118
};
119119

120-
// Custom override client with new API key
121-
EasyPostClient tempClient = Client!;
122-
tempClient.Configuration.ApiKey = referralApiKey;
123-
return await tempClient.Request<PaymentMethod>(Method.Post, "credit_cards", ApiVersion.Current, parameters);
120+
// Store the old API key
121+
string oldApiKey = Client!.Configuration.ApiKey;
122+
123+
// Change API key temporarily to referral user's API key.
124+
Client.Configuration.ApiKey = referralApiKey;
125+
126+
// Make request
127+
PaymentMethod paymentMethod = await Client.Request<PaymentMethod>(Method.Post, "credit_cards", ApiVersion.Current, parameters);
128+
129+
// Restore old API key
130+
Client!.Configuration.ApiKey = oldApiKey;
131+
132+
return paymentMethod;
124133
}
125134

126135
/// <summary>

0 commit comments

Comments
 (0)