Skip to content

Commit 9698331

Browse files
authored
Correct test given api key change. (#1711)
Given the tests weren't running, honestly not that bad of a problem after the merge.
1 parent 91db87d commit 9698331

2 files changed

Lines changed: 16 additions & 14 deletions

File tree

cwms-data-api/src/main/java/cwms/cda/data/dao/AuthDao.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public class AuthDao extends Dao<DataApiPrincipal> {
6363
public static final String AUTH_ERROR_MSG = "Authentication failed. The API Key may be invalid or no longer active.";
6464
private static final String API_KEY_V1_PREFIX = "ak1_";
6565
private static final int API_KEY_ID_LENGTH = 12;
66+
private static final int API_KEY_SECRET_LENGTH = 256;
67+
public static final int API_KEY_TOTAL_LENGTH = API_KEY_ID_LENGTH + API_KEY_SECRET_LENGTH;
6668
// At this level we just care that the user has permissions in *any* office
6769
private static final String RETRIEVE_GROUPS_OF_USER =
6870
ResourceHelper.getResourceAsString("/cwms/data/sql/user_groups.sql", AuthDao.class);
@@ -514,7 +516,7 @@ public ApiKey createApiKey(DataApiPrincipal p, ApiKey sourceData) throws CwmsAut
514516
private static String generateSecretKey(SecureRandom randomSource) {
515517
return randomSource.ints('0', 'z') // allow a-zA-Z0-9
516518
.filter(i -> (i <= 57 || i >= 65) && (i <= 90 || i >= 97)) // actually filter to above
517-
.limit(256)
519+
.limit(API_KEY_SECRET_LENGTH)
518520
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
519521
.toString();
520522
}

cwms-data-api/src/test/java/cwms/cda/api/auth/ApiKeyControllerTestIT.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@
4141

4242
/**
4343
* Forced order is used here to allow better error reporting
44-
* but to let all the tests run and make sure
44+
* but to let all the tests run and make sure
4545
*/
4646
@Tag("integration")
4747
@TestMethodOrder(OrderAnnotation.class)
4848
@TestInstance(Lifecycle.PER_CLASS)
4949
public class ApiKeyControllerTestIT extends DataApiTestIT {
5050

51-
51+
5252
private final String KEY_NAME = "TestKey1";
5353
private final String EXPIRED_KEY_NAME = "TestKey2-Expired";
5454

@@ -76,7 +76,7 @@ void test_api_key_creation_no_expiration(String authType, TestAccounts.KeyUser t
7676
.statusCode(is(HttpCode.CREATED.getStatus()))
7777
.body("user-id",is(key.getUserId().toUpperCase()))
7878
.body("key-name",is(key.getKeyName()))
79-
.body("api-key.size()",is(256))
79+
.body("api-key.size()",is(AuthDao.API_KEY_TOTAL_LENGTH))
8080
.body("created",not(equalTo(null)))
8181
.body("expires",is(equalTo(null)))
8282
.extract().as(ApiKey.class);
@@ -90,8 +90,8 @@ void test_api_key_creation_no_expiration(String authType, TestAccounts.KeyUser t
9090
@AuthType(user = TestAccounts.KeyUser.SPK_NORMAL)
9191
void test_api_key_creation_with_expiration(String authType, TestAccounts.KeyUser theUser, RequestSpecification authSpec) {
9292
final String keyName = "TestKey1-Expires";
93-
94-
93+
94+
9595
final ApiKey key = new ApiKey(theUser.getName(),keyName,null,null,ZonedDateTime.now());
9696
final ApiKey expiredKey = new ApiKey(key.getUserId(),EXPIRED_KEY_NAME,null,null,ZonedDateTime.now().minusMinutes(1L));
9797

@@ -108,7 +108,7 @@ void test_api_key_creation_with_expiration(String authType, TestAccounts.KeyUser
108108
.statusCode(is(HttpCode.CREATED.getStatus()))
109109
.body("user-id",is(key.getUserId().toUpperCase()))
110110
.body("key-name",is(key.getKeyName()))
111-
.body("api-key.size()",is(256))
111+
.body("api-key.size()",is(AuthDao.API_KEY_TOTAL_LENGTH))
112112
.body("created",not(equalTo(null)))
113113
.body("expires",not(equalTo(null)))
114114
.extract().as(ApiKey.class);
@@ -127,15 +127,15 @@ void test_api_key_creation_with_expiration(String authType, TestAccounts.KeyUser
127127
.statusCode(is(HttpCode.CREATED.getStatus()))
128128
.body("user-id",is(expiredKey.getUserId().toUpperCase()))
129129
.body("key-name",is(expiredKey.getKeyName()))
130-
.body("api-key.size()",is(256))
130+
.body("api-key.size()",is(AuthDao.API_KEY_TOTAL_LENGTH))
131131
.body("created",not(equalTo(null)))
132132
.body("expires",not(equalTo(null)))
133133
.extract().as(ApiKey.class);
134134
realKeys.add(returnedKey);
135135

136136

137137
final String bodyWithSpecificExpiresFormat = "{\"user-id\": \"" + theUser.getName() + "\",\"key-name\": \"foo\",\"api-key\": \"string\",\"expires\": \"2023-09-23T14:20:00.908Z\"}";
138-
returnedKey =
138+
returnedKey =
139139
given()
140140
.log().ifValidationFails(LogDetail.ALL,true)
141141
.spec(authSpec)
@@ -148,7 +148,7 @@ void test_api_key_creation_with_expiration(String authType, TestAccounts.KeyUser
148148
.statusCode(is(HttpCode.CREATED.getStatus()))
149149
.body("user-id",is(expiredKey.getUserId().toUpperCase()))
150150
.body("key-name",is("foo"))
151-
.body("api-key.size()",is(256))
151+
.body("api-key.size()",is(AuthDao.API_KEY_TOTAL_LENGTH))
152152
.body("created",not(equalTo(null)))
153153
.body("expires",not(equalTo(null)))
154154
.extract().as(ApiKey.class);
@@ -184,7 +184,7 @@ void test_api_key_creation_not_other_user(String authType, TestAccounts.KeyUser
184184
@ArgumentsSource(UserSpecSource.class)
185185
@AuthType(user = TestAccounts.KeyUser.SPK_NORMAL)
186186
void test_api_key_listing(String authType, TestAccounts.KeyUser theUser, RequestSpecification authSpec) {
187-
List<ApiKey> keys =
187+
List<ApiKey> keys =
188188
given()
189189
.log().ifValidationFails(LogDetail.ALL,true)
190190
.spec(authSpec)
@@ -215,12 +215,12 @@ void test_api_key_listing(String authType, TestAccounts.KeyUser theUser, Request
215215
.statusCode(HttpCode.OK.getStatus());
216216
}
217217

218-
218+
219219
// use api key
220220
@Test
221221
@Order(5)
222222
public void test_key_usage() throws Exception {
223-
223+
224224
createLocation("ApiKey-Test Location",true,"SPK");
225225
String json = loadResourceAsString("cwms/cda/api/location_create_spk.json");
226226
Location location = new Location.Builder(Formats.parseContent(Formats.parseHeader(Formats.JSON, Location.class),
@@ -394,7 +394,7 @@ private void assertContainsKey(ApiKey expectedKey, List<ApiKey> returnedSet) {
394394
ZonedDateTime expectedExpires = expected.getExpires();
395395
if (expectedKeyExpires == null && expectedExpires == null) {
396396
return;
397-
} else if((expectedKeyExpires != null && expectedExpires != null)
397+
} else if((expectedKeyExpires != null && expectedExpires != null)
398398
&& expectedExpires.isEqual(expectedKeyExpires)) {
399399
return;
400400
}

0 commit comments

Comments
 (0)