protected RequestPostProcessor userToken() { return new RequestPostProcessor() { @Override public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) { // If the tests requires setup logic for users, you can place it here. // Authorization headers or cookies for users should be added here as well. String accessToken; try { accessToken = getAccessToken("test", "test"); } catch (Exception e) { throw new RuntimeException(e); } request.addHeader("Authorization", "Bearer " + accessToken); return documentAuthorization(request, "User access token required."); } }; }
@Test public void whenUserInfoNotInSessionThenEmptySessionExceptionIsThrown() throws Exception{ mockMvc.perform(get("/summary") .accept(MediaType.APPLICATION_JSON) .with(new RequestPostProcessor() { @Override public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) { request.getSession(true); return request; } })) .andExpect(status().isInternalServerError()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)); }
@Test public void whenUserInfoHasInvalidBarcodeThenPatronLibraryAccessExceptionIsThrown() throws Exception { final UserInfo userInfo = new UserInfo("BadUUN","BadBarcode","BadSurname"); mockMvc.perform(get("/summary") .accept(MediaType.APPLICATION_JSON) .with(new RequestPostProcessor() { @Override public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) { MockHttpSession mockSession = (MockHttpSession) request.getSession(true); mockSession.setAttribute(UserInfo.SESSION_ATTR, userInfo); request.setSession(mockSession); return request; } })) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("$.*", hasSize(9))) .andExpect(jsonPath("$.message", nullValue())) .andExpect(jsonPath("$.payload", nullValue())) .andReturn(); }
/** * Build a {@link org.springframework.test.web.servlet.MockMvc} instance. */ @Override @SuppressWarnings("rawtypes") public final MockMvc build() { WebApplicationContext wac = initWebAppContext(); ServletContext servletContext = wac.getServletContext(); MockServletConfig mockServletConfig = new MockServletConfig(servletContext); for (MockMvcConfigurer configurer : this.configurers) { RequestPostProcessor processor = configurer.beforeMockMvcCreated(this, wac); if (processor != null) { if (this.defaultRequestBuilder == null) { this.defaultRequestBuilder = MockMvcRequestBuilders.get("/"); } if (this.defaultRequestBuilder instanceof ConfigurableSmartRequestBuilder) { ((ConfigurableSmartRequestBuilder) this.defaultRequestBuilder).with(processor); } } } Filter[] filterArray = this.filters.toArray(new Filter[this.filters.size()]); return super.createMockMvc(filterArray, mockServletConfig, wac, this.defaultRequestBuilder, this.globalResultMatchers, this.globalResultHandlers, this.dispatchOptions); }
@Override public RequestPostProcessor beforeMockMvcCreated(ConfigurableMockMvcBuilder<?> builder, WebApplicationContext context) { return request -> { request.setUserPrincipal(mock(Principal.class)); return request; }; }
@Test public void testAuthenticatedUser() throws Exception { restUserMockMvc.perform(get("/api/authenticate") .with(new RequestPostProcessor() { public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) { request.setRemoteUser("test"); return request; } }) .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().string("test")); }
public RequestPostProcessor bearerToken(final String clientid, final String username) { return mockRequest -> { OAuth2Authentication auth = oAuth2Authentication(clientid, username); OAuth2AccessToken token = tokenservice.createAccessToken(auth); mockRequest.addHeader("Authorization", "Bearer " + token.getValue()); return mockRequest; }; }
@Test public void testAuthenticatedUser() throws Exception { restUserMockMvc.perform(get("/app/rest/authenticate") .with(new RequestPostProcessor() { public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) { request.setRemoteUser("test"); return request; } }) .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().string("test")); }
@Test public void testAuthenticatedUser() throws Exception { restUserMockMvc.perform(get("/api/authenticate") .with(new RequestPostProcessor() { public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) { request.setRemoteUser("test"); return request; } }) .accept(MediaType.APPLICATION_JSON_UTF8)) .andExpect(status().isOk()) .andExpect(content().string("test")); }
public RequestPostProcessor oauth2Authentication(String username, Set<String> scopes) { return oauth2Authentication(username, scopes, Collections.emptySet()); }
public RequestPostProcessor oauth2Authentication(String username) { return oauth2Authentication(username, Collections.emptySet()); }
private RequestPostProcessor userCredentials() { return httpBasic("user", "password"); }
private RequestPostProcessor setupRequest() { return request -> { request.setRemoteAddr("127.0.0.1"); return request; }; }
private ResultActions testPostpage0(RequestPostProcessor postProcessor) throws Exception { log.info("postpage"); Long problem_id = null; return mvc.perform(get("/postpage").with(postProcessor) .param("problem_id", Objects.toString(problem_id, ""))); }
@Override public RequestPostProcessor beforeMockMvcCreated(ConfigurableMockMvcBuilder<?> builder, WebApplicationContext cxt) { return null; }
public void setForwardPostProcessor(RequestPostProcessor forwardPostProcessor) { this.forwardPostProcessor = forwardPostProcessor; }
public static RequestPostProcessor userAlice() { return user("Alice").roles("USER"); }
public static RequestPostProcessor userBob() { return user("Bob").roles("USER"); }
public static RequestPostProcessor userAdmin() { return user("admin").roles("USER", "ADMIN"); }
@Override public RequestPostProcessor beforeMockMvcCreated(ConfigurableMockMvcBuilder<?> builder, WebApplicationContext context) { AbstractControllerTest.this.lazySpringConstraintValidatorFactory.setDelegate(new SpringConstraintValidatorFactory(context.getAutowireCapableBeanFactory())); AbstractControllerTest.this.lazySpringConstraintValidatorFactory.setBeanFactory(context.getAutowireCapableBeanFactory()); return null; }
@Test public void testHelloUserWithRole() throws Exception { RequestPostProcessor bearerToken = helper.bearerToken("myclientwith", "user"); mvc.perform(get("/hello").with(bearerToken)).andExpect(status().isOk()).andExpect(content().string("Hello user")); }
@Test public void testHelloAliceWithRole() throws Exception { RequestPostProcessor bearerToken = helper.bearerToken("myclientwith", "alice"); mvc.perform(get("/hello").with(bearerToken)).andExpect(status().isOk()).andExpect(content().string("Hello alice")); }
@Test public void testHelloWithoutRole() throws Exception { RequestPostProcessor bearerToken = helper.bearerToken("myclientwithout", "user"); mvc.perform(get("/hello").with(bearerToken)).andExpect(status().isForbidden()); }