From b7b5306619cc4420bd0ae494881ee7161391dec4 Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Sat, 2 Mar 2024 09:53:24 +0000 Subject: feature: Make the first user an admin automatically --- packages/web/lib/testUtils.ts | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) (limited to 'packages/web/lib') diff --git a/packages/web/lib/testUtils.ts b/packages/web/lib/testUtils.ts index 142ad844..bad78463 100644 --- a/packages/web/lib/testUtils.ts +++ b/packages/web/lib/testUtils.ts @@ -25,13 +25,15 @@ export async function seedUsers(db: TestDB) { .returning(); } -export function getApiCaller(db: TestDB, userId: string) { +export function getApiCaller(db: TestDB, userId?: string) { const createCaller = createCallerFactory(appRouter); return createCaller({ - user: { - id: userId, - role: "user", - }, + user: userId + ? { + id: userId, + role: "user", + } + : null, db, }); } @@ -40,20 +42,29 @@ export type APICallerType = ReturnType; export interface CustomTestContext { apiCallers: APICallerType[]; + unauthedAPICaller: APICallerType; db: TestDB; } -export async function buildTestContext(): Promise { +export async function buildTestContext( + seedDB: boolean, +): Promise { const db = getTestDB(); - const users = await seedUsers(db); + let users: Awaited> = []; + if (seedDB) { + users = await seedUsers(db); + } const callers = users.map((u) => getApiCaller(db, u.id)); return { apiCallers: callers, + unauthedAPICaller: getApiCaller(db), db, }; } -export const defaultBeforeEach = async (context: object) => { - Object.assign(context, await buildTestContext()); -}; +export function defaultBeforeEach(seedDB: boolean = true) { + return async (context: object) => { + Object.assign(context, await buildTestContext(seedDB)); + }; +} -- cgit v1.2.3-70-g09d2