From 0c26b6fef4d011ac519aafc95e0a681bb77546c0 Mon Sep 17 00:00:00 2001 From: Dmitriy Kopylenko Date: Wed, 12 Dec 2018 16:33:33 -0500 Subject: [PATCH] SHIBUI-1031: 1038 --- .../security/controller/UsersController.java | 22 ++++++++++++++++--- .../UsersControllerIntegrationTests.groovy | 15 +++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/security/controller/UsersController.java b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/security/controller/UsersController.java index 7d95d5582..cd4ddc2d4 100644 --- a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/security/controller/UsersController.java +++ b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/security/controller/UsersController.java @@ -4,6 +4,8 @@ import edu.internet2.tier.shibboleth.admin.ui.security.repository.UserRepository; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @@ -29,16 +31,30 @@ public UsersController(UserRepository userRepository) { this.userRepository = userRepository; } + @Transactional(readOnly = true) @GetMapping public List getAll() { return userRepository.findAll(); } + @Transactional(readOnly = true) @GetMapping("/{username}") public ResponseEntity getOne(@PathVariable String username) { - return ResponseEntity - .ok(userRepository.findByUsername(username) - .orElseThrow(() -> new HttpClientErrorException(NOT_FOUND, String.format("User with username [%s] not found", username)))); + return ResponseEntity.ok(findUserOrThrowHttp404(username)); + } + + @Transactional + @DeleteMapping("/{username}") + public ResponseEntity deleteOne(@PathVariable String username) { + User user = findUserOrThrowHttp404(username); + userRepository.delete(user); + return ResponseEntity.noContent().build(); + } + + + private User findUserOrThrowHttp404(String username) { + return userRepository.findByUsername(username) + .orElseThrow(() -> new HttpClientErrorException(NOT_FOUND, String.format("User with username [%s] not found", username))); } } diff --git a/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/security/controller/UsersControllerIntegrationTests.groovy b/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/security/controller/UsersControllerIntegrationTests.groovy index 310278888..14d38e6b8 100644 --- a/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/security/controller/UsersControllerIntegrationTests.groovy +++ b/backend/src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/security/controller/UsersControllerIntegrationTests.groovy @@ -47,4 +47,19 @@ class UsersControllerIntegrationTests extends Specification { result.body.errorCode == '404' result.body.errorMessage == 'User with username [bogus] not found' } + + def 'DELETE ONE existing user'() { + when: 'GET request is made for one existing user' + def result = this.restTemplate.getForEntity("$RESOURCE_URI/admin", Map) + + then: 'Request completed with HTTP 200' + result.statusCodeValue == 200 + + when: 'DELETE request is made' + this.restTemplate.delete("$RESOURCE_URI/admin") + result = this.restTemplate.getForEntity("$RESOURCE_URI/admin", Map) + + then: 'The deleted user is gone' + result.statusCodeValue == 404 + } } \ No newline at end of file