28 lines
1.0 KiB
TypeScript
28 lines
1.0 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
import { resolveCurrentUser } from "../src/auth-session";
|
|
import type { Env } from "../src/env";
|
|
|
|
function envWithAuth(response: Response): Env {
|
|
return {
|
|
AUTH: { fetch: vi.fn().mockResolvedValue(response) },
|
|
DB: {} as D1Database,
|
|
ATTACHMENTS: {} as R2Bucket,
|
|
};
|
|
}
|
|
|
|
describe("resolveCurrentUser", () => {
|
|
it("returns the Better Auth user id from cfw-auth", async () => {
|
|
const env = envWithAuth(Response.json({ user: { id: "user_123", email: "a@example.com" } }));
|
|
|
|
await expect(resolveCurrentUser(new Request("https://attachment.local/api/attachments"), env))
|
|
.resolves.toEqual({ id: "user_123", email: "a@example.com" });
|
|
});
|
|
|
|
it("throws authentication-required when cfw-auth has no session", async () => {
|
|
const env = envWithAuth(Response.json(null));
|
|
|
|
await expect(resolveCurrentUser(new Request("https://attachment.local/api/attachments"), env))
|
|
.rejects.toMatchObject({ status: 401, code: "authentication-required" });
|
|
});
|
|
});
|