Fixed and updated E2E test suite. Added user credential registration tests. Coverage for backend and frontend.
This commit is contained in:
Vendored
+147
@@ -0,0 +1,147 @@
|
||||
import { test as base, type Page, type CDPSession } from '@playwright/test'
|
||||
import { existsSync, mkdirSync, writeFileSync, readFileSync } from 'fs'
|
||||
import { join, dirname } from 'path'
|
||||
import { fileURLToPath } from 'url'
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url))
|
||||
const coverageDir = join(__dirname, '..', '..', 'coverage-frontend')
|
||||
|
||||
// Check if frontend coverage is enabled
|
||||
const COLLECT_COVERAGE = process.env.COVERAGE === '1' || process.env.COVERAGE === 'true'
|
||||
|
||||
interface CoverageEntry {
|
||||
url: string
|
||||
scriptId: string
|
||||
source?: string
|
||||
functions: Array<{
|
||||
functionName: string
|
||||
ranges: Array<{
|
||||
startOffset: number
|
||||
endOffset: number
|
||||
count: number
|
||||
}>
|
||||
isBlockCoverage: boolean
|
||||
}>
|
||||
}
|
||||
|
||||
/**
|
||||
* Collect V8 JavaScript coverage from the page.
|
||||
*/
|
||||
async function startCoverage(page: Page): Promise<CDPSession | null> {
|
||||
if (!COLLECT_COVERAGE) return null
|
||||
|
||||
try {
|
||||
const cdp = await page.context().newCDPSession(page)
|
||||
await cdp.send('Profiler.enable')
|
||||
await cdp.send('Profiler.startPreciseCoverage', {
|
||||
callCount: true,
|
||||
detailed: true,
|
||||
})
|
||||
return cdp
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
async function stopCoverage(cdp: CDPSession | null, testName: string): Promise<void> {
|
||||
if (!cdp) return
|
||||
|
||||
try {
|
||||
const { result } = await cdp.send('Profiler.takePreciseCoverage')
|
||||
await cdp.send('Profiler.stopPreciseCoverage')
|
||||
await cdp.send('Profiler.disable')
|
||||
|
||||
// Filter to only include our app's JavaScript files
|
||||
const appCoverage = result.filter((entry: CoverageEntry) =>
|
||||
entry.url.includes('/auth/') &&
|
||||
entry.url.endsWith('.js') &&
|
||||
!entry.url.includes('node_modules')
|
||||
)
|
||||
|
||||
if (appCoverage.length > 0) {
|
||||
// Ensure coverage directory exists
|
||||
if (!existsSync(coverageDir)) {
|
||||
mkdirSync(coverageDir, { recursive: true })
|
||||
}
|
||||
|
||||
// Save coverage data for this test
|
||||
const safeName = testName.replace(/[^a-z0-9]/gi, '_').substring(0, 50)
|
||||
const coverageFile = join(coverageDir, `coverage-${safeName}-${Date.now()}.json`)
|
||||
writeFileSync(coverageFile, JSON.stringify(appCoverage, null, 2))
|
||||
}
|
||||
} catch (err) {
|
||||
// Silently ignore coverage collection errors
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge all coverage files into a single summary.
|
||||
*/
|
||||
export async function mergeCoverage(): Promise<void> {
|
||||
if (!COLLECT_COVERAGE || !existsSync(coverageDir)) return
|
||||
|
||||
const files = require('fs').readdirSync(coverageDir).filter((f: string) => f.startsWith('coverage-') && f.endsWith('.json'))
|
||||
if (files.length === 0) return
|
||||
|
||||
const merged: Map<string, CoverageEntry> = new Map()
|
||||
|
||||
for (const file of files) {
|
||||
const data: CoverageEntry[] = JSON.parse(readFileSync(join(coverageDir, file), 'utf-8'))
|
||||
for (const entry of data) {
|
||||
const existing = merged.get(entry.url)
|
||||
if (!existing) {
|
||||
merged.set(entry.url, entry)
|
||||
} else {
|
||||
// Merge function coverage counts
|
||||
for (const func of entry.functions) {
|
||||
const existingFunc = existing.functions.find(f => f.functionName === func.functionName)
|
||||
if (existingFunc) {
|
||||
for (let i = 0; i < func.ranges.length; i++) {
|
||||
if (existingFunc.ranges[i]) {
|
||||
existingFunc.ranges[i].count += func.ranges[i].count
|
||||
}
|
||||
}
|
||||
} else {
|
||||
existing.functions.push(func)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Write merged coverage
|
||||
writeFileSync(
|
||||
join(coverageDir, 'coverage-merged.json'),
|
||||
JSON.stringify(Array.from(merged.values()), null, 2)
|
||||
)
|
||||
|
||||
// Generate simple coverage summary
|
||||
let totalFunctions = 0
|
||||
let coveredFunctions = 0
|
||||
|
||||
for (const entry of merged.values()) {
|
||||
for (const func of entry.functions) {
|
||||
totalFunctions++
|
||||
const hasCoverage = func.ranges.some(r => r.count > 0)
|
||||
if (hasCoverage) coveredFunctions++
|
||||
}
|
||||
}
|
||||
|
||||
const percentage = totalFunctions > 0 ? Math.round((coveredFunctions / totalFunctions) * 100) : 0
|
||||
console.log(`\n 📊 Frontend JS Coverage: ${coveredFunctions}/${totalFunctions} functions (${percentage}%)`)
|
||||
console.log(` ✅ Frontend coverage data: ${coverageDir}/coverage-merged.json\n`)
|
||||
}
|
||||
|
||||
/**
|
||||
* Extended test with coverage collection.
|
||||
* This wraps each test to collect V8 coverage data.
|
||||
*/
|
||||
export const testWithCoverage = base.extend<{
|
||||
coverageSession: CDPSession | null
|
||||
}>({
|
||||
coverageSession: async ({ page }, use, testInfo) => {
|
||||
const cdp = await startCoverage(page)
|
||||
await use(cdp)
|
||||
await stopCoverage(cdp, testInfo.title)
|
||||
},
|
||||
})
|
||||
+79
-24
@@ -1,9 +1,10 @@
|
||||
import { type Page } from '@playwright/test'
|
||||
import { existsSync, readFileSync } from 'fs'
|
||||
import { existsSync, readFileSync, writeFileSync } from 'fs'
|
||||
import { join, dirname } from 'path'
|
||||
import { fileURLToPath } from 'url'
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url))
|
||||
const stateFile = join(__dirname, '..', '..', 'test-data', 'test-state.json')
|
||||
|
||||
/**
|
||||
* WebSocket helpers for passkey registration and authentication.
|
||||
@@ -26,7 +27,6 @@ export interface AuthenticationResult {
|
||||
* Get the bootstrap reset token from the test state file.
|
||||
*/
|
||||
export function getBootstrapResetToken(): string | undefined {
|
||||
const stateFile = join(__dirname, '..', '..', 'test-data', 'test-state.json')
|
||||
if (existsSync(stateFile)) {
|
||||
try {
|
||||
const state = JSON.parse(readFileSync(stateFile, 'utf-8'))
|
||||
@@ -38,6 +38,51 @@ export function getBootstrapResetToken(): string | undefined {
|
||||
return undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the session cookie name from the test state file.
|
||||
*/
|
||||
export function getSessionCookieName(): string {
|
||||
if (existsSync(stateFile)) {
|
||||
try {
|
||||
const state = JSON.parse(readFileSync(stateFile, 'utf-8'))
|
||||
return state.sessionCookie || '__Host-auth'
|
||||
} catch {
|
||||
return '__Host-auth'
|
||||
}
|
||||
}
|
||||
return '__Host-auth'
|
||||
}
|
||||
|
||||
/**
|
||||
* Save a session token to the test state file for sharing across test groups.
|
||||
*/
|
||||
export function saveSessionToken(sessionToken: string): void {
|
||||
if (existsSync(stateFile)) {
|
||||
try {
|
||||
const state = JSON.parse(readFileSync(stateFile, 'utf-8'))
|
||||
state.savedSessionToken = sessionToken
|
||||
writeFileSync(stateFile, JSON.stringify(state, null, 2))
|
||||
} catch {
|
||||
// Ignore errors
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a saved session token from the test state file.
|
||||
*/
|
||||
export function getSavedSessionToken(): string | undefined {
|
||||
if (existsSync(stateFile)) {
|
||||
try {
|
||||
const state = JSON.parse(readFileSync(stateFile, 'utf-8'))
|
||||
return state.savedSessionToken
|
||||
} catch {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform passkey registration via WebSocket.
|
||||
* This runs in the browser context using the virtual authenticator.
|
||||
@@ -79,30 +124,33 @@ export async function registerPasskey(
|
||||
return
|
||||
}
|
||||
|
||||
// This should be the registration options from server
|
||||
// This should be the registration options from server (wrapped in optionsJSON)
|
||||
// Use the native WebAuthn API with the virtual authenticator
|
||||
try {
|
||||
// Extract options from the optionsJSON wrapper
|
||||
const opts = data.optionsJSON
|
||||
|
||||
// Convert base64url challenge to ArrayBuffer
|
||||
const challenge = Uint8Array.from(atob(data.challenge.replace(/-/g, '+').replace(/_/g, '/')), c => c.charCodeAt(0))
|
||||
const challenge = Uint8Array.from(atob(opts.challenge.replace(/-/g, '+').replace(/_/g, '/')), c => c.charCodeAt(0))
|
||||
|
||||
// Build the credential creation options
|
||||
const publicKeyCredentialCreationOptions: CredentialCreationOptions = {
|
||||
publicKey: {
|
||||
challenge: challenge,
|
||||
rp: {
|
||||
name: data.rp.name,
|
||||
id: data.rp.id,
|
||||
name: opts.rp.name,
|
||||
id: opts.rp.id,
|
||||
},
|
||||
user: {
|
||||
id: Uint8Array.from(atob(data.user.id.replace(/-/g, '+').replace(/_/g, '/')), c => c.charCodeAt(0)),
|
||||
name: data.user.name,
|
||||
displayName: data.user.displayName,
|
||||
id: Uint8Array.from(atob(opts.user.id.replace(/-/g, '+').replace(/_/g, '/')), c => c.charCodeAt(0)),
|
||||
name: opts.user.name,
|
||||
displayName: opts.user.displayName,
|
||||
},
|
||||
pubKeyCredParams: data.pubKeyCredParams,
|
||||
authenticatorSelection: data.authenticatorSelection,
|
||||
timeout: data.timeout,
|
||||
attestation: data.attestation,
|
||||
excludeCredentials: data.excludeCredentials?.map((cred: any) => ({
|
||||
pubKeyCredParams: opts.pubKeyCredParams,
|
||||
authenticatorSelection: opts.authenticatorSelection,
|
||||
timeout: opts.timeout,
|
||||
attestation: opts.attestation,
|
||||
excludeCredentials: opts.excludeCredentials?.map((cred: any) => ({
|
||||
...cred,
|
||||
id: Uint8Array.from(atob(cred.id.replace(/-/g, '+').replace(/_/g, '/')), c => c.charCodeAt(0)),
|
||||
})) || [],
|
||||
@@ -187,19 +235,22 @@ export async function authenticatePasskey(
|
||||
return
|
||||
}
|
||||
|
||||
// This should be the authentication options from server
|
||||
// This should be the authentication options from server (wrapped in optionsJSON)
|
||||
try {
|
||||
// Extract options from the optionsJSON wrapper
|
||||
const opts = data.optionsJSON
|
||||
|
||||
// Convert base64url challenge to ArrayBuffer
|
||||
const challenge = Uint8Array.from(atob(data.challenge.replace(/-/g, '+').replace(/_/g, '/')), c => c.charCodeAt(0))
|
||||
const challenge = Uint8Array.from(atob(opts.challenge.replace(/-/g, '+').replace(/_/g, '/')), c => c.charCodeAt(0))
|
||||
|
||||
// Build the credential request options
|
||||
const publicKeyCredentialRequestOptions: CredentialRequestOptions = {
|
||||
publicKey: {
|
||||
challenge: challenge,
|
||||
rpId: data.rpId,
|
||||
timeout: data.timeout,
|
||||
userVerification: data.userVerification,
|
||||
allowCredentials: data.allowCredentials?.map((cred: any) => ({
|
||||
rpId: opts.rpId,
|
||||
timeout: opts.timeout,
|
||||
userVerification: opts.userVerification,
|
||||
allowCredentials: opts.allowCredentials?.map((cred: any) => ({
|
||||
type: cred.type,
|
||||
id: Uint8Array.from(atob(cred.id.replace(/-/g, '+').replace(/_/g, '/')), c => c.charCodeAt(0)),
|
||||
transports: cred.transports,
|
||||
@@ -259,9 +310,10 @@ export async function validateSession(
|
||||
baseUrl: string,
|
||||
sessionToken: string
|
||||
): Promise<{ valid: boolean; user_uuid: string; renewed: boolean }> {
|
||||
const cookieName = getSessionCookieName()
|
||||
const response = await page.request.post(`${baseUrl}/auth/api/validate`, {
|
||||
headers: {
|
||||
'Cookie': `__Host-auth=${sessionToken}`,
|
||||
'Cookie': `${cookieName}=${sessionToken}`,
|
||||
},
|
||||
})
|
||||
return await response.json()
|
||||
@@ -275,9 +327,10 @@ export async function getUserInfo(
|
||||
baseUrl: string,
|
||||
sessionToken: string
|
||||
): Promise<any> {
|
||||
const cookieName = getSessionCookieName()
|
||||
const response = await page.request.post(`${baseUrl}/auth/api/user-info`, {
|
||||
headers: {
|
||||
'Cookie': `__Host-auth=${sessionToken}`,
|
||||
'Cookie': `${cookieName}=${sessionToken}`,
|
||||
},
|
||||
})
|
||||
return await response.json()
|
||||
@@ -291,9 +344,10 @@ export async function logout(
|
||||
baseUrl: string,
|
||||
sessionToken: string
|
||||
): Promise<void> {
|
||||
const cookieName = getSessionCookieName()
|
||||
await page.request.post(`${baseUrl}/auth/api/logout`, {
|
||||
headers: {
|
||||
'Cookie': `__Host-auth=${sessionToken}`,
|
||||
'Cookie': `${cookieName}=${sessionToken}`,
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -306,9 +360,10 @@ export async function createDeviceLink(
|
||||
baseUrl: string,
|
||||
sessionToken: string
|
||||
): Promise<{ url: string; token: string }> {
|
||||
const cookieName = getSessionCookieName()
|
||||
const response = await page.request.post(`${baseUrl}/auth/api/user/create-link`, {
|
||||
headers: {
|
||||
'Cookie': `__Host-auth=${sessionToken}`,
|
||||
'Cookie': `${cookieName}=${sessionToken}`,
|
||||
},
|
||||
})
|
||||
const data = await response.json()
|
||||
|
||||
+53
-2
@@ -1,4 +1,13 @@
|
||||
import { test as base, expect, type CDPSession, type Page } from '@playwright/test'
|
||||
import { existsSync, mkdirSync, writeFileSync } from 'fs'
|
||||
import { join, dirname } from 'path'
|
||||
import { fileURLToPath } from 'url'
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url))
|
||||
const coverageDir = join(__dirname, '..', '..', 'coverage-frontend')
|
||||
|
||||
// Check if frontend coverage is enabled
|
||||
const COLLECT_COVERAGE = process.env.COVERAGE === '1' || process.env.COVERAGE === 'true'
|
||||
|
||||
/**
|
||||
* Virtual Authenticator configuration for WebAuthn testing.
|
||||
@@ -73,12 +82,27 @@ export async function getCredentials(
|
||||
}
|
||||
|
||||
/**
|
||||
* Extended test fixture with virtual authenticator support.
|
||||
* Extended test fixture with virtual authenticator support and optional coverage.
|
||||
*/
|
||||
export const test = base.extend<{
|
||||
virtualAuthenticator: VirtualAuthenticator
|
||||
}>({
|
||||
virtualAuthenticator: async ({ page }, use) => {
|
||||
virtualAuthenticator: async ({ page }, use, testInfo) => {
|
||||
// Start coverage collection if enabled
|
||||
let coverageCdp: CDPSession | null = null
|
||||
if (COLLECT_COVERAGE) {
|
||||
try {
|
||||
coverageCdp = await page.context().newCDPSession(page)
|
||||
await coverageCdp.send('Profiler.enable')
|
||||
await coverageCdp.send('Profiler.startPreciseCoverage', {
|
||||
callCount: true,
|
||||
detailed: true,
|
||||
})
|
||||
} catch {
|
||||
coverageCdp = null
|
||||
}
|
||||
}
|
||||
|
||||
// Create virtual authenticator before test
|
||||
const authenticator = await createVirtualAuthenticator(page)
|
||||
|
||||
@@ -87,6 +111,33 @@ export const test = base.extend<{
|
||||
|
||||
// Cleanup after test
|
||||
await removeVirtualAuthenticator(authenticator)
|
||||
|
||||
// Stop and save coverage
|
||||
if (coverageCdp) {
|
||||
try {
|
||||
const { result } = await coverageCdp.send('Profiler.takePreciseCoverage')
|
||||
await coverageCdp.send('Profiler.stopPreciseCoverage')
|
||||
await coverageCdp.send('Profiler.disable')
|
||||
|
||||
// Filter to only include our app's JavaScript files
|
||||
const appCoverage = result.filter((entry: any) =>
|
||||
entry.url.includes('/auth/') &&
|
||||
entry.url.endsWith('.js') &&
|
||||
!entry.url.includes('node_modules')
|
||||
)
|
||||
|
||||
if (appCoverage.length > 0) {
|
||||
if (!existsSync(coverageDir)) {
|
||||
mkdirSync(coverageDir, { recursive: true })
|
||||
}
|
||||
const safeName = testInfo.title.replace(/[^a-z0-9]/gi, '_').substring(0, 50)
|
||||
const coverageFile = join(coverageDir, `coverage-${safeName}-${Date.now()}.json`)
|
||||
writeFileSync(coverageFile, JSON.stringify(appCoverage, null, 2))
|
||||
}
|
||||
} catch {
|
||||
// Silently ignore coverage collection errors
|
||||
}
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user