test: fix flaky auth tests when OPENCLAW_GATEWAY_TOKEN is present

This commit is contained in:
tyler
2026-02-22 10:32:00 +08:00
committed by Peter Steinberger
parent 9f2b25426b
commit 9b23e5ce1f
2 changed files with 33 additions and 2 deletions

View File

@@ -66,6 +66,11 @@ describe("profile CRUD endpoints", () => {
state.prevGatewayPort = process.env.OPENCLAW_GATEWAY_PORT;
process.env.OPENCLAW_GATEWAY_PORT = String(state.testPort - 2);
state.prevGatewayToken = process.env.OPENCLAW_GATEWAY_TOKEN;
state.prevGatewayPassword = process.env.OPENCLAW_GATEWAY_PASSWORD;
delete process.env.OPENCLAW_GATEWAY_TOKEN;
delete process.env.OPENCLAW_GATEWAY_PASSWORD;
vi.stubGlobal(
"fetch",
vi.fn(async (url: string) => {
@@ -82,6 +87,16 @@ describe("profile CRUD endpoints", () => {
vi.unstubAllGlobals();
vi.restoreAllMocks();
restoreGatewayPortEnv(state.prevGatewayPort);
if (state.prevGatewayToken !== undefined) {
process.env.OPENCLAW_GATEWAY_TOKEN = state.prevGatewayToken;
} else {
delete process.env.OPENCLAW_GATEWAY_TOKEN;
}
if (state.prevGatewayPassword !== undefined) {
process.env.OPENCLAW_GATEWAY_PASSWORD = state.prevGatewayPassword;
} else {
delete process.env.OPENCLAW_GATEWAY_PASSWORD;
}
await stopBrowserControlServer();
});

View File

@@ -1,4 +1,4 @@
import { describe, expect, it } from "vitest";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { resolveGatewayRuntimeConfig } from "./server-runtime-config.js";
const TRUSTED_PROXY_AUTH = {
@@ -103,6 +103,21 @@ describe("resolveGatewayRuntimeConfig", () => {
});
describe("token/password auth modes", () => {
let originalToken: string | undefined;
beforeEach(() => {
originalToken = process.env.OPENCLAW_GATEWAY_TOKEN;
delete process.env.OPENCLAW_GATEWAY_TOKEN;
});
afterEach(() => {
if (originalToken !== undefined) {
process.env.OPENCLAW_GATEWAY_TOKEN = originalToken;
} else {
delete process.env.OPENCLAW_GATEWAY_TOKEN;
}
});
it.each([
{
name: "lan binding with token",
@@ -126,7 +141,8 @@ describe("resolveGatewayRuntimeConfig", () => {
{
name: "token mode without token",
cfg: { gateway: { bind: "lan" as const, auth: { mode: "token" as const } } },
expectedMessage: "gateway auth mode is token, but no token was configured",
expectedMessage:
"gateway auth mode is token, but no token was configured (set gateway.auth.token or OPENCLAW_GATEWAY_TOKEN)",
},
{
name: "lan binding with explicit none auth",