Guard API tests against destructive non-loopback targets
This commit is contained in:
1 parent
4e47e350bf
commit
7627c36642
3 files changed
+33
-1
No files matched your search
@@ -14,6 +14,7 @@ import { spawnSync } from 'node:child_process';
|
||||
import { mkdirSync, writeFileSync } from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { assertDisposableTarget } from '../tests/api/paths';
|
||||
|
||||
const ROOT_DIR = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
||||
const COMPOSE_FILE = 'docker-compose.test.yml';
|
||||
@@ -51,6 +52,8 @@ Environment:
|
||||
COMPOSE_PROJECT_NAME Compose project name (default umami-api-test)
|
||||
PLAYWRIGHT_BASE_URL Test an already-running server instead of starting the
|
||||
Compose stack (pass --clickhouse if it stores analytics there)
|
||||
API_ALLOW_DESTRUCTIVE=1 Required for non-loopback PLAYWRIGHT_BASE_URL targets: the suite
|
||||
recreates websites, ingests data and toggles global 2FA
|
||||
API_SKIP_SEED=1 Reuse the previous seed when re-running against a kept stack
|
||||
(each run seeds additively otherwise)
|
||||
`;
|
||||
@@ -240,6 +243,12 @@ async function main() {
|
||||
|
||||
try {
|
||||
if (EXTERNAL_TARGET) {
|
||||
try {
|
||||
assertDisposableTarget(BASE_URL);
|
||||
} catch (error) {
|
||||
console.error((error as Error).message);
|
||||
return 2;
|
||||
}
|
||||
console.log(`Using the already-running server at ${BASE_URL} (PLAYWRIGHT_BASE_URL is set).`);
|
||||
} else {
|
||||
console.log(`Starting test stack (profile: ${profile}, port: ${PORT})...`);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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.',
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user