test: cover legacy MCP token authentication

This commit is contained in:
tianma-if committed 2026-08-11 00:50:41 +08:00
1 parent 639e7e6335
commit 02c1dad066
1 file changed
+23 -8
+23 -8
View File
@@ -80,19 +80,34 @@ sqlite.run(
["nb_other", "ws_other", "Private", 10],
);
const token = "edgeever_mcp_test_token";
const tokenHash = new Bun.CryptoHasher("sha256").update(token).digest("hex");
// Simulate a token created before migration 0007 began retaining token plaintext.
// Upgraded rows have a valid hash and their original granular scopes, while
// token_value remains NULL and therefore cannot be copied again from the UI.
const legacyToken = "edgeever_mcp_test_token";
const legacyTokenHash = new Bun.CryptoHasher("sha256").update(legacyToken).digest("hex");
const legacyScopes = [
"read:notebooks",
"write:notebooks",
"read:memos",
"write:memos",
"read:resources",
"read:tags",
];
sqlite.run(
"INSERT INTO api_tokens (id, workspace_id, name, token_hash, token_value, scopes_json) VALUES (?, ?, ?, ?, ?, ?)",
"INSERT INTO api_tokens (id, workspace_id, name, token_hash, scopes_json) VALUES (?, ?, ?, ?, ?)",
[
"tok_test",
"ws_default",
"MCP regression",
tokenHash,
token,
JSON.stringify(["read:notebooks", "write:notebooks", "read:memos", "write:memos", "read:resources", "read:tags"]),
"MCP Token 1",
legacyTokenHash,
JSON.stringify(legacyScopes),
],
);
const storedLegacyToken = sqlite
.query("SELECT token_value, scopes_json FROM api_tokens WHERE id = ?")
.get("tok_test");
assert.equal(storedLegacyToken.token_value, null);
assert.deepEqual(JSON.parse(storedLegacyToken.scopes_json), legacyScopes);
const env = {
DB: new SqliteD1Database(sqlite),
@@ -105,7 +120,7 @@ const fetchMcp = (payload, options = {}) =>
new Request("https://edgeever.test/mcp", {
method: options.method ?? "POST",
headers: {
...(options.auth === false ? {} : { Authorization: `Bearer ${token}` }),
...(options.auth === false ? {} : { Authorization: `Bearer ${legacyToken}` }),
Accept: "application/json, text/event-stream",
"Content-Type": "application/json",
...(options.headers ?? {}),