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 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 | /**
* Block P1 — Password reset flow.
*
* Covers `src/lib/password-reset.ts` (token primitives, request creation,
* token consumption) and the `src/routes/password-reset.tsx` HTTP surface
* (forgot-password GET/POST, reset-password GET/POST).
*
* Strategy:
* - The library tests stub the `db` module (K1 spread-from-real pattern)
* so they don't require Neon. The stub stores rows in in-memory arrays
* keyed by `tableName(t)` and re-uses the same fluent chain shape that
* mcp-write.test.ts established.
* - Email sending is replaced via `__setEmailForTests` so we can assert
* "an email was queued for this user" without hitting a real provider.
* - The HTTP-surface tests use `app.request(...)` so the real Hono router
* answers — including the `csrfProtect` middleware (which exempts
* unauth POSTs via the session-cookie shortcut).
*
* All `mock.module(...)` calls capture the REAL module first and restore
* in `afterAll` so we don't poison every test file that runs after us.
*/
import {
describe,
it,
expect,
mock,
beforeEach,
afterEach,
afterAll,
} from "bun:test";
// Capture real modules BEFORE any mock.module() so afterAll can restore them.
const _real_db = await import("../db");
// ---------------------------------------------------------------------------
// In-memory DB stub
// ---------------------------------------------------------------------------
interface FakeRow { [k: string]: any; }
interface FakeStore {
users: FakeRow[];
password_reset_tokens: FakeRow[];
sessions: FakeRow[];
}
const store: FakeStore = { users: [], password_reset_tokens: [], sessions: [] };
function resetStore() {
store.users = [];
store.password_reset_tokens = [];
store.sessions = [];
}
function tableName(t: any): keyof FakeStore | "?" {
if (!t || typeof t !== "object") return "?";
if ("tokenHash" in t && "usedAt" in t) return "password_reset_tokens";
if ("token" in t && "expiresAt" in t && !("tokenHash" in t)) return "sessions";
if ("passwordHash" in t && "username" in t) return "users";
return "?";
}
let _whereFilter: any = null;
function makeEq(lhs: any, rhs: any) {
return { __op: "eq", lhs, rhs };
}
function colKey(lhs: any): string | null {
if (!lhs || typeof lhs !== "object") return null;
const name: string = lhs.name || "";
const camel = name.replace(/_([a-z])/g, (_: string, c: string) => c.toUpperCase());
return camel || null;
}
function applyFilter(rows: FakeRow[], where: any): FakeRow[] {
if (!where) return rows;
const k = colKey(where.lhs);
if (!k) return rows;
return rows.filter((r) => r[k] === where.rhs);
}
const _inserted: { table: string; values: any }[] = [];
let _lastSelectTable: keyof FakeStore | "?" = "?";
let _lastUpdateTable: keyof FakeStore | "?" = "?";
let _lastDeleteTable: keyof FakeStore | "?" = "?";
const selectChain: any = {
from(t: any) { _lastSelectTable = tableName(t); return selectChain; },
where(w: any) { _whereFilter = w; return selectChain; },
orderBy() { return selectChain; },
async limit(_n: number) {
const tbl = _lastSelectTable;
const where = _whereFilter;
_whereFilter = null;
if (tbl === "?") return [];
const rows = applyFilter(store[tbl] as FakeRow[], where);
return rows.slice(0, _n);
},
};
const fakeDb: any = {
select(_s?: any) { return selectChain; },
insert(t: any) {
const tbl = tableName(t);
return {
async values(v: any) {
const row = { ...v, id: v.id || crypto.randomUUID() };
if (tbl !== "?") (store[tbl] as FakeRow[]).push(row);
_inserted.push({ table: tbl, values: row });
return { rows: [row] };
},
returning() {
return {
async values(v: any) {
const row = { ...v, id: v.id || crypto.randomUUID() };
if (tbl !== "?") (store[tbl] as FakeRow[]).push(row);
_inserted.push({ table: tbl, values: row });
return [row];
},
};
},
};
},
update(t: any) {
_lastUpdateTable = tableName(t);
return {
set(s: any) {
const tbl = _lastUpdateTable;
return {
async where(w: any) {
if (tbl === "?") return;
const rows = applyFilter(store[tbl] as FakeRow[], w);
for (const r of rows) Object.assign(r, s);
},
};
},
};
},
delete(t: any) {
_lastDeleteTable = tableName(t);
return {
async where(w: any) {
const tbl = _lastDeleteTable;
if (tbl === "?") return;
const remaining = (store[tbl] as FakeRow[]).filter(
(r) => !applyFilter([r], w).length
);
(store[tbl] as FakeRow[]).length = 0;
(store[tbl] as FakeRow[]).push(...remaining);
},
};
},
};
mock.module("../db", () => ({ ..._real_db, db: fakeDb }));
const _real_drizzle = await import("drizzle-orm");
mock.module("drizzle-orm", () => ({
..._real_drizzle,
eq: (lhs: any, rhs: any) => makeEq(lhs, rhs),
}));
// ---------------------------------------------------------------------------
// Email seam
// ---------------------------------------------------------------------------
const _emails: { to: string; subject: string; text: string; html?: string }[] = [];
import {
generateResetToken,
createPasswordResetRequest,
consumeResetToken,
inspectResetToken,
buildResetUrl,
__setEmailForTests,
} from "../lib/password-reset";
__setEmailForTests((msg: any) => {
_emails.push({ to: msg.to, subject: msg.subject, text: msg.text, html: msg.html });
return { ok: true, provider: "log" as const };
});
afterAll(() => {
__setEmailForTests(null);
mock.module("../db", () => _real_db);
mock.module("drizzle-orm", () => _real_drizzle);
});
beforeEach(() => {
resetStore();
_inserted.length = 0;
_emails.length = 0;
});
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
async function sha256Hex(input: string): Promise<string> {
const buf = new TextEncoder().encode(input);
const digest = await crypto.subtle.digest("SHA-256", buf);
return Array.from(new Uint8Array(digest))
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
}
function seedUser(overrides: Partial<FakeRow> = {}): FakeRow {
const u: FakeRow = {
id: crypto.randomUUID(),
username: "ada",
email: "ada@example.com",
passwordHash: "$2b$10$oldhashplaceholder0123456789012345678901234567890123",
updatedAt: new Date(),
...overrides,
};
store.users.push(u);
return u;
}
// ---------------------------------------------------------------------------
// generateResetToken
// ---------------------------------------------------------------------------
describe("generateResetToken", () => {
it("emits 64 hex chars of plaintext + a matching sha256 hash", async () => {
const { plaintext, hash } = generateResetToken();
expect(plaintext).toMatch(/^[0-9a-f]{64}$/);
expect(hash).toMatch(/^[0-9a-f]{64}$/);
expect(hash).toBe(await sha256Hex(plaintext));
});
it("emits unique plaintexts on repeated calls", () => {
const a = generateResetToken();
const b = generateResetToken();
expect(a.plaintext).not.toBe(b.plaintext);
expect(a.hash).not.toBe(b.hash);
});
});
// ---------------------------------------------------------------------------
// createPasswordResetRequest
// ---------------------------------------------------------------------------
describe("createPasswordResetRequest", () => {
it("creates a token row and queues an email for a known user", async () => {
const u = seedUser({ email: "ada@example.com", username: "ada" });
const res = await createPasswordResetRequest("ada@example.com", { requestIp: "127.0.0.1" });
expect(res.ok).toBe(true);
await new Promise((r) => setTimeout(r, 5));
expect(store.password_reset_tokens.length).toBe(1);
const row = store.password_reset_tokens[0]!;
expect(row.userId).toBe(u.id);
expect(row.tokenHash).toMatch(/^[0-9a-f]{64}$/);
expect(row.expiresAt instanceof Date).toBe(true);
expect(row.requestIp).toBe("127.0.0.1");
expect(_emails.length).toBe(1);
expect(_emails[0]!.to).toBe("ada@example.com");
expect(_emails[0]!.subject).toBe("Reset your Gluecron password");
expect(_emails[0]!.text).toContain("Hi ada,");
expect(_emails[0]!.text).toContain("/reset-password?token=");
expect(_emails[0]!.text).toContain("utm_source=password_reset");
expect(_emails[0]!.html).toContain("Reset password");
});
it("returns ok for unknown emails (no enumeration) and does NOT create a token", async () => {
const res = await createPasswordResetRequest("nobody@example.com");
expect(res.ok).toBe(true);
await new Promise((r) => setTimeout(r, 5));
expect(store.password_reset_tokens.length).toBe(0);
expect(_emails.length).toBe(0);
});
it("returns ok for garbage inputs without throwing", async () => {
expect((await createPasswordResetRequest("")).ok).toBe(true);
expect((await createPasswordResetRequest("not-an-email")).ok).toBe(true);
expect(store.password_reset_tokens.length).toBe(0);
expect(_emails.length).toBe(0);
});
it("normalizes email case before lookup", async () => {
seedUser({ email: "ada@example.com", username: "ada" });
const res = await createPasswordResetRequest("ADA@Example.COM");
expect(res.ok).toBe(true);
await new Promise((r) => setTimeout(r, 5));
expect(store.password_reset_tokens.length).toBe(1);
});
});
// ---------------------------------------------------------------------------
// consumeResetToken
// ---------------------------------------------------------------------------
describe("consumeResetToken", () => {
it("rejects garbage tokens with reason=invalid", async () => {
const res = await consumeResetToken("not-a-real-token", "newpassword1");
expect(res.ok).toBe(false);
expect(res.reason).toBe("invalid");
});
it("rejects an empty token", async () => {
const res = await consumeResetToken("", "newpassword1");
expect(res.ok).toBe(false);
});
it("rejects passwords shorter than 8 chars with reason=weak", async () => {
const u = seedUser();
const { plaintext, hash } = generateResetToken();
store.password_reset_tokens.push({
id: crypto.randomUUID(),
userId: u.id,
tokenHash: hash,
expiresAt: new Date(Date.now() + 60_000),
usedAt: null,
});
const res = await consumeResetToken(plaintext, "short");
expect(res.ok).toBe(false);
expect(res.reason).toBe("weak");
});
it("rejects expired tokens", async () => {
const u = seedUser();
const { plaintext, hash } = generateResetToken();
store.password_reset_tokens.push({
id: crypto.randomUUID(),
userId: u.id,
tokenHash: hash,
expiresAt: new Date(Date.now() - 60_000),
usedAt: null,
});
const res = await consumeResetToken(plaintext, "longenoughpassword");
expect(res.ok).toBe(false);
expect(res.reason).toBe("expired");
});
it("rejects already-used tokens", async () => {
const u = seedUser();
const { plaintext, hash } = generateResetToken();
store.password_reset_tokens.push({
id: crypto.randomUUID(),
userId: u.id,
tokenHash: hash,
expiresAt: new Date(Date.now() + 60_000),
usedAt: new Date(Date.now() - 1000),
});
const res = await consumeResetToken(plaintext, "longenoughpassword");
expect(res.ok).toBe(false);
expect(res.reason).toBe("used");
});
it("happy path: rotates password hash, marks token used, drops sessions", async () => {
const u = seedUser({ passwordHash: "OLD-HASH" });
store.sessions.push({ id: crypto.randomUUID(), userId: u.id, token: "sess-1", expiresAt: new Date(Date.now() + 86_400_000) });
store.sessions.push({ id: crypto.randomUUID(), userId: u.id, token: "sess-2", expiresAt: new Date(Date.now() + 86_400_000) });
const other = seedUser({ username: "bob", email: "bob@example.com" });
store.sessions.push({ id: crypto.randomUUID(), userId: other.id, token: "sess-other", expiresAt: new Date(Date.now() + 86_400_000) });
const { plaintext, hash } = generateResetToken();
store.password_reset_tokens.push({
id: crypto.randomUUID(),
userId: u.id,
tokenHash: hash,
expiresAt: new Date(Date.now() + 60_000),
usedAt: null,
});
const res = await consumeResetToken(plaintext, "brandnewpass");
expect(res.ok).toBe(true);
const updatedUser = store.users.find((r) => r.id === u.id)!;
expect(updatedUser.passwordHash).not.toBe("OLD-HASH");
expect(updatedUser.passwordHash).not.toBe("brandnewpass");
expect(updatedUser.passwordHash.length).toBeGreaterThan(20);
const tokenRow = store.password_reset_tokens[0]!;
expect(tokenRow.usedAt instanceof Date).toBe(true);
expect(store.sessions.filter((s) => s.userId === u.id).length).toBe(0);
expect(store.sessions.filter((s) => s.userId === other.id).length).toBe(1);
});
it("a second consume of the same token is rejected as already used", async () => {
const u = seedUser();
const { plaintext, hash } = generateResetToken();
store.password_reset_tokens.push({
id: crypto.randomUUID(),
userId: u.id,
tokenHash: hash,
expiresAt: new Date(Date.now() + 60_000),
usedAt: null,
});
const first = await consumeResetToken(plaintext, "newpass-one");
expect(first.ok).toBe(true);
const second = await consumeResetToken(plaintext, "newpass-two");
expect(second.ok).toBe(false);
expect(second.reason).toBe("used");
});
});
// ---------------------------------------------------------------------------
// inspectResetToken
// ---------------------------------------------------------------------------
describe("inspectResetToken", () => {
it("reports valid for an unused, unexpired token", async () => {
const u = seedUser();
const { plaintext, hash } = generateResetToken();
store.password_reset_tokens.push({
id: crypto.randomUUID(),
userId: u.id,
tokenHash: hash,
expiresAt: new Date(Date.now() + 60_000),
usedAt: null,
});
const r = await inspectResetToken(plaintext);
expect(r.valid).toBe(true);
});
it("reports invalid for an unknown token", async () => {
const r = await inspectResetToken("not-a-real-token");
expect(r.valid).toBe(false);
expect(r.reason).toBe("invalid");
});
it("reports expired for an expired token", async () => {
const u = seedUser();
const { plaintext, hash } = generateResetToken();
store.password_reset_tokens.push({
id: crypto.randomUUID(),
userId: u.id,
tokenHash: hash,
expiresAt: new Date(Date.now() - 1000),
usedAt: null,
});
const r = await inspectResetToken(plaintext);
expect(r.valid).toBe(false);
expect(r.reason).toBe("expired");
});
});
// ---------------------------------------------------------------------------
// buildResetUrl
// ---------------------------------------------------------------------------
describe("buildResetUrl", () => {
it("includes the token and utm tag", () => {
const u = buildResetUrl("abc123");
expect(u).toContain("/reset-password?token=abc123");
expect(u).toContain("utm_source=password_reset");
});
});
// ---------------------------------------------------------------------------
// HTTP surface — pull in the app AFTER the module mocks are installed.
// ---------------------------------------------------------------------------
const app = (await import("../app")).default;
describe("GET /forgot-password", () => {
it("renders the email-entry form with a CSRF input", async () => {
const res = await app.request("/forgot-password");
expect(res.status).toBe(200);
const html = await res.text();
expect(html).toContain("Reset your password");
expect(html).toContain('name="email"');
expect(html).toContain('name="_csrf"');
expect(html).toContain("Send reset link");
});
it("?sent=1 renders the generic success page", async () => {
const res = await app.request("/forgot-password?sent=1");
expect(res.status).toBe(200);
const html = await res.text();
expect(html).toContain("Check your inbox");
expect(html).toContain("If we have an account");
});
});
describe("POST /forgot-password", () => {
it("always redirects to ?sent=1, regardless of whether the email exists", async () => {
seedUser({ email: "ada@example.com" });
const res1 = await app.request("/forgot-password", {
method: "POST",
headers: { "content-type": "application/x-www-form-urlencoded" },
body: new URLSearchParams({ email: "ada@example.com" }),
});
expect(res1.status).toBe(302);
expect(res1.headers.get("location")).toContain("/forgot-password?sent=1");
const res2 = await app.request("/forgot-password", {
method: "POST",
headers: { "content-type": "application/x-www-form-urlencoded" },
body: new URLSearchParams({ email: "ghost@example.com" }),
});
expect(res2.status).toBe(302);
expect(res2.headers.get("location")).toContain("/forgot-password?sent=1");
});
});
describe("GET /reset-password", () => {
it("renders the form when the token is valid", async () => {
const u = seedUser();
const { plaintext, hash } = generateResetToken();
store.password_reset_tokens.push({
id: crypto.randomUUID(),
userId: u.id,
tokenHash: hash,
expiresAt: new Date(Date.now() + 60_000),
usedAt: null,
});
const res = await app.request(`/reset-password?token=${encodeURIComponent(plaintext)}`);
expect(res.status).toBe(200);
const html = await res.text();
expect(html).toContain("Set a new password");
expect(html).toContain('name="password"');
expect(html).toContain('name="confirm"');
expect(html).toContain('name="token"');
});
it("shows the dead-link page for unknown tokens", async () => {
const res = await app.request("/reset-password?token=garbage");
expect(res.status).toBe(200);
const html = await res.text();
expect(html).toContain("This link is no longer valid");
expect(html).toContain("/forgot-password");
});
it("shows the dead-link page for expired tokens", async () => {
const u = seedUser();
const { plaintext, hash } = generateResetToken();
store.password_reset_tokens.push({
id: crypto.randomUUID(),
userId: u.id,
tokenHash: hash,
expiresAt: new Date(Date.now() - 60_000),
usedAt: null,
});
const res = await app.request(`/reset-password?token=${encodeURIComponent(plaintext)}`);
expect(res.status).toBe(200);
const html = await res.text();
expect(html).toContain("This link is no longer valid");
});
it("shows the dead-link page when no token query is present", async () => {
const res = await app.request("/reset-password");
expect(res.status).toBe(200);
const html = await res.text();
expect(html).toContain("This link is no longer valid");
});
});
describe("POST /reset-password", () => {
it("happy path: rotates password and redirects to /login?success=…", async () => {
const u = seedUser({ passwordHash: "OLD" });
const { plaintext, hash } = generateResetToken();
store.password_reset_tokens.push({
id: crypto.randomUUID(),
userId: u.id,
tokenHash: hash,
expiresAt: new Date(Date.now() + 60_000),
usedAt: null,
});
const res = await app.request("/reset-password", {
method: "POST",
headers: { "content-type": "application/x-www-form-urlencoded" },
body: new URLSearchParams({
token: plaintext,
password: "newpassword12",
confirm: "newpassword12",
}),
});
expect(res.status).toBe(302);
const loc = res.headers.get("location") || "";
expect(loc).toContain("/login");
expect(loc).toContain("success=");
const updated = store.users.find((r) => r.id === u.id)!;
expect(updated.passwordHash).not.toBe("OLD");
});
it("password / confirm mismatch redirects back with an error", async () => {
const u = seedUser();
const { plaintext, hash } = generateResetToken();
store.password_reset_tokens.push({
id: crypto.randomUUID(),
userId: u.id,
tokenHash: hash,
expiresAt: new Date(Date.now() + 60_000),
usedAt: null,
});
const res = await app.request("/reset-password", {
method: "POST",
headers: { "content-type": "application/x-www-form-urlencoded" },
body: new URLSearchParams({
token: plaintext,
password: "newpassword12",
confirm: "different12345",
}),
});
expect(res.status).toBe(302);
const loc = res.headers.get("location") || "";
expect(loc).toContain("/reset-password");
expect(loc).toContain("error=");
});
it("an invalid/used token renders the dead-link page", async () => {
const res = await app.request("/reset-password", {
method: "POST",
headers: { "content-type": "application/x-www-form-urlencoded" },
body: new URLSearchParams({
token: "not-a-real-token",
password: "newpassword12",
confirm: "newpassword12",
}),
});
expect(res.status).toBe(200);
const html = await res.text();
expect(html).toContain("This link is no longer valid");
});
});
// ---------------------------------------------------------------------------
// Rate limit — temporarily flip out of "test" env so the in-memory limiter
// is actually enforced.
// ---------------------------------------------------------------------------
describe("rate limit on /forgot-password", () => {
const _origNodeEnv = process.env.NODE_ENV;
const _origBunEnv = process.env.BUN_ENV;
afterEach(() => {
process.env.NODE_ENV = _origNodeEnv;
process.env.BUN_ENV = _origBunEnv;
});
it("returns 429 after 5 requests inside the 60s window", async () => {
process.env.NODE_ENV = "development";
process.env.BUN_ENV = "development";
const headers = {
"content-type": "application/x-www-form-urlencoded",
"x-forwarded-for": "203.0.113.55",
} as Record<string, string>;
const body = () => new URLSearchParams({ email: "ghost@example.com" });
for (let i = 0; i < 5; i++) {
const res = await app.request("/forgot-password", { method: "POST", headers, body: body() });
expect(res.status).toBe(302);
}
const sixth = await app.request("/forgot-password", { method: "POST", headers, body: body() });
expect(sixth.status).toBe(429);
});
});
|