|
17 | 17 | package io.supertokens.test; |
18 | 18 |
|
19 | 19 | import com.google.gson.JsonObject; |
| 20 | +import io.supertokens.Main; |
20 | 21 | import io.supertokens.ProcessState; |
| 22 | +import io.supertokens.ResourceDistributor; |
21 | 23 | import io.supertokens.authRecipe.AuthRecipe; |
22 | 24 | import io.supertokens.authRecipe.UserPaginationContainer; |
| 25 | +import io.supertokens.authRecipe.exception.AccountInfoAlreadyAssociatedWithAnotherPrimaryUserIdException; |
| 26 | +import io.supertokens.authRecipe.exception.InputUserIdIsNotAPrimaryUserException; |
| 27 | +import io.supertokens.authRecipe.exception.RecipeUserIdAlreadyLinkedWithAnotherPrimaryUserIdException; |
| 28 | +import io.supertokens.authRecipe.exception.RecipeUserIdAlreadyLinkedWithPrimaryUserIdException; |
23 | 29 | import io.supertokens.emailpassword.EmailPassword; |
24 | 30 | import io.supertokens.emailverification.EmailVerification; |
| 31 | +import io.supertokens.emailverification.exception.EmailAlreadyVerifiedException; |
25 | 32 | import io.supertokens.emailverification.exception.EmailVerificationInvalidTokenException; |
| 33 | +import io.supertokens.featureflag.EE_FEATURES; |
| 34 | +import io.supertokens.featureflag.FeatureFlagTestContent; |
| 35 | +import io.supertokens.featureflag.exceptions.FeatureNotEnabledException; |
| 36 | +import io.supertokens.multitenancy.Multitenancy; |
| 37 | +import io.supertokens.multitenancy.exception.BadPermissionException; |
| 38 | +import io.supertokens.multitenancy.exception.CannotModifyBaseConfigException; |
26 | 39 | import io.supertokens.passwordless.Passwordless; |
27 | 40 | import io.supertokens.passwordless.Passwordless.CreateCodeResponse; |
28 | 41 | import io.supertokens.pluginInterface.RECIPE_ID; |
29 | 42 | import io.supertokens.pluginInterface.STORAGE_TYPE; |
30 | 43 | import io.supertokens.pluginInterface.authRecipe.AuthRecipeUserInfo; |
| 44 | +import io.supertokens.pluginInterface.emailpassword.exceptions.DuplicateEmailException; |
| 45 | +import io.supertokens.pluginInterface.emailpassword.exceptions.UnknownUserIdException; |
| 46 | +import io.supertokens.pluginInterface.exceptions.InvalidConfigException; |
| 47 | +import io.supertokens.pluginInterface.exceptions.StorageQueryException; |
| 48 | +import io.supertokens.pluginInterface.exceptions.StorageTransactionLogicException; |
| 49 | +import io.supertokens.pluginInterface.multitenancy.*; |
| 50 | +import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException; |
31 | 51 | import io.supertokens.session.Session; |
32 | 52 | import io.supertokens.storageLayer.StorageLayer; |
| 53 | +import io.supertokens.test.httpRequest.HttpResponseException; |
| 54 | +import io.supertokens.thirdparty.InvalidProviderConfigException; |
33 | 55 | import io.supertokens.thirdparty.ThirdParty; |
34 | 56 | import io.supertokens.usermetadata.UserMetadata; |
35 | 57 | import io.supertokens.version.Version; |
|
38 | 60 | import org.junit.Rule; |
39 | 61 | import org.junit.Test; |
40 | 62 | import org.junit.rules.TestRule; |
41 | | -import org.reflections.Reflections; |
42 | 63 |
|
| 64 | +import java.io.IOException; |
| 65 | +import java.security.NoSuchAlgorithmException; |
| 66 | +import java.security.spec.InvalidKeySpecException; |
43 | 67 | import java.util.*; |
44 | 68 | import java.util.concurrent.ExecutorService; |
45 | 69 | import java.util.concurrent.Executors; |
46 | 70 | import java.util.concurrent.TimeUnit; |
47 | 71 | import java.util.concurrent.atomic.AtomicInteger; |
48 | 72 | import java.util.function.Function; |
49 | | -import java.util.stream.Collectors; |
50 | 73 |
|
51 | 74 | import static org.junit.Assert.*; |
52 | 75 |
|
@@ -660,6 +683,95 @@ public void deleteUserTest() throws Exception { |
660 | 683 | assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED)); |
661 | 684 | } |
662 | 685 |
|
| 686 | + @Test |
| 687 | + public void loadUsersByAccountInfoReturnsUsersForTenantOnly() |
| 688 | + throws InterruptedException, TenantOrAppNotFoundException, InvalidProviderConfigException, |
| 689 | + StorageQueryException, FeatureNotEnabledException, IOException, InvalidConfigException, |
| 690 | + CannotModifyBaseConfigException, BadPermissionException, DuplicateEmailException, HttpResponseException, |
| 691 | + AccountInfoAlreadyAssociatedWithAnotherPrimaryUserIdException, InputUserIdIsNotAPrimaryUserException, |
| 692 | + RecipeUserIdAlreadyLinkedWithAnotherPrimaryUserIdException, UnknownUserIdException, |
| 693 | + RecipeUserIdAlreadyLinkedWithPrimaryUserIdException, EmailVerificationInvalidTokenException, |
| 694 | + StorageTransactionLogicException, NoSuchAlgorithmException, EmailAlreadyVerifiedException, |
| 695 | + InvalidKeySpecException { |
| 696 | + String[] args = {"../"}; |
| 697 | + |
| 698 | + TestingProcessManager.TestingProcess process = TestingProcessManager.start(args); |
| 699 | + assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED)); |
| 700 | + |
| 701 | + if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) { |
| 702 | + return; |
| 703 | + } |
| 704 | + |
| 705 | + Main main = process.getProcess(); |
| 706 | + |
| 707 | + FeatureFlagTestContent.getInstance(process.getProcess()) |
| 708 | + .setKeyValue(FeatureFlagTestContent.ENABLED_FEATURES, new EE_FEATURES[]{ |
| 709 | + EE_FEATURES.MULTI_TENANCY, EE_FEATURES.ACCOUNT_LINKING}); |
| 710 | + |
| 711 | + // tenant 1 |
| 712 | + TenantIdentifier tenantIdentifier = ResourceDistributor.getAppForTesting(); |
| 713 | + |
| 714 | + // tenant 2 |
| 715 | + JsonObject config2 = new JsonObject(); |
| 716 | + TenantIdentifier tenantIdentifier2 = new TenantIdentifier(tenantIdentifier.getConnectionUriDomain(), |
| 717 | + tenantIdentifier.getAppId(), "t1"); |
| 718 | + |
| 719 | + Multitenancy.addNewOrUpdateAppOrTenant( |
| 720 | + main, |
| 721 | + tenantIdentifier, |
| 722 | + new TenantConfig( |
| 723 | + tenantIdentifier2, |
| 724 | + new EmailPasswordConfig(true), |
| 725 | + new ThirdPartyConfig(true, null), |
| 726 | + new PasswordlessConfig(true), |
| 727 | + null, null, config2 |
| 728 | + ) |
| 729 | + ); |
| 730 | + |
| 731 | + AuthRecipeUserInfo publicTenantUser = EmailPassword.signUp(tenantIdentifier, |
| 732 | + StorageLayer.getStorage(main), main, |
| 733 | + |
| 734 | + |
| 735 | + EmailPassword.signUp(tenantIdentifier2, StorageLayer.getStorage(main), main, |
| 736 | + |
| 737 | + |
| 738 | + List<JsonObject> webauthnUsers = io.supertokens.test.webauthn.Utils.registerUsers(main, 1); |
| 739 | + |
| 740 | + String token = EmailVerification.generateEmailVerificationToken(main, |
| 741 | + publicTenantUser. getSupertokensUserId(), "[email protected]"); |
| 742 | + EmailVerification.verifyEmail(main, token); |
| 743 | + |
| 744 | + AuthRecipe.CreatePrimaryUserResult result = AuthRecipe.createPrimaryUser(main, |
| 745 | + tenantIdentifier.toAppIdentifier(), StorageLayer.getStorage(main), |
| 746 | + publicTenantUser.getSupertokensUserId()); |
| 747 | + |
| 748 | + publicTenantUser = AuthRecipe.getUserById(tenantIdentifier.toAppIdentifier(), StorageLayer.getStorage(main), |
| 749 | + result.user.getSupertokensUserId()); |
| 750 | + |
| 751 | + AuthRecipe.linkAccounts(main, |
| 752 | + webauthnUsers.getFirst().getAsJsonObject("user").get("id").getAsString(), |
| 753 | + publicTenantUser.getSupertokensUserId()); |
| 754 | + |
| 755 | + AuthRecipeUserInfo[] usersByEmail = AuthRecipe.getUsersByAccountInfo(tenantIdentifier2, |
| 756 | + StorageLayer.getStorage(tenantIdentifier2, main), false, |
| 757 | + "[email protected]", null, null, null, null); |
| 758 | + |
| 759 | + assertEquals(1, usersByEmail.length); |
| 760 | + assertEquals(1, usersByEmail[0].loginMethods.length); |
| 761 | + assertEquals(1, usersByEmail[0].tenantIds.size()); |
| 762 | + assertEquals("t1", usersByEmail[0].tenantIds.toArray()[0]); |
| 763 | + |
| 764 | + |
| 765 | + usersByEmail = AuthRecipe.getUsersByAccountInfo(tenantIdentifier, |
| 766 | + StorageLayer.getStorage(tenantIdentifier, main), false, |
| 767 | + "[email protected]", null, null, null, null); |
| 768 | + |
| 769 | + assertEquals(1, usersByEmail.length); |
| 770 | + assertEquals(2, usersByEmail[0].loginMethods.length); |
| 771 | + assertEquals(1, usersByEmail[0].tenantIds.size()); |
| 772 | + assertEquals("public", usersByEmail[0].tenantIds.toArray()[0]); |
| 773 | + } |
| 774 | + |
663 | 775 | private static List<String> getAuthRecipes() { |
664 | 776 | return Arrays.asList("emailpassword", "thirdparty", "passwordless"); |
665 | 777 | } |
|
0 commit comments