From 7ae05d3eabf99db460e4d39b89541d8ab2146e7d Mon Sep 17 00:00:00 2001 From: romanetar Date: Thu, 19 Nov 2026 16:10:01 +0100 Subject: [PATCH] fix(badges): find tickets by number and enforce summit in badge endpoints The create, delete, update type, add feature and remove feature badge endpoints are documented to take the ticket id or number in {ticket_id}. SummitOrderService fell back to the lookup by number but dropped its result, so a ticket found by number was treated as missing and the endpoints always answered 404. createBadge and deleteBadge also overwrote the summit received as parameter with the summit of the order right before comparing them, so a badge could be created or deleted through the URL of another summit. Both now compare against the summit of the URL and answer 404. Document {ticket_id} as ticket id or number (string) in the OpenAPI attributes of the five endpoints and add tests for each endpoint called with the ticket number, with an unknown number, and through another summit. --- .../OAuth2SummitTicketApiController.php | 10 +- app/Services/Model/Imp/SummitOrderService.php | 12 +- tests/oauth2/OAuth2SummitTicketsApiTest.php | 221 ++++++++++++++++++ 3 files changed, 231 insertions(+), 12 deletions(-) diff --git a/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitTicketApiController.php b/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitTicketApiController.php index b77584a41..aba2a01a6 100644 --- a/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitTicketApiController.php +++ b/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitTicketApiController.php @@ -1210,7 +1210,7 @@ public function getAttendeeBadge($summit_id, $ticket_id) tags: ['Tickets'], parameters: [ new OA\Parameter(name: 'id', in: 'path', required: true, description: 'Summit ID', schema: new OA\Schema(type: 'string')), - new OA\Parameter(name: 'ticket_id', in: 'path', required: true, description: 'Ticket ID', schema: new OA\Schema(type: 'integer')), + new OA\Parameter(name: 'ticket_id', in: 'path', required: true, description: 'Ticket ID or number', schema: new OA\Schema(type: 'string')), ], requestBody: new OA\RequestBody( required: false, @@ -1264,7 +1264,7 @@ public function createAttendeeBadge($summit_id, $ticket_id) tags: ['Tickets'], parameters: [ new OA\Parameter(name: 'id', in: 'path', required: true, description: 'Summit ID', schema: new OA\Schema(type: 'string')), - new OA\Parameter(name: 'ticket_id', in: 'path', required: true, description: 'Ticket ID', schema: new OA\Schema(type: 'integer')), + new OA\Parameter(name: 'ticket_id', in: 'path', required: true, description: 'Ticket ID or number', schema: new OA\Schema(type: 'string')), ], responses: [ new OA\Response(response: Response::HTTP_NO_CONTENT, description: 'Badge deleted successfully'), @@ -1300,7 +1300,7 @@ public function deleteAttendeeBadge($summit_id, $ticket_id) tags: ['Tickets'], parameters: [ new OA\Parameter(name: 'id', in: 'path', required: true, description: 'Summit ID', schema: new OA\Schema(type: 'string')), - new OA\Parameter(name: 'ticket_id', in: 'path', required: true, description: 'Ticket ID', schema: new OA\Schema(type: 'integer')), + new OA\Parameter(name: 'ticket_id', in: 'path', required: true, description: 'Ticket ID or number', schema: new OA\Schema(type: 'string')), new OA\Parameter(name: 'type_id', in: 'path', required: true, description: 'Badge Type ID', schema: new OA\Schema(type: 'integer')), ], responses: [ @@ -1346,7 +1346,7 @@ public function updateAttendeeBadgeType($summit_id, $ticket_id, $type_id) tags: ['Tickets'], parameters: [ new OA\Parameter(name: 'id', in: 'path', required: true, description: 'Summit ID', schema: new OA\Schema(type: 'string')), - new OA\Parameter(name: 'ticket_id', in: 'path', required: true, description: 'Ticket ID', schema: new OA\Schema(type: 'integer')), + new OA\Parameter(name: 'ticket_id', in: 'path', required: true, description: 'Ticket ID or number', schema: new OA\Schema(type: 'string')), new OA\Parameter(name: 'feature_id', in: 'path', required: true, description: 'Feature ID', schema: new OA\Schema(type: 'integer')), ], responses: [ @@ -1392,7 +1392,7 @@ public function addAttendeeBadgeFeature($summit_id, $ticket_id, $feature_id) tags: ['Tickets'], parameters: [ new OA\Parameter(name: 'id', in: 'path', required: true, description: 'Summit ID', schema: new OA\Schema(type: 'string')), - new OA\Parameter(name: 'ticket_id', in: 'path', required: true, description: 'Ticket ID', schema: new OA\Schema(type: 'integer')), + new OA\Parameter(name: 'ticket_id', in: 'path', required: true, description: 'Ticket ID or number', schema: new OA\Schema(type: 'string')), new OA\Parameter(name: 'feature_id', in: 'path', required: true, description: 'Feature ID', schema: new OA\Schema(type: 'integer')), ], responses: [ diff --git a/app/Services/Model/Imp/SummitOrderService.php b/app/Services/Model/Imp/SummitOrderService.php index 9f490a955..6937e4ebd 100644 --- a/app/Services/Model/Imp/SummitOrderService.php +++ b/app/Services/Model/Imp/SummitOrderService.php @@ -3339,7 +3339,7 @@ public function updateBadgeType(Summit $summit, $ticket_id, int $type_id): Summi $ticket = $this->ticket_repository->getByIdExclusiveLock(intval($ticket_id)); if (is_null($ticket)) - $this->ticket_repository->getByNumberExclusiveLock(strval($ticket_id)); + $ticket = $this->ticket_repository->getByNumberExclusiveLock(strval($ticket_id)); if (!$ticket instanceof SummitAttendeeTicket) throw new EntityNotFoundException('Ticket not found.'); @@ -3378,7 +3378,7 @@ public function addAttendeeBadgeFeature(Summit $summit, $ticket_id, int $feature $ticket = $this->ticket_repository->getByIdExclusiveLock(intval($ticket_id)); if (is_null($ticket)) - $this->ticket_repository->getByNumberExclusiveLock(strval($ticket_id)); + $ticket = $this->ticket_repository->getByNumberExclusiveLock(strval($ticket_id)); if (!$ticket instanceof SummitAttendeeTicket) throw new EntityNotFoundException('Ticket not found.'); @@ -3418,7 +3418,7 @@ public function removeAttendeeBadgeFeature(Summit $summit, $ticket_id, int $feat $ticket = $this->ticket_repository->getByIdExclusiveLock(intval($ticket_id)); if (is_null($ticket)) - $this->ticket_repository->getByNumberExclusiveLock(strval($ticket_id)); + $ticket = $this->ticket_repository->getByNumberExclusiveLock(strval($ticket_id)); if (!$ticket instanceof SummitAttendeeTicket) throw new EntityNotFoundException('Ticket not found.'); @@ -3636,13 +3636,12 @@ public function deleteBadge(Summit $summit, $ticket_id): void $this->tx_service->transaction(function () use ($summit, $ticket_id) { $ticket = $this->ticket_repository->getByIdExclusiveLock(intval($ticket_id)); if (is_null($ticket)) - $this->ticket_repository->getByNumberExclusiveLock(strval($ticket_id)); + $ticket = $this->ticket_repository->getByNumberExclusiveLock(strval($ticket_id)); if (!$ticket instanceof SummitAttendeeTicket) throw new EntityNotFoundException('ticket not found'); $order = $ticket->getOrder(); - $summit = $order->getSummit(); if ($order->getSummitId() != $summit->getId()) throw new EntityNotFoundException('ticket not found'); @@ -3669,13 +3668,12 @@ public function createBadge(Summit $summit, $ticket_id, array $payload): SummitA return $this->tx_service->transaction(function () use ($summit, $ticket_id, $payload) { $ticket = $this->ticket_repository->getByIdExclusiveLock(intval($ticket_id)); if (is_null($ticket)) - $this->ticket_repository->getByNumberExclusiveLock(strval($ticket_id)); + $ticket = $this->ticket_repository->getByNumberExclusiveLock(strval($ticket_id)); if (!$ticket instanceof SummitAttendeeTicket) throw new EntityNotFoundException('ticket not found'); $order = $ticket->getOrder(); - $summit = $order->getSummit(); if ($order->getSummitId() != $summit->getId()) throw new EntityNotFoundException('ticket not found'); diff --git a/tests/oauth2/OAuth2SummitTicketsApiTest.php b/tests/oauth2/OAuth2SummitTicketsApiTest.php index 555617c4a..6addb4725 100644 --- a/tests/oauth2/OAuth2SummitTicketsApiTest.php +++ b/tests/oauth2/OAuth2SummitTicketsApiTest.php @@ -938,6 +938,227 @@ public function testDeleteAttendeeBadge() $this->assertResponseStatus(204); } + /** + * @return array + */ + private function badgeApiHeaders(): array + { + return [ + "HTTP_Authorization" => " Bearer " . $this->access_token, + "CONTENT_TYPE" => "application/json" + ]; + } + + public function testCreateAttendeeBadgeByTicketNumber() + { + $ticket = self::$summit_orders[0]->getFirstTicket(); + $headers = $this->badgeApiHeaders(); + + // remove the badge of the fixture first, by id + $this->action( + "DELETE", + "OAuth2SummitTicketApiController@deleteAttendeeBadge", + ['id' => self::$summit->getId(), 'ticket_id' => $ticket->getId()], + [], [], [], $headers + ); + + $response = $this->action( + "POST", + "OAuth2SummitTicketApiController@createAttendeeBadge", + ['id' => self::$summit->getId(), 'ticket_id' => $ticket->getNumber()], + [], [], [], $headers, + json_encode([ + 'badge_type_id' => self::$badge_type_2->getId(), + 'features' => [self::$badge_features[0]->getId(), self::$badge_features[1]->getId()] + ]) + ); + + $this->assertResponseStatus(201); + $badge = json_decode($response->getContent()); + $this->assertTrue(!is_null($badge)); + $this->assertTrue(count($badge->features) == 2); + } + + public function testDeleteAttendeeBadgeByTicketNumber() + { + $ticket = self::$summit_orders[0]->getFirstTicket(); + + $this->action( + "DELETE", + "OAuth2SummitTicketApiController@deleteAttendeeBadge", + ['id' => self::$summit->getId(), 'ticket_id' => $ticket->getNumber()], + [], [], [], $this->badgeApiHeaders() + ); + + $this->assertResponseStatus(204); + } + + public function testUpdateAttendeeBadgeTypeByTicketNumber() + { + $ticket = self::$summit_orders[0]->getFirstTicket(); + + $response = $this->action( + "PUT", + "OAuth2SummitTicketApiController@updateAttendeeBadgeType", + [ + 'id' => self::$summit->getId(), + 'ticket_id' => $ticket->getNumber(), + 'type_id' => self::$badge_type_2->getId(), + ], + [], [], [], $this->badgeApiHeaders() + ); + + $this->assertResponseStatus(201); + $badge = json_decode($response->getContent()); + $this->assertTrue(!is_null($badge)); + } + + public function testAddAttendeeBadgeFeatureByTicketNumber() + { + $ticket = self::$summit_orders[0]->getFirstTicket(); + + $response = $this->action( + "PUT", + "OAuth2SummitTicketApiController@addAttendeeBadgeFeature", + [ + 'id' => self::$summit->getId(), + 'ticket_id' => $ticket->getNumber(), + 'feature_id' => self::$badge_features[0]->getId(), + ], + [], [], [], $this->badgeApiHeaders() + ); + + $this->assertResponseStatus(201); + $badge = json_decode($response->getContent()); + $this->assertTrue(!is_null($badge)); + $this->assertTrue(count($badge->features) == 1); + } + + public function testRemoveAttendeeBadgeFeatureByTicketNumber() + { + $ticket = self::$summit_orders[0]->getFirstTicket(); + $headers = $this->badgeApiHeaders(); + + // recreate the badge of the fixture with two features, by id + $this->action( + "DELETE", + "OAuth2SummitTicketApiController@deleteAttendeeBadge", + ['id' => self::$summit->getId(), 'ticket_id' => $ticket->getId()], + [], [], [], $headers + ); + + $this->action( + "POST", + "OAuth2SummitTicketApiController@createAttendeeBadge", + ['id' => self::$summit->getId(), 'ticket_id' => $ticket->getId()], + [], [], [], $headers, + json_encode([ + 'badge_type_id' => self::$badge_type_2->getId(), + 'features' => [self::$badge_features[0]->getId(), self::$badge_features[1]->getId()] + ]) + ); + $this->assertResponseStatus(201); + + $response = $this->action( + "DELETE", + "OAuth2SummitTicketApiController@removeAttendeeBadgeFeature", + [ + 'id' => self::$summit->getId(), + 'ticket_id' => $ticket->getNumber(), + 'feature_id' => self::$badge_features[0]->getId(), + ], + [], [], [], $headers + ); + + $this->assertResponseStatus(201); + $badge = json_decode($response->getContent()); + $this->assertTrue(!is_null($badge)); + $this->assertTrue(count($badge->features) == 1); + } + + public function testBadgeEndpointsWithUnknownTicketNumber() + { + $headers = $this->badgeApiHeaders(); + $summit_id = self::$summit->getId(); + $unknown = 'TICKET_DOES_NOT_EXIST'; + + $calls = [ + ["POST", "createAttendeeBadge", ['id' => $summit_id, 'ticket_id' => $unknown], json_encode([])], + ["DELETE", "deleteAttendeeBadge", ['id' => $summit_id, 'ticket_id' => $unknown], null], + ["PUT", "updateAttendeeBadgeType", [ + 'id' => $summit_id, 'ticket_id' => $unknown, 'type_id' => self::$badge_type_2->getId() + ], null], + ["PUT", "addAttendeeBadgeFeature", [ + 'id' => $summit_id, 'ticket_id' => $unknown, 'feature_id' => self::$badge_features[0]->getId() + ], null], + ["DELETE", "removeAttendeeBadgeFeature", [ + 'id' => $summit_id, 'ticket_id' => $unknown, 'feature_id' => self::$badge_features[0]->getId() + ], null], + ]; + + foreach ($calls as [$method, $action, $params, $body]) { + $this->action( + $method, + "OAuth2SummitTicketApiController@" . $action, + $params, + [], [], [], $headers, + $body + ); + $this->assertResponseStatus(404, $action); + } + } + + public function testCreateAttendeeBadgeThroughAnotherSummit() + { + $ticket = self::$summit_orders[0]->getFirstTicket(); + $headers = $this->badgeApiHeaders(); + + // remove the badge of the fixture so the only reason to fail is the summit + $this->action( + "DELETE", + "OAuth2SummitTicketApiController@deleteAttendeeBadge", + ['id' => self::$summit->getId(), 'ticket_id' => $ticket->getId()], + [], [], [], $headers + ); + $this->assertResponseStatus(204); + + foreach ([$ticket->getId(), $ticket->getNumber()] as $ticket_id) { + $this->action( + "POST", + "OAuth2SummitTicketApiController@createAttendeeBadge", + ['id' => self::$summit2->getId(), 'ticket_id' => $ticket_id], + [], [], [], $headers, + json_encode(['badge_type_id' => self::$badge_type_2->getId()]) + ); + $this->assertResponseStatus(404); + } + + self::$em->clear(); + $ticket = self::$em->find(get_class($ticket), $ticket->getId()); + $this->assertFalse($ticket->hasBadge()); + } + + public function testDeleteAttendeeBadgeThroughAnotherSummit() + { + $ticket = self::$summit_orders[0]->getFirstTicket(); + $ticket_id = $ticket->getId(); + $ticket_number = $ticket->getNumber(); + $this->assertTrue($ticket->hasBadge()); + + foreach ([$ticket_id, $ticket_number] as $id) { + $this->action( + "DELETE", + "OAuth2SummitTicketApiController@deleteAttendeeBadge", + ['id' => self::$summit2->getId(), 'ticket_id' => $id], + [], [], [], $this->badgeApiHeaders() + ); + $this->assertResponseStatus(404); + } + + self::$em->clear(); + $ticket = self::$em->find(get_class($ticket), $ticket_id); + $this->assertTrue($ticket->hasBadge()); + } public function testPrintAttendeeBadge() {