2025-03-04 17:25:46 -08:00
|
|
|
import { generateId } from 'ai';
|
|
|
|
|
import { getUnixTime } from 'date-fns';
|
2025-04-20 22:27:20 -07:00
|
|
|
import { test, expect, type Page } from '@playwright/test';
|
2025-03-04 17:25:46 -08:00
|
|
|
|
|
|
|
|
test.use({ storageState: { cookies: [], origins: [] } });
|
|
|
|
|
|
|
|
|
|
const testEmail = `test-${getUnixTime(new Date())}@playwright.com`;
|
|
|
|
|
const testPassword = generateId(16);
|
|
|
|
|
|
|
|
|
|
class AuthPage {
|
|
|
|
|
constructor(private page: Page) {}
|
|
|
|
|
|
|
|
|
|
async gotoLogin() {
|
|
|
|
|
await this.page.goto('/login');
|
|
|
|
|
await expect(this.page.getByRole('heading')).toContainText('Sign In');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async gotoRegister() {
|
|
|
|
|
await this.page.goto('/register');
|
|
|
|
|
await expect(this.page.getByRole('heading')).toContainText('Sign Up');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async register(email: string, password: string) {
|
|
|
|
|
await this.gotoRegister();
|
|
|
|
|
await this.page.getByPlaceholder('user@acme.com').click();
|
|
|
|
|
await this.page.getByPlaceholder('user@acme.com').fill(email);
|
|
|
|
|
await this.page.getByLabel('Password').click();
|
|
|
|
|
await this.page.getByLabel('Password').fill(password);
|
|
|
|
|
await this.page.getByRole('button', { name: 'Sign Up' }).click();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async login(email: string, password: string) {
|
|
|
|
|
await this.gotoLogin();
|
|
|
|
|
await this.page.getByPlaceholder('user@acme.com').click();
|
|
|
|
|
await this.page.getByPlaceholder('user@acme.com').fill(email);
|
|
|
|
|
await this.page.getByLabel('Password').click();
|
|
|
|
|
await this.page.getByLabel('Password').fill(password);
|
|
|
|
|
await this.page.getByRole('button', { name: 'Sign In' }).click();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async expectToastToContain(text: string) {
|
|
|
|
|
await expect(this.page.getByTestId('toast')).toContainText(text);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
test.describe
|
|
|
|
|
.serial('authentication', () => {
|
|
|
|
|
let authPage: AuthPage;
|
|
|
|
|
|
|
|
|
|
test.beforeEach(async ({ page }) => {
|
|
|
|
|
authPage = new AuthPage(page);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('redirect to login page when unauthenticated', async ({ page }) => {
|
|
|
|
|
await page.goto('/');
|
|
|
|
|
await expect(page.getByRole('heading')).toContainText('Sign In');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('register a test account', async ({ page }) => {
|
|
|
|
|
await authPage.register(testEmail, testPassword);
|
|
|
|
|
await expect(page).toHaveURL('/');
|
|
|
|
|
await authPage.expectToastToContain('Account created successfully!');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('register test account with existing email', async () => {
|
|
|
|
|
await authPage.register(testEmail, testPassword);
|
|
|
|
|
await authPage.expectToastToContain('Account already exists!');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('log into account', async ({ page }) => {
|
|
|
|
|
await authPage.login(testEmail, testPassword);
|
|
|
|
|
|
|
|
|
|
await page.waitForURL('/');
|
|
|
|
|
await expect(page).toHaveURL('/');
|
|
|
|
|
await expect(page.getByPlaceholder('Send a message...')).toBeVisible();
|
|
|
|
|
});
|
|
|
|
|
});
|