36 lines
1.6 KiB
TypeScript
36 lines
1.6 KiB
TypeScript
import { eq } from "drizzle-orm";
|
|
import { afterAll, expect, test } from "vitest";
|
|
|
|
import { users } from "@/db/schema";
|
|
import { insertAdminCredential, seedAdmin } from "@/modules/auth/admin-bootstrap";
|
|
|
|
import { startPostgres } from "./postgres";
|
|
|
|
const testDb = await startPostgres();
|
|
afterAll(() => testDb.stop());
|
|
|
|
const input = { email: "existing@example.test", name: "Admin", password: "dummy seed password" };
|
|
const seed = (email: string) =>
|
|
seedAdmin({ ...input, email }, (credential) => insertAdminCredential(testDb.database, credential));
|
|
|
|
test("fails for an existing address without changing its role or verification", async () => {
|
|
const [{ id }] = await testDb.database.insert(users)
|
|
.values({ email: "Existing@example.test", name: "Editorial" })
|
|
.returning({ id: users.id });
|
|
await expect(seed(input.email)).rejects.toThrow("email is already registered");
|
|
const [account] = await testDb.database.select().from(users).where(eq(users.id, id));
|
|
expect(account).toMatchObject({ role: "EDITORIAL", emailVerified: false, name: "Editorial" });
|
|
});
|
|
|
|
test("rolls back the admin user when its credential insert fails", async () => {
|
|
const email = "rollback@example.test";
|
|
await testDb.pool.query("alter table account add constraint test_reject_credential check (provider_id <> 'credential')");
|
|
try {
|
|
await expect(seed(email)).rejects.toThrow();
|
|
const found = await testDb.database.select().from(users).where(eq(users.email, email));
|
|
expect(found).toEqual([]);
|
|
} finally {
|
|
await testDb.pool.query("alter table account drop constraint test_reject_credential");
|
|
}
|
|
});
|