Skip to content

Commit

Permalink
fix: License tests (#656)
Browse files Browse the repository at this point in the history
* fix: license tests

* fix: added test

* fix: pr comment
  • Loading branch information
sattvikc authored May 1, 2023
1 parent 5dde394 commit f4aa012
Show file tree
Hide file tree
Showing 4 changed files with 343 additions and 4 deletions.
14 changes: 13 additions & 1 deletion src/test/java/io/supertokens/test/TestingProcessManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,17 @@ String[] getArgs() {
}

public void kill() throws InterruptedException {
kill(true);
}

public void kill(boolean removeAllInfo) throws InterruptedException {
if (killed) {
return;
}
// we check if there are multiple user pool IDs loaded, and if there are,
// we clear all the info before killing cause otherwise those extra dbs will retain info
// across tests
if (StorageLayer.hasMultipleUserPools(this.main)) {
if (removeAllInfo && StorageLayer.hasMultipleUserPools(this.main)) {
try {
main.deleteAllInformationForTesting();
} catch (Exception e) {
Expand All @@ -143,6 +147,14 @@ public void kill() throws InterruptedException {
killed = true;
}

public void killWithoutDeletingData() throws InterruptedException {
if (killed) {
return;
}
main.killForTestingAndWaitForShutdown();
killed = true;
}

public EventAndException checkOrWaitForEvent(PROCESS_STATE state) throws InterruptedException {
return checkOrWaitForEvent(state, 15000);
}
Expand Down
7 changes: 5 additions & 2 deletions src/test/java/io/supertokens/test/multitenant/LogTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import io.supertokens.featureflag.FeatureFlagTestContent;
import io.supertokens.multitenancy.Multitenancy;
import io.supertokens.pluginInterface.multitenancy.*;
import io.supertokens.storageLayer.StorageLayer;
import io.supertokens.test.TestingProcessManager;
import io.supertokens.test.Utils;
import org.junit.AfterClass;
Expand Down Expand Up @@ -107,7 +106,9 @@ public void testLogThatEachLineIsUniqueOnStartup() throws Exception {
new PasswordlessConfig(true),
new JsonObject()), false);

process.kill();
assertEquals(7, Multitenancy.getAllTenants(process.getProcess()).length);

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

ByteArrayOutputStream stdOutput = new ByteArrayOutputStream();
Expand Down Expand Up @@ -139,6 +140,8 @@ public void testLogThatEachLineIsUniqueOnStartup() throws Exception {

assertEquals(uniqueLines.size(), lines.length);

assertEquals(7, Multitenancy.getAllTenants(process.getProcess()).length);

process.kill();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,290 @@
/*
* Copyright (c) 2023, VRAI Labs and/or its affiliates. All rights reserved.
*
* This software is licensed under the Apache License, Version 2.0 (the
* "License") as published by the Apache Software Foundation.
*
* You may not use this file except in compliance with the License. You may
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package io.supertokens.test.multitenant.api;

import com.google.gson.JsonObject;
import io.supertokens.ProcessState;
import io.supertokens.featureflag.exceptions.FeatureNotEnabledException;
import io.supertokens.multitenancy.exception.BadPermissionException;
import io.supertokens.multitenancy.exception.CannotModifyBaseConfigException;
import io.supertokens.pluginInterface.exceptions.InvalidConfigException;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
import io.supertokens.pluginInterface.multitenancy.TenantIdentifier;
import io.supertokens.pluginInterface.multitenancy.ThirdPartyConfig;
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException;
import io.supertokens.storageLayer.StorageLayer;
import io.supertokens.test.TestingProcessManager;
import io.supertokens.test.Utils;
import io.supertokens.test.httpRequest.HttpResponseException;
import io.supertokens.thirdparty.InvalidProviderConfigException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.*;

public class TestLicenseBehaviour {
TestingProcessManager.TestingProcess process;

@AfterClass
public static void afterTesting() {
Utils.afterTesting();
}

@After
public void afterEach() throws InterruptedException {
process.kill();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
}

@Before
public void beforeEach() throws InterruptedException, InvalidProviderConfigException,
StorageQueryException, FeatureNotEnabledException, TenantOrAppNotFoundException, IOException,
InvalidConfigException, CannotModifyBaseConfigException, BadPermissionException {
Utils.reset();

String[] args = {"../"};

this.process = TestingProcessManager.start(args);
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));
}

private final String OPAQUE_KEY_WITH_MULTITENANCY_FEATURE = "ijaleljUd2kU9XXWLiqFYv5br8nutTxbyBqWypQdv2N-" +
"BocoNriPrnYQd0NXPm8rVkeEocN9ayq0B7c3Pv-BTBIhAZSclXMlgyfXtlwAOJk=9BfESEleW6LyTov47dXu";

@Test
public void testAllowLicenseRemovalForCoreWithMultitenancy() throws Exception {
TestMultitenancyAPIHelper.addLicense(OPAQUE_KEY_WITH_MULTITENANCY_FEATURE, process.getProcess());

JsonObject coreConfig = new JsonObject();
StorageLayer.getStorage(new TenantIdentifier(null, null, null), process.getProcess())
.modifyConfigToAddANewUserPoolForTesting(coreConfig, 1);

TestMultitenancyAPIHelper.createApp(
process.getProcess(),
new TenantIdentifier(null, null, null),
"a1", true, true, true,
coreConfig);

TestMultitenancyAPIHelper.createTenant(
process.getProcess(),
new TenantIdentifier(null, "a1", null),
"t1", true, true, true,
coreConfig);

TestMultitenancyAPIHelper.removeLicense(process.getProcess());

// Sign up and get user info
JsonObject userInfo = TestMultitenancyAPIHelper.epSignUp(new TenantIdentifier(null, "a1", "t1"), "user@example.com", "password", process.getProcess());
JsonObject userInfo2 = TestMultitenancyAPIHelper.getEpUserById(new TenantIdentifier(null, "a1", "t1"), userInfo.get("id").getAsString(), process.getProcess());
assertEquals(userInfo, userInfo2);
}

@Test
public void testThatCreationOfNewTenantIsNotAllowedAfterLicenseRemoval() throws Exception {
TestMultitenancyAPIHelper.addLicense(OPAQUE_KEY_WITH_MULTITENANCY_FEATURE, process.getProcess());

JsonObject coreConfig = new JsonObject();
StorageLayer.getStorage(new TenantIdentifier(null, null, null), process.getProcess())
.modifyConfigToAddANewUserPoolForTesting(coreConfig, 1);

TestMultitenancyAPIHelper.createApp(
process.getProcess(),
new TenantIdentifier(null, null, null),
"a1", true, true, true,
coreConfig);

TestMultitenancyAPIHelper.createTenant(
process.getProcess(),
new TenantIdentifier(null, "a1", null),
"t1", true, true, true,
coreConfig);

TestMultitenancyAPIHelper.removeLicense(process.getProcess());

try {
TestMultitenancyAPIHelper.createTenant(
process.getProcess(),
new TenantIdentifier(null, "a1", null),
"t2", true, true, true,
coreConfig);
fail();
} catch (HttpResponseException e) {
assertEquals("Http error. Status Code: 402. Message: Cannot use feature: multi_tenancy, because the license key is missing, or doesn't have this feature enabled.", e.getMessage());
}
}

@Test
public void testThatCoreCanRestartWithAllTheTenantsWithoutLicenseKey() throws Exception {
TestMultitenancyAPIHelper.addLicense(OPAQUE_KEY_WITH_MULTITENANCY_FEATURE, process.getProcess());

JsonObject coreConfig = new JsonObject();
StorageLayer.getStorage(new TenantIdentifier(null, null, null), process.getProcess())
.modifyConfigToAddANewUserPoolForTesting(coreConfig, 1);

TestMultitenancyAPIHelper.createApp(
process.getProcess(),
new TenantIdentifier(null, null, null),
"a1", true, true, true,
coreConfig);

TestMultitenancyAPIHelper.createTenant(
process.getProcess(),
new TenantIdentifier(null, "a1", null),
"t1", true, true, true,
coreConfig);

JsonObject tenants = TestMultitenancyAPIHelper.listConnectionUriDomains(new TenantIdentifier(null, null, null),
process.getProcess());

TestMultitenancyAPIHelper.removeLicense(process.getProcess());

// Restart the core
process.kill(false);
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));

String[] args = {"../"};
this.process = TestingProcessManager.start(args);
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

JsonObject tenants2 = TestMultitenancyAPIHelper.listConnectionUriDomains(new TenantIdentifier(null, null, null),
process.getProcess());

// Ensure all tenants are loaded back correctly
assertEquals(tenants, tenants2);

try {
TestMultitenancyAPIHelper.createTenant(
process.getProcess(),
new TenantIdentifier(null, "a1", null),
"t2", true, true, true,
coreConfig);
fail();
} catch (HttpResponseException e) {
assertEquals("Http error. Status Code: 402. Message: Cannot use feature: multi_tenancy, because the license key is missing, or doesn't have this feature enabled.", e.getMessage());
}
}

@Test
public void testThatAddingThirdPartyConfigIsNotAllowedAfterLicenseRemoval() throws Exception {
TestMultitenancyAPIHelper.addLicense(OPAQUE_KEY_WITH_MULTITENANCY_FEATURE, process.getProcess());

JsonObject coreConfig = new JsonObject();
StorageLayer.getStorage(new TenantIdentifier(null, null, null), process.getProcess())
.modifyConfigToAddANewUserPoolForTesting(coreConfig, 1);

TestMultitenancyAPIHelper.createApp(
process.getProcess(),
new TenantIdentifier(null, null, null),
"a1", true, true, true,
coreConfig);

TestMultitenancyAPIHelper.createTenant(
process.getProcess(),
new TenantIdentifier(null, "a1", null),
"t1", true, true, true,
coreConfig);

TestMultitenancyAPIHelper.removeLicense(process.getProcess());

try {
TestMultitenancyAPIHelper.addOrUpdateThirdPartyProviderConfig(
new TenantIdentifier(null, "a1", "t1"),
new ThirdPartyConfig.Provider(
"google", "Google", null, null, null, null, null, null, null, null, null, null, null, null
),
process.getProcess());
fail();
} catch (HttpResponseException e) {
assertEquals("Http error. Status Code: 402. Message: Cannot use feature: multi_tenancy, because the license key is missing, or doesn't have this feature enabled.", e.getMessage());
}
}

@Test
public void testThatAssociationOfUserWithAnotherTenantIsNotAllowedAfterLicenseRemoval() throws Exception {
TestMultitenancyAPIHelper.addLicense(OPAQUE_KEY_WITH_MULTITENANCY_FEATURE, process.getProcess());

JsonObject coreConfig = new JsonObject();
StorageLayer.getStorage(new TenantIdentifier(null, null, null), process.getProcess())
.modifyConfigToAddANewUserPoolForTesting(coreConfig, 1);

TestMultitenancyAPIHelper.createApp(
process.getProcess(),
new TenantIdentifier(null, null, null),
"a1", true, true, true,
coreConfig);

TestMultitenancyAPIHelper.createTenant(
process.getProcess(),
new TenantIdentifier(null, "a1", null),
"t1", true, true, true,
coreConfig);

TestMultitenancyAPIHelper.createTenant(
process.getProcess(),
new TenantIdentifier(null, "a1", null),
"t2", true, true, true,
coreConfig);

TestMultitenancyAPIHelper.removeLicense(process.getProcess());

JsonObject userInfo = TestMultitenancyAPIHelper.epSignUp(new TenantIdentifier(null, "a1", "t1"), "user@example.com", "password", process.getProcess());

try {
TestMultitenancyAPIHelper.associateUserToTenant(new TenantIdentifier(null, "a1", "t2"), userInfo.get("id").getAsString(), process.getProcess());
fail();
} catch (HttpResponseException e) {
assertEquals("Http error. Status Code: 402. Message: Cannot use feature: multi_tenancy, because the license key is missing, or doesn't have this feature enabled.", e.getMessage());
}
}

@Test
public void testUpdationOfBaseTenantIsAllowedWithoutLicense() throws Exception {
TestMultitenancyAPIHelper.addLicense(OPAQUE_KEY_WITH_MULTITENANCY_FEATURE, process.getProcess());

JsonObject coreConfig = new JsonObject();
StorageLayer.getStorage(new TenantIdentifier(null, null, null), process.getProcess())
.modifyConfigToAddANewUserPoolForTesting(coreConfig, 1);

TestMultitenancyAPIHelper.createApp(
process.getProcess(),
new TenantIdentifier(null, null, null),
"a1", true, true, true,
coreConfig);

TestMultitenancyAPIHelper.createTenant(
process.getProcess(),
new TenantIdentifier(null, "a1", null),
"t1", true, true, true,
coreConfig);

TestMultitenancyAPIHelper.removeLicense(process.getProcess());

JsonObject config = new JsonObject();
TestMultitenancyAPIHelper.createConnectionUriDomain(process.main, new TenantIdentifier(null, null, null), null, true, true, true, new JsonObject() );
TestMultitenancyAPIHelper.addOrUpdateThirdPartyProviderConfig(
new TenantIdentifier(null, null, null),
new ThirdPartyConfig.Provider(
"google", "Google", null, null, null, null, null, null, null, null, null, null, null, null
),
process.getProcess());
}
}
Loading

0 comments on commit f4aa012

Please sign in to comment.