|
1 | | -import { afterAll, expect, test, beforeEach, vi } from "vitest"; |
| 1 | +import { expect, test, vi } from "vitest"; |
2 | 2 | import { |
3 | 3 | DynamoDBClient, |
4 | | - PutItemCommand, |
5 | | - GetItemCommand, |
6 | 4 | ScanCommand, |
7 | 5 | QueryCommand, |
8 | 6 | TransactWriteItemsCommand, |
@@ -50,125 +48,68 @@ smMock.on(GetSecretValueCommand).resolves({ |
50 | 48 | SecretString: secretJson, |
51 | 49 | }); |
52 | 50 |
|
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]"); |
59 | 52 |
|
60 | 53 | test("Happy path: Fetch all linkry redirects with proper roles", async () => { |
61 | | - // Create a test JWT with roles |
62 | | - |
63 | | - // Mock successful DynamoDB operations |
64 | 54 | ddbMock.on(QueryCommand).resolves({ |
65 | | - Items: [], // Simulate no existing records |
| 55 | + Items: [], |
66 | 56 | }); |
67 | 57 |
|
68 | | - // Make the request to the /api/v1/linkry/redir endpoint |
| 58 | + ddbMock |
| 59 | + .on(ScanCommand) |
| 60 | + .resolvesOnce({ |
| 61 | + Items: [], |
| 62 | + }) |
| 63 | + .rejects(); |
| 64 | + |
69 | 65 | const response = await app.inject({ |
70 | 66 | method: "GET", |
71 | 67 | url: "/api/v1/linkry/redir", |
72 | 68 | headers: { |
73 | | - Authorization: `Bearer ${testJwt}`, // Include the JWT with roles |
| 69 | + Authorization: `Bearer ${testJwt}`, |
74 | 70 | }, |
75 | 71 | }); |
76 | 72 |
|
77 | 73 | expect(response.statusCode).toBe(200); |
78 | 74 | }); |
79 | 75 |
|
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 | +}); |
126 | 95 |
|
127 | 96 | test("Happy path: Create a new linkry redirect", async () => { |
128 | | - // Mock successful DynamoDB operations |
129 | 97 | ddbMock.on(QueryCommand).resolves({ |
130 | | - Items: [], // Simulate no existing records for the slug |
| 98 | + Items: [], |
131 | 99 | }); |
132 | 100 |
|
133 | | - ddbMock.on(TransactWriteItemsCommand).resolves({}); // Simulate successful insertion |
| 101 | + ddbMock.on(TransactWriteItemsCommand).resolves({}); |
134 | 102 |
|
135 | | - // Define the request payload |
136 | 103 | const payload = { |
137 | 104 | access: [], |
138 | | - counter: 0, |
139 | | - isEdited: true, |
140 | 105 | redirect: "https://www.acm.illinois.edu/", |
141 | 106 | slug: "acm-test-slug", |
142 | 107 | }; |
143 | 108 |
|
144 | | - // Make the request to the /api/v1/linkry/redir/ endpoint |
145 | 109 | const response = await supertest(app.server) |
146 | 110 | .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); |
149 | 113 |
|
150 | | - // Assert the response status code |
151 | 114 | expect(response.statusCode).toBe(201); |
152 | 115 | }); |
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); |
0 commit comments