Skip to content

Commit

Permalink
test: Config normalisation test (#658)
Browse files Browse the repository at this point in the history
* fix: added tests for config normalisation

* fix: fixed comment

* fix: fixed comment
  • Loading branch information
sattvikc authored May 1, 2023
1 parent f4aa012 commit 5892703
Showing 1 changed file with 212 additions and 0 deletions.
212 changes: 212 additions & 0 deletions src/test/java/io/supertokens/test/multitenant/ConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.supertokens.ProcessState;
import io.supertokens.cliOptions.CLIOptions;
import io.supertokens.config.Config;
import io.supertokens.config.CoreConfig;
import io.supertokens.config.CoreConfigTestContent;
import io.supertokens.featureflag.EE_FEATURES;
import io.supertokens.featureflag.FeatureFlagTestContent;
Expand All @@ -45,6 +46,7 @@

import java.io.File;
import java.io.IOException;
import java.util.List;

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertFalse;
Expand Down Expand Up @@ -982,4 +984,214 @@ public void testThatDifferentTenantsInSameAppCannotHaveDifferentAPIKeys() throws
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));

}

@Test
public void testConfigNormalisation() throws Exception {
TenantIdentifier[][] testCases = new TenantIdentifier[][]{
new TenantIdentifier[]{
new TenantIdentifier("c1", null, null),
},
new TenantIdentifier[]{
new TenantIdentifier("c1", null, null),
new TenantIdentifier("c1", "a1", null),
},
new TenantIdentifier[]{
new TenantIdentifier("c1", null, null),
new TenantIdentifier("c1", "a1", null),
new TenantIdentifier("c1", "a1", "t1"),
},
new TenantIdentifier[]{
new TenantIdentifier("c1", null, null),
new TenantIdentifier("c1", null, "t1"),
},
new TenantIdentifier[]{
new TenantIdentifier("a1", null, null),
},
new TenantIdentifier[]{
new TenantIdentifier(null, "a1", null),
new TenantIdentifier(null, "a1", "t1"),
},
new TenantIdentifier[]{
new TenantIdentifier(null, null, "t1"),
},
};

for (TenantIdentifier[] testCase : testCases) {

Utils.reset();
String[] args = {"../"};

Utils.setValueInConfig("email_verification_token_lifetime", "1000");
TestingProcessManager.TestingProcess process = TestingProcessManager.start(args, false);
FeatureFlagTestContent.getInstance(process.getProcess())
.setKeyValue(FeatureFlagTestContent.ENABLED_FEATURES, new EE_FEATURES[]{EE_FEATURES.MULTI_TENANCY});
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

for (int i = 0; i < testCase.length; i++) {
TenantIdentifier tenantIdentifier = testCase[i];

{ // create without setting value and test it
JsonObject coreConfigJson = new JsonObject();
StorageLayer.getStorage(new TenantIdentifier(null, null, null), process.getProcess())
.modifyConfigToAddANewUserPoolForTesting(coreConfigJson, 1);

Multitenancy.addNewOrUpdateAppOrTenant(process.getProcess(), new TenantConfig(
tenantIdentifier,
new EmailPasswordConfig(true),
new ThirdPartyConfig(true, null),
new PasswordlessConfig(true),
coreConfigJson
), false);

CoreConfig coreConfig = Config.getConfig(tenantIdentifier, process.getProcess());
// Check for previous value in the hierarchy
assertEquals((i+1) * 1000, coreConfig.getEmailVerificationTokenLifetime());

}

{ // set a new value and test that it works
JsonObject coreConfigJson = new JsonObject();
coreConfigJson.addProperty("email_verification_token_lifetime", (i+2) * 1000);
StorageLayer.getStorage(new TenantIdentifier(null, null, null), process.getProcess())
.modifyConfigToAddANewUserPoolForTesting(coreConfigJson, 1);

Multitenancy.addNewOrUpdateAppOrTenant(process.getProcess(), new TenantConfig(
tenantIdentifier,
new EmailPasswordConfig(true),
new ThirdPartyConfig(true, null),
new PasswordlessConfig(true),
coreConfigJson
), false);

CoreConfig coreConfig2 = Config.getConfig(tenantIdentifier, process.getProcess());
assertEquals((i+2) * 1000, coreConfig2.getEmailVerificationTokenLifetime());
}
}

process.kill();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
}
}

