From e36572511b93d50eb1cef45c47a8388ae0615bf4 Mon Sep 17 00:00:00 2001 From: Mihaly Lengyel Date: Thu, 14 Sep 2023 14:11:24 +0200 Subject: [PATCH] test: make tests more backwards compatible --- ...multitenancy.dynamic_login_methods.test.js | 142 ++++-------------- .../multitenancy.tenant_interactions.test.js | 54 +------ test/helpers.js | 56 +++++++ test/server/index.js | 1 + 4 files changed, 92 insertions(+), 161 deletions(-) diff --git a/test/end-to-end/multitenancy.dynamic_login_methods.test.js b/test/end-to-end/multitenancy.dynamic_login_methods.test.js index 67add160c..7d1cf66b9 100644 --- a/test/end-to-end/multitenancy.dynamic_login_methods.test.js +++ b/test/end-to-end/multitenancy.dynamic_login_methods.test.js @@ -36,6 +36,9 @@ import { clickOnProviderButton, loginWithAuth0, isMultitenancySupported, + isAccountLinkingSupported, + isMultitenancyManagementEndpointsSupported, + setupTenant, } from "../helpers"; import { TEST_CLIENT_BASE_URL, @@ -56,7 +59,7 @@ describe("SuperTokens Multitenancy dynamic login methods", function () { let pageCrashed; before(async function () { - const isSupported = await isMultitenancySupported(); + const isSupported = (await isMultitenancySupported()) && (await isMultitenancyManagementEndpointsSupported()); if (!isSupported) { this.skip(); } @@ -128,13 +131,18 @@ describe("SuperTokens Multitenancy dynamic login methods", function () { page.waitForNavigation({ waitUntil: "networkidle0" }), ]); const providers = await getProvidersLabels(page); - compareArrayContents(providers, [ - "Continue with Github", - "Continue with Google", - "Continue with Facebook", - "Continue with Auth0", - "Continue with Mock Provider", - ]); + compareArrayContents( + providers, + (await isAccountLinkingSupported()) + ? [ + "Continue with Github", + "Continue with Google", + "Continue with Facebook", + "Continue with Auth0", + "Continue with Mock Provider", + ] + : ["Continue with Github", "Continue with Google", "Continue with Facebook", "Continue with Auth0"] + ); const inputNames = await getInputNames(page); assert.deepStrictEqual(inputNames, ["email", "password"]); }); @@ -370,13 +378,18 @@ describe("SuperTokens Multitenancy dynamic login methods", function () { ]); const providers = await getProvidersLabels(page); - compareArrayContents(providers, [ - "Continue with Github", - "Continue with Google", - "Continue with Facebook", - "Continue with Auth0", - "Continue with Mock Provider", - ]); + compareArrayContents( + providers, + (await isAccountLinkingSupported()) + ? [ + "Continue with Github", + "Continue with Google", + "Continue with Facebook", + "Continue with Auth0", + "Continue with Mock Provider", + ] + : ["Continue with Github", "Continue with Google", "Continue with Facebook", "Continue with Auth0"] + ); assert.strictEqual(await getProviderLogoCount(page), 3); }); @@ -851,41 +864,7 @@ function clearDynamicLoginMethodsSettings(page) { } export async function enableDynamicLoginMethods(page, mockLoginMethods, tenantId = "public") { - let coreResp = await fetch(`${connectionURI}/recipe/multitenancy/tenant`, { - method: "PUT", - headers: new Headers([ - ["content-type", "application/json"], - ["rid", "multitenancy"], - ]), - body: JSON.stringify({ - tenantId, - emailPasswordEnabled: mockLoginMethods.emailPassword?.enabled === true, - thirdPartyEnabled: mockLoginMethods.thirdParty?.enabled === true, - passwordlessEnabled: mockLoginMethods.passwordless?.enabled === true, - coreConfig: {}, - }), - }); - assert.strictEqual(coreResp.status, 200); - - for (const provider of mockLoginMethods["thirdParty"]?.providers) { - coreResp = await fetch(`${connectionURI}/${tenantId}/recipe/multitenancy/config/thirdparty`, { - method: "PUT", - headers: new Headers([ - ["content-type", "application/json"], - ["rid", "multitenancy"], - ]), - body: JSON.stringify({ - skipValidation: true, - config: { - ...providerConfigs[provider.id.split("-")[0]], - thirdPartyId: provider.id, - name: provider.name, - }, - }), - }); - - assert.strictEqual(coreResp.status, 200); - } + await setupTenant(tenantId, mockLoginMethods); return page.evaluate(() => { window.localStorage.setItem("usesDynamicLoginMethods", "true"); @@ -901,66 +880,3 @@ export async function enableDynamicLoginMethods(page, mockLoginMethods, tenantId function compareArrayContents(actual, expected) { return assert.deepStrictEqual(actual.sort(), expected.sort()); } - -const providerConfigs = { - apple: { - clients: [ - { - clientId: "4398792-io.supertokens.example.service", - additionalConfig: { - keyId: "7M48Y4RYDL", - privateKey: - "-----BEGIN PRIVATE KEY-----\nMIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgu8gXs+XYkqXD6Ala9Sf/iJXzhbwcoG5dMh1OonpdJUmgCgYIKoZIzj0DAQehRANCAASfrvlFbFCYqn3I2zeknYXLwtH30JuOKestDbSfZYxZNMqhF/OzdZFTV0zc5u5s3eN+oCWbnvl0hM+9IW0UlkdA\n-----END PRIVATE KEY-----", - teamId: "YWQCXGJRJL", - }, - }, - ], - }, - github: { - clients: [ - { - clientSecret: "e97051221f4b6426e8fe8d51486396703012f5bd", - clientId: "467101b197249757c71f", - }, - ], - }, - google: { - clients: [ - { - clientId: "1060725074195-kmeum4crr01uirfl2op9kd5acmi9jutn.apps.googleusercontent.com", - clientSecret: "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", - }, - ], - }, - auth0: { - // this contains info about forming the authorisation redirect URL without the state params and without the redirect_uri param - authorizationEndpoint: `https://${process.env.AUTH0_DOMAIN}/authorize`, - authorizationEndpointQueryParams: { - scope: "openid profile email email_verified", - }, - jwksURI: `https://${process.env.AUTH0_DOMAIN}/.well-known/jwks.json`, - tokenEndpoint: `https://${process.env.AUTH0_DOMAIN}/oauth/token`, - clients: [ - { - clientId: process.env.AUTH0_CLIENT_ID, - clientSecret: process.env.AUTH0_CLIENT_SECRET, - }, - ], - userInfoMap: { - fromIdTokenPayload: { - userId: "sub", - email: "email", - emailVerified: "email_verified", - }, - }, - }, - test: { - // We add a client since it's required - clients: [ - { - clientId: "1060725074195-kmeum4crr01uirfl2op9kd5acmi9jutn.apps.googleusercontent.com", - clientSecret: "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", - }, - ], - }, -}; diff --git a/test/end-to-end/multitenancy.tenant_interactions.test.js b/test/end-to-end/multitenancy.tenant_interactions.test.js index 03e2eb701..26e4ea296 100644 --- a/test/end-to-end/multitenancy.tenant_interactions.test.js +++ b/test/end-to-end/multitenancy.tenant_interactions.test.js @@ -40,6 +40,11 @@ import { getTextByDataSupertokens, getVerificationEmailErrorTitle, isMultitenancySupported, + isMultitenancyManagementEndpointsSupported, + setupTenant, + addUserToTenant, + removeUserFromTenant, + removeTenant, } from "../helpers"; import { TEST_CLIENT_BASE_URL, @@ -59,7 +64,7 @@ describe("SuperTokens Multitenancy tenant interactions", function () { let pageCrashed; before(async function () { - const isSupported = await isMultitenancySupported(); + const isSupported = (await isMultitenancySupported()) && (await isMultitenancyManagementEndpointsSupported()); if (!isSupported) { this.skip(); } @@ -1319,53 +1324,6 @@ export async function enableDynamicLoginMethods(page) { }); } -async function setupTenant(tenantId, mockLoginMethods) { - let coreResp = await fetch(`${TEST_APPLICATION_SERVER_BASE_URL}/setupTenant`, { - method: "POST", - headers: new Headers([["content-type", "application/json"]]), - body: JSON.stringify({ - tenantId, - mockLoginMethods, - }), - }); - assert.strictEqual(coreResp.status, 200); -} - -async function addUserToTenant(tenantId, recipeUserId) { - let coreResp = await fetch(`${TEST_APPLICATION_SERVER_BASE_URL}/addUserToTenant`, { - method: "POST", - headers: new Headers([["content-type", "application/json"]]), - body: JSON.stringify({ - tenantId, - recipeUserId, - }), - }); - assert.strictEqual(coreResp.status, 200); -} - -async function removeUserFromTenant(tenantId, recipeUserId) { - let coreResp = await fetch(`${TEST_APPLICATION_SERVER_BASE_URL}/removeUserFromTenant`, { - method: "POST", - headers: new Headers([["content-type", "application/json"]]), - body: JSON.stringify({ - tenantId, - recipeUserId, - }), - }); - assert.strictEqual(coreResp.status, 200); -} - -async function removeTenant(tenantId) { - let coreResp = await fetch(`${TEST_APPLICATION_SERVER_BASE_URL}/removeTenant`, { - method: "POST", - headers: new Headers([["content-type", "application/json"]]), - body: JSON.stringify({ - tenantId, - }), - }); - assert.strictEqual(coreResp.status, 200); -} - async function epSignUp(page, email, fieldErrors, generalError, emailVerificationRequired) { const link = await getSignInOrSignUpSwitchLink(page); const linkText = await link.evaluate((e) => e.textContent); diff --git a/test/helpers.js b/test/helpers.js index 59b54a4c4..21f6f2a2f 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -844,6 +844,15 @@ export async function isMultitenancySupported() { return true; } +export async function isMultitenancyManagementEndpointsSupported() { + const features = await getFeatureFlags(); + if (!features.includes("multitenancyManagementEndpoints")) { + return false; + } + + return true; +} + export async function isAccountLinkingSupported() { const features = await getFeatureFlags(); if (!features.includes("accountlinking")) { @@ -867,3 +876,50 @@ export async function setGeneralErrorToLocalStorage(recipeName, action, page) { export async function getTestEmail() { return `john.doe+${Date.now()}@supertokens.io`; } + +export async function setupTenant(tenantId, mockLoginMethods) { + let coreResp = await fetch(`${TEST_APPLICATION_SERVER_BASE_URL}/setupTenant`, { + method: "POST", + headers: new Headers([["content-type", "application/json"]]), + body: JSON.stringify({ + tenantId, + mockLoginMethods, + }), + }); + assert.strictEqual(coreResp.status, 200); +} + +export async function addUserToTenant(tenantId, recipeUserId) { + let coreResp = await fetch(`${TEST_APPLICATION_SERVER_BASE_URL}/addUserToTenant`, { + method: "POST", + headers: new Headers([["content-type", "application/json"]]), + body: JSON.stringify({ + tenantId, + recipeUserId, + }), + }); + assert.strictEqual(coreResp.status, 200); +} + +export async function removeUserFromTenant(tenantId, recipeUserId) { + let coreResp = await fetch(`${TEST_APPLICATION_SERVER_BASE_URL}/removeUserFromTenant`, { + method: "POST", + headers: new Headers([["content-type", "application/json"]]), + body: JSON.stringify({ + tenantId, + recipeUserId, + }), + }); + assert.strictEqual(coreResp.status, 200); +} + +export async function removeTenant(tenantId) { + let coreResp = await fetch(`${TEST_APPLICATION_SERVER_BASE_URL}/removeTenant`, { + method: "POST", + headers: new Headers([["content-type", "application/json"]]), + body: JSON.stringify({ + tenantId, + }), + }); + assert.strictEqual(coreResp.status, 200); +} diff --git a/test/server/index.js b/test/server/index.js index ff3902c88..4cc6a5d29 100644 --- a/test/server/index.js +++ b/test/server/index.js @@ -485,6 +485,7 @@ app.get("/test/featureFlags", (req, res) => { if (multitenancySupported) { available.push("multitenancy"); + available.push("multitenancyManagementEndpoints"); } if (accountLinkingSupported) {