-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseSessionActive.test.ts
More file actions
152 lines (133 loc) · 4.52 KB
/
Copy pathuseSessionActive.test.ts
File metadata and controls
152 lines (133 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { jest } from "@jest/globals";
import { renderHook } from "@testing-library/react";
import { AUTH_STATUS, AuthState } from "../src/auth/types/auth";
import {
AUTHENTICATION_STATUS,
AuthenticationState,
} from "../src/auth/types/authentication";
import { TransformRouteFn } from "../src/hooks/useRouteHistory";
const AUTH_STATE_AUTHENTICATED_SETTLED: AuthState = {
isAuthenticated: true,
status: AUTH_STATUS.SETTLED,
};
const AUTH_STATE_PENDING: AuthState = {
isAuthenticated: false,
status: AUTH_STATUS.PENDING,
};
const AUTH_STATE_UNAUTHENTICATED_SETTLED: AuthState = {
isAuthenticated: false,
status: AUTH_STATUS.SETTLED,
};
const AUTHENTICATION_STATE_PENDING: AuthenticationState = {
profile: undefined,
status: AUTHENTICATION_STATUS.PENDING,
};
const AUTHENTICATION_STATE_SETTLED: AuthenticationState = {
profile: { email: "", name: "" },
status: AUTHENTICATION_STATUS.SETTLED,
};
const ROOT_PATH = "/";
const ROUTES = ["/login", "/route1", "/route2"];
let mockRouterQuery: Record<string, string | string[] | undefined> = {};
jest.unstable_mockModule("next/router", () => {
return {
...jest.requireActual<typeof import("next/router")>("next/router"),
default: {
push: jest.fn(),
},
useRouter: jest.fn(() => ({ query: mockRouterQuery })),
};
});
jest.unstable_mockModule("../src/hooks/useRouteHistory", () => ({
useRouteHistory: jest.fn(),
}));
const Router = (await import("next/router")).default;
const { useRouteHistory } = await import("../src/hooks/useRouteHistory");
const { useSessionActive } =
await import("../src/hooks/authentication/session/useSessionActive");
const MOCK_USE_ROUTE_HISTORY = useRouteHistory as jest.MockedFunction<
typeof useRouteHistory
>;
describe("useSessionActive", () => {
beforeEach(() => {
jest.clearAllMocks();
mockRouterQuery = {};
MOCK_USE_ROUTE_HISTORY.mockReset();
MOCK_USE_ROUTE_HISTORY.mockReturnValue({
callbackUrl: jest.fn(
(transformFn?: TransformRouteFn | undefined) =>
transformFn?.(ROUTES) ?? ROOT_PATH,
),
});
});
test("does not redirect if auth status and authentication status is PENDING", () => {
renderHook(() =>
useSessionActive(AUTH_STATE_PENDING, AUTHENTICATION_STATE_PENDING),
);
expect(Router.push).not.toHaveBeenCalled();
});
test("does not redirect if auth status is PENDING and authentication status is SETTLED", () => {
renderHook(() =>
useSessionActive(AUTH_STATE_PENDING, AUTHENTICATION_STATE_SETTLED),
);
expect(Router.push).not.toHaveBeenCalled();
});
test("redirects if auth status and authentication status is SETTLED", () => {
renderHook(() =>
useSessionActive(
AUTH_STATE_UNAUTHENTICATED_SETTLED,
AUTHENTICATION_STATE_SETTLED,
),
);
expect(Router.push).toHaveBeenCalled();
});
test("redirects to callback URL if auth status is SETTLED and authentication status is SETTLED", () => {
renderHook(() =>
useSessionActive(
AUTH_STATE_AUTHENTICATED_SETTLED,
AUTHENTICATION_STATE_SETTLED,
),
);
expect(Router.push).toHaveBeenCalledWith(ROUTES[1]);
});
test("prefers router.query.callbackUrl over route-history fallback", () => {
mockRouterQuery = { callbackUrl: "/from-query" };
renderHook(() =>
useSessionActive(
AUTH_STATE_AUTHENTICATED_SETTLED,
AUTHENTICATION_STATE_SETTLED,
),
);
expect(Router.push).toHaveBeenCalledWith("/from-query");
});
test("falls back to route history when router.query.callbackUrl is an empty string", () => {
mockRouterQuery = { callbackUrl: "" };
renderHook(() =>
useSessionActive(
AUTH_STATE_AUTHENTICATED_SETTLED,
AUTHENTICATION_STATE_SETTLED,
),
);
expect(Router.push).toHaveBeenCalledWith(ROUTES[1]);
});
test("falls back to route history when router.query.callbackUrl is an absolute URL (open-redirect guard)", () => {
mockRouterQuery = { callbackUrl: "https://evil.example/phish" };
renderHook(() =>
useSessionActive(
AUTH_STATE_AUTHENTICATED_SETTLED,
AUTHENTICATION_STATE_SETTLED,
),
);
expect(Router.push).toHaveBeenCalledWith(ROUTES[1]);
});
test("falls back to route history when router.query.callbackUrl is protocol-relative", () => {
mockRouterQuery = { callbackUrl: "//evil.example/phish" };
renderHook(() =>
useSessionActive(
AUTH_STATE_AUTHENTICATED_SETTLED,
AUTHENTICATION_STATE_SETTLED,
),
);
expect(Router.push).toHaveBeenCalledWith(ROUTES[1]);
});
});