@Test
public void testTenantConfigIsNormalisedFromCUD1() throws Exception {
String[] args = {"../"};

Utils.setValueInConfig("email_verification_token_lifetime", "1000");
TestingProcessManager.TestingProcess process = TestingProcessManager.start(args, false);
FeatureFlagTestContent.getInstance(process.getProcess())
.setKeyValue(FeatureFlagTestContent.ENABLED_FEATURES, new EE_FEATURES[]{EE_FEATURES.MULTI_TENANCY});
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

{ // create app without value
JsonObject coreConfigJson = new JsonObject();
StorageLayer.getStorage(new TenantIdentifier(null, null, null), process.getProcess())
.modifyConfigToAddANewUserPoolForTesting(coreConfigJson, 1);
TenantIdentifier tenantIdentifier = new TenantIdentifier(null, "a1", null);
Multitenancy.addNewOrUpdateAppOrTenant(process.getProcess(), new TenantConfig(
tenantIdentifier,
new EmailPasswordConfig(true),
new ThirdPartyConfig(true, null),
new PasswordlessConfig(true),
coreConfigJson
), false);

CoreConfig coreConfig = Config.getConfig(tenantIdentifier, process.getProcess());
// Check for previous value in the hierarchy
assertEquals(1000, coreConfig.getEmailVerificationTokenLifetime());
}

{ // create tenant without value
JsonObject coreConfigJson = new JsonObject();
StorageLayer.getStorage(new TenantIdentifier(null, null, null), process.getProcess())
.modifyConfigToAddANewUserPoolForTesting(coreConfigJson, 1);
TenantIdentifier tenantIdentifier = new TenantIdentifier(null, "a1", "t1");
Multitenancy.addNewOrUpdateAppOrTenant(process.getProcess(), new TenantConfig(
tenantIdentifier,
new EmailPasswordConfig(true),
new ThirdPartyConfig(true, null),
new PasswordlessConfig(true),
coreConfigJson
), false);

CoreConfig coreConfig = Config.getConfig(tenantIdentifier, process.getProcess());
// Check for previous value in the hierarchy
assertEquals(1000, coreConfig.getEmailVerificationTokenLifetime());
}

process.kill();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
}

@Test
public void testTenantConfigIsNormalisedFromCUD2() throws Exception {
String[] args = {"../"};

Utils.setValueInConfig("email_verification_token_lifetime", "1000");
TestingProcessManager.TestingProcess process = TestingProcessManager.start(args, false);
FeatureFlagTestContent.getInstance(process.getProcess())
.setKeyValue(FeatureFlagTestContent.ENABLED_FEATURES, new EE_FEATURES[]{EE_FEATURES.MULTI_TENANCY});
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

{ // create cud with value
JsonObject coreConfigJson = new JsonObject();
coreConfigJson.addProperty("email_verification_token_lifetime", 2000);
StorageLayer.getStorage(new TenantIdentifier(null, null, null), process.getProcess())
.modifyConfigToAddANewUserPoolForTesting(coreConfigJson, 1);
TenantIdentifier tenantIdentifier = new TenantIdentifier("c1", null, null);
Multitenancy.addNewOrUpdateAppOrTenant(process.getProcess(), new TenantConfig(
tenantIdentifier,
new EmailPasswordConfig(true),
new ThirdPartyConfig(true, null),
new PasswordlessConfig(true),
coreConfigJson
), false);

CoreConfig coreConfig = Config.getConfig(tenantIdentifier, process.getProcess());
// Check for previous value in the hierarchy
assertEquals(2000, coreConfig.getEmailVerificationTokenLifetime());
}

{ // create app without value
JsonObject coreConfigJson = new JsonObject();
StorageLayer.getStorage(new TenantIdentifier(null, null, null), process.getProcess())
.modifyConfigToAddANewUserPoolForTesting(coreConfigJson, 1);
TenantIdentifier tenantIdentifier = new TenantIdentifier("c1", "a1", null);
Multitenancy.addNewOrUpdateAppOrTenant(process.getProcess(), new TenantConfig(
tenantIdentifier,
new EmailPasswordConfig(true),
new ThirdPartyConfig(true, null),
new PasswordlessConfig(true),
coreConfigJson
), false);

CoreConfig coreConfig = Config.getConfig(tenantIdentifier, process.getProcess());
// Check for previous value in the hierarchy
assertEquals(2000, coreConfig.getEmailVerificationTokenLifetime());
}

{ // create tenant without value
JsonObject coreConfigJson = new JsonObject();
StorageLayer.getStorage(new TenantIdentifier(null, null, null), process.getProcess())
.modifyConfigToAddANewUserPoolForTesting(coreConfigJson, 1);
TenantIdentifier tenantIdentifier = new TenantIdentifier("c1", "a1", "t1");
Multitenancy.addNewOrUpdateAppOrTenant(process.getProcess(), new TenantConfig(
tenantIdentifier,
new EmailPasswordConfig(true),
new ThirdPartyConfig(true, null),
new PasswordlessConfig(true),
coreConfigJson
), false);

CoreConfig coreConfig = Config.getConfig(tenantIdentifier, process.getProcess());
// Check for previous value in the hierarchy
assertEquals(2000, coreConfig.getEmailVerificationTokenLifetime());
}

process.kill();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
}
}

0 comments on commit 5892703

Please sign in to comment.