From 643442c4c38e4f290e350f610a9ba97e4a06b4f6 Mon Sep 17 00:00:00 2001 From: Parker Date: Mon, 8 Jan 2024 12:08:11 -0700 Subject: [PATCH] Create admin role in test env --- tests/e2e/admin.test.ts | 19 ++++++++++++++++++- tests/playwright-utils.ts | 14 ++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/tests/e2e/admin.test.ts b/tests/e2e/admin.test.ts index 234965f..ace6923 100644 --- a/tests/e2e/admin.test.ts +++ b/tests/e2e/admin.test.ts @@ -2,7 +2,24 @@ import { faker } from '@faker-js/faker' import invariant from 'tiny-invariant' import { expect, test, insertNewAdmin } from '../playwright-utils.ts' -test('login as admin', async ({ page }) => { +test('Admin can log in and see admin panel', async ({ page }) => { + const password = faker.internet.password() + const adminUser = await insertNewAdmin({ password }) + invariant(adminUser.name, 'User name not found') + await page.goto('/login') + await page + .getByRole('textbox', { name: /username/i }) + .fill(adminUser.username) + await page.getByLabel(/^password$/i).fill(password) + await page.getByRole('button', { name: /log in/i }).click() + await expect(page).toHaveURL(`/`) + + await expect(page.getByRole('link', { name: /admin/i })).toBeVisible() +}) + +test('Admin can edit users with proper validation for height and feet', async ({ + page, +}) => { const password = faker.internet.password() const adminUser = await insertNewAdmin({ password }) invariant(adminUser.name, 'User name not found') diff --git a/tests/playwright-utils.ts b/tests/playwright-utils.ts index 9716c1b..9e4ce47 100644 --- a/tests/playwright-utils.ts +++ b/tests/playwright-utils.ts @@ -33,13 +33,23 @@ export async function insertNewUser({ password }: { password?: string } = {}) { export async function insertNewAdmin({ password }: { password?: string } = {}) { const userData = createUser() - const adminRole = await prisma.role.findFirst({ + let adminRole = await prisma.role.findFirst({ where: { name: 'admin', }, }) - if (!adminRole) throw new Error('No admin role defined') + if (!adminRole) { + console.log('Creating admin role') + adminRole = await prisma.role.create({ + data: { + name: 'admin', + permissions: { + create: { name: 'admin' }, + }, + }, + }) + } const user = await prisma.user.create({ data: {