Skip to content

Commit

Permalink
test: update the test server to make cookie names overrideable
Browse files Browse the repository at this point in the history
  • Loading branch information
porcellus committed Oct 31, 2024
1 parent 45f0ac6 commit b99c2c9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
25 changes: 19 additions & 6 deletions test/test-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import Multitenancy from "../../../recipe/multitenancy";
import Passwordless from "../../../recipe/passwordless";
import Session from "../../../recipe/session";
import { verifySession } from "../../../recipe/session/framework/express";
import { getResponseHeaderNameForTokenType, getCookieNameFromTokenType } from "../../../lib/build/recipe/session/utils";
import ThirdParty from "../../../recipe/thirdparty";
import TOTP from "../../../recipe/totp";
import OAuth2Provider from "../../../recipe/oauth2provider";
Expand Down Expand Up @@ -147,6 +148,16 @@ function initST(config: any) {
recipeList.push(
Session.init({
...config,
getResponseHeaderNameForTokenType: callbackWithLog(
"Session.getResponseHeaderNameForTokenType",
config?.getResponseHeaderNameForTokenType,
getResponseHeaderNameForTokenType
),
getCookieNameFromTokenType: callbackWithLog(
"Session.getCookieNameFromTokenType",
config?.getCookieNameFromTokenType,
getCookieNameFromTokenType
),
override: {
apis: overrideBuilderWithLogging("Session.override.apis", config?.override?.apis),
functions: overrideBuilderWithLogging(
Expand All @@ -164,9 +175,9 @@ function initST(config: any) {
shouldDoAutomaticAccountLinking: callbackWithLog(
"AccountLinking.shouldDoAutomaticAccountLinking",
config.shouldDoAutomaticAccountLinking,
{
() => ({
shouldAutomaticallyLink: false,
}
})
),
onAccountLinked: callbackWithLog("AccountLinking.onAccountLinked", config.onAccountLinked),
override: {
Expand Down Expand Up @@ -219,7 +230,9 @@ function initST(config: any) {
getEmailForRecipeUserId: callbackWithLog(
"EmailVerification.getEmailForRecipeUserId",
config?.getEmailForRecipeUserId,
{ status: "UNKNOWN_USER_ID_ERROR" }
() => {
status: "UNKNOWN_USER_ID_ERROR";
}
),
override: {
apis: overrideBuilderWithLogging("EmailVerification.override.apis", config?.override?.apis),
Expand Down Expand Up @@ -360,7 +373,7 @@ app.get("/test/overrideparams", async (req, res, next) => {
});

app.get("/test/featureflag", async (req, res, next) => {
res.json(["removedOverwriteSessionDuringSignInUp"]);
res.json(["removedOverwriteSessionDuringSignInUp", "configurableCookieAndHeaderNames"]);
});

app.post("/test/resetoverrideparams", async (req, res, next) => {
Expand Down Expand Up @@ -490,8 +503,8 @@ function loggingOverrideFunc<T>(name: string, originalImpl: (...args: any[]) =>
};
}

function callbackWithLog<T = undefined>(name: string, overrideName: string, defaultValue?: T) {
const impl = overrideName ? getFunc(overrideName) : () => defaultValue;
function callbackWithLog<T = undefined>(name: string, overrideName: string, defaultImpl?: (...args: any[]) => T) {
const impl = overrideName ? getFunc(overrideName) : defaultImpl ?? (() => undefined);
return loggingOverrideFunc<T>(name, impl);
}

Expand Down
8 changes: 8 additions & 0 deletions test/test-server/src/testFunctionMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ function getSessionVars() {
}

export function getFunc(evalStr: string): (...args: any[]) => any {
if (evalStr.startsWith("defaultValues:")) {
const defaultValues = JSON.parse(evalStr.split("defaultValues:")[1]);
if (!Array.isArray(defaultValues)) {
throw new Error("defaultValues must be an array");
}
return () => defaultValues.pop();
}

if (evalStr.startsWith("session.fetchAndSetClaim")) {
return async (a, c) => {
userIdInCallback = a;
Expand Down

0 comments on commit b99c2c9

Please sign in to comment.