From dec3cbd08d5b62ee5a82048f13c8e055568a4ce6 Mon Sep 17 00:00:00 2001 From: nwithan8 Date: Thu, 28 Sep 2023 14:54:09 -0600 Subject: [PATCH] - Move API key functions to API Key service --- .../com/easypost/service/ApiKeyService.java | 29 ++++++ .../com/easypost/service/UserService.java | 1 + .../{user => api_key}/all_api_keys.json | 0 .../cassettes/{user => api_key}/api_keys.json | 0 .../billing/delete_payment_method.json | 92 +++++++++++++++++ src/test/cassettes/billing/fund_wallet.json | 92 +++++++++++++++++ src/test/java/com/easypost/ApiKeyTest.java | 98 +++++++++++++++++++ src/test/java/com/easypost/UserTest.java | 42 -------- 8 files changed, 312 insertions(+), 42 deletions(-) rename src/test/cassettes/{user => api_key}/all_api_keys.json (100%) rename src/test/cassettes/{user => api_key}/api_keys.json (100%) create mode 100644 src/test/cassettes/billing/delete_payment_method.json create mode 100644 src/test/cassettes/billing/fund_wallet.json create mode 100644 src/test/java/com/easypost/ApiKeyTest.java diff --git a/src/main/java/com/easypost/service/ApiKeyService.java b/src/main/java/com/easypost/service/ApiKeyService.java index a0afa2cbe..c18464e8e 100644 --- a/src/main/java/com/easypost/service/ApiKeyService.java +++ b/src/main/java/com/easypost/service/ApiKeyService.java @@ -1,10 +1,16 @@ package com.easypost.service; +import com.easypost.Constants; import com.easypost.exception.EasyPostException; +import com.easypost.exception.General.FilteringError; import com.easypost.http.Requestor; import com.easypost.http.Requestor.RequestMethod; +import com.easypost.model.ApiKey; import com.easypost.model.ApiKeys; +import java.util.List; +import java.util.Objects; + public class ApiKeyService { private final EasyPostClient client; @@ -28,4 +34,27 @@ public ApiKeys all() throws EasyPostException { return Requestor.request(RequestMethod.GET, endpoint, null, ApiKeys.class, client); } + + /** + * Get this User's API keys. + * + * @param id The ID of the user. + * @return List of ApiKey objects. + * @throws EasyPostException when the request fails. + */ + public List retrieveApiKeysForUser(final String id) throws EasyPostException { + ApiKeys parentKeys = all(); + + if (Objects.equals(id, parentKeys.getId())) { + return parentKeys.getKeys(); + } + + for (int i = 0; i < parentKeys.getChildren().size(); i++) { + if (id.equals(parentKeys.getChildren().get(i).getId())) { + return parentKeys.getChildren().get(i).getKeys(); + } + } + + throw new FilteringError(String.format(Constants.ErrorMessages.NO_OBJECT_FOUND, "API keys")); + } } diff --git a/src/main/java/com/easypost/service/UserService.java b/src/main/java/com/easypost/service/UserService.java index 0ac566f11..25d69df2f 100644 --- a/src/main/java/com/easypost/service/UserService.java +++ b/src/main/java/com/easypost/service/UserService.java @@ -100,6 +100,7 @@ public void delete(final String id) throws EasyPostException { /** * Get this User's API keys. * + * @deprecated Use {@link ApiKeyService#retrieveApiKeysForUser(String)} instead. * @param id The ID of the user. * @return List of ApiKey objects. * @throws EasyPostException when the request fails. diff --git a/src/test/cassettes/user/all_api_keys.json b/src/test/cassettes/api_key/all_api_keys.json similarity index 100% rename from src/test/cassettes/user/all_api_keys.json rename to src/test/cassettes/api_key/all_api_keys.json diff --git a/src/test/cassettes/user/api_keys.json b/src/test/cassettes/api_key/api_keys.json similarity index 100% rename from src/test/cassettes/user/api_keys.json rename to src/test/cassettes/api_key/api_keys.json diff --git a/src/test/cassettes/billing/delete_payment_method.json b/src/test/cassettes/billing/delete_payment_method.json new file mode 100644 index 000000000..b12571735 --- /dev/null +++ b/src/test/cassettes/billing/delete_payment_method.json @@ -0,0 +1,92 @@ +[ + { + "recordedAt": 1686858757, + "request": { + "body": "", + "method": "DELETE", + "headers": { + "Accept-Charset": [ + "UTF-8" + ], + "User-Agent": [ + "REDACTED" + ] + }, + "uri": "https://api.easypost.com/v2/credit_cards/card_..." + }, + "response": { + "body": "{\n \"error\": {\n \"code\": \"NOT_FOUND\",\n \"message\": \"The requested resource could not be found.\",\n \"errors\": []\n }\n}", + "httpVersion": null, + "headers": { + "null": [ + "HTTP/1.1 404 Not Found" + ], + "content-length": [ + "97" + ], + "expires": [ + "0" + ], + "x-node": [ + "bigweb7nuq" + ], + "x-frame-options": [ + "SAMEORIGIN" + ], + "x-download-options": [ + "noopen" + ], + "x-permitted-cross-domain-policies": [ + "none" + ], + "x-backend": [ + "easypost" + ], + "pragma": [ + "no-cache" + ], + "strict-transport-security": [ + "max-age\u003d31536000; includeSubDomains; preload" + ], + "x-canary": [ + "direct" + ], + "x-xss-protection": [ + "1; mode\u003dblock" + ], + "x-content-type-options": [ + "nosniff" + ], + "x-ep-request-uuid": [ + "cd3d20fe648b6c05f40a811300080b54" + ], + "x-proxied": [ + "intlb1nuq 255dce5529", + "intlb1wdc 255dce5529", + "extlb4wdc 5ab12a3ed2" + ], + "referrer-policy": [ + "strict-origin-when-cross-origin" + ], + "x-runtime": [ + "0.015106" + ], + "content-type": [ + "application/json; charset\u003dutf-8" + ], + "x-version-label": [ + "easypost-202306142228-cc76ba2337-master" + ], + "cache-control": [ + "private, no-cache, no-store" + ] + }, + "status": { + "code": 404, + "message": "Not Found" + }, + "uri": "https://api.easypost.com/v2/credit_cards/card_..." + }, + "duration": 626 + } +] \ No newline at end of file diff --git a/src/test/cassettes/billing/fund_wallet.json b/src/test/cassettes/billing/fund_wallet.json new file mode 100644 index 000000000..9c5811c5b --- /dev/null +++ b/src/test/cassettes/billing/fund_wallet.json @@ -0,0 +1,92 @@ +[ + { + "recordedAt": 1686858758, + "request": { + "body": "{\n \"amount\": \"2000\"\n}", + "method": "POST", + "headers": { + "Accept-Charset": [ + "UTF-8" + ], + "User-Agent": [ + "REDACTED" + ], + "Content-Type": [ + "application/json" + ] + }, + "uri": "https://api.easypost.com/v2/credit_cards/card_.../charges" + }, + "response": { + "body": "{\n \"error\": {\n \"code\": \"NOT_FOUND\",\n \"message\": \"The requested resource could not be found.\",\n \"errors\": []\n }\n}", + "httpVersion": null, + "headers": { + "null": [ + "HTTP/1.1 404 Not Found" + ], + "content-length": [ + "97" + ], + "expires": [ + "0" + ], + "x-node": [ + "bigweb5nuq" + ], + "x-frame-options": [ + "SAMEORIGIN" + ], + "x-download-options": [ + "noopen" + ], + "x-permitted-cross-domain-policies": [ + "none" + ], + "x-backend": [ + "easypost" + ], + "pragma": [ + "no-cache" + ], + "strict-transport-security": [ + "max-age\u003d31536000; includeSubDomains; preload" + ], + "x-xss-protection": [ + "1; mode\u003dblock" + ], + "x-content-type-options": [ + "nosniff" + ], + "x-ep-request-uuid": [ + "cd3d20ff648b6c06f43facaa00080bcb" + ], + "x-proxied": [ + "intlb2nuq 255dce5529", + "intlb1wdc 255dce5529", + "extlb4wdc 5ab12a3ed2" + ], + "referrer-policy": [ + "strict-origin-when-cross-origin" + ], + "x-runtime": [ + "0.015488" + ], + "content-type": [ + "application/json; charset\u003dutf-8" + ], + "x-version-label": [ + "easypost-202306142228-cc76ba2337-master" + ], + "cache-control": [ + "private, no-cache, no-store" + ] + }, + "status": { + "code": 404, + "message": "Not Found" + }, + "uri": "https://api.easypost.com/v2/credit_cards/card_.../charges" + }, + "duration": 350 + } +] \ No newline at end of file diff --git a/src/test/java/com/easypost/ApiKeyTest.java b/src/test/java/com/easypost/ApiKeyTest.java new file mode 100644 index 000000000..8fff27c65 --- /dev/null +++ b/src/test/java/com/easypost/ApiKeyTest.java @@ -0,0 +1,98 @@ +package com.easypost; + +import com.easypost.exception.EasyPostException; +import com.easypost.exception.General.FilteringError; +import com.easypost.model.ApiKey; +import com.easypost.model.ApiKeys; +import com.easypost.model.User; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public final class ApiKeyTest { + private static String testUserId = null; + private static TestUtils.VCR vcr; + + /** + * Set up the testing environment for this file. + * + * @throws EasyPostException when the request fails. + */ + @BeforeAll + public static void setUp() throws EasyPostException { + vcr = new TestUtils.VCR("api_key", TestUtils.ApiKey.PRODUCTION); + } + + /** + * Clean up test attributes after each unit test. + */ + @AfterEach + public void cleanup() { + if (testUserId != null) { + try { + User user = vcr.client.user.retrieve(testUserId); + vcr.client.user.delete(user.getId()); + testUserId = null; + } catch (Exception e) { + // in case we try to delete something that's already been deleted + } + } + } + + /** + * Create a user. + * + * @return User object + */ + private static User createUser() throws EasyPostException { + Map params = new HashMap<>(); + params.put("name", "Test User"); + User user = vcr.client.user.create(params); + testUserId = user.getId(); // trigger deletion after test + return user; + } + + /** + * Test retrieving all API keys. + * + * @throws EasyPostException when the request fails. + */ + @Test + public void testAllApiKeys() throws EasyPostException { + vcr.setUpTest("all_api_keys"); + + ApiKeys apikeys = vcr.client.apiKey.all(); + + assertInstanceOf(ApiKeys.class, apikeys); + + List apiKeys = vcr.client.apiKey.retrieveApiKeysForUser(apikeys.getId()); + + assertNotNull(apiKeys); + } + + /** + * Test retrieving all API keys for a user. + * + * @throws EasyPostException when the request fails. + */ + @Test + public void testApiKeys() throws EasyPostException { + vcr.setUpTest("api_keys"); + + User user = createUser(); + + List apiKeys = vcr.client.apiKey.retrieveApiKeysForUser(user.getId()); + + assertNotNull(apiKeys); + + assertThrows(FilteringError.class, () -> vcr.client.apiKey.retrieveApiKeysForUser("invalid_id")); + } +} diff --git a/src/test/java/com/easypost/UserTest.java b/src/test/java/com/easypost/UserTest.java index 52bd809c0..4cb51ca3b 100644 --- a/src/test/java/com/easypost/UserTest.java +++ b/src/test/java/com/easypost/UserTest.java @@ -1,9 +1,6 @@ package com.easypost; import com.easypost.exception.EasyPostException; -import com.easypost.exception.General.FilteringError; -import com.easypost.model.ApiKey; -import com.easypost.model.ApiKeys; import com.easypost.model.Brand; import com.easypost.model.User; import org.junit.jupiter.api.AfterEach; @@ -11,14 +8,11 @@ import org.junit.jupiter.api.Test; import java.util.HashMap; -import java.util.List; import java.util.Map; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertInstanceOf; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; public final class UserTest { @@ -161,42 +155,6 @@ public void testDelete() throws EasyPostException { assertDoesNotThrow(() -> vcr.client.user.delete(user.getId())); } - /** - * Test retrieving all API keys. - * - * @throws EasyPostException when the request fails. - */ - @Test - public void testAllApiKeys() throws EasyPostException { - vcr.setUpTest("all_api_keys"); - - ApiKeys apikeys = vcr.client.apiKey.all(); - - assertInstanceOf(ApiKeys.class, apikeys); - - List apiKeys = vcr.client.user.apiKeys(apikeys.getId()); - - assertNotNull(apiKeys); - } - - /** - * Test retrieving all API keys for a user. - * - * @throws EasyPostException when the request fails. - */ - @Test - public void testApiKeys() throws EasyPostException { - vcr.setUpTest("api_keys"); - - User user = createUser(); - - List apiKeys = vcr.client.user.apiKeys(user.getId()); - - assertNotNull(apiKeys); - - assertThrows(FilteringError.class, () -> vcr.client.user.apiKeys("invlaid_id")); - } - /** * Test updating a brand. *