feat(houseofrc): fetch club events, classes and entries with transponders via the timing API (1.81.0)

This commit is contained in:
larssand committed 2026-11-08 15:53:57 +01:00
1 parent 9c1f5713e6
commit b0f2b92c04
15 files changed
+247 -149

No files matched your search

+30 -13
View File
@@ -733,18 +733,29 @@ test("House of RC: the key is stored server-side only, never returned, and the t
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";
const missing = ok && req.url.startsWith("/v1/events/") && req.url !== "/v1/events/2251";
res.writeHead(!ok ? 401 : missing ? 404 : 200, { "Content-Type": "application/json" });
if (missing) return res.end(JSON.stringify({ message: "Not found." }));
const forbidden = ok && req.url === "/v1/timing/events/3001/entries";
const missing = ok && req.url.startsWith("/v1/timing/events/") && !forbidden && req.url !== "/v1/timing/events/2251/entries";
res.writeHead(!ok ? 401 : forbidden ? 403 : missing ? 404 : 200, { "Content-Type": "application/json" });
if (missing || forbidden) return res.end(JSON.stringify({ message: forbidden ? "This API key may not access this event." : "Not found." }));
if (ok && req.url.startsWith("/v1/events?")) {
return res.end(JSON.stringify({ data: [
{ id: 2251, name: "Deltävling 2 Sundsvall", type: "race", status: "live", start: "2026-12-05T07:00:00Z", city: "Sundsvall", country: "SE", web_url: "https://houseofrc.com/events/2251" },
{ id: 3001, name: "Winter Cup", type: "race", status: null, start: "2026-12-06T07:00:00Z", city: "Oslo", country: "NO" },
], links: { next: null }, meta: {} }));
}
if (ok && req.url === "/v1/events/2251") return res.end(JSON.stringify({ data: { id: 2251, name: "Deltävling 2 Sundsvall", type: "race", start: "2026-12-05T07:00:00Z", city: "Sundsvall", country: "SE", web_url: "https://houseofrc.com/events/2251" } }));
if (ok && req.url === "/results?event_id=2251") {
return res.end(JSON.stringify([{ user_id: 68, name: "Anna Andersson", firstname: "Anna", lastname: "Andersson", number: 7, points: 0, club: "Östersunds RC", country: "Sweden", product: "X12", brand1: "XRAY" }]));
if (ok && req.url === "/v1/timing/events/2251/entries") {
return res.end(JSON.stringify({ data: {
event: { id: 2251, name: "Deltävling 2 Sundsvall", type: "race", start: "2026-12-05T07:00:00Z", city: "Sundsvall", country: "SE", web_url: "https://houseofrc.com/events/2251" },
classes: [{ id: 555, name: "Offroad 2wd", order: 1 }, { id: 556, name: "Camping" }],
entries: [
{ id: 98765, class_id: 555, user_id: 68, status: "confirmed", driver_number: 7, transponders: ["2939397"], club: { id: 233, name: "Jämtlands MK" }, car: { id: 1, name: "X12" }, brands: [{ id: 70, name: "XRAY" }, null, null, null, null] },
{ id: 98766, class_id: 555, user_id: 69, status: "entrycancelled", transponders: [], club: null, car: null, brands: [] },
],
users: [
{ id: 68, name: "Anna Andersson", country: "SE", email: "anna@example.com", phone: "070-1234567", emergency_contact: { name: "Bo", info: "070-7654321" }, age: 41 },
{ id: 69, name: "Cancelled Carl", country: "SE", email: "carl@example.com" },
],
} }));
}
if (ok && req.url.startsWith("/timing/users")) {
// Newest owner first; an unknown number is simply missing.
@@ -753,7 +764,7 @@ test("House of RC: the key is stored server-side only, never returned, and the t
{ id: 9, name: "Old Owner", transponder: "1234567" },
]));
}
res.end(JSON.stringify(ok ? { data: { user: { id: 68, name: "Test Club" }, rate_limit: 60 } } : { error: "unauthorized" }));
res.end(JSON.stringify(ok ? { data: { organization: { id: 233, 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-"));
@@ -779,19 +790,25 @@ test("House of RC: the key is stored server-side only, never returned, and the t
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.deepEqual([test.ok, test.organization.name, test.rateLimit], [true, "Test Club", 60], "a club key answers with its organization");
assert.equal(seen.at(-1).url, "/v1/me");
const lookup = await (await post("/api/houseofrc/transponders", { transponders: ["1234567", "7654321", "1234567", "bad value"] })).json();
assert.equal(seen.at(-1).url, "/timing/users?transponders=1234567%2C7654321", "deduplicated, invalid numbers dropped");
assert.deepEqual(lookup.members, { 1234567: { id: 5, name: "Bo Berg", model: "B7", brand: "Associated" } }, "the newest owner wins");
const events = await (await post("/api/houseofrc/events", { from: "2026-12-01T00:00:00Z", to: "2026-12-31T23:59:59Z", country: "se" })).json();
assert.deepEqual(events.events.map((event) => event.id), [2251], "filtered to the chosen country");
assert.match(seen.at(-1).url, /^\/v1\/events\?start_from=2026-12-01.*sort=start&per_page=100&page=1$/);
const entries = await (await post("/api/houseofrc/entries", { eventId: "https://houseofrc.com/events/2251" })).json();
const events = await (await post("/api/houseofrc/events", { from: "2026-12-01", to: "2026-12-31" })).json();
assert.equal(events.organizationId, 233);
assert.equal(seen.at(-1).url, "/v1/events?parent_id=233&from=2026-12-01&to=2026-12-31&sort=start&per_page=100&page=1", "a club key lists the club's own events");
const response = await post("/api/houseofrc/entries", { eventId: "https://houseofrc.com/events/2251" });
const raw = await response.text();
assert.equal(seen.at(-1).url, "/v1/timing/events/2251/entries");
assert.ok(!/anna@|070-|emergency|"age"/.test(raw), "email, phone, emergency contact and age never reach the browser");
const entries = JSON.parse(raw);
assert.equal(entries.event.name, "Deltävling 2 Sundsvall");
assert.deepEqual(entries.entries, [{ houseOfRcId: 68, name: "Anna Andersson", number: 7, club: "Östersunds RC", country: "Sweden", car: "XRAY X12", brand: "XRAY", points: 0 }], "a pasted link works; car = make + model");
assert.deepEqual(entries.classes.map((item) => item.name), ["Offroad 2wd", "Camping"]);
assert.deepEqual(entries.entries, [{ entryId: 98765, classId: 555, houseOfRcId: 68, name: "Anna Andersson", country: "SE", number: 7, transponders: ["2939397"], club: "Jämtlands MK", car: "XRAY X12", status: "confirmed" }], "cancelled entries left out; car = make + model");
assert.equal((await post("/api/houseofrc/entries", { eventId: "nonsense" })).status, 400);
assert.equal((await (await post("/api/houseofrc/entries", { eventId: "999" })).json()).error, "event_not_found");
assert.equal((await (await post("/api/houseofrc/entries", { eventId: "3001" })).json()).error, "not_your_event");
const keyFile = join(dataDir, "houseofrc.json");
assert.equal(statSync(keyFile).mode & 0o777, 0o600, "only the server user can read the key file");