78.3 List all users (#9)
This commit is contained in:
@ -16,6 +16,7 @@ import org.springframework.security.core.userdetails.UserDetails;
|
|||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import static org.springframework.http.HttpStatus.OK;
|
import static org.springframework.http.HttpStatus.OK;
|
||||||
|
|
||||||
@ -74,6 +75,11 @@ public class UserResource {
|
|||||||
return userService.findByUsername(username);
|
return userService.findByUsername(username);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public List<User> getAllUsers() {
|
||||||
|
return userService.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
private void authenticate(String username, String password) {
|
private void authenticate(String username, String password) {
|
||||||
Authentication auth = new UsernamePasswordAuthenticationToken(username, password);
|
Authentication auth = new UsernamePasswordAuthenticationToken(username, password);
|
||||||
authenticationManager.authenticate(auth);
|
authenticationManager.authenticate(auth);
|
||||||
|
|||||||
@ -12,10 +12,12 @@ import org.junit.jupiter.api.Test;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||||
|
import org.springframework.core.ParameterizedTypeReference;
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.test.context.TestPropertySource;
|
import org.springframework.test.context.TestPropertySource;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
@ -467,4 +469,24 @@ class UserResourceUnSecureTest extends BaseUserTest {
|
|||||||
.hasFieldOrPropertyWithValue("message", String.format("User with username `%s` not found", username).toUpperCase());
|
.hasFieldOrPropertyWithValue("message", String.format("User with username `%s` not found", username).toUpperCase());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
class GetAllUsersTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getAllUsers() {
|
||||||
|
|
||||||
|
//when
|
||||||
|
var responseEntity = restTemplate.exchange("/user", HttpMethod.GET, null, new ParameterizedTypeReference<List<User>>() {
|
||||||
|
});
|
||||||
|
|
||||||
|
//then
|
||||||
|
log.debug("Response Entity: {}", responseEntity);
|
||||||
|
assertThat(responseEntity.getStatusCode()).isEqualTo(OK);
|
||||||
|
assertThat(responseEntity.getBody())
|
||||||
|
.isNotNull()
|
||||||
|
.hasSizeGreaterThan(2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user