-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathauth.proxy.test.ts
More file actions
53 lines (46 loc) · 1.37 KB
/
Copy pathauth.proxy.test.ts
File metadata and controls
53 lines (46 loc) · 1.37 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
// Parent: REQ-0020; TC-0025
import { beforeAll, describe, expect, it, vi } from "vitest";
const captured = vi.hoisted(() => ({ config: null as AuthConfig | null }));
interface AuthConfig {
callbacks: {
authorized: (input: {
auth: { user?: { id: string } } | null;
request: { nextUrl: { pathname: string } };
}) => boolean;
};
}
vi.mock("next-auth", () => ({
default: vi.fn((config: AuthConfig) => {
captured.config = config;
return {
handlers: {},
signIn: vi.fn(),
signOut: vi.fn(),
auth: vi.fn(),
};
}),
}));
vi.mock("next-auth/providers/credentials", () => ({
default: vi.fn((config: unknown) => config),
}));
beforeAll(async () => {
await import("./auth");
});
describe("Auth.js proxy callback wiring", () => {
it("rejects anonymous protected requests and accepts authenticated ones", () => {
const authorized = captured.config?.callbacks.authorized;
expect(authorized).toBeTypeOf("function");
expect(
authorized?.({ auth: null, request: { nextUrl: { pathname: "/admin" } } })
).toBe(false);
expect(
authorized?.({
auth: { user: { id: "10000000-0000-4000-8000-000000000001" } },
request: { nextUrl: { pathname: "/admin" } },
})
).toBe(true);
expect(
authorized?.({ auth: null, request: { nextUrl: { pathname: "/sign-in" } } })
).toBe(true);
});
});