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: test server compatible with 1.17/2.0 #897

Merged
merged 11 commits into from
Aug 5, 2024
6 changes: 4 additions & 2 deletions test/test-server/src/emailpassword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const { logDebugMessage } = logger(namespace);

const router = Router()
.post("/signup", async (req, res, next) => {
const fdiVersion: string = req.headers["fdi-version"] as string;
Copy link
Collaborator

Choose a reason for hiding this comment

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

One improvement we could make here:

  • add a generic helper function serializeAndSendResponse(response, req)
  • extract the fdi version inside serializeAndSendResponse and pass it to the serialization functions
  • apply this wherever possible in the test server (basically replace res.json)

I'm thinking you could do something similar when de-serializing.


try {
logDebugMessage("EmailPassword:signup %j", req.body);
let session = req.body.session && (await convertRequestSessionToSessionObject(req.body.session));
Expand All @@ -21,8 +23,8 @@ const router = Router()
);
res.json({
...response,
...serializeUser(response),
...serializeRecipeUserId(response),
...serializeUser(response, fdiVersion),
...serializeRecipeUserId(response, fdiVersion),
});
} catch (e) {
next(e);
Expand Down
9 changes: 8 additions & 1 deletion test/test-server/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@ const { logDebugMessage } = logger(namespace);

const router = Router()
.post("/createnewsessionwithoutrequestresponse", async (req, res, next) => {
const fdiVersion = req.headers["fdi-version"] as string;

try {
logDebugMessage("Session.createNewSessionWithoutRequestResponse %j", req.body);
const recipeUserId = supertokens.convertToRecipeUserId(req.body.recipeUserId);
let recipeUserId;
if (["1.17", "2.0"].includes(fdiVersion)) {
recipeUserId = supertokens.convertToRecipeUserId(req.body.userId);
} else {
recipeUserId = supertokens.convertToRecipeUserId(req.body.recipeUserId);
}
const response = await Session.createNewSessionWithoutRequestResponse(
req.body.tenantId || "public",
recipeUserId,
Expand Down
21 changes: 19 additions & 2 deletions test/test-server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,21 @@ export async function convertRequestSessionToSessionObject(
return tokens;
}

export function serializeUser(response) {
export function serializeUser(response, fdiVersion: string) {
if (["1.17", "2.0"].includes(fdiVersion)) {
return {
...("user" in response && response.user instanceof supertokens.User
? {
user: {
id: (response.user as supertokens.User).id,
email: (response.user as supertokens.User).emails[0],
timeJoined: (response.user as supertokens.User).timeJoined,
tenantIds: (response.user as supertokens.User).tenantIds,
},
}
: {}),
};
}
return {
...("user" in response && response.user instanceof supertokens.User
? {
Expand All @@ -147,7 +161,10 @@ export function serializeUser(response) {
};
}

export function serializeRecipeUserId(response) {
export function serializeRecipeUserId(response, fdiVersion: string) {
if (["1.17", "2.0"].includes(fdiVersion)) {
return {};
}
return {
...("recipeUserId" in response && response.recipeUserId instanceof supertokens.RecipeUserId
? {
Expand Down
Loading