From 1ec2d6ed805f55bfdefc00639017d4f5e2bc0cde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20G=C3=BCndling?= Date: Fri, 6 Sep 2024 11:51:22 +0200 Subject: [PATCH] Company data tests1 (#148) * add company data tests * format+lint * wip * format * fix tests * wip, fix tests * wip * update playwright.config * update playwright.config * fix tests * test * wip * wip * wip * sleep more * wip * test * fix typo * use docker container for tests --------- Co-authored-by: Steffen Heger --- playwright.config.ts | 11 ++- src/routes/(user)/company/+page.server.ts | 2 +- src/routes/(user)/company/+page.svelte | 8 +- tests/companyData.test.ts | 83 ++++++++++++++++++++ tests/{global.setup.ts => db.setup.ts} | 8 ++ tests/login.setup.ts | 32 ++++++++ tests/test.ts | 92 ----------------------- tests/utils.ts | 35 +++++++++ 8 files changed, 171 insertions(+), 100 deletions(-) create mode 100644 tests/companyData.test.ts rename tests/{global.setup.ts => db.setup.ts} (92%) create mode 100644 tests/login.setup.ts delete mode 100644 tests/test.ts create mode 100644 tests/utils.ts diff --git a/playwright.config.ts b/playwright.config.ts index ef9f6ccb..baaf320a 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -1,6 +1,6 @@ import { defineConfig } from '@playwright/test'; export default defineConfig({ - // // test for development version + // test for development version // webServer: { // command: 'while true; do npm run dev; done', // url: 'http://localhost:5173', @@ -23,12 +23,17 @@ export default defineConfig({ projects: [ { name: 'setup db', - testMatch: /global\.setup\.ts/ + testMatch: /db\.setup\.ts/ + }, + { + name: 'login', + testMatch: /login\.setup\.ts/, + dependencies: ['setup db'] }, { name: 'user test', testMatch: /(.+\.)?(test|spec)\.[jt]s/, - dependencies: ['setup db'] + dependencies: ['setup db', 'login'] } ] }); diff --git a/src/routes/(user)/company/+page.server.ts b/src/routes/(user)/company/+page.server.ts index 3d170bdd..7ac3ccaa 100644 --- a/src/routes/(user)/company/+page.server.ts +++ b/src/routes/(user)/company/+page.server.ts @@ -62,7 +62,7 @@ export const actions = { } if (isNaN(zone) || zone < 1) { - return fail(400, { error: 'Pflichtfahrgebiet nicht gesetzt' }); + return fail(400, { error: 'Pflichtfahrgebiet nicht gesetzt.' }); } let bestAddressGuess: AddressGuess | undefined = undefined; diff --git a/src/routes/(user)/company/+page.svelte b/src/routes/(user)/company/+page.svelte index 9c4199ff..7ebe9843 100644 --- a/src/routes/(user)/company/+page.svelte +++ b/src/routes/(user)/company/+page.svelte @@ -15,7 +15,7 @@
- Stammdaten ihres Unternehmens + Stammdaten Ihres Unternehmens {#if form?.error} @@ -30,9 +30,9 @@ Aktualisierung erfolgreich. - Die Stammdaten ihres Unternehmens Ihres Unternehmens wurden aktualisiert. + + Die Stammdaten Ihres Unternehmens wurden aktualisiert. + {/if} diff --git a/tests/companyData.test.ts b/tests/companyData.test.ts new file mode 100644 index 00000000..6550066c --- /dev/null +++ b/tests/companyData.test.ts @@ -0,0 +1,83 @@ +import { expect, test } from '@playwright/test'; +import { login, ENTREPENEUR } from './utils'; + +test('Set company data, incomplete 1', async ({ page }) => { + await login(page, ENTREPENEUR); + await expect(page.getByRole('heading', { name: 'Stammdaten Ihres Unternehmens' })).toBeVisible(); + + await page.getByLabel('Name').fill('Test'); + await page.getByRole('button', { name: 'Übernehmen' }).click(); + + await expect(page.getByText('Adresse zu kurz.')).toBeVisible(); +}); + +test('Set company data, incomplete 2', async ({ page }) => { + await login(page, ENTREPENEUR); + + await page.getByLabel('Name').fill('Test'); + await page.getByLabel('Unternehmenssitz').fill('Plantagenweg 3, 02827 Görlitz'); + await page.getByRole('button', { name: 'Übernehmen' }).click(); + + await expect( + page.getByText('Die Eingabe muss mindestens 2 Zeichen enthalten.') + ).not.toBeVisible(); + await expect(page.getByText('Gemeinde nicht gesetzt.')).toBeVisible(); +}); + +test('Set company data, incomplete 3', async ({ page }) => { + await login(page, ENTREPENEUR); + + await page.getByLabel('Name').fill('Taxi Weißwasser'); + await page + .getByLabel('Unternehmenssitz') + .fill('Werner-Seelenbinder-Straße 70A, 02943 Weißwasser/Oberlausitz'); + await page.getByLabel('Pflichtfahrgebiet').selectOption({ label: 'Görlitz' }); + await page.getByRole('button', { name: 'Übernehmen' }).click(); + + await expect(page.getByText('Gemeinde nicht gesetzt.')).toBeVisible(); +}); + +test('Set company data, address not in community', async ({ page }) => { + await login(page, ENTREPENEUR); + await expect(page.getByRole('heading', { name: 'Stammdaten Ihres Unternehmens' })).toBeVisible(); + + await page.getByLabel('Name').fill('Taxi Weißwasser'); + await page.getByLabel('Unternehmenssitz').fill('Plantagenweg 3, 02827 Görlitz'); + await page.waitForTimeout(250); + await page.getByLabel('Pflichtfahrgebiet').selectOption({ label: 'Görlitz' }); + await page.getByLabel('Gemeinde').selectOption({ label: 'Weißwasser/O.L.' }); + await page.getByRole('button', { name: 'Übernehmen' }).click(); + + await expect( + page.getByText('Die Addresse liegt nicht in der ausgewählten Gemeinde.') + ).toBeVisible(); +}); + +test('Set company data, complete and consistent', async ({ page }) => { + await login(page, ENTREPENEUR); + await expect(page.getByRole('heading', { name: 'Stammdaten Ihres Unternehmens' })).toBeVisible(); + + await page.getByLabel('Name').fill('Taxi Weißwasser'); + await page + .getByLabel('Unternehmenssitz') + .fill('Werner-Seelenbinder-Straße 70A, 02943 Weißwasser/Oberlausitz'); + await page.waitForTimeout(250); + await page.getByLabel('Pflichtfahrgebiet').selectOption({ label: 'Weißwasser' }); + await page.getByLabel('Gemeinde').selectOption({ label: 'Weißwasser/O.L.' }); + await page.getByRole('button', { name: 'Übernehmen' }).click(); + + await expect(page.getByRole('heading', { name: 'Stammdaten Ihres Unternehmens' })).toBeVisible(); + + const checkData = async () => { + await expect(page.getByLabel('Name')).toHaveValue('Taxi Weißwasser'); + await expect(page.getByLabel('Unternehmenssitz')).toHaveValue( + 'Werner-Seelenbinder-Straße 70A, 02943 Weißwasser/Oberlausitz' + ); + await expect(page.getByLabel('Pflichtfahrgebiet')).toHaveValue('2' /* Görlitz */); + await expect(page.getByLabel('Gemeinde')).toHaveValue('85' /* Weißwasser */); + }; + + await checkData(); + await page.reload(); + await checkData(); +}); diff --git a/tests/global.setup.ts b/tests/db.setup.ts similarity index 92% rename from tests/global.setup.ts rename to tests/db.setup.ts index 4d32dbc1..efb2c661 100644 --- a/tests/global.setup.ts +++ b/tests/db.setup.ts @@ -44,4 +44,12 @@ setup('setup db', async () => { const zonesQuery = fs.readFileSync(zonesSqlPath).toString(); await pool.query(zonesQuery); console.log('zones added'); + + await sleep(1000); }); + +function sleep(ms: number) { + return new Promise((resolve) => { + setTimeout(resolve, ms); + }); +} diff --git a/tests/login.setup.ts b/tests/login.setup.ts new file mode 100644 index 00000000..29e9ca90 --- /dev/null +++ b/tests/login.setup.ts @@ -0,0 +1,32 @@ +import { expect, test } from '@playwright/test'; +import { Kysely, PostgresDialect, sql } from 'kysely'; +import { dbConfig } from './config'; +import pg from 'pg'; +import { login, signup, MAINTAINER, ENTREPENEUR } from './utils'; + +test.describe.configure({ mode: 'serial' }); + +test('signup maintainer', async ({ page }) => { + await signup(page, MAINTAINER); + const db = new Kysely({ + dialect: new PostgresDialect({ + pool: new pg.Pool({ ...dbConfig, database: 'prima' }) + }) + }); + await sql`UPDATE auth_user SET is_maintainer = true WHERE email = 'master@example.com'`.execute( + db + ); + db.destroy(); +}); + +test('signup taxi', async ({ page }) => { + await signup(page, ENTREPENEUR); +}); + +test('activate taxi', async ({ page }) => { + await login(page, MAINTAINER); + await expect(page.getByRole('heading', { name: 'Unternehmer freischalten' })).toBeVisible(); + await page.getByLabel('Email').fill(ENTREPENEUR.email); + await page.getByRole('button', { name: 'Unternehmer freischalten' }).click(); + await expect(page.getByText('Freischalten erfolgreich!')).toBeVisible(); +}); diff --git a/tests/test.ts b/tests/test.ts deleted file mode 100644 index d2849a9a..00000000 --- a/tests/test.ts +++ /dev/null @@ -1,92 +0,0 @@ -import { expect, test, type Page } from '@playwright/test'; -import { Kysely, PostgresDialect, sql } from 'kysely'; -import { dbConfig } from './config'; -import pg from 'pg'; - -type UserCredentials = { - email: string; - password: string; -}; - -const MAINTAINER: UserCredentials = { - email: 'master@example.com', - password: 'longEnough1' -}; - -const ENTREPENEUR: UserCredentials = { - email: 'taxi@example.com', - password: 'longEnough2' -}; - -test.describe.configure({ mode: 'serial' }); - -async function login(page: Page, credentials: UserCredentials) { - await page.goto('/login'); - await expect(page.getByRole('heading', { name: 'Login' })).toBeVisible(); - await page.getByLabel('Email').fill(credentials.email); - await page.getByLabel('Password').fill(credentials.password); - await page.getByRole('button', { name: 'Login' }).click(); -} - -async function signup(page: Page, credentials: UserCredentials) { - await page.goto('/signup'); - await expect(page.getByRole('heading', { name: 'Neuen Account erstellen' })).toBeVisible(); - await page.getByLabel('Email').fill(credentials.email); - await page.getByLabel('Password').fill(credentials.password); - await page.getByRole('button', { name: 'Account erstellen' }).click(); - await expect( - page.getByRole('heading', { name: 'Willkommen beim Projekt PrimaÖV!' }) - ).toBeVisible(); -} - -test('signup maintainer', async ({ page }) => { - await signup(page, MAINTAINER); - const db = new Kysely({ - dialect: new PostgresDialect({ - pool: new pg.Pool({ ...dbConfig, database: 'prima' }) - }) - }); - await sql`UPDATE auth_user SET is_maintainer = true WHERE email = 'master@example.com'`.execute( - db - ); - db.destroy(); -}); - -test('signup taxi', async ({ page }) => { - await signup(page, ENTREPENEUR); -}); - -test('activate taxi', async ({ page }) => { - await login(page, MAINTAINER); - await expect(page.getByRole('heading', { name: 'Unternehmer freischalten' })).toBeVisible(); - await page.getByLabel('Email').fill(ENTREPENEUR.email); - await page.getByRole('button', { name: 'Unternehmer freischalten' }).click(); - await expect(page.getByText('Freischalten erfolgreich!')).toBeVisible(); -}); - -test('taxi set base data', async ({ page }) => { - await login(page, ENTREPENEUR); - - await expect(page.getByRole('heading', { name: 'Stammdaten ihres Unternehmens' })).toBeVisible(); - - await page.getByLabel('Name').fill('Mein Taxi Unternehmen'); - await page.getByLabel('Unternehmenssitz').fill('Wilhelm-Busch-Straße 3, 02625 Bautzen'); - await page.getByLabel('Pflichtfahrgebiet').selectOption({ label: 'Altkreis Bautzen' }); - await page.getByLabel('Gemeinde').selectOption({ label: 'Bautzen' }); - await page.getByRole('button', { name: 'Übernehmen' }).click(); - - await expect(page.getByText('Aktualisierung erfolgreich.')).toBeVisible(); - - const checkData = async () => { - await expect(page.getByLabel('Name')).toHaveValue('Mein Taxi Unternehmen'); - await expect(page.getByLabel('Unternehmenssitz')).toHaveValue( - 'Wilhelm-Busch-Straße 3, 02625 Bautzen' - ); - await expect(page.getByLabel('Pflichtfahrgebiet')).toHaveValue('1' /* Altkreis Bautzen */); - await expect(page.getByLabel('Gemeinde')).toHaveValue('7' /* Bautzen */); - }; - - await checkData(); - await page.reload(); - await checkData(); -}); diff --git a/tests/utils.ts b/tests/utils.ts new file mode 100644 index 00000000..4f695a3e --- /dev/null +++ b/tests/utils.ts @@ -0,0 +1,35 @@ +import { expect, type Page } from '@playwright/test'; + +export type UserCredentials = { + email: string; + password: string; +}; + +export const MAINTAINER: UserCredentials = { + email: 'master@example.com', + password: 'longEnough1' +}; + +export const ENTREPENEUR: UserCredentials = { + email: 'taxi@example.com', + password: 'longEnough2' +}; + +export async function login(page: Page, credentials: UserCredentials) { + await page.goto('/login'); + await expect(page.getByRole('heading', { name: 'Login' })).toBeVisible(); + await page.getByLabel('Email').fill(credentials.email); + await page.getByLabel('Password').fill(credentials.password); + await page.getByRole('button', { name: 'Login' }).click(); +} + +export async function signup(page: Page, credentials: UserCredentials) { + await page.goto('/signup'); + await expect(page.getByRole('heading', { name: 'Neuen Account erstellen' })).toBeVisible(); + await page.getByLabel('Email').fill(credentials.email); + await page.getByLabel('Password').fill(credentials.password); + await page.getByRole('button', { name: 'Account erstellen' }).click(); + await expect( + page.getByRole('heading', { name: 'Willkommen beim Projekt PrimaÖV!' }) + ).toBeVisible(); +}