Skip to content

Commit 67c6933

Browse files
committed
fix unit tests
1 parent adb3c42 commit 67c6933

File tree

1 file changed

+77
-1
lines changed

1 file changed

+77
-1
lines changed

tests/unit/membership.test.ts

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ import init from "../../src/api/index.js";
33
import { EventGetResponse } from "../../src/api/routes/events.js";
44
import { afterEach, describe } from "node:test";
55
import { setPaidMembershipInTable } from "../../src/api/functions/membership.js";
6-
import { DynamoDBClient, QueryCommand } from "@aws-sdk/client-dynamodb";
6+
import {
7+
BatchWriteItemCommand,
8+
DynamoDBClient,
9+
QueryCommand,
10+
ScanCommand,
11+
} from "@aws-sdk/client-dynamodb";
712
import { genericConfig } from "../../src/common/config.js";
813
import { marshall } from "@aws-sdk/util-dynamodb";
914
import { mockClient } from "aws-sdk-client-mock";
15+
import { createJwt } from "./auth.test.js";
1016

1117
const app = await init();
1218
const ddbMock = mockClient(DynamoDBClient);
@@ -172,6 +178,76 @@ describe("Test membership routes", async () => {
172178
isPaidMember: true,
173179
});
174180
});
181+
test("External lists are correctly found", async () => {
182+
const adminJwt = createJwt();
183+
ddbMock.on(ScanCommand).callsFake((command) => {
184+
if (
185+
command.TableName === genericConfig.ExternalMembershipTableName &&
186+
command.IndexName === "keysOnlyIndex"
187+
) {
188+
return Promise.resolve({
189+
Items: [{ memberList: { S: "acmUnitTesting" } }],
190+
});
191+
}
192+
return Promise.reject(
193+
new Error("Table not mocked or not called correctly"),
194+
);
195+
});
196+
const response = await app.inject({
197+
method: "GET",
198+
url: "/api/v1/membership/externalList",
199+
headers: {
200+
authorization: `Bearer ${adminJwt}`,
201+
},
202+
});
203+
expect(response.statusCode).toBe(200);
204+
const json = await response.json();
205+
expect(json).toStrictEqual(["acmUnitTesting"]);
206+
});
207+
test("External list members are correctly patched", async () => {
208+
const adminJwt = createJwt();
209+
ddbMock.on(BatchWriteItemCommand).callsFake((command) => {
210+
if (
211+
(command.RequestItems = {
212+
[genericConfig.ExternalMembershipTableName]: [
213+
{
214+
PutRequest: {
215+
Item: {
216+
memberList: { S: "acmUnitTesting" },
217+
netId: { S: "acmtest2" },
218+
},
219+
},
220+
},
221+
{
222+
DeleteRequest: {
223+
Item: {
224+
memberList: { S: "acmUnitTesting" },
225+
netId: { S: "acmtest3" },
226+
},
227+
},
228+
},
229+
],
230+
})
231+
) {
232+
return Promise.resolve({});
233+
}
234+
return Promise.reject(
235+
new Error("Table not mocked or not called correctly"),
236+
);
237+
});
238+
let response = await app.inject({
239+
method: "PATCH",
240+
url: "/api/v1/membership/externalList/acmUnitTesting",
241+
headers: {
242+
authorization: `Bearer ${adminJwt}`,
243+
},
244+
body: {
245+
add: ["acmtest2"],
246+
remove: ["acmtest3"],
247+
},
248+
});
249+
expect(response.statusCode).toBe(201);
250+
});
175251
afterEach(async () => {
176252
ddbMock.reset();
177253
});

0 commit comments

Comments
 (0)