50 lines
1.7 KiB
TypeScript
50 lines
1.7 KiB
TypeScript
|
|
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
|
||
|
|
|
||
|
|
// Mock the pocketbase module so getAdminPocketBase doesn't try to open a socket.
|
||
|
|
const authWithPassword = vi.fn().mockResolvedValue({ token: "mock-token" });
|
||
|
|
vi.mock("pocketbase", () => {
|
||
|
|
// Must be a class (or function) because the production code does `new PocketBase(url)`.
|
||
|
|
const FakePocketBase = vi.fn().mockImplementation(function () {
|
||
|
|
return {
|
||
|
|
collection: () => ({
|
||
|
|
authWithPassword,
|
||
|
|
}),
|
||
|
|
};
|
||
|
|
});
|
||
|
|
return { default: FakePocketBase };
|
||
|
|
});
|
||
|
|
|
||
|
|
import { getAdminPocketBase } from "./pocketbase-admin";
|
||
|
|
|
||
|
|
describe("getAdminPocketBase", () => {
|
||
|
|
const ORIGINAL_EMAIL = process.env.PB_ADMIN_EMAIL;
|
||
|
|
const ORIGINAL_PASSWORD = process.env.PB_ADMIN_PASSWORD;
|
||
|
|
|
||
|
|
beforeEach(() => {
|
||
|
|
process.env.PB_ADMIN_EMAIL = "admin@example.com";
|
||
|
|
process.env.PB_ADMIN_PASSWORD = "secret";
|
||
|
|
authWithPassword.mockClear();
|
||
|
|
});
|
||
|
|
|
||
|
|
afterEach(() => {
|
||
|
|
if (ORIGINAL_EMAIL === undefined) delete process.env.PB_ADMIN_EMAIL;
|
||
|
|
else process.env.PB_ADMIN_EMAIL = ORIGINAL_EMAIL;
|
||
|
|
if (ORIGINAL_PASSWORD === undefined) delete process.env.PB_ADMIN_PASSWORD;
|
||
|
|
else process.env.PB_ADMIN_PASSWORD = ORIGINAL_PASSWORD;
|
||
|
|
});
|
||
|
|
|
||
|
|
it("returns a PocketBase instance and authenticates with the admin credentials", async () => {
|
||
|
|
const pb = await getAdminPocketBase();
|
||
|
|
expect(pb).toBeDefined();
|
||
|
|
expect(authWithPassword).toHaveBeenCalledWith("admin@example.com", "secret");
|
||
|
|
});
|
||
|
|
|
||
|
|
it("throws a clear error when admin env vars are missing", async () => {
|
||
|
|
delete process.env.PB_ADMIN_EMAIL;
|
||
|
|
delete process.env.PB_ADMIN_PASSWORD;
|
||
|
|
await expect(getAdminPocketBase()).rejects.toThrow(
|
||
|
|
/PB_ADMIN_EMAIL and PB_ADMIN_PASSWORD are required/
|
||
|
|
);
|
||
|
|
});
|
||
|
|
});
|