Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  1. @RunWith(SpringRunner.class)
  2. @ContextConfiguration(classes = { CoreConfig.class, UserManagementConfig.class, UserManagementServiceConfig.class, LoginController.class }, initializers = ConfigFileApplicationContextInitializer.class)
  3. @WebAppConfiguration
  4. @WebMvcTest
  5. @ActiveProfiles({"core"})
  6. public class LoginControllerTest extends WithServiceInitializedTest {
  7.  
  8. private MockMvc mockMvc;
  9.  
  10. @Autowired
  11. private Filter springSecurityFilterChain;
  12.  
  13. @Autowired
  14. private WebApplicationContext webApplicationContext;
  15.  
  16. @Before
  17. public void setUp() {
  18. mockMvc = MockMvcBuilders
  19. .webAppContextSetup(webApplicationContext)
  20. .addFilters(springSecurityFilterChain)
  21. .alwaysDo(print())
  22. .build();
  23. }
  24.  
  25. @Test
  26. @WithMockUser(value = "root", roles = { "AUTH", "USER_MANAGEMENT" })
  27. public void testCookieLogin() throws Exception {
  28. HttpSessionCsrfTokenRepository httpSessionCsrfTokenRepository = new HttpSessionCsrfTokenRepository();
  29. CsrfToken csrfToken = httpSessionCsrfTokenRepository.generateToken(new MockHttpServletRequest());
  30. MockHttpServletRequestBuilder req = get(CoreHttpPathStore.CONTEXT_PATH)
  31. .header("Accept", "*/*")
  32. .header("Cache-Control", "no-cache")
  33. .header("Connection", "keep-alive")
  34. .header("content-type", "application/json");
  35. MvcResult resultBefore = mockMvc.perform(
  36. req
  37. )
  38. .andDo(result -> setSessionBackOnRequestBuilder(req, result.getRequest()))
  39. // .andExpect(authenticated())
  40. .andExpect(status().isUnauthorized())
  41. // .andExpect(cookie().exists("XSRF-TOKEN"))
  42. // .andExpect(cookie().exists("JSESSIONID"))
  43. .andReturn();
  44. MockHttpServletRequestBuilder reqLogin = post(CoreHttpPathStore.LOGIN)
  45. .requestAttr("X-XSRF-TOKEN", csrfToken)
  46. .contentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE)
  47. .param("username", "root")
  48. .param("password", "password");
  49. MvcResult result = mockMvc.perform(
  50. reqLogin
  51. )
  52. .andDo(result2 -> setSessionBackOnRequestBuilder(req, result2.getRequest()))
  53. .andExpect(status().isOk())
  54. // .andExpect(cookie().exists("JSESSIONID"))
  55. .andReturn();
  56. Cookie sessionId = result.getResponse().getCookie("JSESSIONID");
  57. Cookie token = result.getResponse().getCookie("XSRF-TOKEN");
  58. assertThat(sessionId).isEqualTo("yes");
  59. assertThat(token).isEqualTo("ye2s");
  60. }
  61.  
  62. private MockHttpServletRequest setSessionBackOnRequestBuilder(final MockHttpServletRequestBuilder requestBuilder,
  63. final MockHttpServletRequest request) {
  64. requestBuilder.session((MockHttpSession) request.getSession());
  65. return request;
  66. }
  67.  
  68. @Test
  69. public void testClientHomeWithError() throws Exception {
  70. MvcResult result = this.mockMvc.perform(
  71. get(CoreHttpPathStore.LOGIN_OAUTH_CLIENT_CB, "dummyClientId")
  72. .accept(MediaType.APPLICATION_JSON_UTF8_VALUE)
  73. )
  74. .andExpect(status().isUnauthorized())
  75. .andReturn();
  76. assertThat(result.getResponse().getContentAsString()).isEqualTo(""" + HttpStatus.UNAUTHORIZED.getReasonPhrase() + """);
  77. }
  78.  
  79. @Test
  80. @WithMockUser(value = "root", roles = { "AUTH", "USER_MANAGEMENT" })
  81. public void testClientHomeWithSuccess() throws Exception {
  82. MvcResult result = this.mockMvc.perform(
  83. get(CoreHttpPathStore.REST_PATH)
  84. .accept(MediaType.APPLICATION_JSON_UTF8_VALUE)
  85. .with(user("root").roles("AUTH", "USER_MANAGEMENT"))
  86. )
  87. .andExpect(status().isOk())
  88. .andReturn();
  89. assertThat(result.getResponse().getContentAsString()).isEqualTo(""" + HttpStatus.UNAUTHORIZED.getReasonPhrase() + """);
  90. }
  91. }
  92.  
  93. MockHttpServletRequest:
  94. HTTP Method = GET
  95. Request URI = /
  96. Parameters = {}
  97. Headers = {Accept=[*/*], Cache-Control=[no-cache], Connection=[keep-alive], Content-Type=[application/json]}
  98.  
  99. Handler:
  100. Type = null
  101.  
  102. Async:
  103. Async started = false
  104. Async result = null
  105.  
  106. Resolved Exception:
  107. Type = null
  108.  
  109. ModelAndView:
  110. View name = null
  111. View = null
  112. Model = null
  113.  
  114. FlashMap:
  115. Attributes = null
  116.  
  117. MockHttpServletResponse:
  118. Status = 401
  119. Error message = null
  120. Headers = {X-Content-Type-Options=[nosniff], X-XSS-Protection=[1; mode=block], Cache-Control=[no-cache, no-store, max-age=0, must-revalidate], Pragma=[no-cache], Expires=[0], X-Frame-Options=[DENY]}
  121. Content type = null
  122. Body = "Unauthorized"
  123. Forwarded URL = null
  124. Redirected URL = null
  125. Cookies = []
  126.  
  127. MockHttpServletRequest:
  128. HTTP Method = POST
  129. Request URI = /login
  130. Parameters = {username=[root], password=[password]}
  131. Headers = {Content-Type=[application/x-www-form-urlencoded]}
  132.  
  133. Handler:
  134. Type = null
  135.  
  136. Async:
  137. Async started = false
  138. Async result = null
  139.  
  140. Resolved Exception:
  141. Type = null
  142.  
  143. ModelAndView:
  144. View name = null
  145. View = null
  146. Model = null
  147.  
  148. FlashMap:
  149. Attributes = null
  150.  
  151. MockHttpServletResponse:
  152. Status = 401
  153. Error message = null
  154. Headers = {X-Content-Type-Options=[nosniff], X-XSS-Protection=[1; mode=block], Cache-Control=[no-cache, no-store, max-age=0, must-revalidate], Pragma=[no-cache], Expires=[0], X-Frame-Options=[DENY]}
  155. Content type = null
  156. Body = "Unauthorized"
  157. Forwarded URL = null
  158. Redirected URL = null
  159. Cookies = []
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement