Guard API tests against destructive non-loopback targets

This commit is contained in:
Mike Cao committed 2026-09-06 20:41:22 -07:00
1 parent 4e47e350bf
commit 7627c36642
3 files changed
+33 -1

No files matched your search

+3 -1
View File
@@ -3,7 +3,7 @@ import { existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';
import { type FullConfig, request } from '@playwright/test';
import { ApiClient } from './client';
import { disableCoverageRecording } from './coverage/recorder';
import { COVERAGE_DIR, SEED_FILE } from './paths';
import { assertDisposableTarget, COVERAGE_DIR, SEED_FILE } from './paths';
import { seedEnvironment } from './seed/setup';
const HEARTBEAT_TIMEOUT_MS = 120_000;
@@ -44,6 +44,8 @@ export default async function globalSetup(config: FullConfig) {
throw new Error('playwright.api.config.ts must define use.baseURL');
}
assertDisposableTarget(baseURL);
// API_SKIP_SEED=1 reuses the seed from a previous run against a kept stack
// (fast iteration on a single spec).
const skipSeed = !!process.env.API_SKIP_SEED && existsSync(SEED_FILE);
+21
View File
@@ -20,3 +20,24 @@ export const RUNTIME_DIR = path.join(
export const COVERAGE_DIR = path.join(RUNTIME_DIR, 'coverage');
export const SEED_FILE = path.join(RUNTIME_DIR, 'seed.json');
export const OPENAPI_FILE = path.join(ROOT_DIR, 'public/openapi.json');
const LOOPBACK_HOSTS = new Set(['localhost', '127.0.0.1', '[::1]', '::1']);
/**
* The suite is destructive: global setup deletes and recreates fixed-ID websites
* and ingests analytics data, and specs toggle the global 2FA requirement. Only
* loopback targets (the local Compose stack) run without an explicit opt-in.
*/
export function assertDisposableTarget(baseURL: string) {
const { hostname } = new URL(baseURL);
if (LOOPBACK_HOSTS.has(hostname) || process.env.API_ALLOW_DESTRUCTIVE === '1') {
return;
}
throw new Error(
`Refusing to run the API suite against ${baseURL}: it deletes and recreates websites, ` +
'ingests analytics data and changes the global 2FA setting. Point PLAYWRIGHT_BASE_URL ' +
'at a disposable server and set API_ALLOW_DESTRUCTIVE=1 to confirm.',
);
}