-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproxy.test.ts
More file actions
55 lines (39 loc) · 1.67 KB
/
Copy pathproxy.test.ts
File metadata and controls
55 lines (39 loc) · 1.67 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
/** @jest-environment node */
import { NextRequest } from "next/server";
import { SESSION } from "@/const/cookie";
import { decrypt } from "@/lib/session";
import { proxy } from "./proxy";
jest.mock("@/lib/session", () => ({
decrypt: jest.fn(),
}));
const mockDecrypt = jest.mocked(decrypt);
function createRequest(path: string, session?: string) {
return new NextRequest(`http://localhost${path}`, {
headers: session ? { cookie: `${SESSION}=${session}` } : undefined,
});
}
describe("proxy", () => {
afterEach(() => {
jest.restoreAllMocks();
});
it("redirects anonymous dashboard requests to login without logging request data", async () => {
mockDecrypt.mockResolvedValue(null);
const log = jest.spyOn(console, "log").mockImplementation();
const response = await proxy(createRequest("/dashboard"));
expect(response.status).toBe(307);
expect(response.headers.get("location")).toBe("http://localhost/login");
expect(log).not.toHaveBeenCalled();
});
it("redirects authenticated users away from public routes", async () => {
mockDecrypt.mockResolvedValue({ uid: 1, name: "Admin", role: 3 });
const response = await proxy(createRequest("/login", "encrypted-session"));
expect(response.status).toBe(307);
expect(response.headers.get("location")).toBe("http://localhost/dashboard");
});
it("allows authenticated dashboard requests to continue", async () => {
mockDecrypt.mockResolvedValue({ uid: 1, name: "Admin", role: 3 });
const response = await proxy(createRequest("/dashboard/emails", "encrypted-session"));
expect(response.status).toBe(200);
expect(response.headers.get("location")).toBeNull();
});
});