content: package com.konsol.beatstream.web.rest;
  
  import static org.hamcrest.Matchers.emptyString;
  import static org.hamcrest.Matchers.is;
  import static org.hamcrest.Matchers.not;
  import static org.hamcrest.Matchers.nullValue;
  import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  
  import com.fasterxml.jackson.databind.ObjectMapper;
  import com.konsol.beatstream.IntegrationTest;
  import com.konsol.beatstream.domain.User;
  import com.konsol.beatstream.repository.UserRepository;
  import com.konsol.beatstream.web.rest.vm.LoginVM;
  import org.junit.jupiter.api.Test;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
  import org.springframework.http.MediaType;
  import org.springframework.security.crypto.password.PasswordEncoder;
  import org.springframework.test.web.servlet.MockMvc;
  
  /**
   * Integration tests for the {@link AuthenticateController} REST controller.
   */
  @AutoConfigureMockMvc
  @IntegrationTest
  class AuthenticateControllerIT {
  
      @Autowired
      private ObjectMapper om;
  
      @Autowired
      private UserRepository userRepository;
  
      @Autowired
      private PasswordEncoder passwordEncoder;
  
      @Autowired
      private MockMvc mockMvc;
  
      @Test
      void testAuthorize() throws Exception {
          User user = new User();
          user.setLogin("user-jwt-controller");
          user.setEmail("user-jwt-controller@example.com");
          user.setActivated(true);
          user.setPassword(passwordEncoder.encode("test"));
  
          userRepository.save(user);
  
          LoginVM login = new LoginVM();
          login.setUsername("user-jwt-controller");
          login.setPassword("test");
          mockMvc
              .perform(post("/api/authenticate").contentType(MediaType.APPLICATION_JSON).content(om.writeValueAsBytes(login)))
              .andExpect(status().isOk())
              .andExpect(jsonPath("$.id_token").isString())
              .andExpect(jsonPath("$.id_token").isNotEmpty())
              .andExpect(header().string("Authorization", not(nullValue())))
              .andExpect(header().string("Authorization", not(is(emptyString()))));
      }
  
      @Test
      void testAuthorizeWithRememberMe() throws Exception {
          User user = new User();
          user.setLogin("user-jwt-controller-remember-me");
          user.setEmail("user-jwt-controller-remember-me@example.com");
          user.setActivated(true);
          user.setPassword(passwordEncoder.encode("test"));
  
          userRepository.save(user);
  
          LoginVM login = new LoginVM();
          login.setUsername("user-jwt-controller-remember-me");
          login.setPassword("test");
          login.setRememberMe(true);
          mockMvc
              .perform(post("/api/authenticate").contentType(MediaType.APPLICATION_JSON).content(om.writeValueAsBytes(login)))
              .andExpect(status().isOk())
              .andExpect(jsonPath("$.id_token").isString())
              .andExpect(jsonPath("$.id_token").isNotEmpty())
              .andExpect(header().string("Authorization", not(nullValue())))
              .andExpect(header().string("Authorization", not(is(emptyString()))));
      }
  
      @Test
      void testAuthorizeFails() throws Exception {
          LoginVM login = new LoginVM();
          login.setUsername("wrong-user");
          login.setPassword("wrong password");
          mockMvc
              .perform(post("/api/authenticate").contentType(MediaType.APPLICATION_JSON).content(om.writeValueAsBytes(login)))
              .andExpect(status().isUnauthorized())
              .andExpect(jsonPath("$.id_token").doesNotExist())
              .andExpect(header().doesNotExist("Authorization"));
      }
  }
  