-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathroute.test.ts
More file actions
69 lines (57 loc) · 2.17 KB
/
route.test.ts
File metadata and controls
69 lines (57 loc) · 2.17 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
import { describe, it, expect, vi, beforeEach } from "vitest";
import { NextRequest } from "next/server";
const mockExchangeCodeForSession = vi.fn();
const mockSignOut = vi.fn();
vi.mock("@/lib/supabase/server", () => ({
createClient: vi.fn().mockResolvedValue({
auth: {
exchangeCodeForSession: (...args: unknown[]) =>
mockExchangeCodeForSession(...args),
signOut: (...args: unknown[]) => mockSignOut(...args),
},
}),
}));
import { GET } from "./route";
beforeEach(() => {
vi.clearAllMocks();
});
describe("GET /auth/callback", () => {
it("exchanges code for session and redirects to sign-in with confirmed=true", async () => {
mockExchangeCodeForSession.mockResolvedValue({ error: null });
mockSignOut.mockResolvedValue({ error: null });
const request = new NextRequest(
"http://localhost:3000/auth/callback?code=test-auth-code",
);
const response = await GET(request);
expect(mockExchangeCodeForSession).toHaveBeenCalledWith("test-auth-code");
expect(mockSignOut).toHaveBeenCalled();
expect(response.status).toBe(307);
expect(response.headers.get("location")).toBe(
"http://localhost:3000/sign-in?confirmed=true",
);
});
it("redirects to sign-in without confirmed param when code exchange fails", async () => {
mockExchangeCodeForSession.mockResolvedValue({
error: { message: "Invalid code" },
});
const request = new NextRequest(
"http://localhost:3000/auth/callback?code=bad-code",
);
const response = await GET(request);
expect(mockExchangeCodeForSession).toHaveBeenCalledWith("bad-code");
expect(mockSignOut).not.toHaveBeenCalled();
expect(response.status).toBe(307);
expect(response.headers.get("location")).toBe(
"http://localhost:3000/sign-in",
);
});
it("redirects to sign-in when no code param is provided", async () => {
const request = new NextRequest("http://localhost:3000/auth/callback");
const response = await GET(request);
expect(mockExchangeCodeForSession).not.toHaveBeenCalled();
expect(response.status).toBe(307);
expect(response.headers.get("location")).toBe(
"http://localhost:3000/sign-in",
);
});
});