Skip to content

Commit 969dad1

Browse files
committed
test(e2e): harden shared auth proof
1 parent fa018f5 commit 969dad1

8 files changed

Lines changed: 279 additions & 20 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export type AuthProfileStoreAssertionOptions = {
2+
missingMessage?: string;
3+
envRefMessage?: string;
4+
rawKeyMessage?: string;
5+
rawKeyNeedle?: string;
6+
};
7+
8+
export declare function readSharedAuthProfileStoreText(stateDir: string): string;
9+
10+
export declare function assertNoLegacyPrimaryAuthRows(stateDir: string): void;
11+
12+
export declare function assertOpenAiEnvAuthProfileStore(
13+
storeJson: string,
14+
options?: AuthProfileStoreAssertionOptions,
15+
): void;

‎scripts/e2e/lib/auth-profile-store-assertions.mjs‎

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,69 @@ export function readSharedAuthProfileStoreText(stateDir) {
1212
let db;
1313
try {
1414
db = new DatabaseSync(dbPath, { readOnly: true });
15+
const schema = db
16+
.prepare("SELECT type FROM sqlite_schema WHERE name = ? LIMIT 1")
17+
.get("auth_profile_stores");
18+
if (!schema) {
19+
return "";
20+
}
21+
if (schema.type !== "table") {
22+
throw new Error(`auth_profile_stores is ${String(schema.type)}, not a table`);
23+
}
1524
const row = db
1625
.prepare("SELECT store_json FROM auth_profile_stores WHERE store_key = ?")
1726
.get("shared");
1827
return typeof row?.store_json === "string" ? row.store_json : "";
19-
} catch {
20-
return "";
28+
} catch (error) {
29+
const detail = error instanceof Error ? error.message : String(error);
30+
throw new Error(`could not read the shared auth profile store: ${detail}`, {
31+
cause: error,
32+
});
33+
} finally {
34+
db?.close();
35+
}
36+
}
37+
38+
export function assertNoLegacyPrimaryAuthRows(stateDir) {
39+
const dbPath = path.join(stateDir, "agents", "main", "agent", "openclaw-agent.sqlite");
40+
if (!fs.existsSync(dbPath)) {
41+
return;
42+
}
43+
let db;
44+
try {
45+
db = new DatabaseSync(dbPath, { readOnly: true });
46+
const legacyRows = [
47+
{
48+
table: "auth_profile_store",
49+
query: "SELECT 1 AS present FROM auth_profile_store WHERE store_key = ? LIMIT 1",
50+
},
51+
{
52+
table: "auth_profile_state",
53+
query: "SELECT 1 AS present FROM auth_profile_state WHERE state_key = ? LIMIT 1",
54+
},
55+
];
56+
for (const entry of legacyRows) {
57+
const schema = db
58+
.prepare("SELECT type FROM sqlite_schema WHERE name = ? LIMIT 1")
59+
.get(entry.table);
60+
if (!schema) {
61+
continue;
62+
}
63+
if (schema.type !== "table") {
64+
throw new Error(`${entry.table} is ${String(schema.type)}, not a table`);
65+
}
66+
if (db.prepare(entry.query).get("primary")) {
67+
throw new Error(`onboard preserved a retired primary row in ${entry.table}`);
68+
}
69+
}
70+
} catch (error) {
71+
if (error instanceof Error && error.message.startsWith("onboard preserved a retired")) {
72+
throw error;
73+
}
74+
const detail = error instanceof Error ? error.message : String(error);
75+
throw new Error(`could not validate the main-agent auth database: ${detail}`, {
76+
cause: error,
77+
});
2178
} finally {
2279
db?.close();
2380
}

‎scripts/e2e/lib/codex-on-demand/assertions.mjs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { spawnSync } from "node:child_process";
33
import fs from "node:fs";
44
import path from "node:path";
55
import {
6+
assertNoLegacyPrimaryAuthRows,
67
assertOpenAiEnvAuthProfileStore,
78
readSharedAuthProfileStoreText,
89
} from "../auth-profile-store-assertions.mjs";
@@ -127,7 +128,9 @@ if (providerRuntime && providerRuntime !== "codex") {
127128
throw new Error(`unexpected OpenAI provider runtime: ${providerRuntime}`);
128129
}
129130

130-
const authRaw = readSharedAuthProfileStoreText(stateDir());
131+
const openClawStateDir = stateDir();
132+
assertNoLegacyPrimaryAuthRows(openClawStateDir);
133+
const authRaw = readSharedAuthProfileStoreText(openClawStateDir);
131134
if (!authRaw) {
132135
throw new Error("auth profile SQLite store row was not persisted");
133136
}

‎scripts/e2e/lib/npm-onboard-channel-agent/assertions.mjs‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
assertOpenAiRequestLogUsed,
77
} from "../agent-turn-output.mjs";
88
import {
9+
assertNoLegacyPrimaryAuthRows,
910
assertOpenAiEnvAuthProfileStore,
1011
readSharedAuthProfileStoreText,
1112
} from "../auth-profile-store-assertions.mjs";
@@ -83,20 +84,11 @@ function assertOnboardState() {
8384
const home = process.argv[3];
8485
const stateDir = path.join(home, ".openclaw");
8586
const configPath = path.join(stateDir, "openclaw.json");
86-
const legacyAuthDatabase = path.join(
87-
stateDir,
88-
"agents",
89-
"main",
90-
"agent",
91-
"openclaw-agent.sqlite",
92-
);
9387

9488
if (!fs.existsSync(configPath)) {
9589
throw new Error("onboard did not write openclaw.json");
9690
}
97-
if (fs.existsSync(legacyAuthDatabase)) {
98-
throw new Error("onboard created the retired main-agent auth database");
99-
}
91+
assertNoLegacyPrimaryAuthRows(stateDir);
10092
const authStoreText = readSharedAuthProfileStoreText(stateDir);
10193
if (!authStoreText) {
10294
throw new Error("onboard did not persist auth profile store");

‎scripts/e2e/lib/release-scenarios/assertions.mjs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
assertOpenAiRequestLogUsed,
77
} from "../agent-turn-output.mjs";
88
import {
9+
assertNoLegacyPrimaryAuthRows,
910
assertOpenAiEnvAuthProfileStore,
1011
readSharedAuthProfileStoreText,
1112
} from "../auth-profile-store-assertions.mjs";
@@ -75,6 +76,7 @@ function configureMockOpenAi() {
7576
function assertOpenAiEnvRef() {
7677
const rawKey = process.argv[3];
7778
assert(fs.existsSync(configPath()), "openclaw.json missing");
79+
assertNoLegacyPrimaryAuthRows(stateDir());
7880
assertOpenAiEnvAuthProfileStore(readSharedAuthProfileStoreText(stateDir()), {
7981
missingMessage: "OpenAI env ref was not persisted",
8082
envRefMessage: "OpenAI env ref was not persisted",
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
import { mkdirSync, writeFileSync } from "node:fs";
2+
import path from "node:path";
3+
import { DatabaseSync } from "node:sqlite";
4+
import { afterEach, describe, expect, it } from "vitest";
5+
import {
6+
assertNoLegacyPrimaryAuthRows,
7+
readSharedAuthProfileStoreText,
8+
} from "../../scripts/e2e/lib/auth-profile-store-assertions.mjs";
9+
import { useAutoCleanupTempDirTracker } from "../helpers/temp-dir.js";
10+
11+
const tempDirs = useAutoCleanupTempDirTracker(afterEach);
12+
13+
function makeStateDir(): string {
14+
const root = tempDirs.make("openclaw-auth-profile-assertions-");
15+
return path.join(root, ".openclaw");
16+
}
17+
18+
function writeSharedDatabase(
19+
stateDir: string,
20+
options: { asView?: boolean; storeJson?: string } = {},
21+
): string {
22+
const dbPath = path.join(stateDir, "state", "openclaw.sqlite");
23+
mkdirSync(path.dirname(dbPath), { recursive: true });
24+
const db = new DatabaseSync(dbPath);
25+
try {
26+
if (options.asView) {
27+
db.exec(`
28+
CREATE VIEW auth_profile_stores AS
29+
SELECT 'shared' AS store_key, '{}' AS store_json, 1 AS updated_at;
30+
`);
31+
} else {
32+
db.exec(`
33+
CREATE TABLE auth_profile_stores (
34+
store_key TEXT NOT NULL PRIMARY KEY,
35+
store_json TEXT NOT NULL,
36+
updated_at INTEGER NOT NULL
37+
) STRICT;
38+
`);
39+
db.prepare("INSERT INTO auth_profile_stores VALUES (?, ?, ?)").run(
40+
"shared",
41+
options.storeJson ?? "{}",
42+
Date.now(),
43+
);
44+
}
45+
} finally {
46+
db.close();
47+
}
48+
return dbPath;
49+
}
50+
51+
function writeAgentDatabase(
52+
stateDir: string,
53+
options: {
54+
stateKeys?: string[];
55+
storeKeys?: string[];
56+
storeAsView?: boolean;
57+
} = {},
58+
): string {
59+
const dbPath = path.join(stateDir, "agents", "main", "agent", "openclaw-agent.sqlite");
60+
mkdirSync(path.dirname(dbPath), { recursive: true });
61+
const db = new DatabaseSync(dbPath);
62+
try {
63+
if (options.storeAsView) {
64+
db.exec(`
65+
CREATE VIEW auth_profile_store AS
66+
SELECT 'primary' AS store_key, '{}' AS store_json, 1 AS updated_at;
67+
`);
68+
} else {
69+
db.exec(`
70+
CREATE TABLE auth_profile_store (
71+
store_key TEXT NOT NULL PRIMARY KEY,
72+
store_json TEXT NOT NULL,
73+
updated_at INTEGER NOT NULL
74+
) STRICT;
75+
`);
76+
for (const key of options.storeKeys ?? []) {
77+
db.prepare("INSERT INTO auth_profile_store VALUES (?, '{}', ?)").run(key, Date.now());
78+
}
79+
}
80+
db.exec(`
81+
CREATE TABLE auth_profile_state (
82+
state_key TEXT NOT NULL PRIMARY KEY,
83+
state_json TEXT NOT NULL,
84+
updated_at INTEGER NOT NULL
85+
) STRICT;
86+
`);
87+
for (const key of options.stateKeys ?? []) {
88+
db.prepare("INSERT INTO auth_profile_state VALUES (?, '{}', ?)").run(key, Date.now());
89+
}
90+
} finally {
91+
db.close();
92+
}
93+
return dbPath;
94+
}
95+
96+
describe("auth profile store E2E assertions", () => {
97+
it("reads the canonical shared row", () => {
98+
const stateDir = makeStateDir();
99+
writeSharedDatabase(stateDir, { storeJson: '{"version":1}' });
100+
101+
expect(readSharedAuthProfileStoreText(stateDir)).toBe('{"version":1}');
102+
});
103+
104+
it("returns empty when the shared database or table is absent", () => {
105+
const stateDir = makeStateDir();
106+
107+
expect(readSharedAuthProfileStoreText(stateDir)).toBe("");
108+
const dbPath = path.join(stateDir, "state", "openclaw.sqlite");
109+
mkdirSync(path.dirname(dbPath), { recursive: true });
110+
new DatabaseSync(dbPath).close();
111+
expect(readSharedAuthProfileStoreText(stateDir)).toBe("");
112+
});
113+
114+
it("fails closed for a corrupt shared database", () => {
115+
const stateDir = makeStateDir();
116+
const dbPath = path.join(stateDir, "state", "openclaw.sqlite");
117+
mkdirSync(path.dirname(dbPath), { recursive: true });
118+
writeFileSync(dbPath, "not sqlite");
119+
120+
expect(() => readSharedAuthProfileStoreText(stateDir)).toThrow(
121+
"could not read the shared auth profile store",
122+
);
123+
});
124+
125+
it("fails closed when the shared auth table is replaced by a view", () => {
126+
const stateDir = makeStateDir();
127+
writeSharedDatabase(stateDir, { asView: true });
128+
129+
expect(() => readSharedAuthProfileStoreText(stateDir)).toThrow(
130+
"auth_profile_stores is view, not a table",
131+
);
132+
});
133+
134+
it("permits unrelated main-agent auth rows", () => {
135+
const stateDir = makeStateDir();
136+
writeAgentDatabase(stateDir, {
137+
stateKeys: ["last-good"],
138+
storeKeys: ["workspace"],
139+
});
140+
141+
expect(() => assertNoLegacyPrimaryAuthRows(stateDir)).not.toThrow();
142+
});
143+
144+
it.each(["auth_profile_store", "auth_profile_state"] as const)(
145+
"rejects a retired primary row in %s",
146+
(table) => {
147+
const stateDir = makeStateDir();
148+
writeAgentDatabase(stateDir, {
149+
stateKeys: table === "auth_profile_state" ? ["primary"] : [],
150+
storeKeys: table === "auth_profile_store" ? ["primary"] : [],
151+
});
152+
153+
expect(() => assertNoLegacyPrimaryAuthRows(stateDir)).toThrow(
154+
`onboard preserved a retired primary row in ${table}`,
155+
);
156+
},
157+
);
158+
159+
it("fails closed for a corrupt main-agent database", () => {
160+
const stateDir = makeStateDir();
161+
const dbPath = path.join(stateDir, "agents", "main", "agent", "openclaw-agent.sqlite");
162+
mkdirSync(path.dirname(dbPath), { recursive: true });
163+
writeFileSync(dbPath, "not sqlite");
164+
165+
expect(() => assertNoLegacyPrimaryAuthRows(stateDir)).toThrow(
166+
"could not validate the main-agent auth database",
167+
);
168+
});
169+
170+
it("fails closed when a retired auth table is replaced by a view", () => {
171+
const stateDir = makeStateDir();
172+
writeAgentDatabase(stateDir, { storeAsView: true });
173+
174+
expect(() => assertNoLegacyPrimaryAuthRows(stateDir)).toThrow(
175+
"auth_profile_store is view, not a table",
176+
);
177+
});
178+
});

‎test/scripts/codex-install-assertions.test.ts‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Codex Install Assertions tests cover Codex plugin install E2E helpers.
22
import { spawnSync } from "node:child_process";
33
import { createHash } from "node:crypto";
4-
import { chmodSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
4+
import { chmodSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
55
import os from "node:os";
66
import path from "node:path";
77
import { DatabaseSync } from "node:sqlite";
@@ -688,14 +688,26 @@ describe("Codex install helpers", () => {
688688
it("accepts a complete on-demand Codex npm install fixture", () => {
689689
const root = makeTempDir(tempDirs, "openclaw-codex-on-demand-");
690690
createCodexInstallFixture(root);
691+
const agentDatabasePath = path.join(
692+
root,
693+
"state",
694+
"agents",
695+
"main",
696+
"agent",
697+
"openclaw-agent.sqlite",
698+
);
699+
mkdirSync(path.dirname(agentDatabasePath), { recursive: true });
700+
const agentDatabase = new DatabaseSync(agentDatabasePath);
701+
try {
702+
agentDatabase.exec("CREATE TABLE unrelated_state (key TEXT PRIMARY KEY)");
703+
} finally {
704+
agentDatabase.close();
705+
}
691706

692707
const result = runCodexOnDemandAssertions(root);
693708

694709
expect(result.status).toBe(0);
695710
expect(result.stderr).toBe("");
696-
expect(
697-
existsSync(path.join(root, "state", "agents", "main", "agent", "openclaw-agent.sqlite")),
698-
).toBe(false);
699711
});
700712

701713
it("rejects on-demand fixtures without the canonical SQLite install record", () => {

‎test/scripts/npm-onboard-channel-agent-assertions.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ describe("npm onboard channel agent assertions", () => {
298298
}
299299
});
300300

301-
it("rejects a fresh install that recreates the retired main-agent auth database", () => {
301+
it("permits an unrelated main-agent database", () => {
302302
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-onboard-assertions-"));
303303
const legacyAgentDir = path.join(tempDir, ".openclaw", "agents", "main", "agent");
304304

@@ -319,8 +319,8 @@ describe("npm onboard channel agent assertions", () => {
319319

320320
const result = runOnboardAssert(tempDir);
321321

322-
expect(result.status).not.toBe(0);
323-
expect(result.stderr).toContain("onboard created the retired main-agent auth database");
322+
expect(result.status).toBe(0);
323+
expect(result.stderr).toBe("");
324324
} finally {
325325
fs.rmSync(tempDir, { force: true, recursive: true });
326326
}

0 commit comments

Comments
 (0)