Skip to content

Commit

Permalink
Showing 3 changed files with 39 additions and 9 deletions.
@@ -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)
@@ -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<User> getAll() {
public List<User> 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))));
}

}
@@ -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'
}
}

0 comments on commit 7f25830

Please sign in to comment.