회원가입 컨트롤러

@Controller
@RequestMapping("/signup")
public class SignUpController {

	private final AccountService accountService;

	public SignUpController(AccountService accountService) {
		this.accountService = accountService;
	}

	@GetMapping
	public String signup(Model model) {
		model.addAttribute("account", new Account());
		return "signup";
	}

	@PostMapping
	public String processSignUp(@ModelAttribute Account account) {
		account.setRole("USER");
		accountService.createAccount(account);
		return "redirect:/";
	}
}

이제 form으로 회원가입을 받을 수가 있게 되었다. 이 과정에서 csrf를 사용하는 것을 보자

테스트 작성은 어떻게 할 것인가?

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class SignUpControllerTest {

	@Autowired
	MockMvc mockMvc;

	@Test
	public void signUpForm() throws Exception {
		mockMvc.perform(get("/signup"))
			.andDo(print())
			.andExpect(status().isOk())
			.andExpect(content().string(containsString("_csrf")));

	}

	@Test
	public void processSignUp() throws Exception {
		mockMvc.perform(post("/signup")
				.param("username", "cmlee")
				.param("password", "123")
				.with(csrf()))
			.andExpect(status().is3xxRedirection());
	}
}