Skip to content

Commit

Permalink
fix: tests (#1051)
Browse files Browse the repository at this point in the history
* fix: logout apis

* fix: session revoke in logout

* fix: tests

* fix: tests

* fix: tests

* fix: tests
  • Loading branch information
sattvikc authored Sep 27, 2024
1 parent 6f225c5 commit eedbc91
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 19 deletions.
19 changes: 16 additions & 3 deletions src/main/java/io/supertokens/config/CoreConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,17 @@ public class CoreConfig {
@IgnoreForAnnotationCheck
private boolean isNormalizedAndValid = false;

@IgnoreForAnnotationCheck
private static boolean disableOAuthValidationForTest = false;

@TestOnly
public static void setDisableOAuthValidationForTest(boolean val) {
if (!Main.isTesting) {
throw new IllegalStateException("This method can only be called during testing");
}
disableOAuthValidationForTest = val;
}

public static Set<String> getValidFields() {
CoreConfig coreConfig = new CoreConfig();
JsonObject coreConfigObj = new GsonBuilder().serializeNulls().create().toJsonTree(coreConfig).getAsJsonObject();
Expand Down Expand Up @@ -901,9 +912,11 @@ void normalizeAndValidate(Main main, boolean includeConfigFilePath) throws Inval
}
}

List<String> configsTogetherSet = Arrays.asList(oauth_provider_public_service_url, oauth_provider_admin_service_url, oauth_provider_consent_login_base_url);
if(isAnySet(configsTogetherSet) && !isAllSet(configsTogetherSet)) {
throw new InvalidConfigException("If any of the following is set, all of them has to be set: oauth_provider_public_service_url, oauth_provider_admin_service_url, oauth_provider_consent_login_base_url");
if (!disableOAuthValidationForTest) {
List<String> configsTogetherSet = Arrays.asList(oauth_provider_public_service_url, oauth_provider_admin_service_url, oauth_provider_consent_login_base_url);
if(isAnySet(configsTogetherSet) && !isAllSet(configsTogetherSet)) {
throw new InvalidConfigException("If any of the following is set, all of them has to be set: oauth_provider_public_service_url, oauth_provider_admin_service_url, oauth_provider_consent_login_base_url");
}
}

isNormalizedAndValid = true;
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/io/supertokens/test/CronjobTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ public void testThatCronJobsHaveTenantsInfoAfterRestart() throws Exception {
count++;
}
}
assertEquals(9, count);
assertEquals(10, count);
}

