|
1 | | -import { Handler, Response } from "express"; |
| 1 | +import { Handler, NextFunction, Response } from "express"; |
2 | 2 | import { GoogleApis } from "googleapis"; |
3 | 3 | import mergeWith, { default as mockMergeWith } from "lodash.mergewith"; |
4 | 4 | import { randomUUID } from "node:crypto"; |
5 | 5 | 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"; |
8 | 16 | import { createMockCalendarListEntry as mockCalendarListCreate } from "@core/__tests__/helpers/gcal.factory"; |
9 | 17 | import { gSchema$CalendarListEntry } from "@core/types/gcal"; |
10 | 18 | import { UserMetadata } from "@core/types/user.types"; |
@@ -49,12 +57,12 @@ function mockGoogleapis() { |
49 | 57 | function mockSuperToken() { |
50 | 58 | const userMetadata = new Map<string, UserMetadata>(); |
51 | 59 |
|
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) => { |
58 | 66 | try { |
59 | 67 | const cookies = (req.headers.cookie?.split(";") ?? [])?.reduce( |
60 | 68 | (items, item) => { |
@@ -98,10 +106,13 @@ function mockSuperToken() { |
98 | 106 | }, |
99 | 107 | } as SessionContainerInterface; |
100 | 108 |
|
101 | | - return next ? next() : undefined; |
| 109 | + return next?.(); |
102 | 110 | } |
103 | 111 |
|
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 | + } |
105 | 116 | } catch (error) { |
106 | 117 | if (next) { |
107 | 118 | next(error); |
@@ -163,6 +174,45 @@ function mockSuperToken() { |
163 | 174 | return mergeWith(userMetadataModule, { default: userMetadataModule }); |
164 | 175 | }, |
165 | 176 | ); |
| 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 | + ); |
166 | 216 | } |
167 | 217 |
|
168 | 218 | function mockWinstonLogger() { |
|
0 commit comments