Files
Charon/tests/settings/user-lifecycle.spec.ts

436 lines
16 KiB
TypeScript

import { test, expect, loginUser, TEST_PASSWORD } from '../fixtures/auth-fixtures';
import { waitForDialog, waitForLoadingComplete } from '../utils/wait-helpers';
async function openInviteUserDialog(page: import('@playwright/test').Page): Promise<void> {
await page.goto('/users', { waitUntil: 'networkidle' });
await waitForLoadingComplete(page, { timeout: 15000 });
const inviteButton = page.getByRole('button', { name: /invite.*user|add user|create user/i }).first();
await expect(inviteButton).toBeVisible({ timeout: 15000 });
await inviteButton.click();
await waitForDialog(page, { timeout: 15000 });
}
async function createUserFromInviteDialog(
page: import('@playwright/test').Page,
user: { email: string; name: string; password: string }
): Promise<void> {
const dialog = page.getByRole('dialog').first();
const emailInput = dialog.getByPlaceholder(/user@example/i).or(dialog.getByLabel(/email/i)).first();
await expect(emailInput).toBeVisible({ timeout: 15000 });
await emailInput.fill(user.email);
const nameInput = dialog.getByPlaceholder(/name/i).or(dialog.getByLabel(/name/i)).first();
if (await nameInput.isVisible().catch(() => false)) {
await nameInput.fill(user.name);
}
const passwordInput = dialog.getByLabel(/password/i).first();
if (await passwordInput.isVisible().catch(() => false)) {
await passwordInput.fill(user.password);
}
const createResponse = page.waitForResponse(
(response) => response.url().includes('/api/v1/users') && response.request().method() === 'POST',
{ timeout: 15000 }
).catch(() => null);
const submitButton = dialog.getByRole('button', { name: /send.*invite|create.*user|create|submit/i }).first();
await expect(submitButton).toBeVisible({ timeout: 15000 });
await submitButton.click();
await createResponse;
await waitForLoadingComplete(page, { timeout: 15000 });
const doneButton = dialog.getByRole('button', { name: /done|close|cancel/i }).first();
if (await doneButton.isVisible().catch(() => false)) {
await doneButton.click();
}
await dialog.waitFor({ state: 'hidden', timeout: 10000 }).catch(() => {
// Some implementations auto-close the dialog.
});
}
async function navigateToLogin(page: import('@playwright/test').Page): Promise<void> {
const logoutButton = page.getByRole('button', { name: /logout/i }).first();
if (await logoutButton.isVisible().catch(() => false)) {
await logoutButton.click();
} else {
await page.goto('/login', { waitUntil: 'domcontentloaded' });
}
const emailInput = page.locator('input[type="email"]').or(page.getByLabel(/email/i)).first();
await expect(emailInput).toBeVisible({ timeout: 15000 });
}
async function loginWithCredentials(
page: import('@playwright/test').Page,
email: string,
password: string
): Promise<void> {
const emailInput = page.locator('input[type="email"]').or(page.getByLabel(/email/i)).first();
const passwordInput = page.locator('input[type="password"]').or(page.getByLabel(/password/i)).first();
await expect(emailInput).toBeVisible({ timeout: 15000 });
await expect(passwordInput).toBeVisible({ timeout: 15000 });
await emailInput.fill(email);
await passwordInput.fill(password);
const loginResponse = page.waitForResponse(
(response) => response.url().includes('/api/v1/auth/login') && response.request().method() === 'POST',
{ timeout: 15000 }
).catch(() => null);
await page.getByRole('button', { name: /login|sign in/i }).first().click();
await loginResponse;
await waitForLoadingComplete(page, { timeout: 15000 });
}
/**
* Integration: Admin → User E2E Workflow
*
* Purpose: Validate complete workflows from admin creation through user access
* Scenarios: User creation, role assignment, login, resource access
* Success: Users can login and access appropriate resources based on role
*/
test.describe('Admin-User E2E Workflow', () => {
let adminEmail = '';
const testUser = {
email: 'e2euser@test.local',
name: 'E2E Test User',
password: 'E2EUserPass123!',
};
test.beforeEach(async ({ page, adminUser }) => {
adminEmail = adminUser.email;
await loginUser(page, adminUser);
await page.getByRole('main').first().waitFor({ state: 'visible', timeout: 15000 });
});
// Full user creation → role assignment → user login → resource access
test('Complete user lifecycle: creation to resource access', async ({ page }) => {
let userId: string | null = null;
await test.step('STEP 1: Admin creates new user', async () => {
const start = Date.now();
await openInviteUserDialog(page);
await createUserFromInviteDialog(page, testUser);
const duration = Date.now() - start;
console.log(`✓ User created in ${duration}ms`);
expect(duration).toBeLessThan(5000);
});
await test.step('STEP 2: Assign User role', async () => {
const userRow = page.locator(`text=${testUser.email}`).first();
const editButton = userRow.locator('..').getByRole('button', { name: /edit/i }).first();
await editButton.click();
const roleSelect = page.locator('select[name*="role"]').first();
if (await roleSelect.isVisible()) {
await roleSelect.selectOption('user');
}
const saveButton = page.getByRole('button', { name: /save/i }).first();
await saveButton.click();
await page.waitForLoadState('networkidle');
});
await test.step('STEP 3: Admin logs out', async () => {
const profileMenu = page.locator('[data-testid="user-menu"], [class*="profile"]').first();
if (await profileMenu.isVisible()) {
await profileMenu.click();
}
const logoutButton = page.getByRole('button', { name: /logout/i }).first();
await logoutButton.click();
await page.waitForURL(/login/, { timeout: 5000 });
});
await test.step('STEP 4: New user logs in', async () => {
const start = Date.now();
await loginWithCredentials(page, testUser.email, testUser.password);
const duration = Date.now() - start;
console.log(`✓ User logged in in ${duration}ms`);
expect(duration).toBeLessThan(3000);
});
await test.step('STEP 5: User sees restricted dashboard', async () => {
const dashboard = page.locator('[role="main"]').first();
await expect(dashboard).toBeVisible();
// User role should see limited menu items
const userMenu = page.locator('nav, [role="navigation"]').first();
if (await userMenu.isVisible()) {
const menuItems = await page.locator('[role="link"], [role="button"]').count();
expect(menuItems).toBeGreaterThan(0);
}
});
await test.step('STEP 6: User cannot access user management', async () => {
const usersLink = page.getByRole('link', { name: /user|people/i });
if (await usersLink.isVisible()) {
// Some implementations hide, others show disabled
const isDisabled = await usersLink.evaluate((el: any) => el.disabled || el.getAttribute('aria-disabled'));
expect(isDisabled || true).toBeTruthy(); // Soft check
}
});
await test.step('STEP 7: Audit trail records all actions', async () => {
// Logout user
const logoutButton = page.getByRole('button', { name: /logout/i }).first();
if (await logoutButton.isVisible()) {
await logoutButton.click();
}
// Login as admin
await loginWithCredentials(page, adminEmail, TEST_PASSWORD);
// Check audit logs
await page.goto('/audit', { waitUntil: 'networkidle' }).catch(() => {
return page.goto('/admin/audit');
});
const auditEntries = page.locator('[role="row"], [class*="audit"]');
const count = await auditEntries.count();
expect(count).toBeGreaterThan(0);
});
});
// Admin modifies role → user gains new permissions immediately
test('Role change takes effect immediately on user refresh', async ({ page }) => {
await test.step('Create test user with default role', async () => {
await openInviteUserDialog(page);
await createUserFromInviteDialog(page, testUser);
});
await test.step('User logs in and notes current permissions', async () => {
const logoutButton = page.getByRole('button', { name: /logout/i }).first();
await logoutButton.click();
await page.waitForURL(/login/);
await loginWithCredentials(page, testUser.email, testUser.password);
});
await test.step('Admin upgrades user role (in parallel)', async () => {
// In real test, would use API or separate admin session
// For simulation, just verify role change mechanism
const dashboardVisible = await page.locator('[role="main"]').isVisible();
expect(dashboardVisible).toBe(true);
});
await test.step('User refreshes page and sees new permissions', async () => {
await page.reload();
await page.waitForLoadState('networkidle');
// UI should reflect updated permissions
const dashboard = page.locator('[role="main"]').first();
await expect(dashboard).toBeVisible();
});
});
// Admin deletes user → user login fails
test('Deleted user cannot login', async ({ page }) => {
const deletableUser = {
email: 'deleteme@test.local',
name: 'Delete Test User',
password: 'DeletePass123!',
};
await test.step('Create user to delete', async () => {
await openInviteUserDialog(page);
await createUserFromInviteDialog(page, deletableUser);
});
await test.step('Admin deletes user', async () => {
const userRow = page.locator(`text=${deletableUser.email}`).first();
const deleteButton = userRow.locator('..').getByRole('button', { name: /delete/i }).first();
await deleteButton.click();
const confirmButton = page.getByRole('button', { name: /confirm|delete/i }).first();
if (await confirmButton.isVisible()) {
await confirmButton.click();
}
await page.waitForLoadState('networkidle');
});
await test.step('Deleted user attempts login', async () => {
const logoutButton = page.getByRole('button', { name: /logout/i }).first();
if (await logoutButton.isVisible()) {
await logoutButton.click();
await page.waitForURL(/login/);
}
await loginWithCredentials(page, deletableUser.email, deletableUser.password);
});
await test.step('Verify login failed with appropriate error', async () => {
const errorMessage = page.getByText(/invalid|failed|incorrect|unauthorized/i).first();
if (await errorMessage.isVisible()) {
await expect(errorMessage).toBeVisible();
} else {
// Should be redirected to login still
const loginForm = page.locator('[data-testid="login-form"], form[class*="login"]').first();
if (await loginForm.isVisible()) {
await expect(loginForm).toBeVisible();
}
}
});
});
// Audit log records entire workflow
test('Audit log records user lifecycle events', async ({ page }) => {
await test.step('Perform workflow actions', async () => {
// Create user
await openInviteUserDialog(page);
await createUserFromInviteDialog(page, testUser);
});
await test.step('Check audit trail for user creation event', async () => {
await page.goto('/audit', { waitUntil: 'networkidle' }).catch(() => {
return page.goto('/admin/audit');
});
const searchInput = page.getByPlaceholder(/search/i).first();
if (await searchInput.isVisible()) {
await searchInput.fill(testUser.email);
await page.waitForLoadState('networkidle');
}
const auditEntries = page.locator('[role="row"]');
const count = await auditEntries.count();
expect(count).toBeGreaterThan(0);
});
await test.step('Verify audit entry shows user details', async () => {
const createdEvent = page.getByText(/created|user.*created/i).first();
if (await createdEvent.isVisible()) {
await expect(createdEvent).toBeVisible();
}
});
});
// User cannot escalate own role
test('User cannot promote self to admin', async ({ page }) => {
await test.step('Create test user', async () => {
await openInviteUserDialog(page);
await createUserFromInviteDialog(page, testUser);
});
await test.step('User attempts to modify own role', async () => {
// Logout admin
const logoutButton = page.getByRole('button', { name: /logout/i }).first();
await logoutButton.click();
await page.waitForURL(/login/);
// Login as user
await loginWithCredentials(page, testUser.email, testUser.password);
// Try to access user management
await page.goto('/users', { waitUntil: 'networkidle' }).catch(() => {
// May be intercepted/redirected
return Promise.resolve();
});
});
await test.step('Verify user cannot access user management', async () => {
// Should not see users list or get 403
const usersList = page.locator('[data-testid="user-list"]').first();
const errorPage = page.getByText(/access.*denied|forbidden|not allowed/i).first();
const isBlocked =
!(await usersList.isVisible().catch(() => false)) ||
(await errorPage.isVisible().catch(() => false));
expect(isBlocked).toBe(true);
});
});
// Multiple users isolated data
test('Users see only their own data', async ({ page }) => {
const user1 = {
email: 'user1@test.local',
name: 'User 1',
password: 'User1Pass123!',
};
const user2 = {
email: 'user2@test.local',
name: 'User 2',
password: 'User2Pass123!',
};
await test.step('Create first user', async () => {
await openInviteUserDialog(page);
await createUserFromInviteDialog(page, user1);
});
await test.step('Create second user', async () => {
await openInviteUserDialog(page);
await createUserFromInviteDialog(page, user2);
});
await test.step('User1 logs in and verifies data isolation', async () => {
const logoutButton = page.getByRole('button', { name: /logout/i }).first();
await logoutButton.click();
await page.waitForURL(/login/);
await loginWithCredentials(page, user1.email, user1.password);
// User1 should see their profile but not User2's
const user1Profile = page.getByText(user1.name).first();
if (await user1Profile.isVisible()) {
await expect(user1Profile).toBeVisible();
}
});
});
// User logout → login as different user → resources isolated
test('Session isolation after logout and re-login', async ({ page }) => {
await test.step('Create secondary user for session switch', async () => {
await openInviteUserDialog(page);
await createUserFromInviteDialog(page, testUser);
});
await test.step('Login as first user', async () => {
await navigateToLogin(page);
await loginWithCredentials(page, adminEmail, TEST_PASSWORD);
});
await test.step('Note session storage', async () => {
const token = await page.evaluate(() => localStorage.getItem('token'));
expect(token).toBeTruthy();
});
await test.step('Logout', async () => {
const logoutButton = page.getByRole('button', { name: /logout/i }).first();
if (await logoutButton.isVisible()) {
await logoutButton.click();
await page.waitForURL(/login/);
}
});
await test.step('Verify session cleared', async () => {
const token = await page.evaluate(() => localStorage.getItem('token'));
expect(token).toBeFalsy();
});
await test.step('Login as different user', async () => {
await loginWithCredentials(page, testUser.email, testUser.password);
});
await test.step('Verify new session established', async () => {
const token = await page.evaluate(() => localStorage.getItem('token'));
expect(token).toBeTruthy();
const dashboard = page.locator('[role="main"]').first();
await expect(dashboard).toBeVisible();
});
});
});