Skip to content

Commit eae3932

Browse files
committed
Build basic unit tests
1 parent 5c53d90 commit eae3932

File tree

1 file changed

+147
-12
lines changed

1 file changed

+147
-12
lines changed

tests/unit/organizations.test.ts

Lines changed: 147 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,153 @@
11
import { afterAll, expect, test, beforeEach } from "vitest";
2+
import {
3+
DynamoDBClient,
4+
GetItemCommand,
5+
QueryCommand,
6+
} from "@aws-sdk/client-dynamodb";
7+
import { marshall } from "@aws-sdk/util-dynamodb";
28
import init from "../../src/api/index.js";
9+
import { describe } from "node:test";
10+
import { mockClient } from "aws-sdk-client-mock";
11+
import { genericConfig } from "../../src/common/config.js";
312

413
const app = await init();
5-
test("Test getting the list of organizations succeeds", async () => {
6-
const response = await app.inject({
7-
method: "GET",
8-
url: "/api/v1/organizations",
14+
const ddbMock = mockClient(DynamoDBClient);
15+
16+
const acmMeta = {
17+
primaryKey: "DEFINE#ACM",
18+
leadsEntraGroup: "a3c37a24-1e21-4338-813f-15478eb40137",
19+
links: [
20+
{
21+
type: "DISCORD",
22+
url: "https://go.acm.illinois.edu/discord",
23+
},
24+
],
25+
website: "https://www.acm.illinois.edu",
26+
};
27+
describe("Organization info tests", async () => {
28+
test("Test getting the list of organizations succeeds", async () => {
29+
const response = await app.inject({
30+
method: "GET",
31+
url: "/api/v1/organizations",
32+
});
33+
expect(response.statusCode).toBe(200);
34+
await response.json();
35+
});
36+
test("Test getting info about an org succeeds", async () => {
37+
ddbMock
38+
.on(GetItemCommand, {
39+
TableName: genericConfig.SigInfoTableName,
40+
Key: { primaryKey: { S: "DEFINE#ACM" } },
41+
})
42+
.resolves({
43+
Item: marshall(acmMeta),
44+
});
45+
ddbMock
46+
.on(QueryCommand, {
47+
TableName: genericConfig.SigInfoTableName,
48+
KeyConditionExpression: "primaryKey = :leadName",
49+
ExpressionAttributeValues: {
50+
":leadName": { S: "LEAD#ACM" },
51+
},
52+
})
53+
.resolves({
54+
Items: [
55+
{
56+
primaryKey: "LEAD#ACM",
57+
name: "John Doe",
58+
title: "Chair",
59+
username: "[email protected]",
60+
},
61+
].map((x) => marshall(x)),
62+
});
63+
const response = await app.inject({
64+
method: "GET",
65+
url: "/api/v1/organizations/ACM",
66+
});
67+
expect(response.statusCode).toBe(200);
68+
const responseJson = await response.json();
69+
expect(responseJson).toStrictEqual({
70+
id: "ACM",
71+
website: "https://www.acm.illinois.edu",
72+
leads: [
73+
{
74+
username: "[email protected]",
75+
name: "John Doe",
76+
title: "Chair",
77+
},
78+
],
79+
links: [
80+
{
81+
type: "DISCORD",
82+
url: "https://go.acm.illinois.edu/discord",
83+
},
84+
],
85+
});
86+
});
87+
test("Test getting info about an unknown org returns a ValidationError", async () => {
88+
ddbMock
89+
.on(GetItemCommand, {
90+
TableName: genericConfig.SigInfoTableName,
91+
Key: { primaryKey: { S: "DEFINE#ACM" } },
92+
})
93+
.resolves({
94+
Item: undefined,
95+
});
96+
ddbMock
97+
.on(QueryCommand, {
98+
TableName: genericConfig.SigInfoTableName,
99+
KeyConditionExpression: "primaryKey = :leadName",
100+
ExpressionAttributeValues: {
101+
":leadName": { S: "LEAD#ACM" },
102+
},
103+
})
104+
.rejects();
105+
const response = await app.inject({
106+
method: "GET",
107+
url: "/api/v1/organizations/ACM",
108+
});
109+
expect(response.statusCode).toBe(400);
110+
});
111+
test("Test that getting org with no leads succeeds", async () => {
112+
ddbMock
113+
.on(GetItemCommand, {
114+
TableName: genericConfig.SigInfoTableName,
115+
Key: { primaryKey: { S: "DEFINE#ACM" } },
116+
})
117+
.resolves({
118+
Item: marshall(acmMeta),
119+
});
120+
ddbMock
121+
.on(QueryCommand, {
122+
TableName: genericConfig.SigInfoTableName,
123+
KeyConditionExpression: "primaryKey = :leadName",
124+
ExpressionAttributeValues: {
125+
":leadName": { S: "LEAD#ACM" },
126+
},
127+
})
128+
.resolves({ Items: [] });
129+
const response = await app.inject({
130+
method: "GET",
131+
url: "/api/v1/organizations/ACM",
132+
});
133+
expect(response.statusCode).toBe(200);
134+
const responseJson = await response.json();
135+
expect(responseJson).toStrictEqual({
136+
id: "ACM",
137+
website: "https://www.acm.illinois.edu",
138+
leads: [],
139+
links: [
140+
{
141+
type: "DISCORD",
142+
url: "https://go.acm.illinois.edu/discord",
143+
},
144+
],
145+
});
146+
});
147+
afterAll(async () => {
148+
await app.close();
149+
});
150+
beforeEach(() => {
151+
(app as any).nodeCache.flushAll();
9152
});
10-
expect(response.statusCode).toBe(200);
11-
await response.json();
12-
});
13-
afterAll(async () => {
14-
await app.close();
15-
});
16-
beforeEach(() => {
17-
(app as any).nodeCache.flushAll();
18153
});

0 commit comments

Comments
 (0)