kikosiak

Untitled

Jun 9th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.50 KB | None | 0 0
  1. package com.car.rental.project;
  2.  
  3. import com.car.rental.project.model.User;
  4. import com.car.rental.project.service.UserService;
  5. import com.fasterxml.jackson.databind.ObjectMapper;
  6. import org.hamcrest.Matchers;
  7. import org.junit.Before;
  8. import org.junit.jupiter.api.Test;
  9. import org.junit.runner.RunWith;
  10. import org.mockito.Mockito;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
  13. import org.springframework.boot.test.context.SpringBootTest;
  14. import org.springframework.test.context.junit4.SpringRunner;
  15. import org.springframework.test.web.servlet.MockMvc;
  16. import org.springframework.test.web.servlet.result.ModelResultMatchers;
  17.  
  18. import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
  19. import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
  20. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
  21. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
  22. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
  23.  
  24. @RunWith(SpringRunner.class)
  25. @SpringBootTest
  26. @AutoConfigureMockMvc
  27. class UserControllerTest {
  28.  
  29. @Autowired
  30. private MockMvc mockMvc;
  31.  
  32. @Autowired
  33. private UserService userService;
  34.  
  35. @Before
  36. public void setUp() {
  37.         User user = new User();
  38.         Mockito.when(userService.findByUsername(user.getUsername()))
  39.                 .thenReturn(user);
  40.     }
  41.  
  42.     @Test
  43.     void souldRegisterNewUser() throws Exception{
  44.     User u = new User();
  45.     u.setUsername("user432");
  46.     u.setPassword("user123123");
  47.     u.setPasswordConfirm("user123123");
  48.  
  49.     this.mockMvc.perform(post("/registration")
  50.             .param("username",u.getUsername())
  51.             .param("password",u.getPassword())
  52.             .param("passwordConfirm",u.getPasswordConfirm()))
  53.             .andExpect(status().isFound())
  54.             .andExpect(redirectedUrl("/index"))
  55.             .andExpect(flash().attribute("register", Matchers.equalTo("true")));
  56.     }
  57.  
  58.     @Test
  59.     void souldNotRegisterNewUserBecousePasswordIsBad() throws Exception{
  60.         User u = new User();
  61.         u.setUsername("user432");
  62.         u.setPassword("user1231231");
  63.         u.setPasswordConfirm("user123123");
  64.  
  65.         this.mockMvc.perform(post("/registration")
  66.                 .param("username",u.getUsername())
  67.                 .param("password",u.getPassword())
  68.                 .param("passwordConfirm",u.getPasswordConfirm()))
  69.                 .andExpect(status().isFound())
  70.                 .andExpect(redirectedUrl("/index"))
  71.                 .andExpect(flash().attribute("error",Matchers.equalTo("password")));
  72.     }
  73.  
  74.     @Test
  75.     void indexShouldReturnOk() throws Exception {
  76.         this.mockMvc.perform(get("/")).andExpect(status().isOk());
  77.     }
  78.  
  79.     @Test
  80.     void flotaShouldReturnOk() throws Exception {
  81.         this.mockMvc.perform(get("/flota").with(user("user").roles("USER"))).andExpect(status().isOk());
  82.     }
  83.  
  84.     @Test
  85.     void adminPanelShouldReturnForbidden() throws Exception {
  86.         this.mockMvc.perform(get("/adminPanel").with(user("user").roles("USER"))).andExpect(status().isForbidden());
  87.     }
  88.  
  89.     @Test
  90.     void flotaShouldRedirect() throws Exception {
  91.         this.mockMvc.perform(get("/flota"))
  92.                 .andExpect(status().is(302))
  93.                 .andExpect(redirectedUrl("http://localhost/login"));
  94.     }
  95. }
Add Comment
Please, Sign In to add comment