Skip to content

Commit

Permalink
Create admin role in test env
Browse files Browse the repository at this point in the history
  • Loading branch information
parkerdavis1 committed Jan 8, 2024
1 parent 13c13cc commit 643442c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
19 changes: 18 additions & 1 deletion tests/e2e/admin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
14 changes: 12 additions & 2 deletions tests/playwright-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 643442c

Please sign in to comment.