diff --git a/server/src/middleware/__generated__/routes.ts b/server/src/middleware/__generated__/routes.ts index dbba03c07..75dd6b398 100644 --- a/server/src/middleware/__generated__/routes.ts +++ b/server/src/middleware/__generated__/routes.ts @@ -806,7 +806,6 @@ export function RegisterRoutes(app: Router) { }); // WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa app.post('/events/signup', - authenticateMiddleware([{"jwt":[]}]), ...(fetchMiddlewares(EventController)), ...(fetchMiddlewares(EventController.prototype.eventSignup)), diff --git a/server/src/middleware/__generated__/swagger.json b/server/src/middleware/__generated__/swagger.json index d0850be56..27405b340 100644 --- a/server/src/middleware/__generated__/swagger.json +++ b/server/src/middleware/__generated__/swagger.json @@ -1661,11 +1661,7 @@ } }, "description": "Signs up for an event", - "security": [ - { - "jwt": [] - } - ], + "security": [], "parameters": [], "requestBody": { "required": true, diff --git a/server/src/middleware/tests/EventController.test.ts b/server/src/middleware/tests/EventController.test.ts new file mode 100644 index 000000000..0954d0d0b --- /dev/null +++ b/server/src/middleware/tests/EventController.test.ts @@ -0,0 +1,73 @@ +import EventService from "data-layer/services/EventService" +import { request } from "../routes.setup" +import { Event, EventReservation } from "../../data-layer/models/firebase" +import { dateToFirestoreTimeStamp } from "data-layer/adapters/DateUtils" + +const startDate = dateToFirestoreTimeStamp(new Date(2024, 1, 1)) +const endDate = dateToFirestoreTimeStamp(new Date(2024, 1, 2)) +const event1: Event = { + title: "UASC New event", + location: "UASC", + start_date: startDate, + end_date: endDate +} +const reservation1: EventReservation = { + first_name: "John", + last_name: "Doe", + email: "test@email.com", + is_member: true +} + +describe("EventController endpoint tests", () => { + const eventService = new EventService() + describe("/events/signup", () => { + it("should return 404 if the event does not exist", async () => { + const res = await request.post("/events/signup").send({ + event_id: "non-existent-event", + reservation: reservation1 + }) + expect(res.status).toEqual(404) + }) + + it("should return 400 if the event is full", async () => { + const event = await eventService.createEvent({ + ...event1, + max_occupancy: 0 + }) + const res = await request.post("/events/signup").send({ + event_id: event.id, + reservation: reservation1 + }) + expect(res.status).toEqual(400) + expect(res.body.error).toEqual("Maximum event occupancy reached.") + }) + + it("should return 400 if already signed up to event", async () => { + const event = await eventService.createEvent(event1) + await eventService.addReservation(event.id, reservation1) + const res = await request.post("/events/signup").send({ + event_id: event.id, + reservation: reservation1 + }) + expect(res.status).toEqual(400) + expect(res.body.error).toEqual( + "You have already signed up for this event." + ) + }) + + it("should allow user to signup to an event", async () => { + const event = await eventService.createEvent(event1) + const res = await request.post("/events/signup").send({ + event_id: event.id, + reservation: reservation1 + }) + expect(res.status).toEqual(200) + expect(res.body.message).toEqual("Successfully signed up for event.") + expect(res.body.data).toEqual({ + first_name: reservation1.first_name, + last_name: reservation1.last_name, + email: reservation1.email + }) + }) + }) +}) diff --git a/server/src/service-layer/controllers/EventController.ts b/server/src/service-layer/controllers/EventController.ts index 8f99bc011..546f94c62 100644 --- a/server/src/service-layer/controllers/EventController.ts +++ b/server/src/service-layer/controllers/EventController.ts @@ -1,7 +1,7 @@ import EventService from "data-layer/services/EventService" import { EventSignupBody } from "service-layer/request-models/EventRequests" import { EventSignupResponse } from "service-layer/response-models/EventResponse" -import { Body, Controller, Post, Route, Security, SuccessResponse } from "tsoa" +import { Body, Controller, Post, Route, SuccessResponse } from "tsoa" @Route("events") export class EventController extends Controller { @@ -9,7 +9,6 @@ export class EventController extends Controller { * Signs up for an event */ @SuccessResponse("200", "Successfully signed up for Event") - @Security("jwt") @Post("signup") public async eventSignup( @Body() requestBody: EventSignupBody @@ -24,7 +23,10 @@ export class EventController extends Controller { } // Check if the event is full const reservations = await eventService.getAllReservations(event_id) - if (reservations.length >= fetchedEvent.max_occupancy) { + if ( + fetchedEvent.max_occupancy !== undefined && + reservations.length >= fetchedEvent.max_occupancy + ) { this.setStatus(400) return { error: "Maximum event occupancy reached." } }