Skip to content

Commit

Permalink
fix: fixes for mongo plugin (#817)
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc authored Sep 19, 2023
1 parent e993e14 commit abdcfbc
Show file tree
Hide file tree
Showing 16 changed files with 308 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/main/java/io/supertokens/session/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,13 @@ public static SessionInformationHolder createNewSession(TenantIdentifierWithStor
sessionHandle += "_" + tenantIdentifierWithStorage.getTenantId();
}

String primaryUserId = tenantIdentifierWithStorage.getAuthRecipeStorage()
.getPrimaryUserIdStrForUserId(tenantIdentifierWithStorage.toAppIdentifier(), recipeUserId);
if (primaryUserId == null) {
primaryUserId = recipeUserId;
String primaryUserId = recipeUserId;
if (tenantIdentifierWithStorage.getStorage().getType().equals(STORAGE_TYPE.SQL)) {
tenantIdentifierWithStorage.getAuthRecipeStorage()
.getPrimaryUserIdStrForUserId(tenantIdentifierWithStorage.toAppIdentifier(), recipeUserId);
if (primaryUserId == null) {
primaryUserId = recipeUserId;
}
}

String antiCsrfToken = enableAntiCsrf ? UUID.randomUUID().toString() : null;
Expand Down Expand Up @@ -845,11 +848,13 @@ public static String[] getAllNonExpiredSessionHandlesForUser(
Set<String> userIds = new HashSet<>();
userIds.add(userId);
if (fetchSessionsForAllLinkedAccounts) {
AuthRecipeUserInfo primaryUser = appIdentifierWithStorage.getAuthRecipeStorage()
.getPrimaryUserById(appIdentifierWithStorage, userId);
if (primaryUser != null) {
for (LoginMethod lM : primaryUser.loginMethods) {
userIds.add(lM.getSupertokensUserId());
if (appIdentifierWithStorage.getStorage().getType().equals(STORAGE_TYPE.SQL)) {
AuthRecipeUserInfo primaryUser = appIdentifierWithStorage.getAuthRecipeStorage()
.getPrimaryUserById(appIdentifierWithStorage, userId);
if (primaryUser != null) {
for (LoginMethod lM : primaryUser.loginMethods) {
userIds.add(lM.getSupertokensUserId());
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.supertokens.emailpassword.EmailPassword;
import io.supertokens.emailpassword.exceptions.EmailChangeNotAllowedException;
import io.supertokens.emailpassword.exceptions.WrongCredentialsException;
import io.supertokens.pluginInterface.STORAGE_TYPE;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
import io.supertokens.storageLayer.StorageLayer;
import io.supertokens.test.TestingProcessManager;
Expand Down Expand Up @@ -60,6 +61,10 @@ public void timeTakenFor500SignInParallel() throws Exception {
TestingProcessManager.TestingProcess process = TestingProcessManager.start(args);
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

ExecutorService ex = Executors.newFixedThreadPool(1000);
int numberOfThreads = 500;

Expand Down Expand Up @@ -104,6 +109,10 @@ public void timeTakenFor500SignInUpParallel() throws Exception {
TestingProcessManager.TestingProcess process = TestingProcessManager.start(args);
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

if (StorageLayer.isInMemDb(process.getProcess())) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ public void testCreatingSessionWithAndWithoutAPIKey() throws Exception {
TestingProcessManager.TestingProcess process = TestingProcessManager.start(args);
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

String userId = "userId";
JsonObject userDataInJWT = new JsonObject();
userDataInJWT.addProperty("key", "value");
Expand Down Expand Up @@ -272,6 +276,10 @@ public void testCreatingSessionWithAndWithoutAPIKeyWhenSuperTokensSaaSSecretIsAl
TestingProcessManager.TestingProcess process = TestingProcessManager.start(args);
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

String userId = "userId";
JsonObject userDataInJWT = new JsonObject();
userDataInJWT.addProperty("key", "value");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ public void makeEmailPasswordPrimaryUserSuccess() throws Exception {
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

AuthRecipeUserInfo emailPasswordUser = EmailPassword.signUp(process.getProcess(), "[email protected]",
"pass1234");

Expand Down Expand Up @@ -191,6 +195,10 @@ public void makeThirdPartyPrimaryUserSuccess() throws Exception {
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

ThirdParty.SignInUpResponse signInUp = ThirdParty.signInUp(process.getProcess(), "google",
"user-google",
"[email protected]");
Expand Down Expand Up @@ -226,6 +234,10 @@ public void makePasswordlessEmailPrimaryUserSuccess() throws Exception {
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

Passwordless.CreateCodeResponse code = Passwordless.createCode(process.getProcess(), "[email protected]", null, null,
null);
Passwordless.ConsumeCodeResponse pResp = Passwordless.consumeCode(process.getProcess(), code.deviceId,
Expand Down Expand Up @@ -261,6 +273,10 @@ public void makePasswordlessPhonePrimaryUserSuccess() throws Exception {
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

Passwordless.CreateCodeResponse code = Passwordless.createCode(process.getProcess(), null, "1234", null,
null);
Passwordless.ConsumeCodeResponse pResp = Passwordless.consumeCode(process.getProcess(), code.deviceId,
Expand Down Expand Up @@ -296,6 +312,10 @@ public void alreadyPrimaryUsertest() throws Exception {
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

AuthRecipeUserInfo emailPasswordUser = EmailPassword.signUp(process.getProcess(), "[email protected]",
"pass1234");

Expand Down Expand Up @@ -332,6 +352,10 @@ public void makePrimaryUserFailsCauseAnotherAccountWithSameEmailAlreadyAPrimaryU
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

AuthRecipeUserInfo emailPasswordUser = EmailPassword.signUp(process.getProcess(), "[email protected]",
"pass1234");

Expand Down Expand Up @@ -363,6 +387,10 @@ public void makePrimarySucceedsEvenIfAnotherAccountWithSameEmailButIsNotAPrimary
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

AuthRecipeUserInfo emailPasswordUser = EmailPassword.signUp(process.getProcess(), "[email protected]",
"pass1234");

Expand All @@ -387,6 +415,10 @@ public void makePrimaryUserFailsCauseAnotherAccountWithSameEmailAlreadyAPrimaryU
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

Multitenancy.addNewOrUpdateAppOrTenant(process.main, new TenantIdentifier(null, null, null),
new TenantConfig(new TenantIdentifier(null, null, "t1"), new EmailPasswordConfig(true),
new ThirdPartyConfig(true, new ThirdPartyConfig.Provider[0]), new PasswordlessConfig(true),
Expand Down Expand Up @@ -429,6 +461,10 @@ public void makePrimarySucceedsEvenIfAnotherAccountWithSameEmailButInADifferentT
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

Multitenancy.addNewOrUpdateAppOrTenant(process.main, new TenantIdentifier(null, null, null),
new TenantConfig(new TenantIdentifier(null, null, "t1"), new EmailPasswordConfig(true),
new ThirdPartyConfig(true, new ThirdPartyConfig.Provider[0]), new PasswordlessConfig(true),
Expand Down Expand Up @@ -463,6 +499,10 @@ public void makePrimaryUserFailsCauseOfUnknownUserId() throws Exception {
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

try {
AuthRecipe.createPrimaryUser(process.main, "random");
assert (false);
Expand All @@ -483,6 +523,10 @@ public void makingPrimaryUserFailsCauseAlreadyLinkedToAnotherAccount() throws Ex
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

AuthRecipeUserInfo emailPasswordUser1 = EmailPassword.signUp(process.getProcess(), "[email protected]",
"pass1234");
AuthRecipeUserInfo emailPasswordUser2 = EmailPassword.signUp(process.getProcess(), "[email protected]",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.supertokens.passwordless.Passwordless;
import io.supertokens.passwordless.exceptions.*;
import io.supertokens.pluginInterface.RECIPE_ID;
import io.supertokens.pluginInterface.STORAGE_TYPE;
import io.supertokens.pluginInterface.authRecipe.AuthRecipeUserInfo;
import io.supertokens.pluginInterface.emailpassword.exceptions.DuplicateEmailException;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
Expand Down Expand Up @@ -104,6 +105,10 @@ public void testListUsersByAccountInfoForUnlinkedAccounts() throws Exception {
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

AuthRecipeUserInfo user1 = createEmailPasswordUser(process.getProcess(), "[email protected]", "password1");
AuthRecipeUserInfo user2 = createThirdPartyUser(process.getProcess(), "google", "userid1", "[email protected]");
AuthRecipeUserInfo user3 = createPasswordlessUserWithEmail(process.getProcess(), "[email protected]");
Expand Down Expand Up @@ -151,6 +156,10 @@ public void testListUsersByAccountInfoForUnlinkedAccountsWithUnionOption() throw
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

AuthRecipeUserInfo user1 = createEmailPasswordUser(process.getProcess(), "[email protected]", "password1");
AuthRecipeUserInfo user2 = createThirdPartyUser(process.getProcess(), "google", "userid1", "[email protected]");
AuthRecipeUserInfo user3 = createPasswordlessUserWithEmail(process.getProcess(), "[email protected]");
Expand Down Expand Up @@ -197,6 +206,10 @@ public void testUnknownAccountInfo() throws Exception {
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

TenantIdentifierWithStorage tenantIdentifierWithStorage = TenantIdentifier.BASE_TENANT.withStorage(StorageLayer.getBaseStorage(process.getProcess()));
assertEquals(0, AuthRecipe.getUsersByAccountInfo(tenantIdentifierWithStorage, false, "[email protected]", null, null, null).length);
assertEquals(0, AuthRecipe.getUsersByAccountInfo(tenantIdentifierWithStorage, false, null, null, "google", "userid1").length);
Expand All @@ -217,6 +230,10 @@ public void testListUserByAccountInfoWhenAccountsAreLinked1() throws Exception {
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

AuthRecipeUserInfo user1 = EmailPassword.signUp(process.getProcess(), "[email protected]", "password1");
Thread.sleep(50);
AuthRecipeUserInfo user2 = ThirdParty.signInUp(process.getProcess(), "google", "userid1", "[email protected]").user;
Expand Down Expand Up @@ -254,6 +271,10 @@ public void testListUserByAccountInfoWhenAccountsAreLinked2() throws Exception {
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

AuthRecipeUserInfo user1 = EmailPassword.signUp(process.getProcess(), "[email protected]", "password1");
Thread.sleep(50);
AuthRecipeUserInfo user2 = EmailPassword.signUp(process.getProcess(), "[email protected]", "password2");
Expand Down Expand Up @@ -285,6 +306,10 @@ public void testListUserByAccountInfoWhenAccountsAreLinked3() throws Exception {
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

AuthRecipeUserInfo user1 = createEmailPasswordUser(process.getProcess(), "[email protected]", "password1");
Thread.sleep(50);
AuthRecipeUserInfo user2 = createPasswordlessUserWithEmail(process.getProcess(), "[email protected]");
Expand Down Expand Up @@ -316,6 +341,10 @@ public void testListUserByAccountInfoWhenAccountsAreLinked4() throws Exception {
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

AuthRecipeUserInfo user1 = createEmailPasswordUser(process.getProcess(), "[email protected]", "password1");
Thread.sleep(50);
AuthRecipeUserInfo user2 = createPasswordlessUserWithPhone(process.getProcess(), "+919876543210");
Expand Down Expand Up @@ -349,6 +378,10 @@ public void testListUserByAccountInfoWhenAccountsAreLinked5() throws Exception {
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

AuthRecipeUserInfo user1 = createEmailPasswordUser(process.getProcess(), "[email protected]", "password1");
Thread.sleep(50);
AuthRecipeUserInfo user2 = createThirdPartyUser(process.getProcess(), "google", "userid1", "[email protected]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
import io.supertokens.passwordless.Passwordless;
import io.supertokens.passwordless.exceptions.*;
import io.supertokens.pluginInterface.RECIPE_ID;
import io.supertokens.pluginInterface.STORAGE_TYPE;
import io.supertokens.pluginInterface.authRecipe.AuthRecipeUserInfo;
import io.supertokens.pluginInterface.emailpassword.exceptions.DuplicateEmailException;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
import io.supertokens.pluginInterface.exceptions.StorageTransactionLogicException;
import io.supertokens.pluginInterface.passwordless.exception.DuplicateLinkCodeHashException;
import io.supertokens.storageLayer.StorageLayer;
import io.supertokens.test.TestingProcessManager;
import io.supertokens.test.Utils;
import io.supertokens.thirdparty.ThirdParty;
Expand Down Expand Up @@ -102,6 +104,10 @@ public void testAllLoginMethods() throws Exception {
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

AuthRecipeUserInfo user1 = createEmailPasswordUser(process.getProcess(), "[email protected]", "password1");
Thread.sleep(50);
AuthRecipeUserInfo user2 = createThirdPartyUser(process.getProcess(), "google", "userid1", "[email protected]");
Expand Down Expand Up @@ -162,6 +168,10 @@ public void testUnknownUserIdReturnsNull() throws Exception {
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

assertNull(AuthRecipe.getUserById(process.getProcess(), "unknownid"));

process.kill();
Expand All @@ -179,6 +189,10 @@ public void testLoginMethodsAreSortedByTime() throws Exception {
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

// Create users
AuthRecipeUserInfo user4 = createPasswordlessUserWithPhone(process.getProcess(), "+919876543210");
Thread.sleep(50);
Expand Down
Loading

0 comments on commit abdcfbc

Please sign in to comment.