|
| 1 | +import * as jose from "jose"; |
| 2 | +import SuperTokensNextjsSSRAPIWrapper from "../../../lib/ts/nextjs/ssr"; |
| 3 | +import type { CookiesStore } from "../../../lib/ts/nextjs/types"; |
| 4 | + |
| 5 | +jest.mock("jose", () => ({ |
| 6 | + createRemoteJWKSet: jest.fn(), |
| 7 | + jwtVerify: jest.fn(), |
| 8 | +})); |
| 9 | + |
| 10 | +describe("nextjs/ssr", () => { |
| 11 | + describe("getSSRSession", () => { |
| 12 | + const API_BASE_PATH = "/api/auth"; |
| 13 | + const CURRENT_PATH_COOKIE_NAME = "sCurrentPath"; |
| 14 | + const WEBSITE_BASE_PATH = "/auth"; |
| 15 | + const REFRESH_PATH = `${API_BASE_PATH}/session/refresh`; |
| 16 | + const AUTH_PATH = WEBSITE_BASE_PATH; |
| 17 | + const FORCE_LOGOUT_PARAM = "forceLogout=true"; |
| 18 | + |
| 19 | + const buildRedirectUrl = (basePath: string, params: string[]) => { |
| 20 | + return `${basePath}?${params.join("&")}`; |
| 21 | + }; |
| 22 | + |
| 23 | + const getRedirectParam = (redirectTo: string) => { |
| 24 | + return `stRedirectTo=${redirectTo}`; |
| 25 | + }; |
| 26 | + |
| 27 | + const createFrontToken = ( |
| 28 | + overrides: Partial<{ |
| 29 | + uid: string; |
| 30 | + ate: number; |
| 31 | + up: Record<string, any>; |
| 32 | + }> = {} |
| 33 | + ) => { |
| 34 | + const defaultToken = { |
| 35 | + uid: "user123", |
| 36 | + ate: Date.now() + 3600000, // 1 hour from now |
| 37 | + up: { sub: "user123" }, |
| 38 | + ...overrides, |
| 39 | + }; |
| 40 | + return Buffer.from(JSON.stringify(defaultToken)).toString("base64"); |
| 41 | + }; |
| 42 | + |
| 43 | + const mockRedirect = jest.fn((url: string): never => { |
| 44 | + throw url; |
| 45 | + }); |
| 46 | + |
| 47 | + const mockConfig = { |
| 48 | + appInfo: { |
| 49 | + apiBasePath: API_BASE_PATH, |
| 50 | + apiDomain: "http://localhost:3000", |
| 51 | + websiteBasePath: WEBSITE_BASE_PATH, |
| 52 | + appName: "SuperTokens Demo", |
| 53 | + websiteDomain: "http://localhost:3000", |
| 54 | + }, |
| 55 | + }; |
| 56 | + |
| 57 | + const createMockCookiesStore = (cookies: Record<string, string>): CookiesStore => ({ |
| 58 | + get: (name: string) => { |
| 59 | + const value = cookies[name]; |
| 60 | + return { value: value || "" }; |
| 61 | + }, |
| 62 | + set: jest.fn(), |
| 63 | + }); |
| 64 | + |
| 65 | + beforeEach(() => { |
| 66 | + jest.clearAllMocks(); |
| 67 | + SuperTokensNextjsSSRAPIWrapper.init(mockConfig); |
| 68 | + }); |
| 69 | + |
| 70 | + it("should redirect to the auth page if the front token is not found", async () => { |
| 71 | + const currentPath = "/"; |
| 72 | + const cookies = createMockCookiesStore({ [CURRENT_PATH_COOKIE_NAME]: currentPath }); |
| 73 | + await expect(SuperTokensNextjsSSRAPIWrapper.getSSRSession(cookies, mockRedirect)).rejects.toBe( |
| 74 | + buildRedirectUrl(AUTH_PATH, [FORCE_LOGOUT_PARAM, getRedirectParam(currentPath)]) |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + it("should redirect to the auth page if the front token is invalid", async () => { |
| 79 | + const currentPath = "/dashboard"; |
| 80 | + const cookies = createMockCookiesStore({ |
| 81 | + sFrontToken: "invalid-token", |
| 82 | + [CURRENT_PATH_COOKIE_NAME]: currentPath, |
| 83 | + }); |
| 84 | + await expect(SuperTokensNextjsSSRAPIWrapper.getSSRSession(cookies, mockRedirect)).rejects.toBe( |
| 85 | + buildRedirectUrl(AUTH_PATH, [FORCE_LOGOUT_PARAM, getRedirectParam(currentPath)]) |
| 86 | + ); |
| 87 | + }); |
| 88 | + |
| 89 | + it("should redirect to the auth page if the access token is invalid", async () => { |
| 90 | + const currentPath = "/profile"; |
| 91 | + const mockFrontToken = createFrontToken(); |
| 92 | + const cookies = createMockCookiesStore({ |
| 93 | + sFrontToken: mockFrontToken, |
| 94 | + sAccessToken: "invalid-access-token", |
| 95 | + [CURRENT_PATH_COOKIE_NAME]: currentPath, |
| 96 | + }); |
| 97 | + |
| 98 | + (jose.jwtVerify as jest.Mock).mockRejectedValueOnce({ isValid: false }); |
| 99 | + |
| 100 | + await expect(SuperTokensNextjsSSRAPIWrapper.getSSRSession(cookies, mockRedirect)).rejects.toBe( |
| 101 | + buildRedirectUrl(AUTH_PATH, [FORCE_LOGOUT_PARAM, getRedirectParam(currentPath)]) |
| 102 | + ); |
| 103 | + }); |
| 104 | + |
| 105 | + it("should redirect to the refresh path if the front token is expired", async () => { |
| 106 | + const currentPath = "/settings"; |
| 107 | + const mockFrontToken = createFrontToken({ |
| 108 | + ate: Date.now() - 1000, // Expired |
| 109 | + }); |
| 110 | + |
| 111 | + const cookies = createMockCookiesStore({ |
| 112 | + sFrontToken: mockFrontToken, |
| 113 | + [CURRENT_PATH_COOKIE_NAME]: currentPath, |
| 114 | + }); |
| 115 | + |
| 116 | + await expect(SuperTokensNextjsSSRAPIWrapper.getSSRSession(cookies, mockRedirect)).rejects.toBe( |
| 117 | + buildRedirectUrl(REFRESH_PATH, [getRedirectParam(currentPath)]) |
| 118 | + ); |
| 119 | + }); |
| 120 | + |
| 121 | + it("should redirect to the refresh path if the access token was not found", async () => { |
| 122 | + const currentPath = "/account"; |
| 123 | + const mockFrontToken = createFrontToken(); |
| 124 | + const cookies = createMockCookiesStore({ |
| 125 | + sFrontToken: mockFrontToken, |
| 126 | + [CURRENT_PATH_COOKIE_NAME]: currentPath, |
| 127 | + }); |
| 128 | + |
| 129 | + await expect(SuperTokensNextjsSSRAPIWrapper.getSSRSession(cookies, mockRedirect)).rejects.toBe( |
| 130 | + buildRedirectUrl(REFRESH_PATH, [getRedirectParam(currentPath)]) |
| 131 | + ); |
| 132 | + }); |
| 133 | + |
| 134 | + it("should redirect to the refresh path if token payloads do not match", async () => { |
| 135 | + const currentPath = "/home"; |
| 136 | + const mockFrontToken = createFrontToken({ |
| 137 | + up: { sub: "user123", someData: "value1" }, |
| 138 | + }); |
| 139 | + |
| 140 | + const cookies = createMockCookiesStore({ |
| 141 | + sFrontToken: mockFrontToken, |
| 142 | + sAccessToken: "valid-access-token", |
| 143 | + [CURRENT_PATH_COOKIE_NAME]: currentPath, |
| 144 | + }); |
| 145 | + |
| 146 | + (jose.jwtVerify as jest.Mock).mockResolvedValueOnce({ |
| 147 | + payload: { sub: "user123", someData: "different-value", exp: Date.now() + 3600000 }, |
| 148 | + isValid: true, |
| 149 | + }); |
| 150 | + |
| 151 | + await expect(SuperTokensNextjsSSRAPIWrapper.getSSRSession(cookies, mockRedirect)).rejects.toBe( |
| 152 | + buildRedirectUrl(REFRESH_PATH, [getRedirectParam(currentPath)]) |
| 153 | + ); |
| 154 | + }); |
| 155 | + |
| 156 | + it("should return the session if tokens match", async () => { |
| 157 | + const mockPayload = { sub: "user123", someData: "value1", exp: Date.now() + 360000 }; |
| 158 | + const mockFrontToken = createFrontToken({ |
| 159 | + up: mockPayload, |
| 160 | + }); |
| 161 | + |
| 162 | + const cookies = createMockCookiesStore({ |
| 163 | + sFrontToken: mockFrontToken, |
| 164 | + sAccessToken: "valid-access-token", |
| 165 | + }); |
| 166 | + |
| 167 | + (jose.jwtVerify as jest.Mock).mockResolvedValueOnce({ |
| 168 | + payload: mockPayload, |
| 169 | + isValid: true, |
| 170 | + }); |
| 171 | + |
| 172 | + const result = await SuperTokensNextjsSSRAPIWrapper.getSSRSession(cookies, mockRedirect); |
| 173 | + expect(result).toEqual({ |
| 174 | + userId: "user123", |
| 175 | + accessTokenPayload: { isValid: true, payload: mockPayload }, |
| 176 | + doesSessionExist: true, |
| 177 | + loading: false, |
| 178 | + invalidClaims: [], |
| 179 | + accessDeniedValidatorError: undefined, |
| 180 | + }); |
| 181 | + }); |
| 182 | + |
| 183 | + it("redirects to the correct page based on the sCurrentPath cookie", async () => { |
| 184 | + // Test with a simple path |
| 185 | + const authCookies = createMockCookiesStore({ |
| 186 | + [CURRENT_PATH_COOKIE_NAME]: "/dashboard", |
| 187 | + }); |
| 188 | + |
| 189 | + await expect(SuperTokensNextjsSSRAPIWrapper.getSSRSession(authCookies, mockRedirect)).rejects.toBe( |
| 190 | + buildRedirectUrl(AUTH_PATH, [FORCE_LOGOUT_PARAM, getRedirectParam("/dashboard")]) |
| 191 | + ); |
| 192 | + |
| 193 | + // Test with a complex path (including query params) |
| 194 | + const complexPathCookies = createMockCookiesStore({ |
| 195 | + [CURRENT_PATH_COOKIE_NAME]: "/settings/profile?tab=security#password", |
| 196 | + }); |
| 197 | + |
| 198 | + await expect(SuperTokensNextjsSSRAPIWrapper.getSSRSession(complexPathCookies, mockRedirect)).rejects.toBe( |
| 199 | + buildRedirectUrl(AUTH_PATH, [ |
| 200 | + FORCE_LOGOUT_PARAM, |
| 201 | + getRedirectParam("/settings/profile?tab=security#password"), |
| 202 | + ]) |
| 203 | + ); |
| 204 | + |
| 205 | + // Test with no current path cookie (should default to "/") |
| 206 | + const cookiesWithoutPath = createMockCookiesStore({}); |
| 207 | + |
| 208 | + await expect(SuperTokensNextjsSSRAPIWrapper.getSSRSession(cookiesWithoutPath, mockRedirect)).rejects.toBe( |
| 209 | + buildRedirectUrl(AUTH_PATH, [FORCE_LOGOUT_PARAM, getRedirectParam("/")]) |
| 210 | + ); |
| 211 | + }); |
| 212 | + }); |
| 213 | +}); |
0 commit comments