diff --git a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/controller/support/RestControllersSupport.java b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/controller/support/RestControllersSupport.java index e210adc94..d918594db 100644 --- a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/controller/support/RestControllersSupport.java +++ b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/controller/support/RestControllersSupport.java @@ -35,13 +35,9 @@ public MetadataResolver findResolverOrThrowHttp404(String resolverResourceId) { return resolver; } - //TODO: Review this handler and update accordingly. Do we still need it? @ExceptionHandler(HttpClientErrorException.class) public ResponseEntity notFoundHandler(HttpClientErrorException ex) { - if(ex.getStatusCode() == NOT_FOUND) { - return ResponseEntity.status(NOT_FOUND).body(ex.getStatusText()); - } - throw ex; + return ResponseEntity.status(ex.getStatusCode()).body(new ErrorResponse(ex.getStatusCode().toString(), ex.getStatusText())); } @ExceptionHandler(ConstraintViolationException.class) 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 8d800cba9..7d95d5582 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 @@ -2,12 +2,18 @@ import edu.internet2.tier.shibboleth.admin.ui.security.model.User; import edu.internet2.tier.shibboleth.admin.ui.security.repository.UserRepository; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.HttpClientErrorException; import java.util.List; +import static org.springframework.http.HttpStatus.NOT_FOUND; + /** * Implementation of the REST resource endpoints exposing system users. * @@ -24,7 +30,15 @@ public UsersController(UserRepository userRepository) { } @GetMapping - List getAll() { + public List getAll() { return userRepository.findAll(); } + + @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)))); + } + } 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 ca1c45c7a..310278888 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 @@ -18,13 +18,33 @@ class UsersControllerIntegrationTests extends Specification { static RESOURCE_URI = '/api/admin/users' - def "GET users"() { - when: 'GET request is made for ALL users in the system' + def 'GET ALL users (when there are existing users)'() { + when: 'GET request is made for ALL users in the system, and system has users in it' def result = this.restTemplate.getForEntity(RESOURCE_URI, Object) - then: "Request completed successfully" + then: 'Request completed with HTTP 200 and returned a list of users' result.statusCodeValue == 200 result.body[0].username == 'admin' result.body[0].roles[0].name == 'ROLE_ADMIN' } + + def 'GET 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 and returned one user' + result.statusCodeValue == 200 + result.body.username == 'admin' + result.body.roles[0].name == 'ROLE_ADMIN' + } + + def 'GET ONE NON-existing user'() { + when: 'GET request is made for one NON-existing user' + def result = this.restTemplate.getForEntity("$RESOURCE_URI/bogus", Map) + + then: 'Request completed with HTTP 404' + result.statusCodeValue == 404 + result.body.errorCode == '404' + result.body.errorMessage == 'User with username [bogus] not found' + } } \ No newline at end of file