File size: 4,464 Bytes
fc93158 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | import { beforeEach, describe, expect, it, vi } from "vitest";
import type { OpenClawConfig } from "../config/config.js";
import { expectGeneratedTokenPersistedToGatewayAuth } from "../test-utils/auth-token-assertions.js";
const mocks = vi.hoisted(() => ({
loadConfig: vi.fn<() => OpenClawConfig>(),
writeConfigFile: vi.fn(async (_cfg: OpenClawConfig) => {}),
}));
vi.mock("../config/config.js", async (importOriginal) => {
const actual = await importOriginal<typeof import("../config/config.js")>();
return {
...actual,
loadConfig: mocks.loadConfig,
writeConfigFile: mocks.writeConfigFile,
};
});
import { ensureBrowserControlAuth } from "./control-auth.js";
describe("ensureBrowserControlAuth", () => {
const expectExplicitModeSkipsAutoAuth = async (mode: "password" | "none") => {
const cfg: OpenClawConfig = {
gateway: {
auth: { mode },
},
browser: {
enabled: true,
},
};
const result = await ensureBrowserControlAuth({ cfg, env: {} as NodeJS.ProcessEnv });
expect(result).toEqual({ auth: {} });
expect(mocks.loadConfig).not.toHaveBeenCalled();
expect(mocks.writeConfigFile).not.toHaveBeenCalled();
};
const expectGeneratedTokenPersisted = (result: {
generatedToken?: string;
auth: { token?: string };
}) => {
expect(mocks.writeConfigFile).toHaveBeenCalledTimes(1);
expectGeneratedTokenPersistedToGatewayAuth({
generatedToken: result.generatedToken,
authToken: result.auth.token,
persistedConfig: mocks.writeConfigFile.mock.calls[0]?.[0],
});
};
beforeEach(() => {
vi.restoreAllMocks();
mocks.loadConfig.mockClear();
mocks.writeConfigFile.mockClear();
});
it("returns existing auth and skips writes", async () => {
const cfg: OpenClawConfig = {
gateway: {
auth: {
token: "already-set",
},
},
};
const result = await ensureBrowserControlAuth({ cfg, env: {} as NodeJS.ProcessEnv });
expect(result).toEqual({ auth: { token: "already-set" } });
expect(mocks.loadConfig).not.toHaveBeenCalled();
expect(mocks.writeConfigFile).not.toHaveBeenCalled();
});
it("auto-generates and persists a token when auth is missing", async () => {
const cfg: OpenClawConfig = {
browser: {
enabled: true,
},
};
mocks.loadConfig.mockReturnValue({
browser: {
enabled: true,
},
});
const result = await ensureBrowserControlAuth({ cfg, env: {} as NodeJS.ProcessEnv });
expectGeneratedTokenPersisted(result);
});
it("skips auto-generation in test env", async () => {
const cfg: OpenClawConfig = {
browser: {
enabled: true,
},
};
const result = await ensureBrowserControlAuth({
cfg,
env: { NODE_ENV: "test" } as NodeJS.ProcessEnv,
});
expect(result).toEqual({ auth: {} });
expect(mocks.loadConfig).not.toHaveBeenCalled();
expect(mocks.writeConfigFile).not.toHaveBeenCalled();
});
it("respects explicit password mode", async () => {
await expectExplicitModeSkipsAutoAuth("password");
});
it("respects explicit none mode", async () => {
await expectExplicitModeSkipsAutoAuth("none");
});
it("reuses auth from latest config snapshot", async () => {
const cfg: OpenClawConfig = {
browser: {
enabled: true,
},
};
mocks.loadConfig.mockReturnValue({
gateway: {
auth: {
token: "latest-token",
},
},
browser: {
enabled: true,
},
});
const result = await ensureBrowserControlAuth({ cfg, env: {} as NodeJS.ProcessEnv });
expect(result).toEqual({ auth: { token: "latest-token" } });
expect(mocks.writeConfigFile).not.toHaveBeenCalled();
});
it("fails when gateway.auth.token SecretRef is unresolved", async () => {
const cfg: OpenClawConfig = {
gateway: {
auth: {
mode: "token",
token: { source: "env", provider: "default", id: "MISSING_GW_TOKEN" },
},
},
browser: {
enabled: true,
},
secrets: {
providers: {
default: { source: "env" },
},
},
};
mocks.loadConfig.mockReturnValue(cfg);
await expect(ensureBrowserControlAuth({ cfg, env: {} as NodeJS.ProcessEnv })).rejects.toThrow(
/MISSING_GW_TOKEN/i,
);
expect(mocks.writeConfigFile).not.toHaveBeenCalled();
});
});
|