test(server): clean up valkey stubbing in server/main.test.ts

This commit is contained in:
Tyler Gillispie 2026-08-23 21:24:21 -07:00
parent 8390dcac3d
commit 5db9108ecd

View File

@ -76,11 +76,7 @@ Deno.test("Tier 1 & 2: GET /api/forward-auth - Missing session", async () => {
headers: { "X-Forwarded-Host": "test.app.local" }, headers: { "X-Forwarded-Host": "test.app.local" },
}); });
let valkeyStub: any; const valkeyStub = stub(valkey, "get", (key: any) => {
if ((valkey.get as any).restore) {
valkeyStub = valkey.get as any;
} else {
valkeyStub = stub(valkey, "get", (key: any) => {
const k = String(key); const k = String(key);
if (k.startsWith("auth:app_by_host:")) { if (k.startsWith("auth:app_by_host:")) {
return Promise.resolve( return Promise.resolve(
@ -89,18 +85,14 @@ Deno.test("Tier 1 & 2: GET /api/forward-auth - Missing session", async () => {
} }
return Promise.resolve(null); return Promise.resolve(null);
}); });
}
const res = await app.request(req); const res = await app.request(req);
assertEquals(res.status, 401); assertEquals(res.status, 401);
valkeyStub.restore(); valkeyStub.restore();
}); });
Deno.test("Tier 1 & 2: GET /api/forward-auth - Expired session", async () => { Deno.test("Tier 1 & 2: GET /api/forward-auth - Expired session", async () => {
let valkeyStub: any; const valkeyStub = stub(valkey, "get", (key: any) => {
if ((valkey.get as any).restore) {
valkeyStub = valkey.get as any;
} else {
valkeyStub = stub(valkey, "get", (key: any) => {
const k = String(key); const k = String(key);
if (k.startsWith("auth:app_by_host:")) { if (k.startsWith("auth:app_by_host:")) {
return Promise.resolve( return Promise.resolve(
@ -109,7 +101,6 @@ Deno.test("Tier 1 & 2: GET /api/forward-auth - Expired session", async () => {
} }
return Promise.resolve(null); return Promise.resolve(null);
}); });
}
const req = new Request("http://localhost/api/forward-auth", { const req = new Request("http://localhost/api/forward-auth", {
headers: { headers: {
@ -131,11 +122,7 @@ Deno.test("Tier 1 & 2: GET /api/forward-auth - Suspended account", async () => {
setMockSql(() => Promise.resolve([mockUser])); setMockSql(() => Promise.resolve([mockUser]));
let valkeyStub: any; const valkeyStub = stub(valkey, "get", (key: any) => {
if ((valkey.get as any).restore) {
valkeyStub = valkey.get as any;
} else {
valkeyStub = stub(valkey, "get", (key: any) => {
const k = String(key); const k = String(key);
if (k.startsWith("auth:app_by_host:")) { if (k.startsWith("auth:app_by_host:")) {
return Promise.resolve( return Promise.resolve(
@ -146,7 +133,6 @@ Deno.test("Tier 1 & 2: GET /api/forward-auth - Suspended account", async () => {
JSON.stringify({ uuid: "user-id-1", username: "alice" }), JSON.stringify({ uuid: "user-id-1", username: "alice" }),
); );
}); });
}
const req = new Request("http://localhost/api/forward-auth", { const req = new Request("http://localhost/api/forward-auth", {
headers: { headers: {
@ -177,17 +163,11 @@ Deno.test("Tier 3: ValidateSession ConnectRPC - Default-Deny", async () => {
}); });
// Re-define valkey stub // Re-define valkey stub
const valkeyStub = stub(valkey, "get", () => {
let valkeyStub: any;
if ((valkey.get as any).restore) {
valkeyStub = valkey.get as any;
} else {
valkeyStub = stub(valkey, "get", () => {
return Promise.resolve( return Promise.resolve(
JSON.stringify({ uuid: "user-1", username: "alice" }), JSON.stringify({ uuid: "user-1", username: "alice" }),
); );
}); });
}
let auditCalled = false; let auditCalled = false;
const originalAudit = auditWrapper.auditLog; const originalAudit = auditWrapper.auditLog;
@ -246,16 +226,11 @@ Deno.test("Tier 3: ValidateSession ConnectRPC - Valid RBAC Grant", async () => {
} }
}); });
let valkeyStub: any; const valkeyStub = stub(valkey, "get", () => {
if ((valkey.get as any).restore) {
valkeyStub = valkey.get as any;
} else {
valkeyStub = stub(valkey, "get", () => {
return Promise.resolve( return Promise.resolve(
JSON.stringify({ uuid: "user-1", username: "alice" }), JSON.stringify({ uuid: "user-1", username: "alice" }),
); );
}); });
}
const req = new Request( const req = new Request(
"http://localhost/auth.v1.AuthService/ValidateSession", "http://localhost/auth.v1.AuthService/ValidateSession",