import { expect, test } from '@playwright/test';
test('redirect to login when unauthenticated', async ({ page }) => {
await page.goto('/');
expect(await page.textContent('h1')).toBe('chartered ✈️');
expect(await page.textContent('button')).toBe('Login');
});
test('register button takes user to register page', async ({ page }) => {
await page.goto('/');
await page.locator('button:has-text("Register")').click();
await expect(page).toHaveURL(/.*register/);
expect(await page.textContent('h1')).toBe('chartered ✈️');
expect(await page.textContent('button')).toBe('Register');
});
test('can successfully register and login', async ({ page }) => {
const username = Math.random().toString(36).substring(2, 7);
const password = 'aaaaaaaa';
await page.goto('/');
await page.locator('button:has-text("Register")').click();
await page.locator('input[id="username"]').fill(username);
await page.locator('input[id="password"]').fill(password);
await page.locator('button:has-text("Register")').click();
await expect(page.locator('[role="alert"]')).toHaveCount(0);
await expect(page).toHaveURL(/.*login/);
await page.locator('input[id="username"]').fill(username);
await page.locator('input[id="password"]').fill(password);
await page.locator('text=Username Password Login >> button').click();
await expect(page).not.toHaveURL(/.*login/);
expect(await page.textContent('h1')).toBe('Welcome to Chartered.');
});
test('can create an organisation and add a user to it', async ({ page }) => {
const username1 = Math.random().toString(36).substring(2, 7);
const username2 = Math.random().toString(36).substring(2, 7);
const password = 'aaaaaaaa';
await page.goto('/');
await page.locator('button:has-text("Register")').click();
await page.locator('input[id="username"]').fill(username1);
await page.locator('input[id="password"]').fill(password);
await page.locator('button:has-text("Register")').click();
await expect(page).toHaveURL(/.*login/);
await page.locator('button:has-text("Register")').click();
await page.locator('input[id="username"]').fill(username2);
await page.locator('input[id="password"]').fill(password);
await page.locator('button:has-text("Register")').click();
await expect(page).toHaveURL(/.*login/);
await page.locator('input[id="username"]').fill(username1);
await page.locator('input[id="password"]').fill(password);
await page.locator('text=Username Password Login >> button').click();
await page.locator('text=Organisations').click();
await page.locator('text=+ Create').click();
const organisation = Math.random().toString(36).substring(2, 7);
await page.locator('input[id="name"]').fill(organisation);
await page.locator('button:has-text("Create")').click();
await page.locator(`text=${organisation}`).click();
await page.locator('main ul button:has-text("Members")').click();
await page.locator('input[placeholder="Start typing a username..."]').fill(username2);
await page.locator(`button:has-text("${username2}")`).click();
await page
.locator(`text=${username2} VISIBLE PUBLISH_VERSION YANK_VERSION MANAGE_USERS CREATE_CRATE Save >> button`)
.click();
await page.reload();
await page.locator('main ul button:has-text("Members")').click();
await expect(page.locator(`text=${username1}`)).toHaveCount(1);
await expect(page.locator(`text=${username2}`)).toHaveCount(1);
});