|
1 | 1 | package io.supertokens.storage.postgresql.test; |
2 | 2 |
|
3 | 3 | import io.supertokens.ProcessState; |
| 4 | +import io.supertokens.authRecipe.AuthRecipe; |
| 5 | +import io.supertokens.emailpassword.EmailPassword; |
| 6 | +import io.supertokens.featureflag.EE_FEATURES; |
| 7 | +import io.supertokens.featureflag.FeatureFlagTestContent; |
| 8 | +import io.supertokens.passwordless.Passwordless; |
4 | 9 | import io.supertokens.pluginInterface.STORAGE_TYPE; |
| 10 | +import io.supertokens.pluginInterface.authRecipe.AuthRecipeStorage; |
| 11 | +import io.supertokens.pluginInterface.authRecipe.AuthRecipeUserInfo; |
5 | 12 | import io.supertokens.pluginInterface.exceptions.StorageQueryException; |
6 | 13 | import io.supertokens.pluginInterface.exceptions.StorageTransactionLogicException; |
7 | 14 | import io.supertokens.pluginInterface.multitenancy.AppIdentifier; |
|
14 | 21 | import io.supertokens.pluginInterface.totp.sqlStorage.TOTPSQLStorage; |
15 | 22 | import io.supertokens.storageLayer.StorageLayer; |
16 | 23 |
|
| 24 | +import io.supertokens.thirdparty.ThirdParty; |
17 | 25 | import org.junit.AfterClass; |
18 | 26 | import org.junit.Before; |
19 | 27 | import org.junit.Rule; |
20 | 28 | import org.junit.Test; |
21 | 29 | import org.junit.rules.TestRule; |
22 | 30 |
|
23 | | -import static org.junit.Assert.assertNotNull; |
| 31 | +import static org.junit.Assert.*; |
24 | 32 |
|
25 | 33 | public class StorageLayerTest { |
26 | 34 |
|
@@ -94,4 +102,52 @@ public void totpCodeLengthTest() throws Exception { |
94 | 102 | insertUsedCodeUtil(storage, code); |
95 | 103 | } |
96 | 104 |
|
| 105 | + @Test |
| 106 | + public void testLinkedAccountUser() throws Exception { |
| 107 | + String[] args = {"../"}; |
| 108 | + |
| 109 | + TestingProcessManager.TestingProcess process = TestingProcessManager.start(args, false); |
| 110 | + FeatureFlagTestContent.getInstance(process.getProcess()) |
| 111 | + .setKeyValue(FeatureFlagTestContent.ENABLED_FEATURES, new EE_FEATURES[]{ |
| 112 | + EE_FEATURES.ACCOUNT_LINKING, EE_FEATURES.MULTI_TENANCY}); |
| 113 | + process.startProcess(); |
| 114 | + assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED)); |
| 115 | + |
| 116 | + AuthRecipeUserInfo user1 = EmailPassword. signUp( process. getProcess(), "[email protected]", "password"); |
| 117 | + Thread.sleep(50); |
| 118 | + AuthRecipeUserInfo user2 = ThirdParty. signInUp( process. getProcess(), "google", "googleid", "[email protected]"). user; |
| 119 | + Thread.sleep(50); |
| 120 | + Passwordless. CreateCodeResponse code1 = Passwordless. createCode( process. getProcess(), "[email protected]", null, null, null); |
| 121 | + AuthRecipeUserInfo user3 = Passwordless.consumeCode(process.getProcess(), code1.deviceId, code1.deviceIdHash, code1.userInputCode, null).user; |
| 122 | + Thread.sleep(50); |
| 123 | + Passwordless.CreateCodeResponse code2 = Passwordless.createCode(process.getProcess(), null, "+919876543210", null, null); |
| 124 | + AuthRecipeUserInfo user4 = Passwordless.consumeCode(process.getProcess(), code2.deviceId, code2.deviceIdHash, code2.userInputCode, null).user; |
| 125 | + |
| 126 | + AuthRecipe.createPrimaryUser(process.getProcess(), user3.getSupertokensUserId()); |
| 127 | + AuthRecipe.linkAccounts(process.getProcess(), user1.getSupertokensUserId(), user3.getSupertokensUserId()); |
| 128 | + AuthRecipe.linkAccounts(process.getProcess(), user2.getSupertokensUserId(), user3.getSupertokensUserId()); |
| 129 | + AuthRecipe.linkAccounts(process.getProcess(), user4.getSupertokensUserId(), user3.getSupertokensUserId()); |
| 130 | + |
| 131 | + String[] userIds = new String[]{ |
| 132 | + user1.getSupertokensUserId(), |
| 133 | + user2.getSupertokensUserId(), |
| 134 | + user3.getSupertokensUserId(), |
| 135 | + user4.getSupertokensUserId() |
| 136 | + }; |
| 137 | + |
| 138 | + for (String userId : userIds){ |
| 139 | + AuthRecipeUserInfo primaryUser = ((AuthRecipeStorage) StorageLayer.getStorage(process.getProcess())).getPrimaryUserById( |
| 140 | + new AppIdentifier(null, null), userId); |
| 141 | + assertEquals(user3.getSupertokensUserId(), primaryUser.getSupertokensUserId()); |
| 142 | + assertEquals(4, primaryUser.loginMethods.length); |
| 143 | + assertTrue(primaryUser.loginMethods[0].timeJoined < primaryUser.loginMethods[1].timeJoined); |
| 144 | + assertTrue(primaryUser.loginMethods[1].timeJoined < primaryUser.loginMethods[2].timeJoined); |
| 145 | + assertTrue(primaryUser.loginMethods[2].timeJoined < primaryUser.loginMethods[3].timeJoined); |
| 146 | + assertEquals(primaryUser.timeJoined, primaryUser.loginMethods[0].timeJoined); |
| 147 | + } |
| 148 | + |
| 149 | + process.kill(); |
| 150 | + assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED)); |
| 151 | + } |
| 152 | + |
97 | 153 | } |
0 commit comments