|
| 1 | +package io.supertokens.ee.test; |
| 2 | + |
| 3 | +import com.google.gson.JsonArray; |
| 4 | +import com.google.gson.JsonElement; |
| 5 | +import com.google.gson.JsonObject; |
| 6 | +import io.supertokens.ProcessState; |
| 7 | +import io.supertokens.cronjobs.CronTaskTest; |
| 8 | +import io.supertokens.ee.EEFeatureFlag; |
| 9 | +import io.supertokens.ee.cronjobs.EELicenseCheck; |
| 10 | +import io.supertokens.ee.test.httpRequest.HttpRequestForTesting; |
| 11 | +import io.supertokens.featureflag.FeatureFlag; |
| 12 | +import io.supertokens.multitenancy.Multitenancy; |
| 13 | +import io.supertokens.pluginInterface.multitenancy.*; |
| 14 | +import io.supertokens.storageLayer.StorageLayer; |
| 15 | +import io.supertokens.webserver.WebserverAPI; |
| 16 | +import org.junit.*; |
| 17 | +import org.junit.rules.TestRule; |
| 18 | + |
| 19 | +import java.util.HashSet; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Set; |
| 22 | + |
| 23 | +public class TestMultitenancyStats { |
| 24 | + @Rule |
| 25 | + public TestRule watchman = Utils.getOnFailure(); |
| 26 | + |
| 27 | + @AfterClass |
| 28 | + public static void afterTesting() { |
| 29 | + Utils.afterTesting(); |
| 30 | + } |
| 31 | + |
| 32 | + @Before |
| 33 | + public void beforeEach() { |
| 34 | + Utils.reset(); |
| 35 | + FeatureFlag.clearURLClassLoader(); |
| 36 | + } |
| 37 | + |
| 38 | + private final String OPAQUE_KEY_WITH_MULTITENANCY_FEATURE = "ijaleljUd2kU9XXWLiqFYv5br8nutTxbyBqWypQdv2N-" + |
| 39 | + "BocoNriPrnYQd0NXPm8rVkeEocN9ayq0B7c3Pv-BTBIhAZSclXMlgyfXtlwAOJk=9BfESEleW6LyTov47dXu"; |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testPaidStatsIsSentForAllAppsInMultitenancy() throws Exception { |
| 43 | + String[] args = {"../../"}; |
| 44 | + |
| 45 | + TestingProcessManager.TestingProcess process = TestingProcessManager.start(args); |
| 46 | + CronTaskTest.getInstance(process.main).setIntervalInSeconds(EELicenseCheck.RESOURCE_KEY, 1); |
| 47 | + Assert.assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED)); |
| 48 | + |
| 49 | + if (StorageLayer.isInMemDb(process.main)) { |
| 50 | + // cause we keep all features enabled in memdb anyway |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + { |
| 55 | + // Add the license |
| 56 | + JsonObject requestBody = new JsonObject(); |
| 57 | + |
| 58 | + requestBody.addProperty("licenseKey", OPAQUE_KEY_WITH_MULTITENANCY_FEATURE); |
| 59 | + |
| 60 | + HttpRequestForTesting.sendJsonPUTRequest(process.getProcess(), "", |
| 61 | + "http://localhost:3567/ee/license", |
| 62 | + requestBody, 10000, 10000, null, WebserverAPI.getLatestCDIVersion().get(), ""); |
| 63 | + } |
| 64 | + |
| 65 | + { |
| 66 | + // Create tenants and apps |
| 67 | + JsonObject config = new JsonObject(); |
| 68 | + StorageLayer.getStorage(new TenantIdentifier(null, null, null), process.getProcess()) |
| 69 | + .modifyConfigToAddANewUserPoolForTesting(config, 1); |
| 70 | + |
| 71 | + Multitenancy.addNewOrUpdateAppOrTenant(process.getProcess(), new TenantConfig( |
| 72 | + new TenantIdentifier("127.0.0.1", null, null), |
| 73 | + new EmailPasswordConfig(true), |
| 74 | + new ThirdPartyConfig(true, null), |
| 75 | + new PasswordlessConfig(true), |
| 76 | + config |
| 77 | + ), false); |
| 78 | + |
| 79 | + Multitenancy.addNewOrUpdateAppOrTenant(process.getProcess(), new TenantConfig( |
| 80 | + new TenantIdentifier("127.0.0.1", "a1", null), |
| 81 | + new EmailPasswordConfig(true), |
| 82 | + new ThirdPartyConfig(true, null), |
| 83 | + new PasswordlessConfig(true), |
| 84 | + config |
| 85 | + ), false); |
| 86 | + |
| 87 | + Multitenancy.addNewOrUpdateAppOrTenant(process.getProcess(), new TenantConfig( |
| 88 | + new TenantIdentifier("127.0.0.1", "a1", "t1"), |
| 89 | + new EmailPasswordConfig(true), |
| 90 | + new ThirdPartyConfig(true, null), |
| 91 | + new PasswordlessConfig(true), |
| 92 | + config |
| 93 | + ), false); |
| 94 | + } |
| 95 | + |
| 96 | + Thread.sleep(2000); // Let all the cron tasks run |
| 97 | + |
| 98 | + List<JsonObject> requests = EEFeatureFlag.getLicenseCheckRequests(); |
| 99 | + Set<TenantIdentifier> tenantIdentifiers = new HashSet<>(); |
| 100 | + |
| 101 | + for (JsonObject request : requests) { |
| 102 | + if (request.has("paidFeatureUsageStats")) { |
| 103 | + JsonObject paidStats = request.getAsJsonObject("paidFeatureUsageStats"); |
| 104 | + if (paidStats.has("multi_tenancy")) { |
| 105 | + JsonObject mtStats = paidStats.getAsJsonObject("multi_tenancy"); |
| 106 | + String cud = mtStats.get("connectionUriDomain").getAsString(); |
| 107 | + String appId = mtStats.get("appId").getAsString(); |
| 108 | + |
| 109 | + JsonArray tenants = mtStats.get("tenants").getAsJsonArray(); |
| 110 | + for (JsonElement tenantElem : tenants) { |
| 111 | + JsonObject tenant = tenantElem.getAsJsonObject(); |
| 112 | + String tenantId = tenant.get("tenantId").getAsString(); |
| 113 | + |
| 114 | + tenantIdentifiers.add(new TenantIdentifier(cud, appId, tenantId)); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + Assert.assertEquals(tenantIdentifiers.size(), 4); |
| 121 | + Assert.assertTrue(tenantIdentifiers.contains(new TenantIdentifier(null, null, null))); |
| 122 | + Assert.assertTrue(tenantIdentifiers.contains(new TenantIdentifier("127.0.0.1", null, null))); |
| 123 | + Assert.assertTrue(tenantIdentifiers.contains(new TenantIdentifier("127.0.0.1", "a1", null))); |
| 124 | + Assert.assertTrue(tenantIdentifiers.contains(new TenantIdentifier("127.0.0.1", "a1", "t1"))); |
| 125 | + |
| 126 | + process.kill(); |
| 127 | + Assert.assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED)); |
| 128 | + } |
| 129 | +} |
0 commit comments