fix(keys): add DB fallback to updateEmailByCustomer, updateKeyEmail, and recover route (BUG-108, BUG-109, BUG-110)
All checks were successful
Build & Deploy to Staging / Build & Deploy to Staging (push) Successful in 13m8s
All checks were successful
Build & Deploy to Staging / Build & Deploy to Staging (push) Successful in 13m8s
- updateEmailByCustomer: DB fallback when stripe_customer_id not in cache - updateKeyEmail: DB fallback when key not in cache - POST /v1/recover: DB fallback when email not in cache (was only on verify) - 6 TDD tests added (keys-email-update.test.ts, recover-initial-db-fallback.test.ts) - 547 tests total, all passing
This commit is contained in:
parent
424a16ed8a
commit
d376d586fe
4 changed files with 286 additions and 4 deletions
109
src/__tests__/keys-email-update.test.ts
Normal file
109
src/__tests__/keys-email-update.test.ts
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
|
||||
// Override the global setup.ts mock for keys — we need the REAL implementation
|
||||
vi.unmock("../services/keys.js");
|
||||
|
||||
vi.mock("../services/db.js", () => ({
|
||||
default: { query: vi.fn(), connect: vi.fn(), on: vi.fn(), end: vi.fn() },
|
||||
pool: { query: vi.fn(), connect: vi.fn(), on: vi.fn(), end: vi.fn() },
|
||||
queryWithRetry: vi.fn().mockResolvedValue({ rows: [], rowCount: 0 }),
|
||||
connectWithRetry: vi.fn().mockResolvedValue(undefined),
|
||||
initDatabase: vi.fn().mockResolvedValue(undefined),
|
||||
cleanupStaleData: vi.fn(),
|
||||
isTransientError: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("../services/logger.js", () => ({
|
||||
default: { info: vi.fn(), error: vi.fn(), warn: vi.fn(), debug: vi.fn() },
|
||||
}));
|
||||
|
||||
import { queryWithRetry } from "../services/db.js";
|
||||
import { updateEmailByCustomer, updateKeyEmail } from "../services/keys.js";
|
||||
|
||||
const mockQuery = vi.mocked(queryWithRetry);
|
||||
|
||||
describe("updateEmailByCustomer DB fallback (BUG-108)", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("returns true and updates DB when key is NOT in cache but IS in DB", async () => {
|
||||
mockQuery
|
||||
.mockResolvedValueOnce({
|
||||
rows: [{
|
||||
key: "df_pro_abc123",
|
||||
tier: "pro",
|
||||
email: "old@example.com",
|
||||
created_at: "2026-01-01T00:00:00.000Z",
|
||||
stripe_customer_id: "cus_456",
|
||||
}],
|
||||
rowCount: 1,
|
||||
} as any)
|
||||
.mockResolvedValueOnce({ rows: [], rowCount: 1 } as any); // UPDATE
|
||||
|
||||
const result = await updateEmailByCustomer("cus_456", "new@example.com");
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(mockQuery).toHaveBeenCalledWith(
|
||||
expect.stringContaining("SELECT"),
|
||||
expect.arrayContaining(["cus_456"])
|
||||
);
|
||||
expect(mockQuery).toHaveBeenCalledWith(
|
||||
expect.stringContaining("UPDATE"),
|
||||
expect.arrayContaining(["new@example.com", "cus_456"])
|
||||
);
|
||||
});
|
||||
|
||||
it("returns false when key is NOT in cache AND NOT in DB", async () => {
|
||||
mockQuery.mockResolvedValueOnce({ rows: [], rowCount: 0 } as any);
|
||||
|
||||
const result = await updateEmailByCustomer("cus_nonexistent", "new@example.com");
|
||||
|
||||
expect(result).toBe(false);
|
||||
const updateCalls = mockQuery.mock.calls.filter(c => (c[0] as string).includes("UPDATE"));
|
||||
expect(updateCalls).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateKeyEmail DB fallback (BUG-109)", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("returns true and updates DB when key is NOT in cache but IS in DB", async () => {
|
||||
mockQuery
|
||||
.mockResolvedValueOnce({
|
||||
rows: [{
|
||||
key: "df_pro_xyz789",
|
||||
tier: "pro",
|
||||
email: "old@example.com",
|
||||
created_at: "2026-01-01T00:00:00.000Z",
|
||||
stripe_customer_id: "cus_789",
|
||||
}],
|
||||
rowCount: 1,
|
||||
} as any)
|
||||
.mockResolvedValueOnce({ rows: [], rowCount: 1 } as any); // UPDATE
|
||||
|
||||
const result = await updateKeyEmail("df_pro_xyz789", "new@example.com");
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(mockQuery).toHaveBeenCalledWith(
|
||||
expect.stringContaining("SELECT"),
|
||||
expect.arrayContaining(["df_pro_xyz789"])
|
||||
);
|
||||
expect(mockQuery).toHaveBeenCalledWith(
|
||||
expect.stringContaining("UPDATE"),
|
||||
expect.arrayContaining(["new@example.com", "df_pro_xyz789"])
|
||||
);
|
||||
});
|
||||
|
||||
it("returns false when key is NOT in cache AND NOT in DB", async () => {
|
||||
mockQuery.mockResolvedValueOnce({ rows: [], rowCount: 0 } as any);
|
||||
|
||||
const result = await updateKeyEmail("df_pro_nonexistent", "new@example.com");
|
||||
|
||||
expect(result).toBe(false);
|
||||
const updateCalls = mockQuery.mock.calls.filter(c => (c[0] as string).includes("UPDATE"));
|
||||
expect(updateCalls).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue