diff --git a/src/main/java/io/supertokens/bulkimport/BulkImport.java b/src/main/java/io/supertokens/bulkimport/BulkImport.java index f2136064d..1f9e38d06 100644 --- a/src/main/java/io/supertokens/bulkimport/BulkImport.java +++ b/src/main/java/io/supertokens/bulkimport/BulkImport.java @@ -16,7 +16,7 @@ package io.supertokens.bulkimport; -import io.supertokens.pluginInterface.bulkimport.BulkImportStorage.BulkImportUserStatus; +import io.supertokens.pluginInterface.bulkimport.BulkImportStorage.BULK_IMPORT_USER_STATUS; import io.supertokens.pluginInterface.bulkimport.BulkImportUser; import io.supertokens.pluginInterface.exceptions.StorageQueryException; import io.supertokens.pluginInterface.multitenancy.AppIdentifierWithStorage; @@ -51,7 +51,7 @@ public static void addUsers(AppIdentifierWithStorage appIdentifierWithStorage, L } public static BulkImportUserPaginationContainer getUsers(AppIdentifierWithStorage appIdentifierWithStorage, - @Nonnull Integer limit, @Nullable BulkImportUserStatus status, @Nullable String paginationToken) + @Nonnull Integer limit, @Nullable BULK_IMPORT_USER_STATUS status, @Nullable String paginationToken) throws StorageQueryException, BulkImportUserPaginationToken.InvalidTokenException { List users; diff --git a/src/main/java/io/supertokens/bulkimport/BulkImportUserUtils.java b/src/main/java/io/supertokens/bulkimport/BulkImportUserUtils.java index 7b1f20a3c..2d3f0003d 100644 --- a/src/main/java/io/supertokens/bulkimport/BulkImportUserUtils.java +++ b/src/main/java/io/supertokens/bulkimport/BulkImportUserUtils.java @@ -80,14 +80,7 @@ private static List getParsedUserRoles(JsonObject userData, String[] all // We already know that the jsonUserRoles is an array of non-empty strings, we will normalise each role now List userRoles = new ArrayList<>(); - jsonUserRoles.forEach(role -> { - String normalisedRole = validateAndNormaliseUserRole(role.getAsString(), errors); - if (Arrays.asList(allUserRoles).contains(normalisedRole)) { - userRoles.add(normalisedRole); - } else { - errors.add("Role " + normalisedRole + " does not exist."); - } - }); + jsonUserRoles.forEach(role -> validateAndNormaliseUserRole(role.getAsString(), allUserRoles, errors)); return userRoles; } @@ -198,13 +191,19 @@ private static String validateAndNormaliseExternalUserId(String externalUserId, return externalUserId.trim(); } - private static String validateAndNormaliseUserRole(String role, List errors) { + private static String validateAndNormaliseUserRole(String role, String[] allUserRoles, List errors) { if (role.length() > 255) { errors.add("role " + role + " is too long. Max length is 255."); } // We just trim the role as per the CreateRoleAPI.java - return role.trim(); + String normalisedRole = role.trim(); + + if (!Arrays.asList(allUserRoles).contains(normalisedRole)) { + errors.add("Role " + normalisedRole + " does not exist."); + } + + return normalisedRole; } private static String validateAndNormaliseTotpSecretKey(String secretKey, List errors) { diff --git a/src/main/java/io/supertokens/webserver/api/bulkimport/BulkImportAPI.java b/src/main/java/io/supertokens/webserver/api/bulkimport/BulkImportAPI.java index 2f1f5f94a..d6e903bb9 100644 --- a/src/main/java/io/supertokens/webserver/api/bulkimport/BulkImportAPI.java +++ b/src/main/java/io/supertokens/webserver/api/bulkimport/BulkImportAPI.java @@ -30,7 +30,7 @@ import io.supertokens.bulkimport.BulkImportUserUtils; import io.supertokens.multitenancy.exception.BadPermissionException; import io.supertokens.output.Logging; -import io.supertokens.pluginInterface.bulkimport.BulkImportStorage.BulkImportUserStatus; +import io.supertokens.pluginInterface.bulkimport.BulkImportStorage.BULK_IMPORT_USER_STATUS; import io.supertokens.pluginInterface.bulkimport.BulkImportUser; import io.supertokens.pluginInterface.exceptions.StorageQueryException; import io.supertokens.pluginInterface.multitenancy.AppIdentifierWithStorage; @@ -69,10 +69,10 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se limit = BulkImport.GET_USERS_DEFAULT_LIMIT; } - BulkImportUserStatus status = null; + BULK_IMPORT_USER_STATUS status = null; if (statusString != null) { try { - status = BulkImportUserStatus.valueOf(statusString); + status = BULK_IMPORT_USER_STATUS.valueOf(statusString); } catch (IllegalArgumentException e) { throw new ServletException(new BadRequestException("Invalid value for status. Pass one of NEW, PROCESSING, or FAILED!")); } diff --git a/src/test/java/io/supertokens/test/bulkimport/BulkImportTest.java b/src/test/java/io/supertokens/test/bulkimport/BulkImportTest.java index 4ff949e09..dba270c3b 100644 --- a/src/test/java/io/supertokens/test/bulkimport/BulkImportTest.java +++ b/src/test/java/io/supertokens/test/bulkimport/BulkImportTest.java @@ -35,7 +35,7 @@ import io.supertokens.pluginInterface.STORAGE_TYPE; import io.supertokens.pluginInterface.bulkimport.BulkImportStorage; import io.supertokens.pluginInterface.bulkimport.BulkImportUser; -import io.supertokens.pluginInterface.bulkimport.BulkImportStorage.BulkImportUserStatus; +import io.supertokens.pluginInterface.bulkimport.BulkImportStorage.BULK_IMPORT_USER_STATUS; import io.supertokens.pluginInterface.bulkimport.sqlStorage.BulkImportSQLStorage; import io.supertokens.pluginInterface.multitenancy.AppIdentifier; import io.supertokens.pluginInterface.multitenancy.AppIdentifierWithStorage; @@ -75,7 +75,7 @@ public void shouldAddUsersInBulkImportUsersTable() throws Exception { BulkImportStorage storage = (BulkImportStorage) StorageLayer.getStorage(process.main); BulkImport.addUsers(new AppIdentifierWithStorage(null, null, storage), users); - List addedUsers = storage.getBulkImportUsers(new AppIdentifier(null, null), null, BulkImportUserStatus.NEW, null, null); + List addedUsers = storage.getBulkImportUsers(new AppIdentifier(null, null), null, BULK_IMPORT_USER_STATUS.NEW, null, null); // Verify that all users are present in addedUsers for (BulkImportUser user : users) { @@ -85,8 +85,8 @@ public void shouldAddUsersInBulkImportUsersTable() throws Exception { .orElse(null); assertNotNull(matchingUser); - assertEquals(BulkImportUserStatus.NEW, matchingUser.status); - assertEquals(user.toString(), matchingUser.toRawData()); + assertEquals(BULK_IMPORT_USER_STATUS.NEW, matchingUser.status); + assertEquals(user.toRawDataForDbStorage(), matchingUser.toRawDataForDbStorage()); } process.kill(); @@ -115,17 +115,17 @@ public void shouldCreatedNewIdsIfDuplicateIdIsFound() throws Exception { AppIdentifierWithStorage appIdentifierWithStorage = new AppIdentifierWithStorage(null, null, storage); BulkImport.addUsers(appIdentifierWithStorage, users); - List addedUsers = storage.getBulkImportUsers(appIdentifierWithStorage, null, BulkImportUserStatus.NEW, null, null); + List addedUsers = storage.getBulkImportUsers(appIdentifierWithStorage, null, BULK_IMPORT_USER_STATUS.NEW, null, null); // Verify that the other properties are same but ids changed for (BulkImportUser user : users) { BulkImportUser matchingUser = addedUsers.stream() - .filter(addedUser -> user.toString().equals(addedUser.toRawData())) + .filter(addedUser -> user.toRawDataForDbStorage().equals(addedUser.toRawDataForDbStorage())) .findFirst() .orElse(null); assertNotNull(matchingUser); - assertEquals(BulkImportUserStatus.NEW, matchingUser.status); + assertEquals(BULK_IMPORT_USER_STATUS.NEW, matchingUser.status); assertFalse(initialIds.contains(matchingUser.id)); } @@ -152,7 +152,7 @@ public void testGetUsersStatusFilter() throws Exception { List users = generateBulkImportUser(10); BulkImport.addUsers(appIdentifierWithStorage, users); - List addedUsers = storage.getBulkImportUsers(appIdentifierWithStorage, null, BulkImportUserStatus.NEW, null, null); + List addedUsers = storage.getBulkImportUsers(appIdentifierWithStorage, null, BULK_IMPORT_USER_STATUS.NEW, null, null); assertEquals(10, addedUsers.size()); } @@ -165,12 +165,12 @@ public void testGetUsersStatusFilter() throws Exception { String[] userIds = users.stream().map(user -> user.id).toArray(String[]::new); storage.startTransaction(con -> { - storage.updateBulkImportUserStatus_Transaction(appIdentifierWithStorage, con, userIds, BulkImportUserStatus.PROCESSING); + storage.updateBulkImportUserStatus_Transaction(appIdentifierWithStorage, con, userIds, BULK_IMPORT_USER_STATUS.PROCESSING); storage.commitTransaction(con); return null; }); - List addedUsers = storage.getBulkImportUsers(appIdentifierWithStorage, null, BulkImportUserStatus.PROCESSING, null, null); + List addedUsers = storage.getBulkImportUsers(appIdentifierWithStorage, null, BULK_IMPORT_USER_STATUS.PROCESSING, null, null); assertEquals(10, addedUsers.size()); } @@ -183,12 +183,12 @@ public void testGetUsersStatusFilter() throws Exception { String[] userIds = users.stream().map(user -> user.id).toArray(String[]::new); storage.startTransaction(con -> { - storage.updateBulkImportUserStatus_Transaction(appIdentifierWithStorage, con, userIds, BulkImportUserStatus.FAILED); + storage.updateBulkImportUserStatus_Transaction(appIdentifierWithStorage, con, userIds, BULK_IMPORT_USER_STATUS.FAILED); storage.commitTransaction(con); return null; }); - List addedUsers = storage.getBulkImportUsers(appIdentifierWithStorage, null, BulkImportUserStatus.FAILED, null, null); + List addedUsers = storage.getBulkImportUsers(appIdentifierWithStorage, null, BULK_IMPORT_USER_STATUS.FAILED, null, null); assertEquals(10, addedUsers.size()); } @@ -248,7 +248,8 @@ public void randomPaginationTest() throws Exception { BulkImportUser expectedUser = sortedUsers.get(indexIntoUsers); assertEquals(expectedUser.id, actualUser.id); - assertEquals(expectedUser.toString(), actualUser.toString()); + assertEquals(expectedUser.status, actualUser.status); + assertEquals(expectedUser.toRawDataForDbStorage(), actualUser.toRawDataForDbStorage()); indexIntoUsers++; } diff --git a/src/test/java/io/supertokens/test/bulkimport/apis/GetBulkImportUsersTest.java b/src/test/java/io/supertokens/test/bulkimport/apis/GetBulkImportUsersTest.java index 5efb99a58..c5c3aeb73 100644 --- a/src/test/java/io/supertokens/test/bulkimport/apis/GetBulkImportUsersTest.java +++ b/src/test/java/io/supertokens/test/bulkimport/apis/GetBulkImportUsersTest.java @@ -151,7 +151,7 @@ public void shouldReturn200Response() throws Exception { assertEquals(1, bulkImportUsers.size()); JsonObject bulkImportUserJson = bulkImportUsers.get(0).getAsJsonObject(); bulkImportUserJson.get("status").getAsString().equals("NEW"); - BulkImportUser.fromJson(bulkImportUserJson).toRawData().equals(rawData); + BulkImportUser.fromJson(bulkImportUserJson).toRawDataForDbStorage().equals(rawData); process.kill(); Assert.assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));