Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: License tests #656

Merged
merged 3 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/test/java/io/supertokens/test/TestingProcessManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ public void kill() throws InterruptedException {
killed = true;
}

public void killWithoutDeletingData() throws InterruptedException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

follow same pattern as postgrs

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if (killed) {
return;
}
main.killForTestingAndWaitForShutdown();
killed = true;
}

public EventAndException checkOrWaitForEvent(PROCESS_STATE state) throws InterruptedException {
return checkOrWaitForEvent(state, 15000);
}
Expand Down
6 changes: 5 additions & 1 deletion src/test/java/io/supertokens/test/multitenant/LogTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ public void testLogThatEachLineIsUniqueOnStartup() throws Exception {
new PasswordlessConfig(true),
new JsonObject()), false);

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

process.killWithoutDeletingData();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));

ByteArrayOutputStream stdOutput = new ByteArrayOutputStream();
Expand Down Expand Up @@ -139,6 +141,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.killWithoutDeletingData();
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.supertokens.test.httpRequest.HttpResponseException;

import java.io.IOException;
import java.util.HashMap;

import static org.junit.Assert.assertEquals;

Expand All @@ -34,7 +35,9 @@ public static void createConnectionUriDomain(Main main, TenantIdentifier sourceT
boolean thirdPartyEnabled, boolean passwordlessEnabled,
JsonObject coreConfig) throws HttpResponseException, IOException {
JsonObject requestBody = new JsonObject();
requestBody.addProperty("connectionUriDomain", connectionUriDomain);
if (connectionUriDomain != null) {
requestBody.addProperty("connectionUriDomain", connectionUriDomain);
}
requestBody.addProperty("emailPasswordEnabled", emailPasswordEnabled);
requestBody.addProperty("thirdPartyEnabled", thirdPartyEnabled);
requestBody.addProperty("passwordlessEnabled", passwordlessEnabled);
Expand Down Expand Up @@ -259,4 +262,35 @@ public static JsonObject tpSignInUp(TenantIdentifier tenantIdentifier, String th

return response.get("user").getAsJsonObject();
}

public static void addLicense(String licenseKey, Main main) throws HttpResponseException, IOException {
JsonObject licenseKeyRequest = new JsonObject();
licenseKeyRequest.addProperty("licenseKey", licenseKey);

JsonObject response = HttpRequestForTesting.sendJsonPUTRequest(main, "",
"http://localhost:3567/ee/license", licenseKeyRequest,
1000, 1000, null,
Utils.getCdiVersionStringLatestForTests(), null);
assertEquals("OK", response.get("status").getAsString());
}

public static void removeLicense(Main main) throws HttpResponseException, IOException {
JsonObject response = HttpRequestForTesting.sendJsonDELETERequest(main, "",
"http://localhost:3567/ee/license", null,
1000, 1000, null,
Utils.getCdiVersionStringLatestForTests(), null);
assertEquals("OK", response.get("status").getAsString());
}

public static JsonObject getEpUserById(TenantIdentifier tenantIdentifier, String userId, Main main)
throws HttpResponseException, IOException {
HashMap<String, String> map = new HashMap<>();
map.put("userId", userId);
JsonObject userResponse = HttpRequestForTesting.sendGETRequest(main, "",
HttpRequestForTesting.getMultitenantUrl(tenantIdentifier, "/recipe/user"),
map, 1000, 1000, null, Utils.getCdiVersionStringLatestForTests(),
"emailpassword");
assertEquals("OK", userResponse.getAsJsonPrimitive("status").getAsString());
return userResponse.getAsJsonObject("user");
}
}