Skip to content

Commit b6088b9

Browse files
committed
fix tests
1 parent 1bb8151 commit b6088b9

File tree

3 files changed

+37
-103
lines changed

3 files changed

+37
-103
lines changed

tests/unit/auth.test.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,12 @@ const ddbMock = mockClient(SecretsManagerClient);
1919

2020
const app = await init();
2121
const jwt_secret = secretObject["jwt_key"];
22-
export function createJwt(
23-
date?: Date,
24-
group?: string,
25-
email?: string,
26-
roles?: string[], // Add roles parameter
27-
) {
22+
export function createJwt(date?: Date, group?: string, email?: string) {
2823
let modifiedPayload = {
2924
...jwtPayload,
3025
email: email || jwtPayload.email,
3126
groups: [...jwtPayload.groups],
32-
roles: roles || jwtPayload.roles, // Use provided roles or default roles
3327
};
34-
3528
if (date) {
3629
const nowMs = Math.floor(date.valueOf() / 1000);
3730
const laterMs = nowMs + 3600 * 24;
@@ -44,9 +37,8 @@ export function createJwt(
4437
}
4538

4639
if (group) {
47-
modifiedPayload.groups[0] = group;
40+
modifiedPayload.groups = [group];
4841
}
49-
5042
return jwt.sign(modifiedPayload, jwt_secret, { algorithm: "HS256" });
5143
}
5244

tests/unit/linkry.test.ts

Lines changed: 34 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { afterAll, expect, test, beforeEach, vi } from "vitest";
1+
import { expect, test, vi } from "vitest";
22
import {
33
DynamoDBClient,
4-
PutItemCommand,
5-
GetItemCommand,
64
ScanCommand,
75
QueryCommand,
86
TransactWriteItemsCommand,
@@ -50,125 +48,68 @@ smMock.on(GetSecretValueCommand).resolves({
5048
SecretString: secretJson,
5149
});
5250

53-
const testJwt = createJwt(
54-
undefined, // No specific date
55-
undefined, // No specific group
56-
"[email protected]", // Test email
57-
["AppRoles.LINKS_MANAGER", "AppRoles.LINKS_ADMIN"], // Add required roles
58-
);
51+
const testJwt = createJwt(undefined, "0", "[email protected]");
5952

6053
test("Happy path: Fetch all linkry redirects with proper roles", async () => {
61-
// Create a test JWT with roles
62-
63-
// Mock successful DynamoDB operations
6454
ddbMock.on(QueryCommand).resolves({
65-
Items: [], // Simulate no existing records
55+
Items: [],
6656
});
6757

68-
// Make the request to the /api/v1/linkry/redir endpoint
58+
ddbMock
59+
.on(ScanCommand)
60+
.resolvesOnce({
61+
Items: [],
62+
})
63+
.rejects();
64+
6965
const response = await app.inject({
7066
method: "GET",
7167
url: "/api/v1/linkry/redir",
7268
headers: {
73-
Authorization: `Bearer ${testJwt}`, // Include the JWT with roles
69+
Authorization: `Bearer ${testJwt}`,
7470
},
7571
});
7672

7773
expect(response.statusCode).toBe(200);
7874
});
7975

80-
//2. Create a new link using supertest
81-
// const eventResponse = await supertest(app.server)
82-
// .post("/api/v1/linkry/redir/")
83-
// .set("Authorization", `Bearer ${testJwt}`)
84-
// .send({
85-
// description: "Test event for ETag verification",
86-
// host: "Social Committee",
87-
// location: "Siebel Center",
88-
// start: "2024-09-25T18:00:00",
89-
// title: "ETag Test Event",
90-
// featured: false,
91-
// });
92-
93-
// expect(eventResponse.statusCode).toBe(201);
94-
// const eventId = eventResponse.body.id;
95-
96-
// test("Happy path: Create or update a linkry redirect", async () => {
97-
// // Mock successful DynamoDB operations
98-
// ddbMock.on(QueryCommand).resolves({
99-
// Items: [], // Simulate no existing records for the slug
100-
// });
101-
102-
// // Define the request payload
103-
// const payload = {
104-
// access: [],
105-
// counter: 0,
106-
// isEdited: true,
107-
// redirect: "https://www.rainbow.com",
108-
// slug: "bQjryt",
109-
// };
110-
111-
// // Make the request to the /api/v1/linkry/redir/ endpoint
112-
// const response = await supertest(app.server)
113-
// .post("/api/v1/linkry/redir/")
114-
// .set("Authorization", `Bearer ${testJwt}`) // Add authorization header
115-
// .send(payload); // Send the payload
116-
117-
// // Assert the response status code
118-
// expect(response.statusCode).toBe(201);
119-
120-
// // Assert the response body (optional, based on your API's response structure)
121-
// expect(response.body).toStrictEqual({
122-
// message: "Linkry redirect created or updated successfully",
123-
// slug: "bQjryt",
124-
// });
125-
// });
76+
test("Make sure that a DB scan is only called for admins", async () => {
77+
const testManagerJwt = createJwt(undefined, "999", "[email protected]");
78+
79+
ddbMock.on(QueryCommand).resolves({
80+
Items: [],
81+
});
82+
83+
ddbMock.on(ScanCommand).rejects();
84+
85+
const response = await app.inject({
86+
method: "GET",
87+
url: "/api/v1/linkry/redir",
88+
headers: {
89+
Authorization: `Bearer ${testManagerJwt}`,
90+
},
91+
});
92+
93+
expect(response.statusCode).toBe(200);
94+
});
12695

12796
test("Happy path: Create a new linkry redirect", async () => {
128-
// Mock successful DynamoDB operations
12997
ddbMock.on(QueryCommand).resolves({
130-
Items: [], // Simulate no existing records for the slug
98+
Items: [],
13199
});
132100

133-
ddbMock.on(TransactWriteItemsCommand).resolves({}); // Simulate successful insertion
101+
ddbMock.on(TransactWriteItemsCommand).resolves({});
134102

135-
// Define the request payload
136103
const payload = {
137104
access: [],
138-
counter: 0,
139-
isEdited: true,
140105
redirect: "https://www.acm.illinois.edu/",
141106
slug: "acm-test-slug",
142107
};
143108

144-
// Make the request to the /api/v1/linkry/redir/ endpoint
145109
const response = await supertest(app.server)
146110
.post("/api/v1/linkry/redir")
147-
.set("Authorization", `Bearer ${testJwt}`) // Include the JWT with roles
148-
.send(payload); // Send the payload
111+
.set("Authorization", `Bearer ${testJwt}`)
112+
.send(payload);
149113

150-
// Assert the response status code
151114
expect(response.statusCode).toBe(201);
152115
});
153-
154-
// const testAdminJwt = createJwt(undefined, "LINKS_ADMIN");
155-
// const testAccessDeniedJwt = createJwt(undefined, "1");
156-
157-
// const adminLinkryResponse = await app.inject({
158-
// method: "GET",
159-
// url: "/api/v1/linkry/redir",
160-
// headers: {
161-
// Authorization: `Bearer ${testAdminJwt}`,
162-
// },
163-
// });
164-
165-
// const accessDeniedLinkryResponse = await app.inject({
166-
// method: "GET",
167-
// url: "/api/v1/linkry/redir",
168-
// headers: {
169-
// Authorization: `Bearer ${testAccessDeniedJwt}`,
170-
// },
171-
// });
172-
173-
// expect(adminLinkryResponse.statusCode).toBe(200);
174-
// expect(accessDeniedLinkryResponse.statusCode).toBe(401);

tests/unit/vitest.setup.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ vi.mock(
4444
"1": [],
4545
LINKS_ADMIN: [AppRoles.LINKS_ADMIN],
4646
"scanner-only": [AppRoles.TICKETS_SCANNER],
47+
"999": [AppRoles.LINKS_MANAGER],
4748
};
4849

4950
return mockGroupRoles[groupId] || [];

0 commit comments

Comments
 (0)