feat(houseofrc): save the club API key server-side and test the connection (1.78.0)
This commit is contained in:
1 parent
7f7af2600d
commit
0c54e8b58a
11 files changed
+280
-4
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { spawn, spawnSync } from "node:child_process";
|
||||
import fs, { mkdtempSync, rmSync, readFileSync } from "node:fs";
|
||||
import fs, { mkdtempSync, rmSync, readFileSync, statSync, existsSync } from "node:fs";
|
||||
import { createServer } from "node:net";
|
||||
import { tmpdir } from "node:os";
|
||||
import { dirname, join } from "node:path";
|
||||
@@ -726,3 +726,58 @@ test("Race Controller Master: one browser holds the lock, take-over needs force
|
||||
rmSync(dataDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("House of RC: the key is stored server-side only, never returned, and the test call uses it", { timeout: 20000 }, async () => {
|
||||
const { createServer } = await import("node:http");
|
||||
const seen = [];
|
||||
const mock = createServer((req, res) => {
|
||||
seen.push({ url: req.url, auth: req.headers.authorization });
|
||||
const ok = req.headers.authorization === "Bearer hrc_test_secret_key_1234";
|
||||
res.writeHead(ok ? 200 : 401, { "Content-Type": "application/json" });
|
||||
res.end(JSON.stringify(ok ? { data: { user: { id: 68, name: "Test Club" }, rate_limit: 60 } } : { error: "unauthorized" }));
|
||||
});
|
||||
await new Promise((resolve) => mock.listen(0, "127.0.0.1", resolve));
|
||||
const dataDir = mkdtempSync(join(tmpdir(), "live-rc-test-hor-"));
|
||||
const port = await reservePort();
|
||||
const baseUrl = `http://127.0.0.1:${port}`;
|
||||
const child = spawn(process.execPath, [join(projectRoot, "server.js")], {
|
||||
cwd: projectRoot,
|
||||
env: { ...process.env, HOST: "127.0.0.1", PORT: String(port), RC_TIMING_DATA_DIR: dataDir, RC_TIMING_PID_FILE: "", HOUSEOFRC_API_KEY: "", HOUSEOFRC_BASE_URL: `http://127.0.0.1:${mock.address().port}` },
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
});
|
||||
const post = (path, body, cookie = "") => fetch(`${baseUrl}${path}`, { method: "POST", headers: { "Content-Type": "application/json", ...(cookie ? { Cookie: cookie } : {}) }, body: JSON.stringify(body) });
|
||||
const status = async () => (await fetch(`${baseUrl}/api/houseofrc/status`)).json();
|
||||
|
||||
try {
|
||||
await waitForServer(child);
|
||||
assert.equal((await status()).configured, false);
|
||||
assert.equal((await (await post("/api/houseofrc/test", {})).json()).error, "not_configured");
|
||||
assert.equal((await post("/api/houseofrc/key", { apiKey: "short" })).status, 400, "rejects something that is not a key");
|
||||
|
||||
assert.equal((await post("/api/houseofrc/key", { apiKey: "hrc_wrong_key_000000" })).status, 200);
|
||||
assert.equal((await (await post("/api/houseofrc/test", {})).json()).error, "unauthorized", "a rejected key is reported as such");
|
||||
|
||||
const saved = await (await post("/api/houseofrc/key", { apiKey: " hrc_test_secret_key_1234 " })).json();
|
||||
assert.deepEqual([saved.configured, saved.source, saved.keyHint], [true, "file", "…1234"]);
|
||||
const test = await (await post("/api/houseofrc/test", {})).json();
|
||||
assert.deepEqual([test.ok, test.user.name, test.rateLimit], [true, "Test Club", 60]);
|
||||
assert.equal(seen.at(-1).url, "/v1/me");
|
||||
|
||||
const keyFile = join(dataDir, "houseofrc.json");
|
||||
assert.equal(statSync(keyFile).mode & 0o777, 0o600, "only the server user can read the key file");
|
||||
for (const path of ["/api/houseofrc/status", "/api/state"]) {
|
||||
assert.ok(!(await (await fetch(`${baseUrl}${path}`)).text()).includes("hrc_test_secret"), `${path} never returns the key`);
|
||||
}
|
||||
|
||||
const cookie = (await post("/api/auth/pin", { newPin: "4711" })).headers.get("set-cookie").split(";")[0];
|
||||
assert.equal((await post("/api/houseofrc/key", { apiKey: "" })).status, 401, "with a PIN, changing the key needs login");
|
||||
assert.equal((await post("/api/houseofrc/test", {})).status, 401, "and so does using it");
|
||||
assert.equal((await post("/api/houseofrc/key", { apiKey: "" }, cookie)).status, 200);
|
||||
assert.equal((await status()).configured, false, "an empty key removes it");
|
||||
assert.equal(existsSync(keyFile), false);
|
||||
} finally {
|
||||
await stopServer(child);
|
||||
mock.close();
|
||||
rmSync(dataDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user