Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public <T> ResponseEntity<T> login(ParameterizedTypeReference<T> responseType) {
- return testRestTemplate.exchange(API_1_0_LOGIN, HttpMethod.POST, null, responseType);
- }
- public static User createValidUser() {
- User user = new User();
- user.setUserId(1L);
- user.setUsername("test-user");
- user.setDisplayName("test-display");
- user.setPassword("P4assword");
- return user;
- }
- @Test
- public void postLogin_withValidCredentials_receiveLoggedInUserId() {
- User inDB = userService.save(createValidUser());
- authenticate();
- ResponseEntity<Map<String, Object>> response = login(new ParameterizedTypeReference<>() {});
- Map<String, Object> body = response.getBody();
- Integer id = (Integer) body.get("id");
- assertThat(id).isEqualTo(inDB.getUserId());
- }
- @PostMapping
- public Map<String, Object> handleLogin() {
- User loggedInUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
- return Collections.singletonMap("id", loggedInUser.getUserId());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement