73 lines
2.2 KiB
TypeScript
73 lines
2.2 KiB
TypeScript
import { expect, type APIRequestContext, type Page } from '@playwright/test'
|
|
import { gotoAppRoute } from './navigation'
|
|
|
|
export type LiveAuthHealth = {
|
|
available: boolean
|
|
fallbackEnabled: boolean
|
|
status: number | null
|
|
reason: string | null
|
|
}
|
|
|
|
export function isLiveAuthSmokeEnabled() {
|
|
return /^(1|true|yes)$/i.test(String(process.env.PLAYWRIGHT_LIVE_AUTH || '').trim())
|
|
}
|
|
|
|
export function getLiveAuthSmokeCredentials() {
|
|
return {
|
|
email: String(process.env.AUTH_SMOKE_EMAIL || 'ci-smoke@scriptoriumai.io').trim(),
|
|
password: String(process.env.AUTH_SMOKE_PASSWORD || 'smoke-password-123'),
|
|
name: String(process.env.AUTH_SMOKE_NAME || 'Smoke Operator').trim(),
|
|
}
|
|
}
|
|
|
|
export async function probeLiveAuthHealth(request: APIRequestContext): Promise<LiveAuthHealth> {
|
|
try {
|
|
const response = await request.get('/api/auth/health', {
|
|
failOnStatusCode: false,
|
|
})
|
|
|
|
if (!response.ok()) {
|
|
return {
|
|
available: false,
|
|
fallbackEnabled: false,
|
|
status: response.status(),
|
|
reason: `auth_health_http_${response.status()}`,
|
|
}
|
|
}
|
|
|
|
const payload = await response.json().catch(() => null) as {
|
|
fallback?: { enabled?: boolean } | null
|
|
} | null
|
|
|
|
return {
|
|
available: true,
|
|
fallbackEnabled: payload?.fallback?.enabled !== false,
|
|
status: response.status(),
|
|
reason: null,
|
|
}
|
|
} catch (error) {
|
|
return {
|
|
available: false,
|
|
fallbackEnabled: false,
|
|
status: null,
|
|
reason: error instanceof Error ? error.message : String(error),
|
|
}
|
|
}
|
|
}
|
|
|
|
export async function signInThroughLiveFallback(page: Page, options?: { nextPath?: string }) {
|
|
const credentials = getLiveAuthSmokeCredentials()
|
|
const nextPath = options?.nextPath || '/app'
|
|
const target = `/login?next=${encodeURIComponent(nextPath)}`
|
|
|
|
await gotoAppRoute(page, target, {
|
|
waitUntil: 'domcontentloaded',
|
|
timeoutMs: 45_000,
|
|
})
|
|
|
|
await expect(page.getByRole('heading', { level: 1, name: 'Welcome back' })).toBeVisible()
|
|
await page.getByPlaceholder('you@company.com').fill(credentials.email)
|
|
await page.getByPlaceholder('Your password').fill(credentials.password)
|
|
await page.getByRole('button', { name: 'Sign In' }).click()
|
|
await page.waitForURL(/\/app(?:[/?#]|$)/, { timeout: 45_000 })
|
|
}
|