forked from UllrAI/SaaS-Starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.test.ts
More file actions
142 lines (111 loc) · 4.76 KB
/
middleware.test.ts
File metadata and controls
142 lines (111 loc) · 4.76 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
import { NextRequest, NextResponse } from "next/server";
import authMiddleware from "./src/middleware";
import { getSessionCookie } from "better-auth/cookies";
// Mock better-auth/cookies
jest.mock("better-auth/cookies", () => ({
getSessionCookie: jest.fn() as jest.MockedFunction<
(cookies: {
get: (name: string) => { value: string } | undefined;
}) => string | null
>,
}));
// Mock Next.js server components
jest.mock("next/server", () => ({
NextResponse: {
redirect: jest.fn((url) => ({ headers: new Headers({ location: url }) })),
next: jest.fn(() => ({})), // Mock next() to return a simple object
},
NextRequest: jest.fn((url) => ({
url: url, // Ensure url is a full string
nextUrl: new URL(url),
cookies: {
get: jest.fn() as jest.MockedFunction<
(name: string) => { value: string } | undefined
>,
},
})),
}));
describe("authMiddleware", () => {
beforeEach(() => {
jest.clearAllMocks();
});
it("should redirect logged-in users from auth pages to /dashboard", async () => {
(getSessionCookie as jest.Mock).mockReturnValue("some-session-cookie");
const request = new NextRequest("http://localhost/login");
const response = await authMiddleware(request);
expect(NextResponse.redirect).toHaveBeenCalledWith(
new URL("/dashboard", request.url),
);
expect(response?.headers.get("location")).toBe(
"http://localhost/dashboard",
);
});
it("should redirect unauthenticated users from dashboard pages to /login with callbackUrl", async () => {
(getSessionCookie as jest.Mock).mockReturnValue(undefined);
const request = new NextRequest("http://localhost/dashboard/settings");
const response = await authMiddleware(request);
const loginUrl = new URL("/login", request.url);
loginUrl.searchParams.set("callbackUrl", "/dashboard/settings");
expect(NextResponse.redirect).toHaveBeenCalledWith(loginUrl);
expect(response?.headers.get("location")).toBe(
"http://localhost/login?callbackUrl=%2Fdashboard%2Fsettings",
);
});
it("should allow logged-in users to access non-auth, non-dashboard pages", async () => {
(getSessionCookie as jest.Mock).mockReturnValue("some-session-cookie");
const request = new NextRequest("http://localhost/some-other-page");
const response = await authMiddleware(request);
expect(NextResponse.next).toHaveBeenCalled();
expect(response).toEqual({});
});
it("should allow unauthenticated users to access non-auth, non-dashboard pages", async () => {
(getSessionCookie as jest.Mock).mockReturnValue(undefined);
const request = new NextRequest("http://localhost/some-other-page");
const response = await authMiddleware(request);
expect(NextResponse.next).toHaveBeenCalled();
expect(response).toEqual({});
});
it("should redirect logged-in users from /signup to /dashboard", async () => {
(getSessionCookie as jest.Mock).mockReturnValue("some-session-cookie");
const request = new NextRequest("http://localhost/signup");
const response = await authMiddleware(request);
expect(NextResponse.redirect).toHaveBeenCalledWith(
new URL("/dashboard", request.url),
);
expect(response?.headers.get("location")).toBe(
"http://localhost/dashboard",
);
});
it("should redirect logged-in users from /auth/sent to /dashboard", async () => {
(getSessionCookie as jest.Mock).mockReturnValue("some-session-cookie");
const request = new NextRequest("http://localhost/auth/sent");
const response = await authMiddleware(request);
expect(NextResponse.redirect).toHaveBeenCalledWith(
new URL("/dashboard", request.url),
);
expect(response?.headers.get("location")).toBe(
"http://localhost/dashboard",
);
});
it("should allow unauthenticated users to access /login", async () => {
(getSessionCookie as jest.Mock).mockReturnValue(undefined);
const request = new NextRequest("http://localhost/login");
const response = await authMiddleware(request);
expect(NextResponse.next).toHaveBeenCalled();
expect(response).toEqual({});
});
it("should allow unauthenticated users to access /signup", async () => {
(getSessionCookie as jest.Mock).mockReturnValue(undefined);
const request = new NextRequest("http://localhost/signup");
const response = await authMiddleware(request);
expect(NextResponse.next).toHaveBeenCalled();
expect(response).toEqual({});
});
it("should allow unauthenticated users to access /auth/sent", async () => {
(getSessionCookie as jest.Mock).mockReturnValue(undefined);
const request = new NextRequest("http://localhost/auth/sent");
const response = await authMiddleware(request);
expect(NextResponse.next).toHaveBeenCalled();
expect(response).toEqual({});
});
});