Skip to content

Commit 69eaca4

Browse files
committed
:bug fix(auth-refresh): update supertokens verifySession mocks
1 parent 6e6adf4 commit 69eaca4

File tree

2 files changed

+65
-13
lines changed

2 files changed

+65
-13
lines changed

packages/backend/src/__tests__/helpers/mock.setup.ts

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
import { Handler, Response } from "express";
1+
import { Handler, NextFunction, Response } from "express";
22
import { GoogleApis } from "googleapis";
33
import mergeWith, { default as mockMergeWith } from "lodash.mergewith";
44
import { randomUUID } from "node:crypto";
55
import { SessionRequest } from "supertokens-node/framework/express";
6-
import { BaseResponse } from "supertokens-node/lib/build/framework";
7-
import { SessionContainerInterface } from "supertokens-node/lib/build/recipe/session/types";
6+
import {
7+
ExpressRequest,
8+
ExpressResponse,
9+
} from "supertokens-node/lib/build/framework/express/framework";
10+
import {
11+
APIOptions,
12+
SessionContainerInterface,
13+
VerifySessionOptions,
14+
} from "supertokens-node/lib/build/recipe/session/types";
15+
import { UserContext } from "supertokens-node/lib/build/types";
816
import { createMockCalendarListEntry as mockCalendarListCreate } from "@core/__tests__/helpers/gcal.factory";
917
import { gSchema$CalendarListEntry } from "@core/types/gcal";
1018
import { UserMetadata } from "@core/types/user.types";
@@ -49,12 +57,12 @@ function mockGoogleapis() {
4957
function mockSuperToken() {
5058
const userMetadata = new Map<string, UserMetadata>();
5159

52-
function verifySession() {
53-
return (
54-
req: SessionRequest,
55-
_res: Response & BaseResponse,
56-
next?: (err?: unknown) => void,
57-
) => {
60+
function verifySession(input: {
61+
verifySessionOptions?: VerifySessionOptions;
62+
options: APIOptions;
63+
userContext: UserContext;
64+
}) {
65+
return (req: SessionRequest, _res: Response, next?: NextFunction) => {
5866
try {
5967
const cookies = (req.headers.cookie?.split(";") ?? [])?.reduce(
6068
(items, item) => {
@@ -98,10 +106,13 @@ function mockSuperToken() {
98106
},
99107
} as SessionContainerInterface;
100108

101-
return next ? next() : undefined;
109+
return next?.();
102110
}
103111

104-
throw new Error("invalid superToken session");
112+
if (input?.verifySessionOptions?.sessionRequired) {
113+
console.log("Invalid session detected in mock");
114+
throw new Error("invalid superToken session");
115+
}
105116
} catch (error) {
106117
if (next) {
107118
next(error);
@@ -163,6 +174,45 @@ function mockSuperToken() {
163174
return mergeWith(userMetadataModule, { default: userMetadataModule });
164175
},
165176
);
177+
178+
mockModule(
179+
"supertokens-node/lib/build/recipe/session/recipe",
180+
(
181+
session: typeof import("supertokens-node/lib/build/recipe/session/recipe"),
182+
) => {
183+
const getInstanceOrThrowError =
184+
session.default.getInstanceOrThrowError.bind(session.default);
185+
186+
const sessionModule = mergeWith(session, {
187+
default: mergeWith(session.default, {
188+
getInstanceOrThrowError: jest.fn(() => {
189+
const instance = getInstanceOrThrowError();
190+
191+
return mergeWith(instance, {
192+
apiImpl: mergeWith(instance.apiImpl, {
193+
verifySession: jest.fn(
194+
async (input: {
195+
verifySessionOptions: VerifySessionOptions | undefined;
196+
options: APIOptions;
197+
userContext: UserContext;
198+
}) => {
199+
const req = input.options.req as ExpressRequest;
200+
const res = input.options.res as ExpressResponse;
201+
202+
verifySession(input)(req.original, res.original);
203+
204+
return Promise.resolve(req.original.session);
205+
},
206+
),
207+
}),
208+
});
209+
}),
210+
}),
211+
});
212+
213+
return sessionModule;
214+
},
215+
);
166216
}
167217

168218
function mockWinstonLogger() {

packages/backend/src/servers/websocket/websocket.server.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ class WebSocketServer {
225225
const userContext = makeDefaultUserContextFromAPI(request);
226226
const sessionRecipe = SessionRecipe.getInstanceOrThrowError();
227227
const session = await sessionRecipe.verifySession(
228-
{ sessionRequired: false },
228+
{ sessionRequired: true },
229229
request,
230230
response,
231231
userContext,
@@ -237,11 +237,13 @@ class WebSocketServer {
237237
} catch (err) {
238238
const error = err as SessionError;
239239

240+
logger.error(error.message, error);
241+
240242
res.writeHead(Status.UNAUTHORIZED, {
241243
"Content-Type": "application/json",
242244
});
243245

244-
res.end(JSON.stringify({ type: error.type, message: error.message }));
246+
res.end(JSON.stringify({ type: error.type, message: "Invalid Session" }));
245247
}
246248
}
247249

0 commit comments

Comments
 (0)