Skip to content

Commit

Permalink
RFID linked tickets can't be transferred. (#249)
Browse files Browse the repository at this point in the history
* RFID linked tickets can't be transferred.

* Can't perform an open transfer if a ticket has been linked in the meanwhile.
  • Loading branch information
bvanderdrift authored and Martijn Janssen committed May 31, 2016
1 parent 05f2498 commit e255fac
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
12 changes: 12 additions & 0 deletions src/main/java/ch/wisv/areafiftylan/service/TicketServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -42,6 +45,8 @@ public class TicketRestIntegrationTest extends IntegrationTest {
private TicketTransferTokenRepository tttRepository;
@Autowired
private TeamRepository teamRepository;
@Autowired
private RFIDLinkRepository rfidLinkRepository;

@Before
public void initTransferTest() {
Expand All @@ -51,6 +56,7 @@ public void initTransferTest() {

@After
public void cleanTransferTest() {
rfidLinkRepository.deleteAll();
teamRepository.deleteAll();
tttRepository.deleteAll();
ticketRepository.deleteAll();
Expand Down Expand Up @@ -207,7 +213,6 @@ public void testDoTransfer_Owner() {
Assert.assertTrue(ticket.getOwner().equals(user));
}


@Test
public void testDoTransfer_Receiver() {
TicketTransferToken ttt = addTicketTransferGetToken();
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit e255fac

Please sign in to comment.