Java Code Examples for org.springframework.mock.web.MockHttpServletRequest#addUserRole()

The following examples show how to use org.springframework.mock.web.MockHttpServletRequest#addUserRole() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ServletModuleTest.java    From crnk-framework with Apache License 2.0 6 votes vote down vote up
@Test
public void testSecurityProviderInstalled() {
	ImmediateResultFactory resultFactory = new ImmediateResultFactory();

	CrnkBoot boot = new CrnkBoot();
	HttpRequestContextProvider provider = new HttpRequestContextProvider(() -> resultFactory, boot.getModuleRegistry());
	ServletModule module = new ServletModule(provider);
	boot.addModule(module);
	boot.boot();

	SecurityProvider securityProvider = boot.getModuleRegistry().getSecurityProvider();
	ServletContext servletContext = Mockito.mock(ServletContext.class);
	MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
	request.setRequestURI("/api/tasks");
	MockHttpServletResponse response = new MockHttpServletResponse();
	request.addUserRole("guest");
	request.addUserRole("admin");

	provider.onRequestStarted(new HttpRequestContextBaseAdapter(new ServletRequestContext(servletContext, request,
			response, "api", HttpHeaders.DEFAULT_CHARSET)));

	Assert.assertFalse(securityProvider.isAuthenticated(null));
	Assert.assertFalse(securityProvider.isUserInRole("doesNotExist", null));
	Assert.assertTrue(securityProvider.isUserInRole("guest", null));
	Assert.assertTrue(securityProvider.isUserInRole("admin", null));
}
 
Example 2
Source File: ApiListenerServletTest.java    From iaf with Apache License 2.0 6 votes vote down vote up
@Test
public void authRoleAuthentication401() throws ServletException, IOException, ListenerException, ConfigurationException {
	String uri = "authRole2";
	addListener(uri, Methods.POST, AuthMethods.AUTHROLE);

	Map<String, String> headers = new HashMap<String, String>();
	headers.put("Authorization", "am9objpkb2U=");
	MockHttpServletRequest request = createRequest(uri, Methods.POST, "{\"tralalalallala\":true}", headers);
	request.setAuthType("BASIC_AUTH");

	request.addUserRole("non-existing-role");

	Response result = service(request);

	assertEquals(401, result.getStatus());
	assertFalse(result.containsHeader("Allow"));
	assertNull(result.getErrorMessage());
}
 
Example 3
Source File: ApiListenerServletTest.java    From iaf with Apache License 2.0 6 votes vote down vote up
@Test
public void authRoleAuthentication200() throws ServletException, IOException, ListenerException, ConfigurationException {
	String uri = "authRole2";
	addListener(uri, Methods.POST, AuthMethods.AUTHROLE);

	Map<String, String> headers = new HashMap<String, String>();
	headers.put("Authorization", "am9objpkb2U=");
	MockHttpServletRequest request = createRequest(uri, Methods.POST, "{\"tralalalallala\":true}", headers);
	request.setAuthType("BASIC_AUTH");

	request.addUserRole("IbisObserver");

	Response result = service(request);

	assertEquals(200, result.getStatus());
	assertTrue(result.containsHeader("Allow"));
	assertNull(result.getErrorMessage());
}