Skip to content

Commit

Permalink
fix: tests (#815)
Browse files Browse the repository at this point in the history
* fix: tests

* fix: tests
  • Loading branch information
sattvikc authored Sep 14, 2023
1 parent 0a22121 commit 35dd880
Show file tree
Hide file tree
Showing 21 changed files with 358 additions and 182 deletions.
34 changes: 21 additions & 13 deletions src/main/java/io/supertokens/authRecipe/AuthRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static boolean unlinkAccounts(Main main, AppIdentifierWithStorage appIden
throws StorageQueryException, UnknownUserIdException, InputUserIdIsNotAPrimaryUserException {
AuthRecipeSQLStorage storage = (AuthRecipeSQLStorage) appIdentifierWithStorage.getAuthRecipeStorage();
try {
return storage.startTransaction(con -> {
UnlinkResult res = storage.startTransaction(con -> {
AuthRecipeUserInfo primaryUser = storage.getPrimaryUserById_Transaction(appIdentifierWithStorage, con,
recipeUserId);
if (primaryUser == null) {
Expand All @@ -93,28 +93,23 @@ public static boolean unlinkAccounts(Main main, AppIdentifierWithStorage appIden
// we are trying to unlink the user ID which is the same as the primary one.
if (primaryUser.loginMethods.length == 1) {
storage.unlinkAccounts_Transaction(appIdentifierWithStorage, con, primaryUser.getSupertokensUserId(), recipeUserId);
Session.revokeAllSessionsForUser(main, appIdentifierWithStorage,
mappingResult == null ? recipeUserId : mappingResult.externalUserId,
false);
return false;
return new UnlinkResult(mappingResult == null ? recipeUserId : mappingResult.externalUserId, false);
} else {
// Here we delete the recipe user id cause if we just unlink, then there will be two
// distinct users with the same ID - which is a broken state.
// The delete will also cause the automatic unlinking.
// We need to make sure that it only deletes sessions for recipeUserId and not other linked
// users who have their sessions for primaryUserId (that is equal to the recipeUserId)
Session.revokeAllSessionsForUser(main, appIdentifierWithStorage,
mappingResult == null ? recipeUserId : mappingResult.externalUserId, false);
deleteUserHelper(con, appIdentifierWithStorage, recipeUserId, false, mappingResult);
return true;
return new UnlinkResult(mappingResult == null ? recipeUserId : mappingResult.externalUserId, true);
}
} else {
storage.unlinkAccounts_Transaction(appIdentifierWithStorage, con, primaryUser.getSupertokensUserId(), recipeUserId);
Session.revokeAllSessionsForUser(main, appIdentifierWithStorage,
mappingResult == null ? recipeUserId : mappingResult.externalUserId, false);
return false;
return new UnlinkResult(mappingResult == null ? recipeUserId : mappingResult.externalUserId, false);
}
});
Session.revokeAllSessionsForUser(main, appIdentifierWithStorage, res.userId, false);
return res.wasLinked;
} catch (StorageTransactionLogicException e) {
if (e.actualException instanceof UnknownUserIdException) {
throw (UnknownUserIdException) e.actualException;
Expand Down Expand Up @@ -788,8 +783,8 @@ private static void deleteUserHelper(TransactionConnection con, AppIdentifierWit
// in reference to
// https://docs.google.com/spreadsheets/d/17hYV32B0aDCeLnSxbZhfRN2Y9b0LC2xUF44vV88RNAA/edit?usp=sharing
// we want to check which state the db is in
if (appIdentifierWithStorage.getAuthRecipeStorage()
.doesUserIdExist(appIdentifierWithStorage, userIdMapping.externalUserId)) {
if (((AuthRecipeSQLStorage) appIdentifierWithStorage.getAuthRecipeStorage())
.doesUserIdExist_Transaction(con, appIdentifierWithStorage, userIdMapping.externalUserId)) {
// db is in state A4
// delete only from auth tables
userIdToDeleteForAuthRecipe = userId;
Expand Down Expand Up @@ -823,11 +818,13 @@ private static void deleteUserHelper(TransactionConnection con, AppIdentifierWit
if (primaryUserIdToDeleteNonAuthRecipe == null) {
deleteAuthRecipeUser(con, appIdentifierWithStorage, userToDelete.getSupertokensUserId(),
true);
return;
}
} else {
// this is always type supertokens user ID cause it's from a user from the database.
io.supertokens.pluginInterface.useridmapping.UserIdMapping mappingResult =
io.supertokens.useridmapping.UserIdMapping.getUserIdMapping(
con,
appIdentifierWithStorage,
userToDelete.getSupertokensUserId(), UserIdType.SUPERTOKENS);
if (mappingResult != null) {
Expand Down Expand Up @@ -866,6 +863,7 @@ private static void deleteUserHelper(TransactionConnection con, AppIdentifierWit
io.supertokens.pluginInterface.useridmapping.UserIdMapping mappingResult = lM.getSupertokensUserId().equals(
userIdToDeleteForAuthRecipe) ? userIdMapping :
io.supertokens.useridmapping.UserIdMapping.getUserIdMapping(
con,
appIdentifierWithStorage,
lM.getSupertokensUserId(), UserIdType.SUPERTOKENS);
deleteUserHelper(con, appIdentifierWithStorage, lM.getSupertokensUserId(), false, mappingResult);
Expand Down Expand Up @@ -963,4 +961,14 @@ public static boolean deleteNonAuthRecipeUser(TenantIdentifierWithStorage

return finalDidExist;
}

private static class UnlinkResult {
public final String userId;
public final boolean wasLinked;

public UnlinkResult(String userId, boolean wasLinked) {
this.userId = userId;
this.wasLinked = wasLinked;
}
}
}
85 changes: 63 additions & 22 deletions src/main/java/io/supertokens/inmemorydb/Start.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@
import io.supertokens.pluginInterface.jwt.JWTSigningKeyInfo;
import io.supertokens.pluginInterface.jwt.exceptions.DuplicateKeyIdException;
import io.supertokens.pluginInterface.jwt.sqlstorage.JWTRecipeSQLStorage;
import io.supertokens.pluginInterface.multitenancy.AppIdentifier;
import io.supertokens.pluginInterface.multitenancy.MultitenancyStorage;
import io.supertokens.pluginInterface.multitenancy.TenantConfig;
import io.supertokens.pluginInterface.multitenancy.TenantIdentifier;
import io.supertokens.pluginInterface.multitenancy.*;
import io.supertokens.pluginInterface.multitenancy.exceptions.DuplicateClientTypeException;
import io.supertokens.pluginInterface.multitenancy.exceptions.DuplicateTenantException;
import io.supertokens.pluginInterface.multitenancy.exceptions.DuplicateThirdPartyIdException;
Expand Down Expand Up @@ -80,6 +77,7 @@
import io.supertokens.pluginInterface.useridmapping.UserIdMappingStorage;
import io.supertokens.pluginInterface.useridmapping.exception.UnknownSuperTokensUserIdException;
import io.supertokens.pluginInterface.useridmapping.exception.UserIdMappingAlreadyExistsException;
import io.supertokens.pluginInterface.useridmapping.sqlStorage.UserIdMappingSQLStorage;
import io.supertokens.pluginInterface.usermetadata.UserMetadataStorage;
import io.supertokens.pluginInterface.usermetadata.sqlStorage.UserMetadataSQLStorage;
import io.supertokens.pluginInterface.userroles.UserRolesStorage;
Expand All @@ -103,7 +101,8 @@
public class Start
implements SessionSQLStorage, EmailPasswordSQLStorage, EmailVerificationSQLStorage, ThirdPartySQLStorage,
JWTRecipeSQLStorage, PasswordlessSQLStorage, UserMetadataSQLStorage, UserRolesSQLStorage, UserIdMappingStorage,
MultitenancyStorage, MultitenancySQLStorage, TOTPSQLStorage, ActiveUsersStorage, DashboardSQLStorage, AuthRecipeSQLStorage {
UserIdMappingSQLStorage, MultitenancyStorage, MultitenancySQLStorage, TOTPSQLStorage, ActiveUsersStorage,
DashboardSQLStorage, AuthRecipeSQLStorage {

private static final Object appenderLock = new Object();
private static final String APP_ID_KEY_NAME = "app_id";
Expand Down Expand Up @@ -1183,6 +1182,16 @@ public boolean doesUserIdExist(AppIdentifier appIdentifier, String userId) throw
}
}

@Override
public boolean doesUserIdExist_Transaction(TransactionConnection con, AppIdentifier appIdentifier, String userId) throws StorageQueryException {
try {
Connection sqlCon = (Connection) con.getConnection();
return GeneralQueries.doesUserIdExist_Transaction(this, sqlCon, appIdentifier, userId);
} catch (SQLException e) {
throw new StorageQueryException(e);
}
}

@Override
public void updateLastActive(AppIdentifier appIdentifier, String userId) throws StorageQueryException {
try {
Expand Down Expand Up @@ -1850,7 +1859,7 @@ public void addRoleToUser(TenantIdentifier tenantIdentifier, String userId, Stri
throws StorageQueryException, UnknownRoleException, DuplicateUserRoleMappingException,
TenantOrAppNotFoundException {
try {
UserRoleQueries.addRoleToUser(this, tenantIdentifier, userId, role);
UserRolesQueries.addRoleToUser(this, tenantIdentifier, userId, role);
} catch (SQLException e) {
if (e instanceof SQLiteException) {
SQLiteConfig config = Config.getConfig(this);
Expand Down Expand Up @@ -1884,7 +1893,7 @@ public void addRoleToUser(TenantIdentifier tenantIdentifier, String userId, Stri
public String[] getRolesForUser(TenantIdentifier tenantIdentifier, String userId) throws
StorageQueryException {
try {
return UserRoleQueries.getRolesForUser(this, tenantIdentifier, userId);
return UserRolesQueries.getRolesForUser(this, tenantIdentifier, userId);
} catch (SQLException e) {
throw new StorageQueryException(e);
}
Expand All @@ -1893,7 +1902,7 @@ public String[] getRolesForUser(TenantIdentifier tenantIdentifier, String userId
private String[] getRolesForUser(AppIdentifier appIdentifier, String userId) throws
StorageQueryException {
try {
return UserRoleQueries.getRolesForUser(this, appIdentifier, userId);
return UserRolesQueries.getRolesForUser(this, appIdentifier, userId);
} catch (SQLException e) {
throw new StorageQueryException(e);
}
Expand All @@ -1903,7 +1912,7 @@ private String[] getRolesForUser(AppIdentifier appIdentifier, String userId) thr
public String[] getUsersForRole(TenantIdentifier tenantIdentifier, String role) throws
StorageQueryException {
try {
return UserRoleQueries.getUsersForRole(this, tenantIdentifier, role);
return UserRolesQueries.getUsersForRole(this, tenantIdentifier, role);
} catch (SQLException e) {
throw new StorageQueryException(e);
}
Expand All @@ -1913,7 +1922,7 @@ public String[] getUsersForRole(TenantIdentifier tenantIdentifier, String role)
public String[] getPermissionsForRole(AppIdentifier appIdentifier, String role) throws
StorageQueryException {
try {
return UserRoleQueries.getPermissionsForRole(this, appIdentifier, role);
return UserRolesQueries.getPermissionsForRole(this, appIdentifier, role);
} catch (SQLException e) {
throw new StorageQueryException(e);
}
Expand All @@ -1923,7 +1932,7 @@ public String[] getPermissionsForRole(AppIdentifier appIdentifier, String role)
public String[] getRolesThatHavePermission(AppIdentifier appIdentifier, String permission)
throws StorageQueryException {
try {
return UserRoleQueries.getRolesThatHavePermission(this, appIdentifier, permission);
return UserRolesQueries.getRolesThatHavePermission(this, appIdentifier, permission);
} catch (SQLException e) {
throw new StorageQueryException(e);
}
Expand All @@ -1932,7 +1941,7 @@ public String[] getRolesThatHavePermission(AppIdentifier appIdentifier, String p
@Override
public boolean deleteRole(AppIdentifier appIdentifier, String role) throws StorageQueryException {
try {
return UserRoleQueries.deleteRole(this, appIdentifier, role);
return UserRolesQueries.deleteRole(this, appIdentifier, role);
} catch (SQLException e) {
throw new StorageQueryException(e);
}
Expand All @@ -1941,7 +1950,7 @@ public boolean deleteRole(AppIdentifier appIdentifier, String role) throws Stora
@Override
public String[] getRoles(AppIdentifier appIdentifier) throws StorageQueryException {
try {
return UserRoleQueries.getRoles(this, appIdentifier);
return UserRolesQueries.getRoles(this, appIdentifier);
} catch (SQLException e) {
throw new StorageQueryException(e);
}
Expand All @@ -1950,7 +1959,7 @@ public String[] getRoles(AppIdentifier appIdentifier) throws StorageQueryExcepti
@Override
public boolean doesRoleExist(AppIdentifier appIdentifier, String role) throws StorageQueryException {
try {
return UserRoleQueries.doesRoleExist(this, appIdentifier, role);
return UserRolesQueries.doesRoleExist(this, appIdentifier, role);
} catch (SQLException e) {
throw new StorageQueryException(e);
}
Expand All @@ -1960,7 +1969,7 @@ public boolean doesRoleExist(AppIdentifier appIdentifier, String role) throws St
public int deleteAllRolesForUser(TenantIdentifier tenantIdentifier, String userId) throws
StorageQueryException {
try {
return UserRoleQueries.deleteAllRolesForUser(this, tenantIdentifier, userId);
return UserRolesQueries.deleteAllRolesForUser(this, tenantIdentifier, userId);
} catch (SQLException e) {
throw new StorageQueryException(e);
}
Expand All @@ -1972,7 +1981,7 @@ public boolean deleteRoleForUser_Transaction(TenantIdentifier tenantIdentifier,
throws StorageQueryException {
Connection sqlCon = (Connection) con.getConnection();
try {
return UserRoleQueries.deleteRoleForUser_Transaction(this, sqlCon, tenantIdentifier, userId,
return UserRolesQueries.deleteRoleForUser_Transaction(this, sqlCon, tenantIdentifier, userId,
role);
} catch (SQLException e) {
throw new StorageQueryException(e);
Expand All @@ -1985,7 +1994,7 @@ public boolean createNewRoleOrDoNothingIfExists_Transaction(AppIdentifier appIde
throws StorageQueryException, TenantOrAppNotFoundException {
Connection sqlCon = (Connection) con.getConnection();
try {
return UserRoleQueries.createNewRoleOrDoNothingIfExists_Transaction(
return UserRolesQueries.createNewRoleOrDoNothingIfExists_Transaction(
this, sqlCon, appIdentifier, role);
} catch (SQLException e) {
if (e instanceof SQLiteException) {
Expand All @@ -2011,7 +2020,7 @@ public void addPermissionToRoleOrDoNothingIfExists_Transaction(AppIdentifier app
throws StorageQueryException, UnknownRoleException {
Connection sqlCon = (Connection) con.getConnection();
try {
UserRoleQueries.addPermissionToRoleOrDoNothingIfExists_Transaction(this, sqlCon, appIdentifier,
UserRolesQueries.addPermissionToRoleOrDoNothingIfExists_Transaction(this, sqlCon, appIdentifier,
role, permission);
} catch (SQLException e) {
if (e instanceof SQLiteException) {
Expand All @@ -2035,7 +2044,7 @@ public boolean deletePermissionForRole_Transaction(AppIdentifier appIdentifier,
throws StorageQueryException {
Connection sqlCon = (Connection) con.getConnection();
try {
return UserRoleQueries.deletePermissionForRole_Transaction(this, sqlCon, appIdentifier, role,
return UserRolesQueries.deletePermissionForRole_Transaction(this, sqlCon, appIdentifier, role,
permission);
} catch (SQLException e) {
throw new StorageQueryException(e);
Expand All @@ -2048,7 +2057,7 @@ public int deleteAllPermissionsForRole_Transaction(AppIdentifier appIdentifier,
throws StorageQueryException {
Connection sqlCon = (Connection) con.getConnection();
try {
return UserRoleQueries.deleteAllPermissionsForRole_Transaction(this, sqlCon, appIdentifier,
return UserRolesQueries.deleteAllPermissionsForRole_Transaction(this, sqlCon, appIdentifier,
role);
} catch (SQLException e) {
throw new StorageQueryException(e);
Expand All @@ -2060,7 +2069,7 @@ public boolean doesRoleExist_Transaction(AppIdentifier appIdentifier, Transactio
throws StorageQueryException {
Connection sqlCon = (Connection) con.getConnection();
try {
return UserRoleQueries.doesRoleExist_transaction(this, sqlCon, appIdentifier, role);
return UserRolesQueries.doesRoleExist_transaction(this, sqlCon, appIdentifier, role);
} catch (SQLException e) {
throw new StorageQueryException(e);
}
Expand All @@ -2071,7 +2080,7 @@ public void deleteAllRolesForUser_Transaction(TransactionConnection con, AppIden
throws StorageQueryException {
try {
Connection sqlCon = (Connection) con.getConnection();
UserRoleQueries.deleteAllRolesForUser_Transaction(sqlCon, this, appIdentifier, userId);
UserRolesQueries.deleteAllRolesForUser_Transaction(sqlCon, this, appIdentifier, userId);
} catch (SQLException e) {
throw new StorageQueryException(e);
}
Expand Down Expand Up @@ -2906,4 +2915,36 @@ public int getUsersCountWithMoreThanOneLoginMethod(AppIdentifier appIdentifier)
throw new StorageQueryException(e);
}
}


@Override
public UserIdMapping getUserIdMapping_Transaction(TransactionConnection con, AppIdentifier appIdentifier, String userId, boolean isSuperTokensUserId)
throws StorageQueryException {
try {
Connection sqlCon = (Connection) con.getConnection();
if (isSuperTokensUserId) {
return UserIdMappingQueries.getuseraIdMappingWithSuperTokensUserId_Transaction(this, sqlCon, appIdentifier,
userId);
}

return UserIdMappingQueries.getUserIdMappingWithExternalUserId_Transaction(this, sqlCon, appIdentifier, userId);
} catch (SQLException e) {
throw new StorageQueryException(e);
}
}

@Override
public UserIdMapping[] getUserIdMapping_Transaction(TransactionConnection con, AppIdentifier appIdentifier, String userId)
throws StorageQueryException {
try {
Connection sqlCon = (Connection) con.getConnection();
return UserIdMappingQueries.getUserIdMappingWithEitherSuperTokensUserIdOrExternalUserId_Transaction(this,
sqlCon,
appIdentifier,
userId);
} catch (SQLException e) {
throw new StorageQueryException(e);
}
}

}
Loading

0 comments on commit 35dd880

Please sign in to comment.