LibreChat/e2e/specs/user-authentication.spec.ts

182 lines
5.9 KiB
TypeScript
Raw Normal View History

import { expect, test } from '@playwright/test';
const baseURL = 'http://localhost:3090';
test.describe('User Authentication Flow', () => {
test.beforeEach(async ({ page }) => {
// Clear any existing auth state for auth tests
await page.context().clearCookies();
await page.context().clearPermissions();
});
test('Should display login page with all required elements', async ({ page }) => {
await page.goto(`${baseURL}/login`);
// Check page title and main elements
await expect(page).toHaveTitle(/LibreChat/);
// Check if we're redirected or if login page is shown
const currentUrl = page.url();
if (currentUrl.includes('/login')) {
await expect(page.getByText('Welcome back')).toBeVisible();
// Check form elements
await expect(page.getByLabel('Email')).toBeVisible();
await expect(page.getByLabel('Password')).toBeVisible();
await expect(page.getByRole('button', { name: 'Continue' })).toBeVisible();
// Check navigation links
await expect(page.getByText('Sign up')).toBeVisible();
} else {
// User might already be authenticated, which is also valid
console.log('User appears to be already authenticated');
}
});
test('Should display registration page with all required elements', async ({ page }) => {
await page.goto(`${baseURL}/register`);
// Check page title and main elements
await expect(page).toHaveTitle(/LibreChat/);
const currentUrl = page.url();
if (currentUrl.includes('/register')) {
// Wait for the main heading to appear first, which indicates the page has loaded
await expect(page.getByText('Create your account')).toBeVisible({ timeout: 15000 });
// Wait for the form to be fully rendered by checking for email field first
await expect(page.getByLabel('Email')).toBeVisible({ timeout: 10000 });
// Check form elements
await expect(page.getByLabel('Full name')).toBeVisible();
await expect(page.getByLabel('Username (optional)')).toBeVisible();
await expect(page.getByTestId('password')).toBeVisible();
await expect(page.getByTestId('confirm_password')).toBeVisible();
// Wait for the Submit registration button to be visible (using correct accessible name)
await expect(page.getByRole('button', { name: 'Submit registration' })).toBeVisible({
timeout: 10000,
});
// Check navigation link
await expect(page.getByText('Login')).toBeVisible();
} else {
console.log('User appears to be already authenticated');
}
});
test('Should validate registration form inputs', async ({ page }) => {
await page.goto(`${baseURL}/register`);
const currentUrl = page.url();
if (!currentUrl.includes('/register')) {
test.skip();
return;
}
// Wait for the form to be fully loaded before trying to interact
await expect(page.getByRole('button', { name: 'Submit registration' })).toBeVisible({
timeout: 10000,
});
// Try to submit empty form
await page.getByRole('button', { name: 'Submit registration' }).click();
// Check for validation errors (may appear after attempting to submit)
const errorChecks = [
page.getByText('Name is required'),
page.getByText('Email is required'),
page.getByText('Password is required'),
];
// Wait for at least one validation error to appear
let errorFound = false;
for (const errorCheck of errorChecks) {
try {
await expect(errorCheck).toBeVisible({ timeout: 2000 });
errorFound = true;
break;
} catch {
// Continue checking other errors
}
}
if (!errorFound) {
console.log('No validation errors found - form might have different validation behavior');
}
});
test('Should validate login form inputs', async ({ page }) => {
await page.goto(`${baseURL}/login`);
const currentUrl = page.url();
if (!currentUrl.includes('/login')) {
test.skip();
return;
}
// Try to submit empty form
await page.getByRole('button', { name: 'Continue' }).click();
// Check for validation errors with more flexible timeout
try {
await expect(page.getByText('Email is required')).toBeVisible({ timeout: 3000 });
} catch {
console.log('Email validation message not found or different text');
}
try {
await expect(page.getByText('Password is required')).toBeVisible({ timeout: 3000 });
} catch {
console.log('Password validation message not found or different text');
}
});
test('Should allow navigation between auth pages', async ({ page }) => {
// Start at login
await page.goto(`${baseURL}/login`);
const loginUrl = page.url();
if (!loginUrl.includes('/login')) {
test.skip();
return;
}
// Navigate to register
await page.getByText('Sign up').click();
await expect(page).toHaveURL(`${baseURL}/register`);
// Navigate back to login
await page.getByText('Login').click();
await expect(page).toHaveURL(`${baseURL}/login`);
});
test('Should redirect to home when accessing auth pages while authenticated', async ({
page,
}) => {
// This test checks if authenticated users are redirected from auth pages
try {
await page.goto(`${baseURL}/c/new`);
const isAuthenticated = await page
.locator('[data-testid="nav-user"]')
.isVisible({ timeout: 3000 });
if (isAuthenticated) {
// Try to visit login page while authenticated
await page.goto(`${baseURL}/login`);
// Should redirect to main app
await expect(page).toHaveURL(`${baseURL}/c/new`);
// Try to visit register page while authenticated
await page.goto(`${baseURL}/register`);
// Should redirect to main app
await expect(page).toHaveURL(`${baseURL}/c/new`);
} else {
test.skip();
}
} catch (error) {
test.skip();
}
});
});