- security headers on every response (strict CSP, nosniff, frame DENY, no-referrer, HSTS behind TLS proxy) and no x-powered-by; hero image set through the CSSOM so the CSP needs no inline styles - error handler: 400 without stack trace for malformed URLs/JSON and multer errors, 500 logged in full - image uploads: type detected from the file signature (PNG/JPEG/WebP), not the client MIME type; multer part limits - storage: atomic JSON writes (tmp + rename) with mode 600, dirs 700; refuse to start when groups.json is corrupted instead of recreating admin; initial admin password written to a 600 file instead of the logs - status file read asynchronously in the background and cached, one read at a time with a 3 s timeout, so a hung NFS mount never blocks requests; cleanup refused when the status is stale or empty - launches: 10 min cooldown after a successful start (status lags behind), ssh BatchMode/ConnectTimeout/IdentitiesOnly, remote stdout/stderr kept in the server log instead of the API response Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
629 lines
20 KiB
JavaScript
629 lines
20 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import test from "node:test";
|
|
|
|
const tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), "gameservers-control-"));
|
|
process.env.STORAGE_DIR = tmpRoot;
|
|
process.env.GAMESERVERS_STATUS_FILE = path.join(tmpRoot, "status.json");
|
|
fs.mkdirSync(path.join(tmpRoot, "server-images"), { recursive: true });
|
|
|
|
const { createApp } = await import("../src/app.js");
|
|
const { hashPassword } = await import("../src/services/crypto.js");
|
|
const { pruneLogs } = await import("../src/services/storage.js");
|
|
const { resetLoginThrottle } = await import("../src/services/loginThrottle.js");
|
|
const { refreshStatus } = await import("../src/services/status.js");
|
|
const { resetLaunchGuard } = await import("../src/services/launchGuard.js");
|
|
|
|
const servers = [
|
|
{
|
|
id: "offline-server",
|
|
game: "Offline Server",
|
|
address: "games.example:1111",
|
|
port: 1111,
|
|
startProfile: "TEST",
|
|
secrets: { password: "secret-1" },
|
|
},
|
|
{
|
|
id: "online-server",
|
|
game: "Online Server",
|
|
address: "games.example:2222",
|
|
port: 2222,
|
|
startProfile: "PALWORLD",
|
|
secrets: { serverPassword: "secret-2", adminSecret: "admin-secret" },
|
|
},
|
|
];
|
|
|
|
const adminLoginPassword = "admin-password";
|
|
// Signature PNG suivie de quelques octets: suffisant pour la detection du format.
|
|
const PNG_BYTES = Buffer.concat([
|
|
Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
|
|
Buffer.from("fake-png-body"),
|
|
]);
|
|
const playerPassword = "player-password";
|
|
|
|
fs.writeFileSync(
|
|
path.join(tmpRoot, "servers.json"),
|
|
`${JSON.stringify(servers, null, 2)}\n`
|
|
);
|
|
fs.writeFileSync(
|
|
process.env.GAMESERVERS_STATUS_FILE,
|
|
`${JSON.stringify(
|
|
{
|
|
servers: [
|
|
{ id: "offline-server", game: "Offline Server", address: "games.example:1111", port: 1111, active: false },
|
|
{ id: "online-server", game: "Online Server", address: "games.example:2222", port: 2222, active: true },
|
|
],
|
|
},
|
|
null,
|
|
2
|
|
)}\n`
|
|
);
|
|
const originalStatus = fs.readFileSync(process.env.GAMESERVERS_STATUS_FILE, "utf8");
|
|
fs.writeFileSync(
|
|
path.join(tmpRoot, "groups.json"),
|
|
`${JSON.stringify(
|
|
[
|
|
{
|
|
id: "admin",
|
|
name: "Admin",
|
|
admin: true,
|
|
passwordHash: await hashPassword(adminLoginPassword),
|
|
permissions: { servers: servers.map((server) => server.id) },
|
|
createdAt: new Date().toISOString(),
|
|
updatedAt: new Date().toISOString(),
|
|
},
|
|
{
|
|
id: "players",
|
|
name: "Players",
|
|
admin: false,
|
|
passwordHash: await hashPassword(playerPassword),
|
|
permissions: { servers: ["offline-server"] },
|
|
createdAt: new Date().toISOString(),
|
|
updatedAt: new Date().toISOString(),
|
|
},
|
|
],
|
|
null,
|
|
2
|
|
)}\n`
|
|
);
|
|
fs.writeFileSync(path.join(tmpRoot, "launch-logs.json"), "[]\n");
|
|
fs.writeFileSync(path.join(tmpRoot, "server-images.json"), "{}\n");
|
|
|
|
await refreshStatus();
|
|
|
|
const app = createApp();
|
|
const server = app.listen(0);
|
|
await new Promise((resolve) => server.once("listening", resolve));
|
|
const baseUrl = `http://127.0.0.1:${server.address().port}`;
|
|
|
|
test.after(() => server.close());
|
|
|
|
test("healthz answers without authentication", async () => {
|
|
const response = await request("/healthz");
|
|
assert.equal(response.status, 200);
|
|
assert.deepEqual(response.body, { ok: true });
|
|
});
|
|
|
|
test("rejects invalid password and accepts group password", async () => {
|
|
const rejected = await request("/api/login", {
|
|
method: "POST",
|
|
body: { password: "wrong" },
|
|
});
|
|
assert.equal(rejected.status, 401);
|
|
|
|
const accepted = await request("/api/login", {
|
|
method: "POST",
|
|
body: { password: playerPassword },
|
|
});
|
|
assert.equal(accepted.status, 200);
|
|
assert.deepEqual(accepted.body.session, { admin: false });
|
|
assert.equal(accepted.body.group, undefined);
|
|
assert.ok(accepted.body.token);
|
|
});
|
|
|
|
test("does not expose group details through non-admin session", async () => {
|
|
const token = await login(playerPassword);
|
|
const response = await request("/api/session", { token });
|
|
|
|
assert.equal(response.status, 200);
|
|
assert.deepEqual(response.body.session, { admin: false });
|
|
assert.equal(response.body.group, undefined);
|
|
});
|
|
|
|
test("filters servers and shows secrets for visible non-admin servers", async () => {
|
|
const token = await login(playerPassword);
|
|
const response = await request("/api/servers", { token });
|
|
|
|
assert.equal(response.status, 200);
|
|
assert.equal(response.body.servers.length, 1);
|
|
assert.equal(response.body.servers[0].id, "offline-server");
|
|
assert.equal(response.body.servers[0].secrets.password, "secret-1");
|
|
assert.equal(response.body.servers[0].hasSecrets, true);
|
|
assert.equal(response.body.servers[0].canStart, false);
|
|
});
|
|
|
|
test("admin sees all servers and secrets", async () => {
|
|
const token = await login(adminLoginPassword);
|
|
const response = await request("/api/servers", { token });
|
|
|
|
assert.equal(response.status, 200);
|
|
assert.equal(response.body.servers.length, 2);
|
|
assert.equal(response.body.servers[0].secrets.password, "secret-1");
|
|
assert.equal(response.body.servers[1].secrets.serverPassword, "secret-2");
|
|
assert.deepEqual(Object.keys(response.body.servers[1].secrets), ["serverPassword"]);
|
|
});
|
|
|
|
test("status file is authoritative for server list and names", async () => {
|
|
fs.writeFileSync(
|
|
process.env.GAMESERVERS_STATUS_FILE,
|
|
`${JSON.stringify(
|
|
{
|
|
servers: [
|
|
{
|
|
id: "offline-server",
|
|
game: "Renamed Server",
|
|
address: "games.example:1111",
|
|
port: 1111,
|
|
active: false,
|
|
},
|
|
{
|
|
id: "new-server",
|
|
game: "New Server",
|
|
address: "games.example:3333",
|
|
port: 3333,
|
|
active: false,
|
|
},
|
|
],
|
|
},
|
|
null,
|
|
2
|
|
)}\n`
|
|
);
|
|
|
|
await refreshStatus();
|
|
|
|
const token = await login(adminLoginPassword);
|
|
const response = await request("/api/servers", { token });
|
|
const names = response.body.servers.map((server) => server.game);
|
|
|
|
assert.deepEqual(names, ["Renamed Server", "New Server"]);
|
|
assert.equal(response.body.servers[0].id, "offline-server");
|
|
assert.equal(response.body.servers[0].canStart, true);
|
|
assert.equal(response.body.servers[1].id, "new-server");
|
|
assert.equal(response.body.servers[1].canStart, false);
|
|
|
|
fs.writeFileSync(process.env.GAMESERVERS_STATUS_FILE, originalStatus);
|
|
await refreshStatus();
|
|
});
|
|
|
|
test("starts an offline authorized server and writes a log", async () => {
|
|
try {
|
|
fs.writeFileSync(
|
|
process.env.GAMESERVERS_STATUS_FILE,
|
|
`${JSON.stringify(
|
|
{
|
|
servers: [
|
|
{
|
|
id: "offline-server",
|
|
game: "Offline Server",
|
|
address: "games.example:1111",
|
|
port: 1111,
|
|
active: false,
|
|
},
|
|
{
|
|
id: "online-server",
|
|
game: "Online Server",
|
|
address: "games.example:2222",
|
|
port: 2222,
|
|
active: false,
|
|
},
|
|
],
|
|
},
|
|
null,
|
|
2
|
|
)}\n`
|
|
);
|
|
|
|
const token = await login(playerPassword);
|
|
const start = await request("/api/servers/offline-server/start", {
|
|
method: "POST",
|
|
token,
|
|
});
|
|
|
|
assert.equal(start.status, 200);
|
|
assert.equal(start.body.status, "started");
|
|
|
|
const adminToken = await login(adminLoginPassword);
|
|
const logs = await request("/api/logs", { token: adminToken });
|
|
assert.equal(logs.status, 200);
|
|
assert.equal(logs.body.logs[0].groupId, "players");
|
|
assert.equal(logs.body.logs[0].serverId, "offline-server");
|
|
assert.equal(start.body.stdout, undefined);
|
|
} finally {
|
|
fs.writeFileSync(process.env.GAMESERVERS_STATUS_FILE, originalStatus);
|
|
await refreshStatus();
|
|
resetLaunchGuard();
|
|
}
|
|
});
|
|
|
|
test("refuses to start online or unauthorized servers", async () => {
|
|
const playerToken = await login(playerPassword);
|
|
const unauthorized = await request("/api/servers/online-server/start", {
|
|
method: "POST",
|
|
token: playerToken,
|
|
});
|
|
assert.equal(unauthorized.status, 404);
|
|
|
|
const adminToken = await login(adminLoginPassword);
|
|
const blockedByActiveServer = await request("/api/servers/offline-server/start", {
|
|
method: "POST",
|
|
token: adminToken,
|
|
});
|
|
assert.equal(blockedByActiveServer.status, 409);
|
|
assert.equal(blockedByActiveServer.body.error, "Un serveur est deja en cours");
|
|
|
|
const online = await request("/api/servers/online-server/start", {
|
|
method: "POST",
|
|
token: adminToken,
|
|
});
|
|
assert.equal(online.status, 409);
|
|
});
|
|
|
|
test("admin can create, update, regenerate, and delete a group", async () => {
|
|
const token = await login(adminLoginPassword);
|
|
const created = await request("/api/admin/groups", {
|
|
method: "POST",
|
|
token,
|
|
body: { name: "Guests", password: "manual-password", permissions: { servers: [] } },
|
|
});
|
|
assert.equal(created.status, 201);
|
|
assert.equal(created.body.password, "manual-password");
|
|
|
|
const manualLoginToken = await login("manual-password");
|
|
assert.ok(manualLoginToken);
|
|
|
|
const groupId = created.body.group.id;
|
|
const updated = await request(`/api/admin/groups/${groupId}/permissions`, {
|
|
method: "PATCH",
|
|
token,
|
|
body: { servers: ["offline-server"] },
|
|
});
|
|
assert.equal(updated.status, 200);
|
|
assert.deepEqual(updated.body.group.permissions.servers, ["offline-server"]);
|
|
|
|
const regenerated = await request(`/api/admin/groups/${groupId}/regenerate-password`, {
|
|
method: "POST",
|
|
token,
|
|
});
|
|
assert.equal(regenerated.status, 200);
|
|
assert.ok(regenerated.body.password);
|
|
|
|
const manualPassword = await request(`/api/admin/groups/${groupId}/password`, {
|
|
method: "PATCH",
|
|
token,
|
|
body: { password: "replacement-password" },
|
|
});
|
|
assert.equal(manualPassword.status, 200);
|
|
assert.equal(manualPassword.body.password, undefined);
|
|
const replacementLoginToken = await login("replacement-password");
|
|
assert.ok(replacementLoginToken);
|
|
|
|
const deleted = await request(`/api/admin/groups/${groupId}`, {
|
|
method: "DELETE",
|
|
token,
|
|
});
|
|
assert.equal(deleted.status, 200);
|
|
});
|
|
|
|
test("admin can upload and delete a server image", async () => {
|
|
const token = await login(adminLoginPassword);
|
|
const formData = new FormData();
|
|
formData.set(
|
|
"image",
|
|
new Blob([PNG_BYTES], { type: "image/png" }),
|
|
"server.png"
|
|
);
|
|
|
|
const uploaded = await request("/api/admin/servers/offline-server/image", {
|
|
method: "POST",
|
|
token,
|
|
body: formData,
|
|
});
|
|
assert.equal(uploaded.status, 200);
|
|
assert.match(uploaded.body.heroImage, /^\/server-images\/offline-server-\d+\.png$/);
|
|
|
|
const serversResponse = await request("/api/servers", { token });
|
|
assert.equal(serversResponse.body.servers[0].heroImage, uploaded.body.heroImage);
|
|
|
|
const deleted = await request("/api/admin/servers/offline-server/image", {
|
|
method: "DELETE",
|
|
token,
|
|
});
|
|
assert.equal(deleted.status, 200);
|
|
|
|
const refreshed = await request("/api/servers", { token });
|
|
assert.equal(refreshed.body.servers[0].heroImage, null);
|
|
});
|
|
|
|
test("admin can update a server password", async () => {
|
|
const token = await login(adminLoginPassword);
|
|
const updated = await request("/api/admin/servers/offline-server/password", {
|
|
method: "PATCH",
|
|
token,
|
|
body: { password: "new-server-password" },
|
|
});
|
|
|
|
assert.equal(updated.status, 200);
|
|
assert.equal(updated.body.hasPassword, true);
|
|
|
|
const serversResponse = await request("/api/servers", { token });
|
|
const server = serversResponse.body.servers.find((entry) => entry.id === "offline-server");
|
|
assert.equal(server.secrets.password, "new-server-password");
|
|
});
|
|
|
|
test("admin can cleanup local config for servers absent from status", async () => {
|
|
const token = await login(adminLoginPassword);
|
|
fs.writeFileSync(
|
|
path.join(tmpRoot, "servers.json"),
|
|
`${JSON.stringify(
|
|
[
|
|
...servers,
|
|
{
|
|
id: "deleted-server",
|
|
game: "Deleted Server",
|
|
address: "games.example:4444",
|
|
port: 4444,
|
|
startProfile: "DELETED",
|
|
secrets: { password: "old" },
|
|
},
|
|
],
|
|
null,
|
|
2
|
|
)}\n`
|
|
);
|
|
fs.writeFileSync(
|
|
path.join(tmpRoot, "server-images.json"),
|
|
`${JSON.stringify({ "deleted-server": { filename: "deleted-server.png", url: "/server-images/deleted-server.png" } })}\n`
|
|
);
|
|
fs.writeFileSync(path.join(tmpRoot, "server-images", "deleted-server.png"), "fake");
|
|
|
|
const cleaned = await request("/api/admin/servers/cleanup", {
|
|
method: "POST",
|
|
token,
|
|
});
|
|
|
|
assert.equal(cleaned.status, 200);
|
|
assert.deepEqual(cleaned.body.removedServers, [
|
|
{ id: "deleted-server", game: "Deleted Server" },
|
|
]);
|
|
assert.deepEqual(cleaned.body.removedImages, ["deleted-server"]);
|
|
|
|
const nextServers = JSON.parse(fs.readFileSync(path.join(tmpRoot, "servers.json"), "utf8"));
|
|
assert.equal(nextServers.some((server) => server.id === "deleted-server"), false);
|
|
assert.equal(fs.existsSync(path.join(tmpRoot, "server-images", "deleted-server.png")), false);
|
|
});
|
|
|
|
test("prunes launch logs older than seven days", () => {
|
|
const logsFile = path.join(tmpRoot, "launch-logs.json");
|
|
fs.writeFileSync(
|
|
logsFile,
|
|
JSON.stringify(
|
|
[
|
|
{ createdAt: new Date(Date.now() - 8 * 24 * 60 * 60 * 1000).toISOString(), groupId: "old" },
|
|
{ createdAt: new Date().toISOString(), groupId: "new" },
|
|
],
|
|
null,
|
|
2
|
|
)
|
|
);
|
|
|
|
const logs = pruneLogs();
|
|
assert.equal(logs.length, 1);
|
|
assert.equal(logs[0].groupId, "new");
|
|
});
|
|
|
|
test("rejects weak or already used group passwords", async () => {
|
|
const token = await login(adminLoginPassword);
|
|
|
|
const tooShort = await request("/api/admin/groups", {
|
|
method: "POST",
|
|
token,
|
|
body: { name: "Short", password: "short-pw" },
|
|
});
|
|
assert.equal(tooShort.status, 400);
|
|
|
|
const duplicateOnCreate = await request("/api/admin/groups", {
|
|
method: "POST",
|
|
token,
|
|
body: { name: "Copycat", password: playerPassword },
|
|
});
|
|
assert.equal(duplicateOnCreate.status, 400);
|
|
|
|
const duplicateOnUpdate = await request("/api/admin/groups/players/password", {
|
|
method: "PATCH",
|
|
token,
|
|
body: { password: adminLoginPassword },
|
|
});
|
|
assert.equal(duplicateOnUpdate.status, 400);
|
|
|
|
const sameGroup = await request("/api/admin/groups/players/password", {
|
|
method: "PATCH",
|
|
token,
|
|
body: { password: playerPassword },
|
|
});
|
|
assert.equal(sameGroup.status, 200);
|
|
|
|
const groups = await request("/api/admin/groups", { token });
|
|
assert.deepEqual(
|
|
groups.body.groups.map((group) => group.id).sort(),
|
|
["admin", "players"]
|
|
);
|
|
});
|
|
|
|
test("changing or regenerating a group password closes its other sessions", async () => {
|
|
const adminToken = await login(adminLoginPassword);
|
|
const created = await request("/api/admin/groups", {
|
|
method: "POST",
|
|
token: adminToken,
|
|
body: { name: "Session Group", password: "session-group-pw-1" },
|
|
});
|
|
const groupId = created.body.group.id;
|
|
|
|
const firstToken = await login("session-group-pw-1");
|
|
assert.equal((await request("/api/servers", { token: firstToken })).status, 200);
|
|
|
|
const changed = await request(`/api/admin/groups/${groupId}/password`, {
|
|
method: "PATCH",
|
|
token: adminToken,
|
|
body: { password: "session-group-pw-2" },
|
|
});
|
|
assert.equal(changed.status, 200);
|
|
assert.equal((await request("/api/servers", { token: firstToken })).status, 401);
|
|
|
|
const secondToken = await login("session-group-pw-2");
|
|
const regenerated = await request(`/api/admin/groups/${groupId}/regenerate-password`, {
|
|
method: "POST",
|
|
token: adminToken,
|
|
});
|
|
assert.equal(regenerated.status, 200);
|
|
assert.equal((await request("/api/servers", { token: secondToken })).status, 401);
|
|
|
|
const thirdToken = await login(regenerated.body.password);
|
|
await request(`/api/admin/groups/${groupId}`, { method: "DELETE", token: adminToken });
|
|
assert.equal((await request("/api/servers", { token: thirdToken })).status, 401);
|
|
});
|
|
|
|
test("admin keeps the session used to change the admin password", async () => {
|
|
const currentToken = await login(adminLoginPassword);
|
|
const otherToken = await login(adminLoginPassword);
|
|
|
|
const changed = await request("/api/admin/groups/admin/password", {
|
|
method: "PATCH",
|
|
token: currentToken,
|
|
body: { password: "temporary-admin-pw" },
|
|
});
|
|
assert.equal(changed.status, 200);
|
|
assert.equal((await request("/api/session", { token: currentToken })).status, 200);
|
|
assert.equal((await request("/api/session", { token: otherToken })).status, 401);
|
|
|
|
const restored = await request("/api/admin/groups/admin/password", {
|
|
method: "PATCH",
|
|
token: currentToken,
|
|
body: { password: adminLoginPassword },
|
|
});
|
|
assert.equal(restored.status, 200);
|
|
});
|
|
|
|
test("blocks login after too many attempts, even with spoofed X-Forwarded-For", async () => {
|
|
resetLoginThrottle();
|
|
try {
|
|
for (let attempt = 1; attempt <= 5; attempt += 1) {
|
|
const response = await request("/api/login", {
|
|
method: "POST",
|
|
body: { password: `wrong-${attempt}` },
|
|
headers: { "X-Forwarded-For": `203.0.113.${attempt}` },
|
|
});
|
|
assert.equal(response.status, 401);
|
|
}
|
|
|
|
const blocked = await request("/api/login", {
|
|
method: "POST",
|
|
body: { password: playerPassword },
|
|
headers: { "X-Forwarded-For": "203.0.113.99" },
|
|
});
|
|
assert.equal(blocked.status, 429);
|
|
} finally {
|
|
resetLoginThrottle();
|
|
}
|
|
|
|
assert.ok(await login(playerPassword));
|
|
});
|
|
|
|
test("rejects uploads whose content is not a real image", async () => {
|
|
const token = await login(adminLoginPassword);
|
|
const formData = new FormData();
|
|
formData.set(
|
|
"image",
|
|
new Blob([Buffer.from("<script>alert(1)</script>")], { type: "image/png" }),
|
|
"evil.png"
|
|
);
|
|
|
|
const uploaded = await request("/api/admin/servers/offline-server/image", {
|
|
method: "POST",
|
|
token,
|
|
body: formData,
|
|
});
|
|
assert.equal(uploaded.status, 400);
|
|
});
|
|
|
|
test("blocks a second launch during the cooldown after a successful start", async () => {
|
|
resetLaunchGuard();
|
|
try {
|
|
fs.writeFileSync(
|
|
process.env.GAMESERVERS_STATUS_FILE,
|
|
`${JSON.stringify({
|
|
servers: [
|
|
{ id: "offline-server", game: "Offline Server", address: "games.example:1111", port: 1111, active: false },
|
|
{ id: "online-server", game: "Online Server", address: "games.example:2222", port: 2222, active: false },
|
|
],
|
|
})}\n`
|
|
);
|
|
await refreshStatus();
|
|
|
|
const token = await login(adminLoginPassword);
|
|
const first = await request("/api/servers/offline-server/start", { method: "POST", token });
|
|
assert.equal(first.status, 200);
|
|
|
|
const list = await request("/api/servers", { token });
|
|
assert.ok(list.body.servers.every((entry) => entry.canStart === false));
|
|
|
|
const second = await request("/api/servers/offline-server/start", { method: "POST", token });
|
|
assert.equal(second.status, 409);
|
|
} finally {
|
|
fs.writeFileSync(process.env.GAMESERVERS_STATUS_FILE, originalStatus);
|
|
await refreshStatus();
|
|
resetLaunchGuard();
|
|
}
|
|
});
|
|
|
|
test("sends security headers and hides x-powered-by", async () => {
|
|
const response = await fetch(`${baseUrl}/`);
|
|
assert.equal(response.status, 200);
|
|
assert.match(response.headers.get("content-security-policy"), /script-src 'self'/);
|
|
assert.equal(response.headers.get("x-content-type-options"), "nosniff");
|
|
assert.equal(response.headers.get("x-frame-options"), "DENY");
|
|
assert.equal(response.headers.get("x-powered-by"), null);
|
|
});
|
|
|
|
test("answers 400 without a stack trace to malformed URLs", async () => {
|
|
const response = await fetch(`${baseUrl}/api/servers/%E0%A4%A/start`, { method: "POST" });
|
|
assert.ok(response.status === 400 || response.status === 401);
|
|
const malformedPage = await fetch(`${baseUrl}/%E0%A4%A`);
|
|
assert.equal(malformedPage.status, 400);
|
|
assert.doesNotMatch(await malformedPage.text(), /at decodeURIComponent|URIError/);
|
|
});
|
|
|
|
async function login(password) {
|
|
const response = await request("/api/login", {
|
|
method: "POST",
|
|
body: { password },
|
|
});
|
|
assert.equal(response.status, 200);
|
|
return response.body.token;
|
|
}
|
|
|
|
async function request(pathname, { method = "GET", token, body, headers: extraHeaders = {} } = {}) {
|
|
const headers = { ...extraHeaders };
|
|
if (token) headers.Authorization = `Bearer ${token}`;
|
|
if (body && !(body instanceof FormData)) headers["Content-Type"] = "application/json";
|
|
|
|
const response = await fetch(`${baseUrl}${pathname}`, {
|
|
method,
|
|
headers,
|
|
body: body && !(body instanceof FormData) ? JSON.stringify(body) : body,
|
|
});
|
|
const payload = await response.json();
|
|
return { status: response.status, body: payload };
|
|
}
|