Skip to content

Commit a36a20e

Browse files
authored
Basic policy evaluator tests (#155)
1 parent 8a366c6 commit a36a20e

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

src/common/policies/events.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ export const hostRestrictionPolicy = createPolicy(
77
"EventsHostRestrictionPolicy",
88
z.object({ host: z.array(z.enum(OrganizationList)) }),
99
(request: FastifyRequest & { username?: string }, params) => {
10+
if (request.method === "GET") {
11+
return {
12+
allowed: true,
13+
message: "Skipped as route not in scope.",
14+
cacheKey: null,
15+
};
16+
}
1017
if (!request.url.startsWith("/api/v1/events")) {
1118
return {
1219
allowed: true,
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import { describe, expect, test, vi } from "vitest";
2+
import { type FastifyRequest } from "fastify";
3+
import { evaluateAllRequestPolicies } from "../../../src/api/plugins/evaluatePolicies.js";
4+
import init from "../../../src/api/index.js";
5+
6+
describe("Policy Evalulator Tests", async () => {
7+
test("Policy evalulation is true for non-event routes.", async () => {
8+
const mockRequest = {
9+
url: "/api/v1/healthz",
10+
body: {
11+
host: "ACM",
12+
featured: true,
13+
},
14+
log: {
15+
info: vi.fn(),
16+
},
17+
username: "[email protected]",
18+
policyRestrictions: [],
19+
} as unknown as FastifyRequest;
20+
const response = await evaluateAllRequestPolicies(mockRequest);
21+
expect(response).toBe(true);
22+
})
23+
test("Policy evalulation skips GET routes.", async () => {
24+
const mockRequest = {
25+
url: "/api/v1/events/123",
26+
method: "GET",
27+
body: {
28+
host: "ACM",
29+
featured: true,
30+
},
31+
log: {
32+
info: vi.fn(),
33+
},
34+
username: "[email protected]",
35+
policyRestrictions: [{
36+
"name": "EventsHostRestrictionPolicy",
37+
"params": {
38+
"host": [
39+
"NONE"
40+
]
41+
}
42+
}],
43+
} as unknown as FastifyRequest;
44+
const response = await evaluateAllRequestPolicies(mockRequest);
45+
expect(response).toBe(true);
46+
})
47+
test("Policy evalulation does not permit featured events even for the correct host.", async () => {
48+
const mockRequest = {
49+
url: "/api/v1/events",
50+
method: "POST",
51+
body: {
52+
host: "ACM",
53+
featured: true,
54+
},
55+
log: {
56+
info: vi.fn(),
57+
},
58+
username: "[email protected]",
59+
policyRestrictions: [{
60+
"name": "EventsHostRestrictionPolicy",
61+
"params": {
62+
"host": [
63+
"ACM"
64+
]
65+
}
66+
}],
67+
} as unknown as FastifyRequest;
68+
const response = await evaluateAllRequestPolicies(mockRequest);
69+
expect(response).toBe(`Denied by policy "EventsHostRestrictionPolicy". Event must not be featured.`)
70+
})
71+
test("Policy evalulation denies incorrect host.", async () => {
72+
const mockRequest = {
73+
url: "/api/v1/events",
74+
method: "DELETE",
75+
body: {
76+
host: "ACM",
77+
featured: false,
78+
},
79+
log: {
80+
info: vi.fn(),
81+
},
82+
username: "[email protected]",
83+
policyRestrictions: [{
84+
"name": "EventsHostRestrictionPolicy",
85+
"params": {
86+
"host": [
87+
"Infrastructure Committee"
88+
]
89+
}
90+
}],
91+
} as unknown as FastifyRequest;
92+
const response = await evaluateAllRequestPolicies(mockRequest);
93+
expect(response).toBe(`Denied by policy "EventsHostRestrictionPolicy". Host must be one of: Infrastructure Committee.`);
94+
})
95+
test("Policy evalulation permits correct host non-featured requests.", async () => {
96+
const mockRequest = {
97+
url: "/api/v1/events",
98+
method: "POST",
99+
body: {
100+
host: "ACM",
101+
featured: false,
102+
},
103+
log: {
104+
info: vi.fn(),
105+
},
106+
username: "[email protected]",
107+
policyRestrictions: [{
108+
"name": "EventsHostRestrictionPolicy",
109+
"params": {
110+
"host": [
111+
"ACM"
112+
]
113+
}
114+
}],
115+
} as unknown as FastifyRequest;
116+
const response = await evaluateAllRequestPolicies(mockRequest);
117+
expect(response).toBe(true);
118+
})
119+
})

tests/unit/vitest.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default defineConfig({
1111
coverage: {
1212
provider: "istanbul",
1313
include: ["src/api/**/*.ts", "src/common/**/*.ts"],
14+
exclude: ["src/api/lambda.ts"],
1415
},
1516
},
1617
resolve: {

0 commit comments

Comments
 (0)