-
Notifications
You must be signed in to change notification settings - Fork 514
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
fix: License tests #656
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
290 changes: 290 additions & 0 deletions
290
src/test/java/io/supertokens/test/multitenant/api/TestLicenseBehaviour.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done