From e255facfdbd45097784e3a2fd830883f6920519e Mon Sep 17 00:00:00 2001 From: Beer van der Drift Date: Tue, 31 May 2016 20:44:03 +0200 Subject: [PATCH] RFID linked tickets can't be transferred. (#249) * RFID linked tickets can't be transferred. * Can't perform an open transfer if a ticket has been linked in the meanwhile. --- .../controller/RFIDController.java | 5 -- .../GlobalControllerExceptionHandler.java | 5 ++ .../service/TicketServiceImpl.java | 12 +++++ .../TicketRestIntegrationTest.java | 46 ++++++++++++++++++- 4 files changed, 62 insertions(+), 6 deletions(-) diff --git a/src/main/java/ch/wisv/areafiftylan/controller/RFIDController.java b/src/main/java/ch/wisv/areafiftylan/controller/RFIDController.java index baeb508b6..0ad56a00e 100644 --- a/src/main/java/ch/wisv/areafiftylan/controller/RFIDController.java +++ b/src/main/java/ch/wisv/areafiftylan/controller/RFIDController.java @@ -68,11 +68,6 @@ public ResponseEntity handleRFIDTakenException(RFIDTakenException e){ return createResponseEntity(HttpStatus.CONFLICT, e.getMessage()); } - @ExceptionHandler(TicketAlreadyLinkedException.class) - public ResponseEntity handleInvalidRFIDException(TicketAlreadyLinkedException e){ - return createResponseEntity(HttpStatus.CONFLICT, e.getMessage()); - } - @ExceptionHandler(InvalidRFIDException.class) public ResponseEntity handleInvalidRFIDException(InvalidRFIDException e){ return createResponseEntity(HttpStatus.BAD_REQUEST, e.getMessage()); diff --git a/src/main/java/ch/wisv/areafiftylan/exception/GlobalControllerExceptionHandler.java b/src/main/java/ch/wisv/areafiftylan/exception/GlobalControllerExceptionHandler.java index b5c7aa6b4..680e93eac 100644 --- a/src/main/java/ch/wisv/areafiftylan/exception/GlobalControllerExceptionHandler.java +++ b/src/main/java/ch/wisv/areafiftylan/exception/GlobalControllerExceptionHandler.java @@ -55,4 +55,9 @@ public ResponseEntity handleTicketNotFoundException(TicketNotFoundException e public ResponseEntity handleInvalidTicketException(InvalidTicketException ex) { return createResponseEntity(HttpStatus.BAD_REQUEST, ex.getMessage()); } + + @ExceptionHandler(TicketAlreadyLinkedException.class) + public ResponseEntity handleInvalidRFIDException(TicketAlreadyLinkedException e){ + return createResponseEntity(HttpStatus.CONFLICT, e.getMessage()); + } } diff --git a/src/main/java/ch/wisv/areafiftylan/service/TicketServiceImpl.java b/src/main/java/ch/wisv/areafiftylan/service/TicketServiceImpl.java index 5a61a33cd..dee76de9d 100644 --- a/src/main/java/ch/wisv/areafiftylan/service/TicketServiceImpl.java +++ b/src/main/java/ch/wisv/areafiftylan/service/TicketServiceImpl.java @@ -27,6 +27,7 @@ public class TicketServiceImpl implements TicketService { private TicketTransferTokenRepository tttRepository; private MailService mailService; private TeamService teamService; + private RFIDService rfidService; @Value("${a5l.user.acceptTransferUrl}") private String acceptTransferUrl; @@ -45,6 +46,11 @@ public TicketServiceImpl(TicketRepository ticketRepository, UserService userServ this.teamService = teamService; } + @Autowired + public void setRFIDService(RFIDService rfidService){ + this.rfidService = rfidService; + } + @Override public Ticket getTicketById(Long ticketId) { Ticket t = ticketRepository.findOne(ticketId); @@ -113,6 +119,8 @@ public TicketTransferToken setupForTransfer(Long ticketId, String goalUserName) if (!ticketTransferTokens.isEmpty()) { throw new DuplicateTicketTransferTokenException(ticketId); + } if(rfidService.isTicketLinked(ticketId)) { + throw new TicketAlreadyLinkedException(); } else { TicketTransferToken ttt = new TicketTransferToken(u, t); @@ -130,6 +138,10 @@ public void transferTicket(String token) { TicketTransferToken ttt = getTicketTransferTokenIfValid(token); Ticket t = ttt.getTicket(); + if(rfidService.isTicketLinked(t.getId())){ + throw new TicketAlreadyLinkedException(); + } + User newOwner = ttt.getUser(); t.setOwner(newOwner); diff --git a/src/test/java/ch/wisv/areafiftylan/TicketRestIntegrationTest.java b/src/test/java/ch/wisv/areafiftylan/TicketRestIntegrationTest.java index 07c1d3c49..1eb577b24 100644 --- a/src/test/java/ch/wisv/areafiftylan/TicketRestIntegrationTest.java +++ b/src/test/java/ch/wisv/areafiftylan/TicketRestIntegrationTest.java @@ -1,12 +1,15 @@ package ch.wisv.areafiftylan; +import ch.wisv.areafiftylan.exception.TicketAlreadyLinkedException; import ch.wisv.areafiftylan.exception.TokenNotFoundException; import ch.wisv.areafiftylan.model.Team; import ch.wisv.areafiftylan.model.Ticket; import ch.wisv.areafiftylan.model.User; +import ch.wisv.areafiftylan.model.relations.RFIDLink; import ch.wisv.areafiftylan.model.util.Gender; import ch.wisv.areafiftylan.model.util.TicketType; import ch.wisv.areafiftylan.security.token.TicketTransferToken; +import ch.wisv.areafiftylan.service.repository.RFIDLinkRepository; import ch.wisv.areafiftylan.service.repository.TeamRepository; import ch.wisv.areafiftylan.service.repository.TicketRepository; import ch.wisv.areafiftylan.service.repository.token.TicketTransferTokenRepository; @@ -42,6 +45,8 @@ public class TicketRestIntegrationTest extends IntegrationTest { private TicketTransferTokenRepository tttRepository; @Autowired private TeamRepository teamRepository; + @Autowired + private RFIDLinkRepository rfidLinkRepository; @Before public void initTransferTest() { @@ -51,6 +56,7 @@ public void initTransferTest() { @After public void cleanTransferTest() { + rfidLinkRepository.deleteAll(); teamRepository.deleteAll(); tttRepository.deleteAll(); ticketRepository.deleteAll(); @@ -207,7 +213,6 @@ public void testDoTransfer_Owner() { Assert.assertTrue(ticket.getOwner().equals(user)); } - @Test public void testDoTransfer_Receiver() { TicketTransferToken ttt = addTicketTransferGetToken(); @@ -231,6 +236,33 @@ public void testDoTransfer_Receiver() { Assert.assertTrue(ticket.getOwner().equals(ticketReceiver)); } + @Test + public void testDoTransferTicketLinked_Receiver() { + TicketTransferToken ttt = addTicketTransferGetToken(); + TicketAlreadyLinkedException e = new TicketAlreadyLinkedException(); + + SessionData login = login(ticketReceiver.getUsername(), ticketReceiverCleartextPassword); + + rfidLinkRepository.saveAndFlush(new RFIDLink("1212121212", ticket)); + + given(). + filter(sessionFilter). + header(login.getCsrfHeader()). + when(). + content(ttt.getToken()). + put(TRANSFER_ENDPOINT). + then(). + statusCode(HttpStatus.SC_CONFLICT). + body(containsString(e.getMessage())); + + String token = ttt.getToken(); + ttt = tttRepository.findByToken(token).orElseThrow(() -> new TokenNotFoundException(token)); + ticket = ticketRepository.findOne(ticket.getId()); + + Assert.assertTrue(ttt.isValid()); + Assert.assertTrue(ticket.getOwner().equals(user)); + } + @Test public void testDoTransfer_Outsider() { TicketTransferToken ttt = addTicketTransferGetToken(); @@ -276,6 +308,18 @@ public void testDuplicateSetupForTransferDifferentCase() { //@formatter:on } + @Test + public void testRFIDLinkedSetupForTransfer() { + TicketAlreadyLinkedException e = new TicketAlreadyLinkedException(); + + rfidLinkRepository.save(new RFIDLink("1212121212", ticket)); + + addTicketTransfer(user.getUsername(), userCleartextPassword) + .then() + .statusCode(HttpStatus.SC_CONFLICT) + .body(containsString(e.getMessage())); + } + @Test public void testCancelTransfer_Anon() { TicketTransferToken ttt = addTicketTransferGetToken();