Skip to content

Commit

Permalink
Merge pull request #205 from AreaFiftyLAN/fix-check_for_duplicate_teams
Browse files Browse the repository at this point in the history
Fix check for duplicate teams with other casing
  • Loading branch information
Martijn Janssen committed May 5, 2016
2 parents 6952b9e + cc2e111 commit 734ff68
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public TeamRestController(TeamService teamService) {
@JsonView(View.Public.class)
@RequestMapping(method = RequestMethod.POST)
ResponseEntity<?> add(@Validated @RequestBody TeamDTO teamDTO, Authentication auth) {
if(teamService.teamnameUsed(teamDTO.getTeamName())){
return createResponseEntity(HttpStatus.CONFLICT, "Team with name \"" + teamDTO.getTeamName() + "\" already exists.");
}

Team team;
// Users can only create teams with themselves as Captain
if (auth.getAuthorities().contains(Role.ROLE_ADMIN)) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/ch/wisv/areafiftylan/service/TeamService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public interface TeamService {

Team getTeamByTeamname(String teamname);

boolean teamnameUsed(String teamname);

Collection<Team> getTeamByCaptainId(Long userId);

Collection<Team> getAllTeams();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,15 @@ public Team getTeamById(Long teamId) {

@Override
public Team getTeamByTeamname(String teamname) {
return teamRepository.findByTeamName(teamname)
return teamRepository.findByTeamNameIgnoreCase(teamname)
.orElseThrow(() -> new TeamNotFoundException("Cant find team with name " + teamname));
}

@Override
public boolean teamnameUsed(String teamname) {
return teamRepository.findByTeamNameIgnoreCase(teamname).isPresent();
}

@Override
public Collection<Team> getTeamByCaptainId(Long userId) {
return teamRepository.findByCaptainId(userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface TeamRepository extends JpaRepository<Team, Long> {

Collection<Team> findAllByCaptainUsername(String username);

Optional<Team> findByTeamName(String teamName);
Optional<Team> findByTeamNameIgnoreCase(String teamName);

Optional<Team> findById(Long teamId);
}
38 changes: 38 additions & 0 deletions src/test/java/ch/wisv/areafiftylan/TeamRestIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,44 @@ public void testCreateTeamAsUserDuplicateTeamName() {
statusCode(HttpStatus.SC_CONFLICT);
//@formatter:on
}

@Test
public void testCreateTeamAsUserDuplicateTeamNameDifferentCasing() {
team1.put("captainUsername", teamCaptain.getUsername());

SessionData login = login("captain", teamCaptainCleartextPassword);

//@formatter:off
given().
filter(sessionFilter).
header(login.getCsrfHeader()).
when().
content(team1).contentType(ContentType.JSON).
post("/teams").
then().
statusCode(HttpStatus.SC_CREATED).
header("Location", containsString("/teams/")).
body("object.teamName", equalTo(team1.get("teamName"))).
body("object.captain.profile.displayName", equalTo(teamCaptain.getProfile().getDisplayName())).
body("object.members.profile.displayName", hasItem(teamCaptain.getProfile().getDisplayName()));

logout();

team1.put("captainUsername", user.getUsername());
team1.put("teamName", "Testteam1");

SessionData login2 = login("user", userCleartextPassword);

given().
filter(sessionFilter).
header(login2.getCsrfHeader()).
when().
content(team1).contentType(ContentType.JSON).
post("/teams").
then().
statusCode(HttpStatus.SC_CONFLICT);
//@formatter:on
}
//endregion

//region Test Get Team
Expand Down

0 comments on commit 734ff68

Please sign in to comment.