CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | /**
* Block L2 — one-command install.
*
* Coverage:
* - GET /install returns 200 + the bash script + correct headers
* - POST /api/v2/auth/install-token refuses Bearer-token callers
* - POST /api/v2/auth/install-token refuses unauthenticated callers
* - POST /api/v2/auth/install-token mints a PAT + writes an audit row when
* called over a real session cookie (DB-backed)
*
* The DB-backed test is gated on `DATABASE_URL` so the suite still runs in
* environments without Postgres — matching the convention used elsewhere
* (see `api-tokens.test.ts`).
*/
import { describe, it, expect } from "bun:test";
import app from "../app";
import { INSTALL_SCRIPT_SRC, SELF_HOST_SCRIPT_SRC } from "../routes/install";
const HAS_DB = Boolean(process.env.DATABASE_URL);
// ---------------------------------------------------------------------------
// 1. GET /install — the curl-able installer
// ---------------------------------------------------------------------------
describe("install — GET /install", () => {
it("returns 200 with the bash script body", async () => {
const res = await app.request("/install");
expect(res.status).toBe(200);
const body = await res.text();
expect(body.length).toBeGreaterThan(0);
expect(body.startsWith("#!")).toBe(true);
});
it("serves Content-Type: text/x-shellscript", async () => {
const res = await app.request("/install");
const ct = res.headers.get("content-type") || "";
expect(ct).toContain("text/x-shellscript");
});
it("serves a public Cache-Control so a CDN can absorb load", async () => {
const res = await app.request("/install");
const cc = res.headers.get("cache-control") || "";
expect(cc).toContain("public");
expect(cc).toContain("max-age=300");
});
it("script body contains the key install-flow markers", async () => {
const res = await app.request("/install");
const body = await res.text();
// Sanity-check the actual script (or fallback) loaded into memory.
expect(body).toContain("#!/usr/bin/env bash");
if (INSTALL_SCRIPT_SRC.includes("set -euo pipefail")) {
// Real script path — assert the user-facing flow it promises.
expect(body).toContain("set -euo pipefail");
expect(body).toContain("/api/v2/auth/install-token");
expect(body).toContain("claude_desktop_config.json");
expect(body).toContain("mcpServers");
}
});
it("INSTALL_SCRIPT_SRC exports a non-empty bash script", () => {
expect(INSTALL_SCRIPT_SRC.length).toBeGreaterThan(0);
expect(INSTALL_SCRIPT_SRC.startsWith("#!")).toBe(true);
});
});
// ---------------------------------------------------------------------------
// 1a. GET /install-server — self-host single-binary installer
// ---------------------------------------------------------------------------
describe("install — GET /install-server", () => {
it("returns 200 with the self-host bash script body", async () => {
const res = await app.request("/install-server");
expect(res.status).toBe(200);
const body = await res.text();
expect(body.length).toBeGreaterThan(0);
expect(body.startsWith("#!/usr/bin/env bash")).toBe(true);
});
it("serves Content-Type: text/x-shellscript", async () => {
const res = await app.request("/install-server");
const ct = res.headers.get("content-type") || "";
expect(ct).toContain("text/x-shellscript");
});
it("serves a public Cache-Control so a CDN can absorb load", async () => {
const res = await app.request("/install-server");
const cc = res.headers.get("cache-control") || "";
expect(cc).toContain("public");
expect(cc).toContain("max-age=300");
});
it("script body contains the key self-host install markers", async () => {
const res = await app.request("/install-server");
const body = await res.text();
// Sanity-check the real script's contract.
if (SELF_HOST_SCRIPT_SRC.includes("set -Eeuo pipefail")) {
expect(body).toContain("set -Eeuo pipefail");
// Platform detection.
expect(body).toContain("uname -s");
// Binary download path matches the /dist/:filename route.
expect(body).toContain("/dist/SHA256SUMS");
expect(body).toContain("gluecron-server-");
// Sha-256 verification gate is present.
expect(body).toContain("SHA-256 mismatch");
// Service installation.
expect(body).toContain("systemd");
}
});
it("rewrites the default HOST to the request origin", async () => {
const res = await app.request("/install-server", {
headers: {
"x-forwarded-proto": "https",
"x-forwarded-host": "self-host.example",
},
});
const body = await res.text();
if (SELF_HOST_SCRIPT_SRC.includes("https://gluecron.com")) {
// The rewrite leaves an explicit fallback to the inbound origin.
expect(body).toContain("https://self-host.example");
}
});
it("SELF_HOST_SCRIPT_SRC exports a non-empty bash script", () => {
expect(SELF_HOST_SCRIPT_SRC.length).toBeGreaterThan(0);
expect(SELF_HOST_SCRIPT_SRC.startsWith("#!")).toBe(true);
});
});
// ---------------------------------------------------------------------------
// 1c. GET /dist/:filename — binary release endpoint
// ---------------------------------------------------------------------------
describe("install — GET /dist/:filename", () => {
it("404s for filenames that don't exist", async () => {
const res = await app.request("/dist/definitely-not-a-real-binary");
expect(res.status).toBe(404);
});
it("rejects path traversal attempts", async () => {
// The route param regex blocks `..` outright — Hono URI-decodes %2F
// back to a slash so the param literally contains the traversal.
const res = await app.request("/dist/..%2Fpackage.json");
expect(res.status).toBe(404);
const body = await res.json();
expect(typeof body.error).toBe("string");
});
});
// ---------------------------------------------------------------------------
// 1b. GET /install/vscode — VS Code extension landing
// ---------------------------------------------------------------------------
describe("install — GET /install/vscode", () => {
it("returns 200 with an HTML install page", async () => {
const res = await app.request("/install/vscode");
expect(res.status).toBe(200);
const ct = res.headers.get("content-type") || "";
expect(ct).toContain("text/html");
const body = await res.text();
expect(body).toContain("Gluecron for VS Code");
// Points users at the extension source.
expect(body).toContain("editor-extensions/vscode");
});
it("serves a cacheable response", async () => {
const res = await app.request("/install/vscode");
const cc = res.headers.get("cache-control") || "";
expect(cc).toContain("public");
});
});
// ---------------------------------------------------------------------------
// 1c. GET /install/jetbrains — JetBrains plugin landing
// ---------------------------------------------------------------------------
describe("install — GET /install/jetbrains", () => {
it("returns 200 with an HTML install page", async () => {
const res = await app.request("/install/jetbrains");
expect(res.status).toBe(200);
const ct = res.headers.get("content-type") || "";
expect(ct).toContain("text/html");
const body = await res.text();
expect(body).toContain("Gluecron for JetBrains");
// Points users at the plugin source + names the JetBrains IDEs we cover.
expect(body).toContain("editor-extensions/jetbrains");
expect(body).toContain("IntelliJ");
expect(body).toContain("WebStorm");
});
it("serves a cacheable response", async () => {
const res = await app.request("/install/jetbrains");
const cc = res.headers.get("cache-control") || "";
expect(cc).toContain("public");
});
});
// ---------------------------------------------------------------------------
// 2. POST /api/v2/auth/install-token — auth contract
// ---------------------------------------------------------------------------
describe("install-token — auth contract", () => {
it("rejects Bearer tokens outright with 401 JSON", async () => {
// Unknown / unresolvable bearer — the apiAuth middleware short-circuits
// before our handler runs, but the contract for the caller is the same:
// 401 JSON with an `error` string. The whole point is that a Bearer
// caller *never* gets a 200 + a fresh PAT.
const res = await app.request("/api/v2/auth/install-token", {
method: "POST",
headers: {
"content-type": "application/json",
authorization: "Bearer glc_" + "a".repeat(64),
},
body: JSON.stringify({ name: "abuse", scope: "admin" }),
});
expect(res.status).toBe(401);
const body = await res.json();
expect(typeof body.error).toBe("string");
expect(body.error.length).toBeGreaterThan(0);
});
it("rejects malformed Bearer (no token) the same way", async () => {
const res = await app.request("/api/v2/auth/install-token", {
method: "POST",
headers: {
"content-type": "application/json",
authorization: "Bearer ",
},
body: JSON.stringify({}),
});
// Either the apiAuth middleware fails the bearer lookup (401) or we
// hit our own bearer-reject branch (401). Either is acceptable; the
// important invariant is "never 200".
expect(res.status).toBe(401);
});
it("rejects unauthenticated callers with 401 JSON", async () => {
const res = await app.request("/api/v2/auth/install-token", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({}),
});
expect(res.status).toBe(401);
const body = await res.json();
expect(typeof body.error).toBe("string");
expect(JSON.stringify(body).toLowerCase()).toContain("session");
});
it("rejects an empty body without a session", async () => {
const res = await app.request("/api/v2/auth/install-token", {
method: "POST",
});
expect(res.status).toBe(401);
});
});
// ---------------------------------------------------------------------------
// 3. POST /api/v2/auth/install-token — successful mint (DB-backed)
// ---------------------------------------------------------------------------
describe("install-token — successful mint", () => {
it.skipIf(!HAS_DB)(
"mints a glc_ PAT + writes auth.install_token.created audit row",
async () => {
const { db } = await import("../db");
const { users, sessions, apiTokens, auditLog } = await import(
"../db/schema"
);
const { eq, and, desc } = await import("drizzle-orm");
// Set up a one-off user + session purely for this test.
const uname = "install-test-" + Math.random().toString(36).slice(2, 10);
const [user] = await db
.insert(users)
.values({
username: uname,
email: `${uname}@example.com`,
passwordHash: "x",
})
.returning();
const sessionToken =
"sess_test_" + Math.random().toString(36).slice(2) + Date.now();
const expiresAt = new Date(Date.now() + 60_000);
await db.insert(sessions).values({
userId: user.id,
token: sessionToken,
expiresAt,
});
try {
const res = await app.request("/api/v2/auth/install-token", {
method: "POST",
headers: {
"content-type": "application/json",
cookie: `session=${sessionToken}`,
},
body: JSON.stringify({ name: "ci-install", scope: "admin" }),
});
expect(res.status).toBe(201);
const body = await res.json();
expect(typeof body.token).toBe("string");
expect(body.token.startsWith("glc_")).toBe(true);
expect(body.token.length).toBe("glc_".length + 64);
expect(body.name).toBe("ci-install");
expect(body.scope).toBe("admin");
expect(body.id).toBeDefined();
// PAT row exists with the right prefix + scopes.
const [row] = await db
.select()
.from(apiTokens)
.where(eq(apiTokens.id, body.id))
.limit(1);
expect(row).toBeDefined();
expect(row!.userId).toBe(user.id);
expect(row!.name).toBe("ci-install");
expect(row!.scopes).toContain("admin");
expect(row!.tokenPrefix).toBe(body.token.slice(0, 12));
// Audit row written under the expected action name.
const [audit] = await db
.select()
.from(auditLog)
.where(
and(
eq(auditLog.userId, user.id),
eq(auditLog.action, "auth.install_token.created")
)
)
.orderBy(desc(auditLog.createdAt))
.limit(1);
expect(audit).toBeDefined();
expect(audit!.targetType).toBe("api_token");
expect(audit!.targetId).toBe(body.id);
} finally {
// Best-effort cleanup. Cascade on users covers sessions + tokens.
try {
await db.delete(users).where(eq(users.id, user.id));
} catch {
/* ignore */
}
}
}
);
it.skipIf(!HAS_DB)(
"defaults name + scope when body is empty",
async () => {
const { db } = await import("../db");
const { users, sessions, apiTokens } = await import("../db/schema");
const { eq } = await import("drizzle-orm");
const uname = "install-test2-" + Math.random().toString(36).slice(2, 10);
const [user] = await db
.insert(users)
.values({
username: uname,
email: `${uname}@example.com`,
passwordHash: "x",
})
.returning();
const sessionToken =
"sess_test_" + Math.random().toString(36).slice(2) + Date.now();
await db.insert(sessions).values({
userId: user.id,
token: sessionToken,
expiresAt: new Date(Date.now() + 60_000),
});
try {
const res = await app.request("/api/v2/auth/install-token", {
method: "POST",
headers: {
"content-type": "application/json",
cookie: `session=${sessionToken}`,
},
body: "",
});
expect(res.status).toBe(201);
const body = await res.json();
expect(body.scope).toBe("admin");
expect(typeof body.name).toBe("string");
expect(body.name.startsWith("gluecron-install-")).toBe(true);
const [row] = await db
.select()
.from(apiTokens)
.where(eq(apiTokens.id, body.id))
.limit(1);
expect(row).toBeDefined();
} finally {
try {
await db.delete(users).where(eq(users.id, user.id));
} catch {
/* ignore */
}
}
}
);
});
|