Skip to content

Commit d8158d0

Browse files
authored
Fix Events API responses (#134)
* fix events API retrieval * fix typing * fix metadata pull * fix live tests
1 parent 6b7aab2 commit d8158d0

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

src/api/routes/events.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ const createProjectionParams = (includeMetadata: boolean = false) => {
5656
host: "#host",
5757
featured: "#featured",
5858
id: "#id",
59+
repeats: "#repeats",
60+
repeatEnds: "#repeatEnds",
5961
...(includeMetadata ? { metadata: "#metadata" } : {}),
6062
};
6163

@@ -179,7 +181,7 @@ const eventsPlugin: FastifyPluginAsyncZodOpenApi = async (
179181
async (request, reply) => {
180182
const upcomingOnly = request.query?.upcomingOnly || false;
181183
const featuredOnly = request.query?.featuredOnly || false;
182-
const includeMetadata = request.query.includeMetadata || true;
184+
const includeMetadata = request.query.includeMetadata || false;
183185
const host = request.query?.host;
184186
const ts = request.query?.ts; // we only use this to disable cache control
185187
const projection = createProjectionParams(includeMetadata);

src/common/policies/definition.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { FastifyRequest } from "fastify";
22
import { hostRestrictionPolicy } from "./events.js";
33
import { z } from "zod";
44
import { AuthorizationPolicyResult } from "./evaluator.js";
5+
56
type Policy<TParamsSchema extends z.ZodObject<any>> = {
67
name: string;
78
paramsSchema: TParamsSchema;
@@ -11,10 +12,8 @@ type Policy<TParamsSchema extends z.ZodObject<any>> = {
1112
) => AuthorizationPolicyResult;
1213
};
1314

14-
// Type to get parameters type from a policy
1515
type PolicyParams<T> = T extends Policy<infer U> ? z.infer<U> : never;
1616

17-
// Type for a registry of policies
1817
type PolicyRegistry = {
1918
[key: string]: Policy<any>;
2019
};
@@ -27,16 +26,15 @@ type TypedPolicyRegistry<T extends PolicyRegistry> = {
2726
};
2827
};
2928

29+
export const AuthorizationPoliciesRegistry: PolicyRegistry = {
30+
EventsHostRestrictionPolicy: hostRestrictionPolicy,
31+
} as const;
32+
3033
export type AvailableAuthorizationPolicies = TypedPolicyRegistry<
3134
typeof AuthorizationPoliciesRegistry
3235
>;
33-
export const AuthorizationPoliciesRegistry = {
34-
EventsHostRestrictionPolicy: hostRestrictionPolicy,
35-
} as const;
3636

3737
export type AvailableAuthorizationPolicy = {
38-
[K in keyof typeof AuthorizationPoliciesRegistry]: {
39-
name: K;
40-
params: PolicyParams<(typeof AuthorizationPoliciesRegistry)[K]>;
41-
};
42-
}[keyof typeof AuthorizationPoliciesRegistry];
38+
name: keyof typeof AuthorizationPoliciesRegistry;
39+
params: PolicyParams<typeof AuthorizationPoliciesRegistry[keyof typeof AuthorizationPoliciesRegistry]>;
40+
};

tests/live/events.test.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ test("getting events for a given host", async () => {
2424
});
2525

2626
describe("Event lifecycle tests", async () => {
27-
let createdEventUuid;
27+
let createdEventUuid: string;
2828
test("creating an event", { timeout: 30000 }, async () => {
2929
const token = await createJwt();
3030
const response = await fetch(`${baseEndpoint}/api/v1/events`, {
@@ -34,13 +34,14 @@ describe("Event lifecycle tests", async () => {
3434
"Content-Type": "application/json",
3535
},
3636
body: JSON.stringify({
37-
title: "Testing Event",
37+
title: "Live Testing Event",
3838
description: "An event of all time",
3939
start: "2024-12-31T02:00:00",
4040
end: "2024-12-31T03:30:00",
4141
location: "ACM Room (Siebel 1104)",
4242
host: "ACM",
4343
featured: true,
44+
repeats: "weekly",
4445
}),
4546
});
4647
const responseJson = await response.json();
@@ -49,6 +50,26 @@ describe("Event lifecycle tests", async () => {
4950
expect(responseJson).toHaveProperty("resource");
5051
createdEventUuid = responseJson.id;
5152
});
53+
test("getting a created event", { timeout: 30000 }, async () => {
54+
if (!createdEventUuid) {
55+
throw new Error("Event UUID not found");
56+
}
57+
const response = await fetch(
58+
`${baseEndpoint}/api/v1/events/${createdEventUuid}?ts=${Date.now()}`,
59+
{
60+
method: "GET",
61+
headers: {
62+
"Content-Type": "application/json",
63+
},
64+
},
65+
);
66+
const responseJson = await response.json();
67+
expect(response.status).toBe(200);
68+
expect(responseJson).toHaveProperty("id");
69+
expect(responseJson).toHaveProperty("repeats");
70+
expect(responseJson["repeatEnds"]).toBeUndefined();
71+
createdEventUuid = responseJson.id;
72+
});
5273

5374
test("deleting a previously-created event", { timeout: 30000 }, async () => {
5475
if (!createdEventUuid) {
@@ -72,7 +93,7 @@ describe("Event lifecycle tests", async () => {
7293
throw new Error("Event UUID not found");
7394
}
7495
const response = await fetch(
75-
`${baseEndpoint}/api/v1/events/${createdEventUuid}`,
96+
`${baseEndpoint}/api/v1/events/${createdEventUuid}?ts=${Date.now()}`,
7697
{
7798
method: "GET",
7899
},

0 commit comments

Comments
 (0)