-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Move API key functions to API Key service
- Loading branch information
Showing
8 changed files
with
312 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, Object> 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<ApiKey> 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<ApiKey> apiKeys = vcr.client.apiKey.retrieveApiKeysForUser(user.getId()); | ||
|
||
assertNotNull(apiKeys); | ||
|
||
assertThrows(FilteringError.class, () -> vcr.client.apiKey.retrieveApiKeysForUser("invalid_id")); | ||
} | ||
} |
Oops, something went wrong.