1
0
mirror of https://github.com/quay/quay.git synced 2026-01-26 06:21:37 +03:00
Files
quay/web/playwright/global-setup.ts
jbpratt 0638c19183 test(web): migrate more tests to playwright (#4767)
* test(web): migrate repository-autopruning to Playwright

Consolidate 17 Cypress tests into 6 Playwright tests:
- policy lifecycle (create, update, delete)
- policy with tag pattern filter
- multiple policies lifecycle
- namespace policy display in repository settings
- registry policy display
- error handling (load failure)

Uses @feature:AUTO_PRUNE tag for automatic test skipping.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* test: enable features by default

Signed-off-by: Brady Pratt <bpratt@redhat.com>

* test(web): migrate create-account.cy.ts to Playwright

Migrates the Create Account Cypress tests to Playwright following
the MIGRATION.md guide:

- Consolidates 10 Cypress tests into 6 focused Playwright tests
- Uses real API calls instead of mocks
- Adds data-testid attributes to CreateAccount component
- Uses @feature:MAILING and @feature:QUOTA_MANAGEMENT tags
  to skip tests when features are not enabled
- Creates custom fixtures for unauthenticated page access
- Implements proper user cleanup after tests

Tests:
- form validation prevents invalid submissions
- creates account with valid inputs and redirects
- shows error for existing username
- navigates to signin page via link
- shows verification message (requires MAILING)
- redirects to updateuser (requires QUOTA_MANAGEMENT)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Signed-off-by: Brady Pratt <bpratt@redhat.com>

* chore(dev) add Mailpit for local email testing

Add Mailpit email testing server to the local development environment
to enable testing of FEATURE_MAILING functionality with Playwright.

Changes:
- Add mailpit service to docker-compose.yaml (ports 8025/1025)
- Enable FEATURE_MAILING and configure SMTP settings in config.yaml
- Add mailpit utilities to Playwright fixtures (getEmails, clearInbox,
  waitForEmail, getEmailBody, isAvailable)

Usage:
  podman-compose up mailpit -d
  # Access Web UI at http://localhost:8025

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* test(web) use mailpit helpers for email confirmation support

Test updates:
- "creates account and redirects to organization" now confirms email
- "redirects to updateuser when user has prompts" now confirms email
- Tests detect FEATURE_MAILING at runtime and adapt accordingly
- Email search uses recipient address for parallel test safety

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Signed-off-by: Brady Pratt <bpratt@redhat.com>

* test(web): use mailpit for email notification test

Replace mocked email authorization with real Mailpit verification
in the notifications.spec.ts test.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Signed-off-by: Brady Pratt <bpratt@redhat.com>

* test(web): mock user response in cypress test

this broke when mailing was enabled

Signed-off-by: Brady Pratt <bpratt@redhat.com>

---------

Signed-off-by: Brady Pratt <bpratt@redhat.com>
Co-authored-by: Claude <noreply@anthropic.com>
2026-01-05 09:47:29 -08:00

152 lines
4.6 KiB
TypeScript

/**
* Playwright Global Setup
*
* This runs ONCE before all tests to set up the test environment.
* Creates initial test users that will be used across all test runs.
*
* Users created:
* - testuser: Regular user for standard tests
* - admin: Superuser for admin tests
* - readonly: User with read-only access (if applicable)
*/
import {chromium, FullConfig} from '@playwright/test';
import {API_URL} from './utils/config';
import {ApiClient} from './utils/api';
import {mailpit} from './utils/mailpit';
export const TEST_USERS = {
// Admin/superuser for admin operations
admin: {
username: 'admin',
password: 'password',
email: 'admin@example.com',
},
// Regular user for standard operations
user: {
username: 'testuser',
password: 'password',
email: 'testuser@example.com',
},
// Readonly user for permission tests
readonly: {
username: 'readonly',
password: 'password',
email: 'readonly@example.com',
},
} as const;
async function globalSetup(config: FullConfig) {
const baseURL = config.projects[0].use.baseURL || 'http://localhost:9000';
console.log(
`[Global Setup] Starting with baseURL: ${baseURL}, apiURL: ${API_URL}`,
);
let browser = null;
try {
browser = await chromium.launch();
// Track failures to report at the end
const failures: string[] = [];
// Check if FEATURE_MAILING is enabled
let mailingEnabled = false;
try {
const configResponse = await fetch(`${API_URL}/config`);
if (configResponse.ok) {
const quayConfig = await configResponse.json();
mailingEnabled = quayConfig?.features?.MAILING === true;
}
} catch {
console.log(
'[Global Setup] Could not fetch config, assuming mailing disabled',
);
}
// Check if Mailpit is available when mailing is enabled
const mailpitAvailable = mailingEnabled && (await mailpit.isAvailable());
if (mailingEnabled) {
console.log(
`[Global Setup] FEATURE_MAILING enabled, Mailpit ${
mailpitAvailable ? 'available' : 'NOT available'
}`,
);
if (mailpitAvailable) {
await mailpit.clearInbox();
}
}
// Create test users (skip if they already exist)
// Each user creation requires a fresh context and CSRF token
for (const [role, user] of Object.entries(TEST_USERS)) {
// Create a fresh context for each user to avoid CSRF token issues
const userContext = await browser.newContext();
const userRequest = userContext.request;
try {
console.log(`[Global Setup] Creating ${role} user: ${user.username}`);
const api = new ApiClient(userRequest);
await api.createUser(user.username, user.password, user.email);
console.log(`[Global Setup] Created ${role} user: ${user.username}`);
// Verify email if mailing is enabled and Mailpit is available
if (mailpitAvailable) {
console.log(
`[Global Setup] Verifying email for ${role} user: ${user.email}`,
);
const confirmLink = await mailpit.waitForConfirmationLink(user.email);
if (confirmLink) {
const page = await userContext.newPage();
await page.goto(confirmLink);
await page.close();
console.log(
`[Global Setup] Email verified for ${role} user: ${user.username}`,
);
} else {
console.warn(
`[Global Setup] No confirmation email found for ${user.email}`,
);
}
}
} catch (error) {
const errorMessage = String(error);
// User might already exist (400 error with "already exists")
if (
errorMessage.includes('already exists') ||
errorMessage.includes('already taken')
) {
console.log(
`[Global Setup] ${role} user already exists: ${user.username}`,
);
} else {
console.error(`[Global Setup] Error creating ${role} user: ${error}`);
failures.push(`${role} (${user.username}): ${error}`);
}
} finally {
await userContext.close();
}
}
// Fail if any required users could not be created
if (failures.length > 0) {
throw new Error(
`Global setup failed: Could not create required test users:\n - ${failures.join(
'\n - ',
)}`,
);
}
console.log('[Global Setup] Complete');
} catch (error) {
console.error('[Global Setup] Failed:', error);
throw error;
} finally {
if (browser) {
await browser.close();
}
}
}
export default globalSetup;