|
8 | 8 | caller, |
9 | 9 | grantGuard, |
10 | 10 | requirePrincipal, |
| 11 | + resolveCaller, |
| 12 | + type ResolvedCaller, |
11 | 13 | type RouteDeps, |
12 | 14 | } from "./deps.ts"; |
13 | 15 |
|
@@ -115,3 +117,161 @@ grantGuard(deps(grantsWith(), requireGrant), "add"); |
115 | 117 | expect(called).toEqual({ resource: "memory", action: "add" }); |
116 | 118 | }); |
117 | 119 | }); |
| 120 | + |
| 121 | +describe("resolveCaller", () => { |
| 122 | + function fakeContext(): { |
| 123 | + ctx: Context<TenantEnv>; |
| 124 | + sets: Record<string, unknown>; |
| 125 | + jsonCalls: { body: unknown; status: number }[]; |
| 126 | + } { |
| 127 | + const sets: Record<string, unknown> = {}; |
| 128 | + const jsonCalls: { body: unknown; status: number }[] = []; |
| 129 | + const ctx = { |
| 130 | + get: (k: string) => sets[k], |
| 131 | + set: (k: string, v: unknown) => { |
| 132 | + sets[k] = v; |
| 133 | + }, |
| 134 | + json: (body: unknown, status: number) => { |
| 135 | + jsonCalls.push({ body, status }); |
| 136 | + return { body, status }; |
| 137 | + }, |
| 138 | + } as unknown as Context<TenantEnv>; |
| 139 | + return { ctx, sets, jsonCalls }; |
| 140 | + } |
| 141 | + |
| 142 | + test("is a no-op passthrough when no callerResolver is configured", async () => { |
| 143 | + const { ctx, sets, jsonCalls } = fakeContext(); |
| 144 | + let nextCalled = false; |
| 145 | + await resolveCaller(deps(grantsWith()))(ctx, async () => { |
| 146 | + nextCalled = true; |
| 147 | + }); |
| 148 | + expect(nextCalled).toBe(true); |
| 149 | + expect(jsonCalls).toHaveLength(0); |
| 150 | + expect(sets.principal).toBeUndefined(); |
| 151 | + expect(sets.tenant).toBeUndefined(); |
| 152 | + }); |
| 153 | + |
| 154 | + test("seats the resolved tenant/principal on the context and calls next()", async () => { |
| 155 | + const { ctx, sets, jsonCalls } = fakeContext(); |
| 156 | + const resolved: ResolvedCaller = { |
| 157 | + tenantId: "tenant-run", |
| 158 | + principalId: "run-principal", |
| 159 | + }; |
| 160 | + const routeDeps: RouteDeps = { |
| 161 | + ...deps(grantsWith()), |
| 162 | + callerResolver: () => resolved, |
| 163 | + }; |
| 164 | + let nextCalled = false; |
| 165 | + await resolveCaller(routeDeps)(ctx, async () => { |
| 166 | + nextCalled = true; |
| 167 | + }); |
| 168 | + expect(nextCalled).toBe(true); |
| 169 | + expect(jsonCalls).toHaveLength(0); |
| 170 | + expect(sets.principal).toMatchObject({ |
| 171 | + id: "run-principal", |
| 172 | + tenantId: "tenant-run", |
| 173 | + }); |
| 174 | + expect(sets.tenant).toMatchObject({ id: "tenant-run" }); |
| 175 | + expect(caller(ctx)).toEqual({ |
| 176 | + scopeId: "tenant-run", |
| 177 | + subjectId: "run-principal", |
| 178 | + }); |
| 179 | + }); |
| 180 | + |
| 181 | + test("responds 401 and never calls next() when the resolver rejects the request", async () => { |
| 182 | + const { ctx, sets, jsonCalls } = fakeContext(); |
| 183 | + const routeDeps: RouteDeps = { |
| 184 | + ...deps(grantsWith()), |
| 185 | + callerResolver: () => null, |
| 186 | + }; |
| 187 | + let nextCalled = false; |
| 188 | + await resolveCaller(routeDeps)(ctx, async () => { |
| 189 | + nextCalled = true; |
| 190 | + }); |
| 191 | + expect(nextCalled).toBe(false); |
| 192 | + expect(jsonCalls).toHaveLength(1); |
| 193 | + expect(jsonCalls[0]?.status).toBe(401); |
| 194 | + expect(jsonCalls[0]?.body).toMatchObject({ |
| 195 | + error: { code: "unauthorized" }, |
| 196 | + }); |
| 197 | + expect(sets.principal).toBeUndefined(); |
| 198 | + }); |
| 199 | + |
| 200 | + test("supports an async callerResolver", async () => { |
| 201 | + const { ctx, sets } = fakeContext(); |
| 202 | + const routeDeps: RouteDeps = { |
| 203 | + ...deps(grantsWith()), |
| 204 | + callerResolver: async () => ({ |
| 205 | + tenantId: "tenant-async", |
| 206 | + principalId: "principal-async", |
| 207 | + }), |
| 208 | + }; |
| 209 | + await resolveCaller(routeDeps)(ctx, async () => {}); |
| 210 | + expect(sets.principal).toMatchObject({ id: "principal-async" }); |
| 211 | + }); |
| 212 | + |
| 213 | + test("rejects an empty-string tenantId/principalId with 500, never seating it", async () => { |
| 214 | + const { ctx, sets, jsonCalls } = fakeContext(); |
| 215 | + const routeDeps: RouteDeps = { |
| 216 | + ...deps(grantsWith()), |
| 217 | + callerResolver: () => ({ tenantId: "", principalId: "" }), |
| 218 | + }; |
| 219 | + let nextCalled = false; |
| 220 | + await resolveCaller(routeDeps)(ctx, async () => { |
| 221 | + nextCalled = true; |
| 222 | + }); |
| 223 | + expect(nextCalled).toBe(false); |
| 224 | + expect(jsonCalls).toHaveLength(1); |
| 225 | + expect(jsonCalls[0]?.status).toBe(500); |
| 226 | + expect(jsonCalls[0]?.body).toMatchObject({ |
| 227 | + error: { code: "invalid_resolved_caller" }, |
| 228 | + }); |
| 229 | + expect(sets.principal).toBeUndefined(); |
| 230 | + expect(sets.tenant).toBeUndefined(); |
| 231 | + }); |
| 232 | + |
| 233 | + test("rejects a whitespace-only tenantId/principalId with 500, never seating it", async () => { |
| 234 | + // "string >= 1" is a LENGTH constraint -- " " has length 1 and would |
| 235 | + // pass it. This is the same class of bug PR #34 fixed in optionalEnv |
| 236 | + // (v.length > 0 accepted " "); this test is the regression guard for |
| 237 | + // it at this boundary. |
| 238 | + const { ctx, sets, jsonCalls } = fakeContext(); |
| 239 | + const routeDeps: RouteDeps = { |
| 240 | + ...deps(grantsWith()), |
| 241 | + callerResolver: () => ({ tenantId: " ", principalId: "\t\n" }), |
| 242 | + }; |
| 243 | + let nextCalled = false; |
| 244 | + await resolveCaller(routeDeps)(ctx, async () => { |
| 245 | + nextCalled = true; |
| 246 | + }); |
| 247 | + expect(nextCalled).toBe(false); |
| 248 | + expect(jsonCalls).toHaveLength(1); |
| 249 | + expect(jsonCalls[0]?.status).toBe(500); |
| 250 | + expect(jsonCalls[0]?.body).toMatchObject({ |
| 251 | + error: { code: "invalid_resolved_caller" }, |
| 252 | + }); |
| 253 | + expect(sets.principal).toBeUndefined(); |
| 254 | + expect(sets.tenant).toBeUndefined(); |
| 255 | + }); |
| 256 | + |
| 257 | + test("rejects a resolved value missing principalId with 500", async () => { |
| 258 | + const { ctx, jsonCalls } = fakeContext(); |
| 259 | + const routeDeps: RouteDeps = { |
| 260 | + ...deps(grantsWith()), |
| 261 | + // Cast past the type system the way a buggy host's JS resolver would. |
| 262 | + callerResolver: () => ({ tenantId: "tenant-run" }) as unknown as ResolvedCaller, |
| 263 | + }; |
| 264 | + await resolveCaller(routeDeps)(ctx, async () => {}); |
| 265 | + expect(jsonCalls[0]?.status).toBe(500); |
| 266 | + }); |
| 267 | + |
| 268 | + test("rejects a non-object resolved value with 500", async () => { |
| 269 | + const { ctx, jsonCalls } = fakeContext(); |
| 270 | + const routeDeps: RouteDeps = { |
| 271 | + ...deps(grantsWith()), |
| 272 | + callerResolver: () => "tenant-run" as unknown as ResolvedCaller, |
| 273 | + }; |
| 274 | + await resolveCaller(routeDeps)(ctx, async () => {}); |
| 275 | + expect(jsonCalls[0]?.status).toBe(500); |
| 276 | + }); |
| 277 | +}); |
0 commit comments