|
| 1 | +import { expect, test } from '@playwright/test' |
| 2 | +import { faker } from '@faker-js/faker' |
| 3 | +import { cleanDatabase } from '../prisma/helpers' |
| 4 | +import { PrismaClient, Role } from '@prisma/client' |
| 5 | +import { encrypt } from '../src/lib/server/utils/crypto' |
| 6 | +import pkg from 'bcryptjs' |
| 7 | + |
| 8 | +const { hashSync } = pkg |
| 9 | +const prisma = new PrismaClient() |
| 10 | + |
| 11 | +type Team = { |
| 12 | + id: number |
| 13 | + name: string |
| 14 | + openAiApiKey: string | null |
| 15 | + createdAt: Date |
| 16 | + updatedAt: Date |
| 17 | +} |
| 18 | + |
| 19 | +async function createTeam(name: string) { |
| 20 | + return await prisma.team.create({ |
| 21 | + data: { |
| 22 | + name: name, |
| 23 | + openAiApiKey: |
| 24 | + process.env.OPENAI_API_KEY && process.env.SECRET_KEY |
| 25 | + ? encrypt(process.env.OPENAI_API_KEY, process.env.SECRET_KEY) |
| 26 | + : null, |
| 27 | + }, |
| 28 | + }) |
| 29 | +} |
| 30 | + |
| 31 | +async function createUser(team: Team) { |
| 32 | + |
| 33 | + return await prisma.user.create({ |
| 34 | + data: { |
| 35 | + email, |
| 36 | + name: 'Test1', |
| 37 | + password: { |
| 38 | + create: { |
| 39 | + hash: hashSync('password', 10), |
| 40 | + }, |
| 41 | + }, |
| 42 | + userTeams: { |
| 43 | + create: { |
| 44 | + teamId: team.id, |
| 45 | + role: Role.OWNER, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + include: { |
| 50 | + userTeams: true, |
| 51 | + }, |
| 52 | + }) |
| 53 | +} |
| 54 | + |
| 55 | +test.describe('app flow tests', () => { |
| 56 | + test.beforeEach(async () => { |
| 57 | + await cleanDatabase() |
| 58 | + }) |
| 59 | + |
| 60 | + test('register flow', async ({ page }) => { |
| 61 | + const name = faker.person.firstName() |
| 62 | + const email = faker.internet.email() |
| 63 | + const password = faker.internet.password() |
| 64 | + |
| 65 | + await page.goto('/') |
| 66 | + await page.getByText('Register').click() |
| 67 | + await expect(page).toHaveURL('/signup') |
| 68 | + |
| 69 | + await page.getByLabel('Name').fill(name) |
| 70 | + await page.getByLabel('Email').fill(email) |
| 71 | + await page.getByLabel('Password', { exact: true }).fill(password) |
| 72 | + await page.getByLabel('Confirm Password').fill(password) |
| 73 | + |
| 74 | + await page.getByText('Sign up').click() |
| 75 | + |
| 76 | + await page.waitForURL('/app/settings/teams') |
| 77 | + |
| 78 | + await expect(page).toHaveURL('/app/settings/teams') |
| 79 | + }), |
| 80 | + test('login flow', async ({ page }) => { |
| 81 | + let team = await createTeam('Test Team') |
| 82 | + let user = await createUser(team) |
| 83 | + |
| 84 | + await page.goto('/') |
| 85 | + await page.getByText('Sign in').click() |
| 86 | + await expect(page).toHaveURL('/signin') |
| 87 | + |
| 88 | + await page.getByLabel('Email').fill(user.email) |
| 89 | + await page.getByLabel('Password', { exact: true }).fill('password') |
| 90 | + await page.getByRole('button').click() |
| 91 | + |
| 92 | + await page.waitForURL('/app/settings/teams') |
| 93 | + |
| 94 | + await expect(page).toHaveURL('/app/settings/teams') |
| 95 | + }), |
| 96 | + test('create a team', async ({ page }) => { |
| 97 | + let team = await createTeam('Test Team') |
| 98 | + let user = await createUser(team) |
| 99 | + |
| 100 | + //log in |
| 101 | + await page.goto('/') |
| 102 | + await page.getByText('Sign in').click() |
| 103 | + await expect(page).toHaveURL('/signin') |
| 104 | + |
| 105 | + await page.getByLabel('Email').fill(user.email) |
| 106 | + await page.getByLabel('Password', { exact: true }).fill('password') |
| 107 | + await page.getByRole('button').click() |
| 108 | + |
| 109 | + await page.waitForURL('/app/settings/teams') |
| 110 | + |
| 111 | + await expect(page).toHaveURL('/app/settings/teams') |
| 112 | + |
| 113 | + //close popup |
| 114 | + await page.getByRole('button', { name: /Close/i }).click() |
| 115 | + |
| 116 | + //create new team |
| 117 | + await page.getByText('New Team').click() |
| 118 | + await expect(page).toHaveURL('/app/settings/teams/new') |
| 119 | + |
| 120 | + await page.getByLabel('Name*').fill('Test Team 2') |
| 121 | + await page.getByLabel('OpenAI API Key*').fill(process.env.OPENAI_API_KEY || '') |
| 122 | + await page.getByRole('button', { name: /Create/i }).click() |
| 123 | + |
| 124 | + await page.waitForURL(new RegExp('app/settings/teams/\\d+')) |
| 125 | + await expect(page).toHaveURL(new RegExp('app/settings/teams/\\d+')) |
| 126 | + }), |
| 127 | + test('create a chat', async ({ page }) => { |
| 128 | + let team = await createTeam('Test Team') |
| 129 | + let user = await createUser(team) |
| 130 | + |
| 131 | + //log in |
| 132 | + await page.goto('/') |
| 133 | + await page.getByText('Sign in').click() |
| 134 | + await expect(page).toHaveURL('/signin') |
| 135 | + |
| 136 | + await page.getByLabel('Email').fill(user.email) |
| 137 | + await page.getByLabel('Password', { exact: true }).fill('password') |
| 138 | + await page.getByRole('button').click() |
| 139 | + |
| 140 | + await page.waitForURL('/app/settings/teams') |
| 141 | + |
| 142 | + await expect(page).toHaveURL('/app/settings/teams') |
| 143 | + //select a team |
| 144 | + await page.getByRole('button', { name: /Select/i }).click() |
| 145 | + await page.waitForURL('/app') |
| 146 | + await expect(page).toHaveURL('/app') |
| 147 | + |
| 148 | + //type |
| 149 | + await page.getByRole('textbox').fill('Dis a test chat') |
| 150 | + //send |
| 151 | + await page.locator('button[name="send"]').click() |
| 152 | + |
| 153 | + await page.waitForTimeout(1000) |
| 154 | + await page.waitForURL(new RegExp('/app/chats/\\d+')) |
| 155 | + await expect(page).toHaveURL(new RegExp('/app/chats/\\d+')) |
| 156 | + }) |
| 157 | +}) |
0 commit comments