process.kill(false);
Expand All @@ -992,7 +992,7 @@ public void testThatCronJobsHaveTenantsInfoAfterRestart() throws Exception {
{
List<List<List<TenantIdentifier>>> tenantsInfos = Cronjobs.getInstance(process.getProcess())
.getTenantInfos();
assertEquals(10, tenantsInfos.size());
assertEquals(11, tenantsInfos.size());
int count = 0;
for (List<List<TenantIdentifier>> tenantsInfo : tenantsInfos) {
if (tenantsInfo != null) {
Expand All @@ -1002,7 +1002,7 @@ public void testThatCronJobsHaveTenantsInfoAfterRestart() throws Exception {
count++;
}
}
assertEquals(9, count);
assertEquals(10, count);
}

process.kill();
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/io/supertokens/test/FeatureFlagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,9 @@ public void testNetworkCallIsMadeInCoreInit() throws Exception {
private final String OPAQUE_KEY_WITH_SECURITY_FEATURE = "tje5MVjlRz0Kwzax-mKksdYpZvwNhQagFdHj=ma=W0H7WET9R0Hcpv" +
"Aui9r3wIk=swO2TIBLQNa94y10VQkzAa0Q0iw6GPzMeftJ4uvbnb1qpGpyf4K0cUwIZ76Pd9kZ";

private final String OPAQUE_KEY_WITH_OAUTH_FEATURE = "hjspBIZu94zCJ2g7w6SMz4ERAKyaLogBpSy8OhgjcLRjsRiH2CXKEEgI" +
"SAikEn2lixgV67=56LrTqHiExBcOuZU-TQoYAaTJuLNNdKxHjXAdgDdB5g1kYDcPANGNEoV-";

@Test
public void testPaidStatsContainsAllEnabledFeatures() throws Exception {
String[] args = {"../"};
Expand All @@ -918,6 +921,7 @@ public void testPaidStatsContainsAllEnabledFeatures() throws Exception {
OPAQUE_KEY_WITH_DASHBOARD_FEATURE,
OPAQUE_KEY_WITH_ACCOUNT_LINKING_FEATURE,
OPAQUE_KEY_WITH_SECURITY_FEATURE,
OPAQUE_KEY_WITH_OAUTH_FEATURE
};

Set<EE_FEATURES> requiredFeatures = new HashSet<>();
Expand Down
35 changes: 22 additions & 13 deletions src/test/java/io/supertokens/test/SuperTokensSaaSSecretTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,8 @@ public void testThatTenantCannotGetProtectedConfigIfSuperTokensSaaSSecretIsSet()
return;
}

CoreConfig.setDisableOAuthValidationForTest(true);

for (int i = 0; i < PROTECTED_CORE_CONFIG.length; i++) {
JsonObject j = new JsonObject();
if (PROTECTED_CORE_CONFIG_VALUES[i] instanceof String) {
Expand All @@ -561,50 +563,57 @@ public void testThatTenantCannotGetProtectedConfigIfSuperTokensSaaSSecretIsSet()
j.addProperty(PROTECTED_CORE_CONFIG[i], (Integer) PROTECTED_CORE_CONFIG_VALUES[i]);
}
Multitenancy.addNewOrUpdateAppOrTenant(process.main, new TenantIdentifier(null, null, null),
new TenantConfig(new TenantIdentifier(null, null, "t" + i), new EmailPasswordConfig(false),
new TenantConfig(new TenantIdentifier(null, "a" + i, null), new EmailPasswordConfig(false),
new ThirdPartyConfig(false, new ThirdPartyConfig.Provider[0]),
new PasswordlessConfig(false),
null, null, j));

{
JsonObject response = HttpRequestForTesting.sendJsonRequest(process.getProcess(), "",
HttpRequestForTesting.getMultitenantUrl(TenantIdentifier.BASE_TENANT,
"/recipe/multitenancy/tenant/list"),
"/recipe/multitenancy/app/list"),
null, 1000, 1000, null,
SemVer.v3_0.get(), "GET", apiKey, "multitenancy");

Assert.assertEquals("OK", response.getAsJsonPrimitive("status").getAsString());

boolean found = false;
for (JsonElement tenant : response.get("tenants").getAsJsonArray()) {
JsonObject tenantObj = tenant.getAsJsonObject();

if (tenantObj.get("tenantId").getAsString().equals("t" + i)) {
for (JsonElement app : response.get("apps").getAsJsonArray()) {
JsonObject appObj = app.getAsJsonObject();
if (appObj.get("appId").getAsString().equals("a" + i)) {
found = true;

assertFalse(tenantObj.get("coreConfig").getAsJsonObject().has(PROTECTED_CORE_CONFIG[i]));
for (JsonElement tenant : appObj.get("tenants").getAsJsonArray()) {
JsonObject tenantObj = tenant.getAsJsonObject();

assertFalse(tenantObj.get("coreConfig").getAsJsonObject().has(PROTECTED_CORE_CONFIG[i]));
}
}
}

Assert.assertTrue(found);
}

{
JsonObject response = HttpRequestForTesting.sendJsonRequest(process.getProcess(), "",
HttpRequestForTesting.getMultitenantUrl(TenantIdentifier.BASE_TENANT,
"/recipe/multitenancy/tenant/list"),
"/recipe/multitenancy/app/list"),
null, 1000, 1000, null,
SemVer.v3_0.get(), "GET", saasSecret, "multitenancy");

Assert.assertEquals("OK", response.getAsJsonPrimitive("status").getAsString());

boolean found = false;
for (JsonElement tenant : response.get("tenants").getAsJsonArray()) {
JsonObject tenantObj = tenant.getAsJsonObject();

if (tenantObj.get("tenantId").getAsString().equals("t" + i)) {
for (JsonElement app : response.get("apps").getAsJsonArray()) {
JsonObject appObj = app.getAsJsonObject();
if (appObj.get("appId").getAsString().equals("a" + i)) {
found = true;

Assert.assertTrue(tenantObj.get("coreConfig").getAsJsonObject().has(PROTECTED_CORE_CONFIG[i]));
for (JsonElement tenant : appObj.get("tenants").getAsJsonArray()) {
JsonObject tenantObj = tenant.getAsJsonObject();

assertTrue(tenantObj.get("coreConfig").getAsJsonObject().has(PROTECTED_CORE_CONFIG[i]));
}
}
}
Assert.assertTrue(found);
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/io/supertokens/test/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import io.supertokens.Main;
import io.supertokens.config.CoreConfig;
import io.supertokens.pluginInterface.PluginInterfaceTesting;
import io.supertokens.pluginInterface.useridmapping.UserIdMapping;
import io.supertokens.storageLayer.StorageLayer;
Expand Down Expand Up @@ -83,6 +84,7 @@ public static void reset() {
PluginInterfaceTesting.isTesting = true;
Main.makeConsolePrintSilent = true;
String installDir = "../";
CoreConfig.setDisableOAuthValidationForTest(false);
try {

// if the default config is not the same as the current config, we must reset the storage layer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@
import io.supertokens.featureflag.EE_FEATURES;
import io.supertokens.featureflag.FeatureFlagTestContent;
import io.supertokens.multitenancy.Multitenancy;
import io.supertokens.oauth.OAuth;
import io.supertokens.oauth.OAuthToken;
import io.supertokens.passwordless.Passwordless;
import io.supertokens.pluginInterface.STORAGE_TYPE;
import io.supertokens.pluginInterface.Storage;
import io.supertokens.pluginInterface.authRecipe.AuthRecipeUserInfo;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
import io.supertokens.pluginInterface.multitenancy.*;
import io.supertokens.pluginInterface.oauth.OAuthStorage;
import io.supertokens.pluginInterface.totp.TOTPDevice;
import io.supertokens.session.Session;
import io.supertokens.storageLayer.StorageLayer;
Expand Down Expand Up @@ -174,6 +177,11 @@ null, null, new JsonObject()
UserIdMapping.createUserIdMapping(process.getProcess(), app.toAppIdentifier(), appStorage,
plUser.user.getSupertokensUserId(), "externalid", null, false);

OAuth.addOrUpdateClientId(process.getProcess(), app.toAppIdentifier(), appStorage, "test", false);
OAuth.createLogoutRequestAndReturnRedirectUri(process.getProcess(), app.toAppIdentifier(), appStorage, "test", "http://localhost", "sessionHandle", "state");
((OAuthStorage) appStorage).addM2MToken(app.toAppIdentifier(), "test", 1000, 2000);
OAuth.revokeSessionHandle(process.getProcess(), app.toAppIdentifier(), appStorage, "sessionHandle");

String[] tablesThatHaveData = appStorage
.getAllTablesInTheDatabaseThatHasDataForAppId(app.getAppId());
tablesThatHaveData = removeStrings(tablesThatHaveData, tablesToIgnore);
Expand Down

0 comments on commit eedbc91

Please sign in to comment.