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 | /**
* Connect Claude — user-facing one-click MCP setup page.
*
* Coverage:
* 1. GET /connect/claude without auth → 302 to /login
* 2. GET /connect/claude/dxt without auth → 302 to /login
* 3. GET /connect/claude with a real session cookie → 200 + body markers
* 4. GET /settings/claude → 302 redirect (alias path)
* 5. POST /connect/claude/token requires auth
* 6. GET /api/connect/status requires auth + returns JSON shape
*
* The DB-backed tests are gated on `DATABASE_URL` to keep the suite green in
* environments without Postgres — matching `install.test.ts` and friends.
*/
import { describe, it, expect } from "bun:test";
import app from "../app";
const HAS_DB = Boolean(process.env.DATABASE_URL);
// ---------------------------------------------------------------------------
// 1. Auth gating — unauthenticated callers
// ---------------------------------------------------------------------------
describe("connect-claude — auth gating", () => {
it("GET /connect/claude without a session → 302 /login", async () => {
const res = await app.request("/connect/claude");
expect(res.status).toBe(302);
expect(res.headers.get("location") || "").toContain("/login");
});
it("GET /connect/claude/dxt without a session → 302 /login", async () => {
const res = await app.request("/connect/claude/dxt");
expect(res.status).toBe(302);
expect(res.headers.get("location") || "").toContain("/login");
});
it("GET /settings/claude without a session → 302 (alias or /login)", async () => {
const res = await app.request("/settings/claude");
// Either the requireAuth middleware redirects to /login OR the alias
// redirects to /connect/claude (which itself requires auth). Both are
// 302 — what matters is "never 200 anon".
expect(res.status).toBe(302);
const loc = res.headers.get("location") || "";
expect(loc).toMatch(/\/login|\/connect\/claude/);
});
it("POST /connect/claude/token without a session → 302 /login", async () => {
const res = await app.request("/connect/claude/token", {
method: "POST",
headers: { "content-type": "application/json" },
body: "{}",
});
expect(res.status).toBe(302);
expect(res.headers.get("location") || "").toContain("/login");
});
it("GET /api/connect/status without a session → 302 /login", async () => {
const res = await app.request("/api/connect/status");
expect(res.status).toBe(302);
expect(res.headers.get("location") || "").toContain("/login");
});
});
// ---------------------------------------------------------------------------
// 2. DB-backed: a real session cookie reaches the rendered page.
// ---------------------------------------------------------------------------
describe("connect-claude — rendered surface (authed)", () => {
it.skipIf(!HAS_DB)(
"GET /connect/claude with a session → 200 + page markers",
async () => {
const { db } = await import("../db");
const { users, sessions } = await import("../db/schema");
const { eq } = await import("drizzle-orm");
const uname = "cc-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_cc_" + 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("/connect/claude", {
headers: { cookie: `session=${sessionToken}` },
});
expect(res.status).toBe(200);
const body = await res.text();
// Hero copy + the MCP endpoint must appear (the JSON snippet and CLI
// command both reference it).
expect(body).toContain("Connect Claude");
expect(body).toContain("/mcp");
// The personalized .dxt download link is rendered.
expect(body).toContain("/connect/claude/dxt");
// The user's username is rendered in the hero eyebrow.
expect(body).toContain(uname);
// Tools list — at least one known tool name is on the page.
expect(body).toContain("gluecron_repo_search");
} finally {
try {
await db.delete(users).where(eq(users.id, user.id));
} catch {
/* ignore */
}
}
}
);
it.skipIf(!HAS_DB)(
"POST /connect/claude/token with a session mints a glc_ PAT",
async () => {
const { db } = await import("../db");
const { users, sessions, apiTokens } = await import("../db/schema");
const { eq } = await import("drizzle-orm");
const uname = "cc-mint-" + 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_cc_" + 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("/connect/claude/token", {
method: "POST",
headers: {
cookie: `session=${sessionToken}`,
"content-type": "application/json",
},
body: "{}",
});
expect(res.status).toBe(201);
const body = await res.json();
expect(typeof body.token).toBe("string");
expect(body.token.startsWith("glc_")).toBe(true);
// Row exists with admin scope.
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!.scopes).toContain("admin");
} finally {
try {
await db.delete(users).where(eq(users.id, user.id));
} catch {
/* ignore */
}
}
}
);
it.skipIf(!HAS_DB)(
"GET /connect/claude/dxt with a session returns a zip with the token embedded",
async () => {
const { db } = await import("../db");
const { users, sessions } = await import("../db/schema");
const { eq } = await import("drizzle-orm");
const uname = "cc-dxt-" + 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_cc_" + 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("/connect/claude/dxt", {
headers: { cookie: `session=${sessionToken}` },
});
expect(res.status).toBe(200);
expect(res.headers.get("content-disposition") || "").toContain(
`gluecron-${uname}.dxt`
);
const buf = new Uint8Array(await res.arrayBuffer());
// Zip magic — PK\x03\x04
expect(buf[0]).toBe(0x50);
expect(buf[1]).toBe(0x4b);
expect(buf[2]).toBe(0x03);
expect(buf[3]).toBe(0x04);
// Must NOT cache (every download is a fresh PAT mint).
expect(res.headers.get("cache-control") || "").toContain("no-store");
} finally {
try {
await db.delete(users).where(eq(users.id, user.id));
} catch {
/* ignore */
}
}
}
);
});
|