Skip to content

Commit

Permalink
fix: PR changes
Browse files Browse the repository at this point in the history
  • Loading branch information
anku255 committed Feb 28, 2024
1 parent 9285632 commit f09cf72
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/main/java/io/supertokens/bulkimport/BulkImport.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<BulkImportUser> users;

Expand Down
19 changes: 9 additions & 10 deletions src/main/java/io/supertokens/bulkimport/BulkImportUserUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,7 @@ private static List<String> 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<String> 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;
}

Expand Down Expand Up @@ -198,13 +191,19 @@ private static String validateAndNormaliseExternalUserId(String externalUserId,
return externalUserId.trim();
}

private static String validateAndNormaliseUserRole(String role, List<String> errors) {
private static String validateAndNormaliseUserRole(String role, String[] allUserRoles, List<String> 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<String> errors) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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!"));
}
Expand Down
27 changes: 14 additions & 13 deletions src/test/java/io/supertokens/test/bulkimport/BulkImportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<BulkImportUser> addedUsers = storage.getBulkImportUsers(new AppIdentifier(null, null), null, BulkImportUserStatus.NEW, null, null);
List<BulkImportUser> 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) {
Expand All @@ -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();
Expand Down Expand Up @@ -115,17 +115,17 @@ public void shouldCreatedNewIdsIfDuplicateIdIsFound() throws Exception {
AppIdentifierWithStorage appIdentifierWithStorage = new AppIdentifierWithStorage(null, null, storage);
BulkImport.addUsers(appIdentifierWithStorage, users);

List<BulkImportUser> addedUsers = storage.getBulkImportUsers(appIdentifierWithStorage, null, BulkImportUserStatus.NEW, null, null);
List<BulkImportUser> 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));
}

Expand All @@ -152,7 +152,7 @@ public void testGetUsersStatusFilter() throws Exception {
List<BulkImportUser> users = generateBulkImportUser(10);
BulkImport.addUsers(appIdentifierWithStorage, users);

List<BulkImportUser> addedUsers = storage.getBulkImportUsers(appIdentifierWithStorage, null, BulkImportUserStatus.NEW, null, null);
List<BulkImportUser> addedUsers = storage.getBulkImportUsers(appIdentifierWithStorage, null, BULK_IMPORT_USER_STATUS.NEW, null, null);
assertEquals(10, addedUsers.size());
}

Expand All @@ -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<BulkImportUser> addedUsers = storage.getBulkImportUsers(appIdentifierWithStorage, null, BulkImportUserStatus.PROCESSING, null, null);
List<BulkImportUser> addedUsers = storage.getBulkImportUsers(appIdentifierWithStorage, null, BULK_IMPORT_USER_STATUS.PROCESSING, null, null);
assertEquals(10, addedUsers.size());
}

Expand All @@ -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<BulkImportUser> addedUsers = storage.getBulkImportUsers(appIdentifierWithStorage, null, BulkImportUserStatus.FAILED, null, null);
List<BulkImportUser> addedUsers = storage.getBulkImportUsers(appIdentifierWithStorage, null, BULK_IMPORT_USER_STATUS.FAILED, null, null);
assertEquals(10, addedUsers.size());
}

Expand Down Expand Up @@ -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++;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit f09cf72

Please sign in to comment.