Skip to content

Commit

Permalink
Fixes #537
Browse files Browse the repository at this point in the history
  • Loading branch information
oharsta committed Oct 11, 2024
1 parent 77b0aac commit f516310
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,35 @@ public ResponseEntity<UpdateExternalEduID> updateEduID(@Parameter(hidden = true)
return ResponseEntity.status(HttpStatus.CREATED).body(externalEduID);
}

@DeleteMapping(value = {"/eduid-delete/{eduid}"})
@PreAuthorize("hasRole('ROLE_remote-creation')")
@Operation(summary = "Delete an eduID",
description = "Delete an eduID",
responses = {
@ApiResponse(responseCode = "204", description = "No content",
content = {@Content(schema = @Schema(implementation = UpdateExternalEduID.class))}),
@ApiResponse(responseCode = "400", description = "BadRequest",
content = {@Content(schema = @Schema(implementation = StatusResponse.class),
examples = {@ExampleObject(value = "{\"status\":400}")})}),
@ApiResponse(responseCode = "404", description = "No eduID found",
content = {@Content(schema = @Schema(implementation = StatusResponse.class),
examples = {@ExampleObject(value = "{\n" +
" \"timestamp\": 1717672263253,\n" +
" \"status\": 404,\n" +
" \"error\": \"Not found\",\n" +
" \"exception\": \"myconext.exceptions.UserNotFoundException\",\n" +
" \"message\": \"User not found\",\n" +
" \"path\": \"/api/remote-creation/eduid-delete\"\n" +
"}")})})})
public ResponseEntity<Void> deleteEduID(@Parameter(hidden = true) @AuthenticationPrincipal(errorOnInvalidType = true) RemoteUser remoteUser,
@PathVariable("eduid") String eduIDValue) {
LOG.info(String.format("DELETE eduid-delete by %s for %s", remoteUser.getUsername(), eduIDValue));
User user = userRepository.findByEduIDS_value(eduIDValue).orElseThrow(() -> new UserNotFoundException("User not found"));
user.getEduIDS().removeIf(eduID -> eduID.getValue().equals(eduIDValue));
userRepository.save(user);
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}

private static IdentityProvider getIdentityProvider(RemoteUser remoteUser, NewExternalEduID externalEduID, String remoteUserName) {
RemoteProvider remoteProvider = new RemoteProvider(
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,35 @@ void updateEduIDExternalAccountUpdateIdemPotent() {
assertEquals(1, user.getExternalLinkedAccounts().size());
}

@Test
void deleteEduID() {
User user = userRepository.findUserByEmail(email).get();
assertEquals(2, user.getEduIDS().size());
given()
.when()
.auth().preemptive().basic(userName, password)
.contentType(ContentType.JSON)
.pathParam("eduid", user.getEduIDS().get(0).getValue())
.delete("/api/remote-creation/eduid-delete/{eduid}")
.then()
.statusCode(204);

User updatedUser = userRepository.findUserByEmail(email).get();
assertEquals(1, updatedUser.getEduIDS().size());
}

@Test
void deleteEduIDNotFound() {
given()
.when()
.auth().preemptive().basic(userName, password)
.contentType(ContentType.JSON)
.pathParam("eduid", "nope")
.delete("/api/remote-creation/eduid-delete/{eduid}")
.then()
.statusCode(404);
}

@Test
void findUserByEduIDValueWithNullCheck() {
assertFalse(super.findUserByEduIDValue(null).isPresent());
Expand Down

0 comments on commit f516310

Please sign in to comment.