Advertisement
Guest User

Untitled

a guest
Mar 8th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.10 KB | None | 0 0
  1. public <T> ResponseEntity<T> login(ParameterizedTypeReference<T> responseType) {
  2.         return testRestTemplate.exchange(API_1_0_LOGIN, HttpMethod.POST, null, responseType);
  3.     }
  4.  
  5. public static User createValidUser() {
  6.         User user = new User();
  7.         user.setUserId(1L);
  8.         user.setUsername("test-user");
  9.         user.setDisplayName("test-display");
  10.         user.setPassword("P4assword");
  11.         return user;
  12.     }
  13.  
  14.  @Test
  15.  public void postLogin_withValidCredentials_receiveLoggedInUserId() {
  16.         User inDB = userService.save(createValidUser());
  17.         authenticate();
  18.         ResponseEntity<Map<String, Object>> response = login(new ParameterizedTypeReference<>() {});
  19.         Map<String, Object> body = response.getBody();
  20.  
  21.         Integer id = (Integer) body.get("id");
  22.         assertThat(id).isEqualTo(inDB.getUserId());
  23.     }
  24.  
  25.  
  26.     @PostMapping
  27.     public Map<String, Object> handleLogin() {
  28.         User loggedInUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
  29.         return Collections.singletonMap("id", loggedInUser.getUserId());
  30.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement