|
| 1 | +import { afterAll, expect, test, beforeEach, vi } from "vitest"; |
| 2 | +import { DynamoDBClient, PutItemCommand } from "@aws-sdk/client-dynamodb"; |
| 3 | +import { mockClient } from "aws-sdk-client-mock"; |
| 4 | +import init from "../../src/index.js"; |
| 5 | +import { createJwt } from "./auth.test.js"; |
| 6 | +import { |
| 7 | + GetSecretValueCommand, |
| 8 | + SecretsManagerClient, |
| 9 | +} from "@aws-sdk/client-secrets-manager"; |
| 10 | +import { secretJson, secretObject } from "./secret.testdata.js"; |
| 11 | +import supertest from "supertest"; |
| 12 | + |
| 13 | +const ddbMock = mockClient(DynamoDBClient); |
| 14 | +const smMock = mockClient(SecretsManagerClient); |
| 15 | +const jwt_secret = secretObject["jwt_key"]; |
| 16 | +vi.stubEnv("JwtSigningKey", jwt_secret); |
| 17 | + |
| 18 | +const app = await init(); |
| 19 | + |
| 20 | +test("Sad path: Not authenticated", async () => { |
| 21 | + await app.ready(); |
| 22 | + const response = await supertest(app.server).post("/api/v1/events").send({ |
| 23 | + description: "Test paid event.", |
| 24 | + end: "2024-09-25T19:00:00", |
| 25 | + featured: true, |
| 26 | + host: "Social Committee", |
| 27 | + location: "Illini Union", |
| 28 | + start: "2024-09-25T18:00:00", |
| 29 | + title: "Fall Semiformal", |
| 30 | + paidEventId: "sp24_semiformal", |
| 31 | + }); |
| 32 | + |
| 33 | + expect(response.statusCode).toBe(403); |
| 34 | +}); |
| 35 | + |
| 36 | +test("Sad path: Authenticated but not authorized", async () => { |
| 37 | + await app.ready(); |
| 38 | + const testJwt = createJwt(undefined, "1"); |
| 39 | + const response = await supertest(app.server) |
| 40 | + .post("/api/v1/events") |
| 41 | + .set("Authorization", `Bearer ${testJwt}`) |
| 42 | + .send({ |
| 43 | + description: "Test paid event.", |
| 44 | + end: "2024-09-25T19:00:00", |
| 45 | + featured: true, |
| 46 | + host: "Social Committee", |
| 47 | + location: "Illini Union", |
| 48 | + start: "2024-09-25T18:00:00", |
| 49 | + title: "Fall Semiformal", |
| 50 | + paidEventId: "sp24_semiformal", |
| 51 | + }); |
| 52 | + expect(response.statusCode).toBe(401); |
| 53 | +}); |
| 54 | +test("Sad path: Prevent empty body request", async () => { |
| 55 | + await app.ready(); |
| 56 | + const testJwt = createJwt(undefined, "0"); |
| 57 | + const response = await supertest(app.server) |
| 58 | + .post("/api/v1/events") |
| 59 | + .set("Authorization", `Bearer ${testJwt}`) |
| 60 | + .send(); |
| 61 | + expect(response.statusCode).toBe(400); |
| 62 | + expect(response.body).toStrictEqual({ |
| 63 | + error: true, |
| 64 | + name: "ValidationError", |
| 65 | + id: 104, |
| 66 | + message: "Required", |
| 67 | + }); |
| 68 | +}); |
| 69 | +test("Sad path: Prevent specifying repeatEnds on non-repeating events", async () => { |
| 70 | + ddbMock.on(PutItemCommand).resolves({}); |
| 71 | + smMock.on(GetSecretValueCommand).resolves({ |
| 72 | + SecretString: secretJson, |
| 73 | + }); |
| 74 | + const testJwt = createJwt(); |
| 75 | + await app.ready(); |
| 76 | + const response = await supertest(app.server) |
| 77 | + .post("/api/v1/events") |
| 78 | + .set("authorization", `Bearer ${testJwt}`) |
| 79 | + .send({ |
| 80 | + description: "Test paid event.", |
| 81 | + end: "2024-09-25T19:00:00", |
| 82 | + featured: false, |
| 83 | + host: "Social Committee", |
| 84 | + location: "Illini Union", |
| 85 | + start: "2024-09-25T18:00:00", |
| 86 | + title: "Fall Semiformal", |
| 87 | + repeatEnds: "2024-09-25T18:00:00", |
| 88 | + paidEventId: "sp24_semiformal", |
| 89 | + }); |
| 90 | + |
| 91 | + expect(response.statusCode).toBe(400); |
| 92 | + expect(response.body).toStrictEqual({ |
| 93 | + error: true, |
| 94 | + name: "ValidationError", |
| 95 | + id: 104, |
| 96 | + message: "repeats is required when repeatEnds is defined", |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
| 100 | +test("Sad path: Prevent specifying unknown repeat frequencies", async () => { |
| 101 | + ddbMock.on(PutItemCommand).resolves({}); |
| 102 | + smMock.on(GetSecretValueCommand).resolves({ |
| 103 | + SecretString: secretJson, |
| 104 | + }); |
| 105 | + const testJwt = createJwt(); |
| 106 | + await app.ready(); |
| 107 | + const response = await supertest(app.server) |
| 108 | + .post("/api/v1/events") |
| 109 | + .set("authorization", `Bearer ${testJwt}`) |
| 110 | + .send({ |
| 111 | + description: "Test paid event.", |
| 112 | + end: "2024-09-25T19:00:00", |
| 113 | + featured: false, |
| 114 | + host: "Social Committee", |
| 115 | + location: "Illini Union", |
| 116 | + start: "2024-09-25T18:00:00", |
| 117 | + title: "Fall Semiformal", |
| 118 | + repeats: "forever_and_ever", |
| 119 | + paidEventId: "sp24_semiformal", |
| 120 | + }); |
| 121 | + |
| 122 | + expect(response.statusCode).toBe(400); |
| 123 | + expect(response.body).toStrictEqual({ |
| 124 | + error: true, |
| 125 | + name: "ValidationError", |
| 126 | + id: 104, |
| 127 | + message: `Invalid enum value. Expected 'weekly' | 'biweekly', received 'forever_and_ever' at "repeats"`, |
| 128 | + }); |
| 129 | +}); |
| 130 | + |
| 131 | +test("Happy path: Adding a non-repeating, featured, paid event", async () => { |
| 132 | + ddbMock.on(PutItemCommand).resolves({}); |
| 133 | + smMock.on(GetSecretValueCommand).resolves({ |
| 134 | + SecretString: secretJson, |
| 135 | + }); |
| 136 | + const testJwt = createJwt(); |
| 137 | + await app.ready(); |
| 138 | + const response = await supertest(app.server) |
| 139 | + .post("/api/v1/events") |
| 140 | + .set("authorization", `Bearer ${testJwt}`) |
| 141 | + .send({ |
| 142 | + description: "Test paid event.", |
| 143 | + end: "2024-09-25T19:00:00", |
| 144 | + featured: true, |
| 145 | + host: "Social Committee", |
| 146 | + location: "Illini Union", |
| 147 | + start: "2024-09-25T18:00:00", |
| 148 | + title: "Fall Semiformal", |
| 149 | + paidEventId: "sp24_semiformal", |
| 150 | + }); |
| 151 | + |
| 152 | + expect(response.statusCode).toBe(200); |
| 153 | + const responseDataJson = response.body as { id: string; resource: string }; |
| 154 | + expect(responseDataJson).toHaveProperty("id"); |
| 155 | + const uuid = responseDataJson["id"]; |
| 156 | + expect(responseDataJson).toEqual({ |
| 157 | + id: uuid, |
| 158 | + resource: `/api/v1/event/${uuid}`, |
| 159 | + }); |
| 160 | +}); |
| 161 | + |
| 162 | +test("Happy path: Adding a weekly repeating, non-featured, paid event", async () => { |
| 163 | + ddbMock.on(PutItemCommand).resolves({}); |
| 164 | + smMock.on(GetSecretValueCommand).resolves({ |
| 165 | + SecretString: secretJson, |
| 166 | + }); |
| 167 | + const testJwt = createJwt(); |
| 168 | + await app.ready(); |
| 169 | + const response = await supertest(app.server) |
| 170 | + .post("/api/v1/events") |
| 171 | + .set("authorization", `Bearer ${testJwt}`) |
| 172 | + .send({ |
| 173 | + description: "Test paid event.", |
| 174 | + end: "2024-09-25T19:00:00", |
| 175 | + featured: false, |
| 176 | + host: "Social Committee", |
| 177 | + location: "Illini Union", |
| 178 | + start: "2024-09-25T18:00:00", |
| 179 | + title: "Fall Semiformal", |
| 180 | + repeats: "weekly", |
| 181 | + paidEventId: "sp24_semiformal", |
| 182 | + }); |
| 183 | + |
| 184 | + expect(response.statusCode).toBe(200); |
| 185 | + const responseDataJson = response.body as { id: string; resource: string }; |
| 186 | + expect(responseDataJson).toHaveProperty("id"); |
| 187 | + const uuid = responseDataJson["id"]; |
| 188 | + expect(responseDataJson).toEqual({ |
| 189 | + id: uuid, |
| 190 | + resource: `/api/v1/event/${uuid}`, |
| 191 | + }); |
| 192 | +}); |
| 193 | + |
| 194 | +afterAll(async () => { |
| 195 | + await app.close(); |
| 196 | + vi.useRealTimers(); |
| 197 | +}); |
| 198 | +beforeEach(() => { |
| 199 | + ddbMock.reset(); |
| 200 | + smMock.reset(); |
| 201 | + vi.useFakeTimers(); |
| 202 | +}); |
0 commit comments