From 3800cff38d5626092ce98e7874eddebd696931e6 Mon Sep 17 00:00:00 2001 From: pizzaburgare <72048430+pizzaburgare@users.noreply.github.com> Date: Fri, 19 Apr 2024 11:29:57 +0200 Subject: [PATCH 1/3] Fixed check for number of tickets when booking Added test that checks that user with 5 tickets cannot book any more. --- Nexpo.Tests/Controllers/TicketsControllerTest.cs | 16 ++++++++++++++++ Nexpo/Controllers/Events/TicketsController.cs | 10 ++++++++++ 2 files changed, 26 insertions(+) diff --git a/Nexpo.Tests/Controllers/TicketsControllerTest.cs b/Nexpo.Tests/Controllers/TicketsControllerTest.cs index 503f5fa..a78405d 100644 --- a/Nexpo.Tests/Controllers/TicketsControllerTest.cs +++ b/Nexpo.Tests/Controllers/TicketsControllerTest.cs @@ -1100,5 +1100,21 @@ public async Task sendTicketViaEmailSeveralTicketsNonLoggedIn(){ Assert.True(response.StatusCode.Equals(HttpStatusCode.Unauthorized),"Wrong status code. Expected: Unauthorized. Received: " + response.StatusCode.ToString()); } + [Fact] + public async Task TooManyTickets() + { + // Student2 already has 5 tickets + var client = await TestUtils.Login("student2"); + var json = new JsonObject + { + { "eventid", -2 }, + { "photook", true } + }; + var payload = new StringContent(json.ToString(), Encoding.UTF8, "application/json"); + var response = await client.PostAsync("api/tickets", payload); + // Verify response - Conflict because the student already has a ticket for the event + Assert.True(response.StatusCode.Equals(HttpStatusCode.TooManyRequests), + "Wrong status code. Expected: TooManyRequests. Received: " + response.ToString()); + } } } diff --git a/Nexpo/Controllers/Events/TicketsController.cs b/Nexpo/Controllers/Events/TicketsController.cs index 2c19dc0..648962f 100644 --- a/Nexpo/Controllers/Events/TicketsController.cs +++ b/Nexpo/Controllers/Events/TicketsController.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; @@ -72,6 +73,15 @@ public async Task PostTicket(CreateTicketDTO DTO) { return Conflict(); } + + // Check for max number of registrations + int max = 5; + int noTickets = (await _ticketRepo.GetAllForUser(userId)).Count(); + if (noTickets >= max) + { + return StatusCode(429, "Too many tickets"); + } + var ticket = new Ticket { From d6c879f3f47a7c134f522fdb3741601af1e77526 Mon Sep 17 00:00:00 2001 From: pizzaburgare <72048430+pizzaburgare@users.noreply.github.com> Date: Fri, 26 Apr 2024 11:22:41 +0200 Subject: [PATCH 2/3] Works by checking for each ticket if the corresponding event has passed Make Event.Start/End DateTime? --- .../Controllers/TicketsControllerTest.cs | 6 +++--- Nexpo/Controllers/Events/TicketsController.cs | 20 ++++++++++++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/Nexpo.Tests/Controllers/TicketsControllerTest.cs b/Nexpo.Tests/Controllers/TicketsControllerTest.cs index a78405d..34d7239 100644 --- a/Nexpo.Tests/Controllers/TicketsControllerTest.cs +++ b/Nexpo.Tests/Controllers/TicketsControllerTest.cs @@ -1103,18 +1103,18 @@ public async Task sendTicketViaEmailSeveralTicketsNonLoggedIn(){ [Fact] public async Task TooManyTickets() { - // Student2 already has 5 tickets + // Student2 already has 5 tickets but not -7 var client = await TestUtils.Login("student2"); var json = new JsonObject { - { "eventid", -2 }, + { "eventid", -7 }, { "photook", true } }; var payload = new StringContent(json.ToString(), Encoding.UTF8, "application/json"); var response = await client.PostAsync("api/tickets", payload); // Verify response - Conflict because the student already has a ticket for the event Assert.True(response.StatusCode.Equals(HttpStatusCode.TooManyRequests), - "Wrong status code. Expected: TooManyRequests. Received: " + response.ToString()); + "Wrong status code. Expected: TooManyRequests. Received: " + response); } } } diff --git a/Nexpo/Controllers/Events/TicketsController.cs b/Nexpo/Controllers/Events/TicketsController.cs index 648962f..46fb202 100644 --- a/Nexpo/Controllers/Events/TicketsController.cs +++ b/Nexpo/Controllers/Events/TicketsController.cs @@ -75,9 +75,23 @@ public async Task PostTicket(CreateTicketDTO DTO) } // Check for max number of registrations - int max = 5; - int noTickets = (await _ticketRepo.GetAllForUser(userId)).Count(); - if (noTickets >= max) + int maxNoTickets = 5; + // Assmues no event found or faulty format of date should not be counted + int noTickets = (await _ticketRepo.GetAllForUser(userId)).Count(ticket => + { + var foundEvent = _eventRepo.Get(ticket.EventId); + if (foundEvent.Result == null) + { + return false; + } + bool isSuccess = DateTime.TryParse(foundEvent.Result.Start, out var eventDateTime); + if (isSuccess) + { + return eventDateTime > DateTime.Now; + } + return false; + }); + if (noTickets >= maxNoTickets) { return StatusCode(429, "Too many tickets"); } From 74768c3ebf00d7913b666d1b41f36a3d1305699c Mon Sep 17 00:00:00 2001 From: pizzaburgare <72048430+pizzaburgare@users.noreply.github.com> Date: Tue, 7 May 2024 18:40:15 +0200 Subject: [PATCH 3/3] Update comment --- Nexpo.Tests/Controllers/TicketsControllerTest.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Nexpo.Tests/Controllers/TicketsControllerTest.cs b/Nexpo.Tests/Controllers/TicketsControllerTest.cs index 34d7239..00763b0 100644 --- a/Nexpo.Tests/Controllers/TicketsControllerTest.cs +++ b/Nexpo.Tests/Controllers/TicketsControllerTest.cs @@ -1103,7 +1103,8 @@ public async Task sendTicketViaEmailSeveralTicketsNonLoggedIn(){ [Fact] public async Task TooManyTickets() { - // Student2 already has 5 tickets but not -7 + // Student2 has 5 tickets already but not for the event with the id -7, + // Therefore booking event -7 should return "too many requests" because the user has too many tickets var client = await TestUtils.Login("student2"); var json = new JsonObject {