Skip to content

Commit

Permalink
Minor updates to eventSignup endpoint and added tests
Browse files Browse the repository at this point in the history
* Removes requirement for JWT in the endpoint.

* Checks if `max_occupancy` is undefined and **only** compares occupancy if `max_occupancy` isn't undefined.
  • Loading branch information
jeffplays2005 committed Sep 1, 2024
1 parent a6f80d3 commit 70fe97e
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 9 deletions.
1 change: 0 additions & 1 deletion server/src/middleware/__generated__/routes.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions server/src/middleware/__generated__/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 73 additions & 0 deletions server/src/middleware/tests/EventController.test.ts
Original file line number Diff line number Diff line change
@@ -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: "[email protected]",
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
})
})
})
})
8 changes: 5 additions & 3 deletions server/src/service-layer/controllers/EventController.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
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 {
/**
* Signs up for an event
*/
@SuccessResponse("200", "Successfully signed up for Event")
@Security("jwt")
@Post("signup")
public async eventSignup(
@Body() requestBody: EventSignupBody
Expand All @@ -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." }
}
Expand Down

0 comments on commit 70fe97e

Please sign in to comment.