카테고리 없음

Spring Security 테스트 - 2

ehdtnn 2021. 1. 4. 22:22
반응형

스프링 시큐리티 테스트

  • formLogin() : 폼 로그인 테스트
  • authenticated() : 인증 
  • unauthenticated() : 인증 실패
  • @Transactional : 개별 테스트 (username 중복 방지)
package me.whiteship.demospringsecurityform.account;

import org.junit.Before;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;

import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated;
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.unauthenticated;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
class AccountControllerTest {

    @Autowired
    MockMvc mockMvc;

    @Autowired
    AccountService accountService;

    @DisplayName("로그인 성공")
    @Test
    @Transactional  
    public void login_success() throws Exception {
        String username = "user1";
        String password = "123";
        Account account = this.createUser(username, password);
        mockMvc.perform(formLogin().user(username).password(password))
                .andExpect(authenticated());
    }

    @DisplayName("로그인 성공 - 트랜잭션 테스트")
    @Test
    @Transactional  // 개별적으로 테스트 하도록 만들어준다.
    public void login_success2() throws Exception {
        String username = "user1";
        String password = "123";
        Account account = this.createUser(username, password);
        mockMvc.perform(formLogin().user(username).password(password))
                .andExpect(authenticated());
    }

    @DisplayName("로그인 실패 - 비밀번호 오류")
    @Test
    @Transactional
    public void login_fail() throws Exception {
        String username = "user1";
        String password = "123";
        Account account = this.createUser(username, password);
        mockMvc.perform(formLogin().user(username).password("12345"))
                .andExpect(unauthenticated());
    }

    private Account createUser(String username, String password) {
        Account account = new Account();
        account.setUsername(username);
        account.setPassword(password);
        account.setRole("USER");
        return accountService.createNew(account);
    }

}

 

반응형