|
| 1 | +import { test as base, expect, Page, BrowserContext } from '@playwright/test'; |
| 2 | + |
| 3 | +import { KeycloakLoginPage } from '../helpers/oidc-auth'; |
| 4 | +import { getKeycloakAdminClient } from '../helpers/keycloak-admin'; |
| 5 | +import { loadTestState } from '../helpers/test-state'; |
| 6 | + |
| 7 | +/** |
| 8 | + * OIDC Test Fixtures |
| 9 | + * |
| 10 | + * Provides reusable fixtures for OIDC E2E tests with: |
| 11 | + * - Dynamic realm support (from global setup) |
| 12 | + * - Browser context isolation |
| 13 | + * - Automatic session cleanup |
| 14 | + */ |
| 15 | + |
| 16 | +export interface OIDCTestConfig { |
| 17 | + keycloakBaseUrl: string; |
| 18 | + keycloakRealm: string; |
| 19 | + openslidesBaseUrl: string; |
| 20 | + testUsers: { |
| 21 | + admin: { username: string; password: string; userId: number }; |
| 22 | + testuser: { username: string; password: string; userId: number }; |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +function getOIDCConfig(): OIDCTestConfig { |
| 27 | + // Get realm from test state (set by global setup) or fallback to env/default |
| 28 | + const state = loadTestState(); |
| 29 | + const realm = state?.realmName || process.env.KEYCLOAK_REALM || 'openslides'; |
| 30 | + |
| 31 | + return { |
| 32 | + keycloakBaseUrl: process.env.KEYCLOAK_URL || 'http://localhost:8180', |
| 33 | + keycloakRealm: realm, |
| 34 | + openslidesBaseUrl: process.env.BASE_URL || 'https://localhost:8000', |
| 35 | + testUsers: { |
| 36 | + admin: { username: 'admin', password: 'admin', userId: 1 }, |
| 37 | + testuser: { username: 'testuser', password: 'testpassword', userId: 2 } |
| 38 | + } |
| 39 | + }; |
| 40 | +} |
| 41 | + |
| 42 | +export interface OIDCFixtures { |
| 43 | + oidcConfig: OIDCTestConfig; |
| 44 | + oidcEnabledContext: BrowserContext; |
| 45 | + isolatedContext: BrowserContext; |
| 46 | + isolatedPage: Page; |
| 47 | + keycloakPage: KeycloakLoginPage; |
| 48 | + cleanupSession: () => Promise<void>; |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * OIDC is now configured via environment variables, not organization settings. |
| 53 | + * These functions are no-ops kept for test compatibility. |
| 54 | + * OIDC is enabled when the Traefik OIDC middleware is configured. |
| 55 | + */ |
| 56 | +async function enableOIDC(_context: BrowserContext): Promise<void> { |
| 57 | + // OIDC is enabled via Traefik middleware configuration, not organization settings. |
| 58 | + // This is a no-op for test compatibility. |
| 59 | +} |
| 60 | + |
| 61 | +async function disableOIDC(_context: BrowserContext): Promise<void> { |
| 62 | + // OIDC is enabled via Traefik middleware configuration, not organization settings. |
| 63 | + // This is a no-op for test compatibility. |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Extended test object with OIDC fixtures. |
| 68 | + */ |
| 69 | +export const test = base.extend<OIDCFixtures>({ |
| 70 | + oidcConfig: async ({}, use) => { |
| 71 | + await use(getOIDCConfig()); |
| 72 | + }, |
| 73 | + |
| 74 | + /** |
| 75 | + * Isolated browser context with clean state. |
| 76 | + * Cookies are cleared before and after use. |
| 77 | + */ |
| 78 | + isolatedContext: async ({ browser }, use) => { |
| 79 | + const context = await browser.newContext({ |
| 80 | + storageState: undefined, // Fresh state |
| 81 | + serviceWorkers: 'block' // Prevent SW interference |
| 82 | + }); |
| 83 | + |
| 84 | + // Clear any cookies that might exist |
| 85 | + await context.clearCookies(); |
| 86 | + |
| 87 | + await use(context); |
| 88 | + |
| 89 | + // Cleanup after test |
| 90 | + await context.clearCookies(); |
| 91 | + await context.close(); |
| 92 | + }, |
| 93 | + |
| 94 | + /** |
| 95 | + * Page with isolated context and cleared storage. |
| 96 | + */ |
| 97 | + isolatedPage: async ({ isolatedContext }, use) => { |
| 98 | + const page = await isolatedContext.newPage(); |
| 99 | + |
| 100 | + // Clear local/session storage on navigation |
| 101 | + await page.addInitScript(() => { |
| 102 | + localStorage.clear(); |
| 103 | + sessionStorage.clear(); |
| 104 | + }); |
| 105 | + |
| 106 | + await use(page); |
| 107 | + |
| 108 | + await page.close(); |
| 109 | + }, |
| 110 | + |
| 111 | + /** |
| 112 | + * Keycloak login page helper. |
| 113 | + */ |
| 114 | + keycloakPage: async ({ page, oidcConfig }, use) => { |
| 115 | + const keycloakPage = new KeycloakLoginPage(page, { |
| 116 | + keycloakBaseUrl: oidcConfig.keycloakBaseUrl, |
| 117 | + realm: oidcConfig.keycloakRealm, |
| 118 | + clientId: 'openslides-client' |
| 119 | + }); |
| 120 | + await use(keycloakPage); |
| 121 | + }, |
| 122 | + |
| 123 | + /** |
| 124 | + * Session cleanup helper that clears Keycloak sessions for test users. |
| 125 | + */ |
| 126 | + cleanupSession: async ({ oidcConfig }, use) => { |
| 127 | + const cleanup = async () => { |
| 128 | + try { |
| 129 | + const admin = getKeycloakAdminClient(); |
| 130 | + await admin.authenticate(); |
| 131 | + |
| 132 | + // Clear sessions for test users |
| 133 | + for (const userKey of ['admin', 'testuser'] as const) { |
| 134 | + const user = oidcConfig.testUsers[userKey]; |
| 135 | + await admin.clearUserSessionsByUsername(oidcConfig.keycloakRealm, user.username); |
| 136 | + } |
| 137 | + } catch (error) { |
| 138 | + console.warn('[OIDC Fixture] Session cleanup warning:', error); |
| 139 | + } |
| 140 | + }; |
| 141 | + |
| 142 | + await use(cleanup); |
| 143 | + |
| 144 | + // Auto-cleanup after test |
| 145 | + await cleanup(); |
| 146 | + } |
| 147 | +}); |
| 148 | + |
| 149 | +export { expect }; |
| 150 | + |
| 151 | +/** |
| 152 | + * Helper to wait for OIDC redirect to complete. |
| 153 | + */ |
| 154 | +export async function waitForOIDCRedirect(page: Page, options?: { timeout?: number }): Promise<void> { |
| 155 | + const timeout = options?.timeout || 30000; |
| 156 | + |
| 157 | + // Wait until we're no longer on the Keycloak login page |
| 158 | + await expect(page).not.toHaveURL(/login-actions/, { timeout }); |
| 159 | + |
| 160 | + // Wait until we're no longer on OpenSlides login page |
| 161 | + await expect(page).not.toHaveURL(/\/login$/, { timeout }); |
| 162 | +} |
| 163 | + |
| 164 | +/** |
| 165 | + * Helper to initiate OIDC login flow with robust button detection. |
| 166 | + */ |
| 167 | +export async function initiateOIDCFlow(page: Page): Promise<void> { |
| 168 | + await page.goto('/login'); |
| 169 | + await page.waitForLoadState('networkidle'); |
| 170 | + |
| 171 | + // Wait for Angular app to be ready |
| 172 | + await page.waitForSelector('os-login-mask', { |
| 173 | + state: 'visible', |
| 174 | + timeout: 15000 |
| 175 | + }); |
| 176 | + |
| 177 | + // Prioritized selector strategy (most specific to least) |
| 178 | + const ssoSelectors = [ |
| 179 | + '[data-testid="sso-login-button"]', // Preferred: explicit test ID |
| 180 | + '[data-cy="sso-login"]', // Alternative test attribute |
| 181 | + 'button:has-text("SSO")', // Text-based fallback |
| 182 | + 'button:has-text("OIDC")', // Alternative text |
| 183 | + 'button:has-text("Single Sign")' // Full text variant |
| 184 | + ]; |
| 185 | + |
| 186 | + for (const selector of ssoSelectors) { |
| 187 | + const button = page.locator(selector); |
| 188 | + const isVisible = await button.isVisible({ timeout: 2000 }).catch(() => false); |
| 189 | + |
| 190 | + if (isVisible) { |
| 191 | + console.log(`[OIDC Fixture] Found SSO button with selector: ${selector}`); |
| 192 | + await button.click(); |
| 193 | + return; |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + // Last resort: find button containing SSO-related text via regex |
| 198 | + const genericButton = page |
| 199 | + .locator('button') |
| 200 | + .filter({ |
| 201 | + hasText: /sso|oidc|single.?sign/i |
| 202 | + }) |
| 203 | + .first(); |
| 204 | + |
| 205 | + if (await genericButton.isVisible({ timeout: 2000 }).catch(() => false)) { |
| 206 | + console.log('[OIDC Fixture] Found SSO button via regex filter'); |
| 207 | + await genericButton.click(); |
| 208 | + return; |
| 209 | + } |
| 210 | + |
| 211 | + throw new Error( |
| 212 | + 'No SSO login button found. Ensure OIDC is enabled and the button has ' + |
| 213 | + 'data-testid="sso-login-button" attribute.' |
| 214 | + ); |
| 215 | +} |
0 commit comments