Skip to content

Commit

Permalink
test: make tests backward compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
porcellus committed Dec 8, 2023
1 parent 24dbe36 commit 477cc09
Show file tree
Hide file tree
Showing 21 changed files with 175 additions and 197 deletions.
15 changes: 11 additions & 4 deletions examples/for-tests-react-16/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import ThirdPartyEmailPassword from "supertokens-auth-react/recipe/thirdpartyema
import ThirdPartyPasswordless from "supertokens-auth-react/recipe/thirdpartypasswordless";
import UserRoles from "supertokens-auth-react/recipe/userroles";
import Multitenancy from "supertokens-auth-react/recipe/multitenancy";
import MultiFactorAuth from "supertokens-auth-react/recipe/multifactorauth";
import TOTP from "supertokens-auth-react/recipe/totp";

import axios from "axios";
import { useSessionContext } from "supertokens-auth-react/recipe/session";
Expand Down Expand Up @@ -178,9 +180,7 @@ const formFields = [
const testContext = getTestContext();

let recipeList = [
MultiFactorAuth.init({
firstFactors: testContext.firstFactors,
}),
TOTP.init(),
Multitenancy.init({
override: {
functions: (oI) => ({
Expand Down Expand Up @@ -224,7 +224,7 @@ let recipeList = [
return implementation.doesSessionExist(...args);
},
getAccessTokenPayloadSecurely(...args) {
log(`GET_JWT_PAYLOAD_SECURELY`);
// log(`GET_JWT_PAYLOAD_SECURELY`);
return implementation.getAccessTokenPayloadSecurely(...args);
},
getUserId(...args) {
Expand Down Expand Up @@ -273,6 +273,13 @@ if (enabledRecipes.includes("thirdpartypasswordless")) {
if (emailVerificationMode !== "OFF") {
recipeList.push(getEmailVerificationConfigs(testContext));
}

if (testContext.enableMFA) {
MultiFactorAuth.init({
firstFactors: testContext.firstFactors,
});
}

SuperTokens.init({
usesDynamicLoginMethods: testContext.usesDynamicLoginMethods,
clientType: testContext.clientType,
Expand Down
11 changes: 9 additions & 2 deletions examples/for-tests-react-16/src/AppWithReactDomRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ import { EmailPasswordPreBuiltUI } from "supertokens-auth-react/recipe/emailpass
import { PasswordlessPreBuiltUI } from "supertokens-auth-react/recipe/passwordless/prebuiltui";
import { ThirdPartyPreBuiltUI } from "supertokens-auth-react/recipe/thirdparty/prebuiltui";
import { MultiFactorAuthPreBuiltUI } from "supertokens-auth-react/recipe/multifactorauth/prebuiltui";
import { TOTPPreBuiltUI } from "supertokens-auth-react/recipe/totp/prebuiltui";
import { AccessDeniedScreen } from "supertokens-auth-react/recipe/session/prebuiltui";
import { getEnabledRecipes } from "./testContext";
import { getEnabledRecipes, getTestContext } from "./testContext";

function AppWithReactDomRouter(props) {
const context = getTestContext();
const enabledRecipes = getEnabledRecipes();
const emailVerificationMode = window.localStorage.getItem("mode") || "OFF";

let recipePreBuiltUIList = [MultiFactorAuthPreBuiltUI];
let recipePreBuiltUIList = [TOTPPreBuiltUI];
if (enabledRecipes.includes("emailpassword")) {
recipePreBuiltUIList.push(EmailPasswordPreBuiltUI);
}
Expand All @@ -43,6 +45,11 @@ function AppWithReactDomRouter(props) {
if (emailVerificationMode !== "OFF") {
recipePreBuiltUIList.push(EmailVerificationPreBuiltUI);
}

if (context.enableMFA) {
recipePreBuiltUIList.push(MultiFactorAuthPreBuiltUI);
}

/**
* For user context tests we add this query param so the additional routes
* dont interfere with other tests
Expand Down
11 changes: 9 additions & 2 deletions examples/for-tests-react-16/src/AppWithReactDomRouterV5.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ import { EmailPasswordPreBuiltUI } from "supertokens-auth-react/recipe/emailpass
import { PasswordlessPreBuiltUI } from "supertokens-auth-react/recipe/passwordless/prebuiltui";
import { ThirdPartyPreBuiltUI } from "supertokens-auth-react/recipe/thirdparty/prebuiltui";
import { AccessDeniedScreen } from "supertokens-auth-react/recipe/session/prebuiltui";
import { getEnabledRecipes } from "./testContext";
import { MultiFactorAuthPreBuiltUI } from "supertokens-auth-react/recipe/multifactorauth/prebuiltui";
import { TOTPPreBuiltUI } from "supertokens-auth-react/recipe/totp/prebuiltui";
import { getEnabledRecipes, getTestContext } from "./testContext";

function AppWithReactDomRouter(props) {
const context = getTestContext();
const enabledRecipes = getEnabledRecipes();
const emailVerificationMode = window.localStorage.getItem("mode") || "OFF";

let recipePreBuiltUIList = [];
let recipePreBuiltUIList = [TOTPPreBuiltUI];
if (enabledRecipes.includes("emailpassword")) {
recipePreBuiltUIList.push(EmailPasswordPreBuiltUI);
}
Expand All @@ -38,6 +41,10 @@ function AppWithReactDomRouter(props) {
recipePreBuiltUIList.push(EmailVerificationPreBuiltUI);
}

if (context.enableMFA) {
recipePreBuiltUIList.push(MultiFactorAuthPreBuiltUI);
}

const websiteBasePath = window.localStorage.getItem("websiteBasePath") || undefined;

const [claimValidators, setClaimValidators] = useState(undefined);
Expand Down
1 change: 1 addition & 0 deletions examples/for-tests-react-16/src/testContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export function getTestContext() {
mockTenantId: localStorage.getItem("mockTenantId"),
clientType: localStorage.getItem("clientType") || undefined,
firstFactors: localStorage.getItem("firstFactors")?.split(", "),
enableMFA: localStorage.getItem("enableMFA") === "true",
};
return ret;
}
Expand Down
45 changes: 7 additions & 38 deletions examples/for-tests/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,47 +172,10 @@ const formFields = [

const testContext = getTestContext();

let storedTOTPDevices = window.localStorage.getItem("totpDevices");
let totpDevices = storedTOTPDevices ? JSON.parse(storedTOTPDevices) : [];

function removeTOTPDevice(deviceName) {
const origLength = totpDevices.length;
totpDevices = totpDevices.filter((d) => d.deviceName !== deviceName);
window.localStorage.setItem("totpDevices", JSON.stringify(totpDevices));
return totpDevices.length !== origLength;
}

function addTOTPDevice(deviceName) {
totpDevices.push({
deviceName,
verified: false,
});
window.localStorage.setItem("totpDevices", JSON.stringify(totpDevices));
}

function verifyTOTPDevice(deviceName) {
totpDevices = totpDevices.filter((d) => d.deviceName !== deviceName);
totpDevices.push({
deviceName,
verified: true,
});
window.localStorage.setItem("totpDevices", JSON.stringify(totpDevices));
}
let tryCount = 0;

setInterval(() => (tryCount = tryCount > 0 ? tryCount - 1 : 0), 30000);
window.resetTOTP = () => {
totpDevices = [];
window.localStorage.setItem("totpDevices", JSON.stringify(totpDevices));
tryCount = 0;
};
let recipeList = [
TOTP.init({
override: {},
}),
MultiFactorAuth.init({
firstFactors: testContext.firstFactors,
}),
Multitenancy.init({
override: {
functions: (oI) => ({
Expand Down Expand Up @@ -257,7 +220,7 @@ let recipeList = [
return implementation.doesSessionExist(...args);
},
getAccessTokenPayloadSecurely(...args) {
log(`GET_JWT_PAYLOAD_SECURELY`);
// log(`GET_JWT_PAYLOAD_SECURELY`);
return implementation.getAccessTokenPayloadSecurely(...args);
},
getUserId(...args) {
Expand Down Expand Up @@ -319,6 +282,12 @@ if (emailVerificationMode !== "OFF") {
recipeList.push(getEmailVerificationConfigs(testContext));
}

if (testContext.enableMFA) {
MultiFactorAuth.init({
firstFactors: testContext.firstFactors,
});
}

SuperTokens.init({
usesDynamicLoginMethods: testContext.usesDynamicLoginMethods,
clientType: testContext.clientType,
Expand Down
9 changes: 7 additions & 2 deletions examples/for-tests/src/AppWithReactDomRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { AccessDeniedScreen } from "supertokens-auth-react/recipe/session/prebui
import { MultiFactorAuthPreBuiltUI } from "supertokens-auth-react/recipe/multifactorauth/prebuiltui";
import { TOTPPreBuiltUI } from "supertokens-auth-react/recipe/totp/prebuiltui";
import { BaseComponent, Home, Contact, Dashboard, DashboardNoAuthRequired } from "./App";
import { getEnabledRecipes } from "./testContext";
import { getEnabledRecipes, getTestContext } from "./testContext";

function AppWithReactDomRouter(props) {
/**
Expand All @@ -32,11 +32,12 @@ function AppWithReactDomRouter(props) {
const keyWithClaimValidators =
claimValidators !== undefined ? claimValidators.map((a) => a.id).join("_") : undefined;

const context = getTestContext();
const enabledRecipes = getEnabledRecipes();
const emailVerificationMode = window.localStorage.getItem("mode") || "OFF";
const websiteBasePath = window.localStorage.getItem("websiteBasePath") || undefined;

let recipePreBuiltUIList = [MultiFactorAuthPreBuiltUI, TOTPPreBuiltUI];
let recipePreBuiltUIList = [TOTPPreBuiltUI];
if (enabledRecipes.includes("emailpassword")) {
recipePreBuiltUIList.push(EmailPasswordPreBuiltUI);
}
Expand All @@ -56,6 +57,10 @@ function AppWithReactDomRouter(props) {
recipePreBuiltUIList.push(EmailVerificationPreBuiltUI);
}

if (context.enableMFA) {
recipePreBuiltUIList.push(MultiFactorAuthPreBuiltUI);
}

return (
<div className="App">
<Router>
Expand Down
1 change: 1 addition & 0 deletions examples/for-tests/src/testContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export function getTestContext() {
localStorage.getItem("firstFactors") !== null
? localStorage.getItem("firstFactors").split(", ")
: undefined,
enableMFA: localStorage.getItem("enableMFA") === "true",
};
return ret;
}
Expand Down
30 changes: 6 additions & 24 deletions test/end-to-end/emailverification.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,17 @@ import {
waitForSTElement,
isGeneralErrorSupported,
setGeneralErrorToLocalStorage,
isAccountLinkingSupported,
backendBeforeEach,
isMFASupported,
} from "../helpers";

describe("SuperTokens Email Verification", function () {
let browser;
let page;
let consoleLogs;
let accountLinkingSupported;
let mfaSupported;

before(async function () {
await backendBeforeEach();
Expand All @@ -67,6 +71,8 @@ describe("SuperTokens Email Verification", function () {
args: ["--no-sandbox", "--disable-setuid-sandbox"],
headless: true,
});
accountLinkingSupported = await isAccountLinkingSupported();
mfaSupported = await isMFASupported();

page = await browser.newPage();
await Promise.all([
Expand Down Expand Up @@ -150,12 +156,10 @@ describe("SuperTokens Email Verification", function () {
assert.deepStrictEqual(consoleLogs, [
"ST_LOGS SESSION OVERRIDE ADD_FETCH_INTERCEPTORS_AND_RETURN_MODIFIED_FETCH",
"ST_LOGS SESSION OVERRIDE ADD_AXIOS_INTERCEPTORS",
"ST_LOGS SESSION OVERRIDE GET_JWT_PAYLOAD_SECURELY",
"ST_LOGS EMAIL_VERIFICATION OVERRIDE IS_EMAIL_VERIFIED",
"ST_LOGS EMAIL_VERIFICATION PRE_API_HOOKS IS_EMAIL_VERIFIED",
"ST_LOGS SESSION ON_HANDLE_EVENT UNAUTHORISED",
"ST_LOGS SESSION ON_HANDLE_EVENT UNAUTHORISED",
"ST_LOGS SESSION OVERRIDE GET_JWT_PAYLOAD_SECURELY",
"ST_LOGS SUPERTOKENS GET_REDIRECTION_URL TO_AUTH",
]);
});
Expand Down Expand Up @@ -206,7 +210,6 @@ describe("SuperTokens Email Verification", function () {
assert.deepStrictEqual(consoleLogs, [
"ST_LOGS SESSION OVERRIDE ADD_FETCH_INTERCEPTORS_AND_RETURN_MODIFIED_FETCH",
"ST_LOGS SESSION OVERRIDE ADD_AXIOS_INTERCEPTORS",
"ST_LOGS SESSION OVERRIDE GET_JWT_PAYLOAD_SECURELY",
"ST_LOGS SESSION OVERRIDE GET_USER_ID",
"ST_LOGS EMAIL_VERIFICATION OVERRIDE IS_EMAIL_VERIFIED",
"ST_LOGS EMAIL_VERIFICATION PRE_API_HOOKS IS_EMAIL_VERIFIED",
Expand Down Expand Up @@ -243,18 +246,13 @@ describe("SuperTokens Email Verification", function () {
"ST_LOGS EMAIL_PASSWORD PRE_API_HOOKS EMAIL_PASSWORD_SIGN_UP",
"ST_LOGS SESSION ON_HANDLE_EVENT SESSION_CREATED",
"ST_LOGS SESSION OVERRIDE GET_USER_ID",
"ST_LOGS SESSION OVERRIDE GET_JWT_PAYLOAD_SECURELY",
"ST_LOGS SESSION OVERRIDE GET_JWT_PAYLOAD_SECURELY",
"ST_LOGS EMAIL_PASSWORD ON_HANDLE_EVENT SUCCESS",
"ST_LOGS SESSION OVERRIDE GET_JWT_PAYLOAD_SECURELY",
"ST_LOGS EMAIL_VERIFICATION GET_REDIRECTION_URL VERIFY_EMAIL",
"ST_LOGS SESSION OVERRIDE GET_JWT_PAYLOAD_SECURELY",
"ST_LOGS SESSION OVERRIDE GET_USER_ID",
"ST_LOGS EMAIL_VERIFICATION OVERRIDE IS_EMAIL_VERIFIED",
"ST_LOGS EMAIL_VERIFICATION PRE_API_HOOKS IS_EMAIL_VERIFIED",
"ST_LOGS SESSION ON_HANDLE_EVENT ACCESS_TOKEN_PAYLOAD_UPDATED",
"ST_LOGS SESSION OVERRIDE GET_USER_ID",
"ST_LOGS SESSION OVERRIDE GET_JWT_PAYLOAD_SECURELY",
"ST_LOGS EMAIL_VERIFICATION OVERRIDE SEND_VERIFICATION_EMAIL",
"ST_LOGS EMAIL_VERIFICATION PRE_API_HOOKS SEND_VERIFY_EMAIL",
"ST_LOGS EMAIL_VERIFICATION ON_HANDLE_EVENT VERIFY_EMAIL_SENT",
Expand Down Expand Up @@ -329,18 +327,13 @@ describe("SuperTokens Email Verification", function () {
"ST_LOGS EMAIL_PASSWORD PRE_API_HOOKS EMAIL_PASSWORD_SIGN_IN",
"ST_LOGS SESSION ON_HANDLE_EVENT SESSION_CREATED",
"ST_LOGS SESSION OVERRIDE GET_USER_ID",
"ST_LOGS SESSION OVERRIDE GET_JWT_PAYLOAD_SECURELY",
"ST_LOGS SESSION OVERRIDE GET_JWT_PAYLOAD_SECURELY",
"ST_LOGS EMAIL_PASSWORD ON_HANDLE_EVENT SUCCESS",
"ST_LOGS SESSION OVERRIDE GET_JWT_PAYLOAD_SECURELY",
"ST_LOGS EMAIL_VERIFICATION GET_REDIRECTION_URL VERIFY_EMAIL",
"ST_LOGS SESSION OVERRIDE GET_JWT_PAYLOAD_SECURELY",
"ST_LOGS SESSION OVERRIDE GET_USER_ID",
"ST_LOGS EMAIL_VERIFICATION OVERRIDE IS_EMAIL_VERIFIED",
"ST_LOGS EMAIL_VERIFICATION PRE_API_HOOKS IS_EMAIL_VERIFIED",
"ST_LOGS SESSION ON_HANDLE_EVENT ACCESS_TOKEN_PAYLOAD_UPDATED",
"ST_LOGS SESSION OVERRIDE GET_USER_ID",
"ST_LOGS SESSION OVERRIDE GET_JWT_PAYLOAD_SECURELY",
"ST_LOGS EMAIL_VERIFICATION OVERRIDE SEND_VERIFICATION_EMAIL",
"ST_LOGS EMAIL_VERIFICATION PRE_API_HOOKS SEND_VERIFY_EMAIL",
"ST_LOGS EMAIL_VERIFICATION ON_HANDLE_EVENT VERIFY_EMAIL_SENT",
Expand Down Expand Up @@ -475,35 +468,25 @@ describe("SuperTokens Email Verification", function () {
"ST_LOGS EMAIL_PASSWORD PRE_API_HOOKS EMAIL_PASSWORD_SIGN_UP",
"ST_LOGS SESSION ON_HANDLE_EVENT SESSION_CREATED",
"ST_LOGS SESSION OVERRIDE GET_USER_ID",
"ST_LOGS SESSION OVERRIDE GET_JWT_PAYLOAD_SECURELY",
"ST_LOGS SESSION OVERRIDE GET_JWT_PAYLOAD_SECURELY",
"ST_LOGS EMAIL_PASSWORD ON_HANDLE_EVENT SUCCESS",
"ST_LOGS SESSION OVERRIDE GET_JWT_PAYLOAD_SECURELY",
"ST_LOGS EMAIL_VERIFICATION GET_REDIRECTION_URL VERIFY_EMAIL",
"ST_LOGS SESSION OVERRIDE GET_JWT_PAYLOAD_SECURELY",
"ST_LOGS SESSION OVERRIDE GET_USER_ID",
"ST_LOGS EMAIL_VERIFICATION OVERRIDE IS_EMAIL_VERIFIED",
"ST_LOGS EMAIL_VERIFICATION PRE_API_HOOKS IS_EMAIL_VERIFIED",
"ST_LOGS SESSION ON_HANDLE_EVENT ACCESS_TOKEN_PAYLOAD_UPDATED",
"ST_LOGS SESSION OVERRIDE GET_USER_ID",
"ST_LOGS SESSION OVERRIDE GET_JWT_PAYLOAD_SECURELY",
"ST_LOGS EMAIL_VERIFICATION OVERRIDE SEND_VERIFICATION_EMAIL",
"ST_LOGS EMAIL_VERIFICATION PRE_API_HOOKS SEND_VERIFY_EMAIL",
"ST_LOGS EMAIL_VERIFICATION ON_HANDLE_EVENT VERIFY_EMAIL_SENT",
"ST_LOGS SESSION OVERRIDE ADD_FETCH_INTERCEPTORS_AND_RETURN_MODIFIED_FETCH",
"ST_LOGS SESSION OVERRIDE ADD_AXIOS_INTERCEPTORS",
"ST_LOGS SESSION OVERRIDE GET_JWT_PAYLOAD_SECURELY",
"ST_LOGS SESSION OVERRIDE GET_USER_ID",
"ST_LOGS EMAIL_VERIFICATION OVERRIDE VERIFY_EMAIL",
"ST_LOGS EMAIL_VERIFICATION PRE_API_HOOKS VERIFY_EMAIL",
"ST_LOGS SESSION ON_HANDLE_EVENT ACCESS_TOKEN_PAYLOAD_UPDATED",
"ST_LOGS SESSION OVERRIDE GET_USER_ID",
"ST_LOGS SESSION OVERRIDE GET_JWT_PAYLOAD_SECURELY",
"ST_LOGS EMAIL_VERIFICATION ON_HANDLE_EVENT EMAIL_VERIFIED_SUCCESSFUL",
"ST_LOGS SESSION OVERRIDE GET_JWT_PAYLOAD_SECURELY",
"ST_LOGS EMAIL_PASSWORD GET_REDIRECTION_URL SUCCESS",
"ST_LOGS SESSION OVERRIDE GET_JWT_PAYLOAD_SECURELY",
"ST_LOGS SESSION OVERRIDE GET_JWT_PAYLOAD_SECURELY",
"ST_LOGS SESSION OVERRIDE GET_USER_ID",
]);
});
Expand Down Expand Up @@ -847,7 +830,6 @@ describe("SuperTokens Email Verification isEmailVerified server error", function
// "ST_LOGS EMAIL_VERIFICATION ON_HANDLE_EVENT VERIFY_EMAIL_SENT",
// "ST_LOGS SESSION OVERRIDE ADD_FETCH_INTERCEPTORS_AND_RETURN_MODIFIED_FETCH",
// "ST_LOGS SESSION OVERRIDE ADD_AXIOS_INTERCEPTORS",
// "ST_LOGS SESSION OVERRIDE GET_JWT_PAYLOAD_SECURELY",
// "ST_LOGS SESSION OVERRIDE GET_USER_ID",
// "ST_LOGS EMAIL_VERIFICATION OVERRIDE IS_EMAIL_VERIFIED",
// "ST_LOGS EMAIL_VERIFICATION PRE_API_HOOKS IS_EMAIL_VERIFIED",
Expand Down
10 changes: 10 additions & 0 deletions test/end-to-end/mfa.firstFactors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
backendBeforeEach,
waitFor,
submitForm,
isMFASupported,
} from "../helpers";
import fetch from "isomorphic-fetch";

Expand All @@ -39,8 +40,14 @@ describe("SuperTokens MFA firstFactors support", function () {
let browser;
let page;
let consoleLogs = [];
let skipped = false;

before(async function () {
if (!(await isMFASupported())) {
skipped = true;
this.skip();
return;
}
await backendBeforeEach();

await fetch(`${TEST_SERVER_BASE_URL}/startst`, {
Expand All @@ -54,6 +61,9 @@ describe("SuperTokens MFA firstFactors support", function () {
});

after(async function () {
if (skipped) {
return;
}
await browser.close();

await fetch(`${TEST_SERVER_BASE_URL}/after`, {
Expand Down
Loading

0 comments on commit 477cc09

Please sign in to comment.