78.3 List all users (#9)

This commit is contained in:
Art
2021-09-09 23:02:43 +03:00
parent 0eedcf8269
commit e400f04a24
2 changed files with 28 additions and 0 deletions

View File

@ -16,6 +16,7 @@ import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
import static org.springframework.http.HttpStatus.OK;
@ -74,6 +75,11 @@ public class UserResource {
return userService.findByUsername(username);
}
@GetMapping
public List<User> getAllUsers() {
return userService.findAll();
}
private void authenticate(String username, String password) {
Authentication auth = new UsernamePasswordAuthenticationToken(username, password);
authenticationManager.authenticate(auth);

View File

@ -12,10 +12,12 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.TestPropertySource;
import java.util.List;
import java.util.Map;
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());
}
}
@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);
}
}
}