use of net.sourceforge.stripes.mock.MockHttpServletRequest in project jspwiki by apache.
the class WikiSessionTest method testUserPrincipal.
@Test
public void testUserPrincipal() throws ServletException, IOException {
MockHttpServletRequest request;
WikiSession wikiSession;
// Changing the UserPrincipal value should cause the user to be authenticated...
request = m_engine.newHttpRequest();
request.setUserPrincipal(new WikiPrincipal("Fred Flintstone"));
runSecurityFilter(m_engine, request);
wikiSession = WikiSession.getWikiSession(m_engine, request);
Assert.assertTrue(wikiSession.isAuthenticated());
Assert.assertEquals("Fred Flintstone", wikiSession.getUserPrincipal().getName());
}
use of net.sourceforge.stripes.mock.MockHttpServletRequest in project jspwiki by apache.
the class WikiSessionTest method authenticatedSession.
public static WikiSession authenticatedSession(TestEngine engine, String id, String password) throws Exception {
// Build anon session
MockHttpServletRequest request = engine.newHttpRequest();
// Log in as anon
runSecurityFilter(engine, request);
// Log in the user with credentials
WikiSession session = WikiSession.getWikiSession(engine, request);
engine.getAuthenticationManager().login(session, request, id, password);
// Make sure the user is actually authenticated
if (!session.isAuthenticated()) {
throw new IllegalStateException("Could not log in authenticated user '" + id + "'");
}
return session;
}
use of net.sourceforge.stripes.mock.MockHttpServletRequest in project jspwiki by apache.
the class WikiSessionTest method assertedSession.
public static WikiSession assertedSession(TestEngine engine, String name, Principal[] roles) throws Exception {
// We can use cookies right?
if (!engine.getAuthenticationManager().allowsCookieAssertions()) {
throw new IllegalStateException("Couldn't set up asserted user: login config doesn't allow cookies.");
}
// Build anon session
MockHttpServletRequest request = engine.newHttpRequest();
Set<String> r = new HashSet<String>();
for (int i = 0; i < roles.length; i++) {
r.add(roles[i].getName());
}
request.setRoles(r);
// Set cookie
Cookie cookie = new Cookie(CookieAssertionLoginModule.PREFS_COOKIE_NAME, name);
request.setCookies(new Cookie[] { cookie });
// Log in
runSecurityFilter(engine, request);
// Make sure the user is actually asserted
WikiSession session = WikiSession.getWikiSession(engine, request);
return session;
}
use of net.sourceforge.stripes.mock.MockHttpServletRequest in project jspwiki by apache.
the class WikiSessionTest method testAuthenticationCookieDefaults.
@Test
public void testAuthenticationCookieDefaults() throws ServletException, IOException {
MockHttpServletRequest request;
WikiSession wikiSession;
// Set the authentication cookie first
MockHttpServletResponse response = new MockHttpServletResponse();
CookieAuthenticationLoginModule.setLoginCookie(m_engine, response, "Fred Flintstone");
Cookie[] cookies = response.getCookies();
Assert.assertEquals(1, cookies.length);
String uid = cookies[0].getValue();
// Adding the magic "authentication cookie" should NOT count as authenticated in the default case
// (because cookie authentication is OFF).
request = m_engine.newHttpRequest();
request.setUserPrincipal(null);
request.setCookies(new Cookie[] { new Cookie("JSPWikiUID", uid) });
runSecurityFilter(m_engine, request);
wikiSession = WikiSession.getWikiSession(m_engine, request);
Assert.assertTrue(wikiSession.isAnonymous());
Assert.assertFalse(wikiSession.isAuthenticated());
Assert.assertEquals("127.0.0.1", wikiSession.getUserPrincipal().getName());
// Clear the authentication cookie
response = new MockHttpServletResponse();
CookieAuthenticationLoginModule.clearLoginCookie(m_engine, request, response);
}
use of net.sourceforge.stripes.mock.MockHttpServletRequest in project jspwiki by apache.
the class WikiSessionTest method testIPAddress.
@Test
public void testIPAddress() throws ServletException, IOException {
MockHttpServletRequest request;
WikiSession wikiSession;
// A naked HTTP request without userPrincipal/remoteUser should be anonymous
request = m_engine.newHttpRequest();
request.setUserPrincipal(null);
runSecurityFilter(m_engine, request);
wikiSession = WikiSession.getWikiSession(m_engine, request);
Assert.assertTrue(wikiSession.isAnonymous());
}
Aggregations