스터디-Spring

[스프링 시큐리티] 스프링 시큐리티 테스트 - Form Login

일태우 2022. 1. 9. 14:25

폼 로그인 / 로그아웃 테스트 (SecurityMockMvcRequestBuilders)

  • perform(formLogin())
  • perform(formLogin().user("admin").password("123"))
  • perform(logout())
    @Test
    @Transactional
    public void login_success() throws Exception {
        String username = "twlee";
        String password = "123";
        Account user = createUser(username, password);
        mockMvc.perform(formLogin().user(user.getUsername()).password(password))
                .andExpect(authenticated());
    }

    @Test
    @Transactional
    public void login_fail() throws Exception {
        String username = "twlee";
        String password = "123";
        Account user = createUser(username, password);
        mockMvc.perform(formLogin().user(user.getUsername()).password("1234"))
                .andExpect(unauthenticated());
    }
  • authenticated, unauthenticated로 응답 확인