|
23 | 23 | import com.walmartlabs.concord.client2.*; |
24 | 24 | import org.junit.jupiter.api.Test; |
25 | 25 |
|
26 | | -import static org.junit.jupiter.api.Assertions.assertTrue; |
27 | | -import static org.junit.jupiter.api.Assertions.fail; |
| 26 | +import java.util.List; |
| 27 | + |
| 28 | +import static org.junit.jupiter.api.Assertions.*; |
28 | 29 |
|
29 | 30 | public class ApiKeyIT extends AbstractServerIT { |
30 | 31 |
|
@@ -62,4 +63,38 @@ public void testOwner() throws Exception { |
62 | 63 | cakr = apiKeyResource.createUserApiKey(new CreateApiKeyRequest().username(userAName)); |
63 | 64 | assertTrue(cakr.getOk()); |
64 | 65 | } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testCreatingKeyWithoutUsername() throws Exception { |
| 69 | + String userName = "userA_" + randomString(); |
| 70 | + |
| 71 | + UsersApi usersApi = new UsersApi(getApiClient()); |
| 72 | + CreateUserResponse user = usersApi.createOrUpdateUser(new CreateUserRequest() |
| 73 | + .username(userName) |
| 74 | + .type(CreateUserRequest.TypeEnum.LOCAL)); |
| 75 | + |
| 76 | + // the new user has no api keys initially |
| 77 | + |
| 78 | + ApiKeysApi apiKeyResource = new ApiKeysApi(getApiClient()); |
| 79 | + List<ApiKeyEntry> keys = apiKeyResource.listUserApiKeys(user.getId()); |
| 80 | + assertEquals(0, keys.size()); |
| 81 | + |
| 82 | + // admin creates a new api key for the new user |
| 83 | + |
| 84 | + CreateApiKeyResponse cakr = apiKeyResource.createUserApiKey(new CreateApiKeyRequest().username(userName)); |
| 85 | + assertTrue(cakr.getOk()); |
| 86 | + keys = apiKeyResource.listUserApiKeys(user.getId()); |
| 87 | + assertEquals(1, keys.size()); |
| 88 | + |
| 89 | + // the new user creates another api key for themselves |
| 90 | + |
| 91 | + setApiKey(cakr.getKey()); |
| 92 | + cakr = apiKeyResource.createUserApiKey(new CreateApiKeyRequest()); |
| 93 | + assertTrue(cakr.getOk()); |
| 94 | + |
| 95 | + // the new user lists all their api keys (should be 2) |
| 96 | + |
| 97 | + keys = apiKeyResource.listUserApiKeys(user.getId()); |
| 98 | + assertEquals(2, keys.size()); |
| 99 | + } |
65 | 100 | } |
0 commit comments