Search in sources :

Example 6 with MockHttpServletRequest

use of net.sourceforge.stripes.mock.MockHttpServletRequest in project jspwiki by apache.

the class WikiSessionTest method testAuthenticationCookieWhenOn.

@Test
public void testAuthenticationCookieWhenOn() throws WikiException, ServletException, IOException {
    Properties props = TestEngine.getTestProperties();
    props.setProperty(AuthenticationManager.PROP_ALLOW_COOKIE_AUTH, "true");
    m_engine = new TestEngine(props);
    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 count as authenticated
    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.assertFalse(wikiSession.isAnonymous());
    Assert.assertTrue(wikiSession.isAuthenticated());
    Assert.assertEquals("Fred Flintstone", wikiSession.getUserPrincipal().getName());
    // Clear the authentication cookie
    response = new MockHttpServletResponse();
    CookieAuthenticationLoginModule.clearLoginCookie(m_engine, request, response);
}
Also used : Cookie(javax.servlet.http.Cookie) MockHttpServletRequest(net.sourceforge.stripes.mock.MockHttpServletRequest) Properties(java.util.Properties) MockHttpServletResponse(net.sourceforge.stripes.mock.MockHttpServletResponse) Test(org.junit.Test)

Example 7 with MockHttpServletRequest

use of net.sourceforge.stripes.mock.MockHttpServletRequest in project jspwiki by apache.

the class CookieAssertionLoginModuleTest method testLogout.

public final void testLogout() {
    MockHttpServletRequest request = m_engine.newHttpRequest();
    Cookie cookie = new Cookie(CookieAssertionLoginModule.PREFS_COOKIE_NAME, "Bullwinkle");
    request.setCookies(new Cookie[] { cookie });
    try {
        CallbackHandler handler = new WebContainerCallbackHandler(m_engine, request);
        LoginModule module = new CookieAssertionLoginModule();
        module.initialize(m_subject, handler, new HashMap<String, Object>(), new HashMap<String, Object>());
        module.login();
        module.commit();
        Set<Principal> principals = m_subject.getPrincipals();
        Assert.assertEquals(1, principals.size());
        Assert.assertTrue(principals.contains(new WikiPrincipal("Bullwinkle")));
        Assert.assertFalse(principals.contains(Role.ANONYMOUS));
        Assert.assertFalse(principals.contains(Role.ALL));
        module.logout();
        Assert.assertEquals(0, principals.size());
    } catch (LoginException e) {
        System.err.println(e.getMessage());
        Assert.assertTrue(false);
    }
}
Also used : Cookie(javax.servlet.http.Cookie) CallbackHandler(javax.security.auth.callback.CallbackHandler) MockHttpServletRequest(net.sourceforge.stripes.mock.MockHttpServletRequest) LoginModule(javax.security.auth.spi.LoginModule) WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) LoginException(javax.security.auth.login.LoginException) WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) Principal(java.security.Principal)

Example 8 with MockHttpServletRequest

use of net.sourceforge.stripes.mock.MockHttpServletRequest in project jspwiki by apache.

the class CommandResolverTest method testFindWikiActionWithParams.

@Test
public void testFindWikiActionWithParams() throws Exception {
    Command a;
    WikiPage page = m_engine.getPage("SinglePage");
    // Passing an EDIT request with page param yields a wrapped action
    MockHttpServletRequest request = m_engine.newHttpRequest("/Edit.jsp?page=SinglePage");
    request.getParameterMap().put("page", new String[] { "SinglePage" });
    a = resolver.findCommand(request, WikiContext.EDIT);
    Assert.assertNotSame(PageCommand.EDIT, a);
    Assert.assertEquals("EditContent.jsp", a.getContentTemplate());
    Assert.assertEquals("Edit.jsp", a.getJSP());
    Assert.assertEquals("%uEdit.jsp?page=%n", a.getURLPattern());
    Assert.assertEquals(page, a.getTarget());
    // Passing an EDIT request with page=Search yields FIND action, *not* edit
    request.setContextPath("/Edit.jsp?page=Search");
    request.getParameterMap().put("page", new String[] { "Search" });
    a = resolver.findCommand(request, WikiContext.EDIT);
    Assert.assertEquals(WikiCommand.FIND, a);
    Assert.assertEquals("FindContent.jsp", a.getContentTemplate());
    Assert.assertEquals("Search.jsp", a.getJSP());
    Assert.assertEquals("%uSearch.jsp", a.getURLPattern());
    Assert.assertNull(a.getTarget());
    // Passing an EDIT request with group="Foo" yields wrapped VIEW_GROUP
    request = m_engine.newHttpRequest("/Group.jsp?group=Foo");
    request.getParameterMap().put("group", new String[] { "Foo" });
    a = resolver.findCommand(request, WikiContext.EDIT);
    Assert.assertNotSame(GroupCommand.VIEW_GROUP, a);
    Assert.assertEquals("GroupContent.jsp", a.getContentTemplate());
    Assert.assertEquals("Group.jsp", a.getJSP());
    Assert.assertEquals("%uGroup.jsp?group=%n", a.getURLPattern());
    Assert.assertEquals(new GroupPrincipal("Foo"), a.getTarget());
}
Also used : GroupPrincipal(org.apache.wiki.auth.GroupPrincipal) MockHttpServletRequest(net.sourceforge.stripes.mock.MockHttpServletRequest) WikiPage(org.apache.wiki.WikiPage) Test(org.junit.Test)

Example 9 with MockHttpServletRequest

use of net.sourceforge.stripes.mock.MockHttpServletRequest in project jspwiki by apache.

the class SearchManagerTest method testSimpleSearch4.

@Test
public void testSimpleSearch4() throws Exception {
    String txt = "It was the dawn of the third age of mankind, ten years after the Earth-Minbari War.";
    MockHttpServletRequest request = m_engine.newHttpRequest();
    request.getParameterMap().put("page", new String[] { "TestPage" });
    WikiContext ctx = m_engine.createContext(request, WikiContext.EDIT);
    m_engine.saveText(ctx, txt);
    Thread.yield();
    Collection res = waitForIndex("mankind", "testSimpleSearch4");
    Assert.assertNotNull("found results", res);
    Assert.assertEquals("result not found", 1, res.size());
    m_engine.saveText(ctx, "[{ALLOW view Authenticated}] It was the dawn of the third age of mankind... page is blocked");
    res = m_mgr.findPages("mankind", ctx);
    Assert.assertNotNull("null result", res);
    Assert.assertEquals("result found, should be blocked", 0, res.size());
    m_engine.deleteTestPage("TestPage");
}
Also used : WikiContext(org.apache.wiki.WikiContext) MockHttpServletRequest(net.sourceforge.stripes.mock.MockHttpServletRequest) Collection(java.util.Collection) Test(org.junit.Test)

Example 10 with MockHttpServletRequest

use of net.sourceforge.stripes.mock.MockHttpServletRequest in project jspwiki by apache.

the class SearchManagerTest method waitForIndex.

/**
 * Should cover for both index and initial delay
 */
Collection waitForIndex(String text, String testName) throws Exception {
    Collection res = null;
    for (long l = 0; l < SLEEP_COUNT; l++) {
        if (res == null || res.isEmpty()) {
            Thread.sleep(SLEEP_TIME);
        } else {
            break;
        }
        MockHttpServletRequest request = m_engine.newHttpRequest();
        WikiContext ctx = m_engine.createContext(request, WikiContext.EDIT);
        res = m_mgr.findPages(text, ctx);
    // debugSearchResults( res );
    }
    return res;
}
Also used : WikiContext(org.apache.wiki.WikiContext) MockHttpServletRequest(net.sourceforge.stripes.mock.MockHttpServletRequest) Collection(java.util.Collection)

Aggregations

MockHttpServletRequest (net.sourceforge.stripes.mock.MockHttpServletRequest)23 Test (org.junit.Test)10 Cookie (javax.servlet.http.Cookie)6 WikiPrincipal (org.apache.wiki.auth.WikiPrincipal)6 Principal (java.security.Principal)4 CallbackHandler (javax.security.auth.callback.CallbackHandler)4 LoginException (javax.security.auth.login.LoginException)4 LoginModule (javax.security.auth.spi.LoginModule)4 WikiContext (org.apache.wiki.WikiContext)4 Collection (java.util.Collection)3 HashSet (java.util.HashSet)2 MockHttpServletResponse (net.sourceforge.stripes.mock.MockHttpServletResponse)2 Locale (java.util.Locale)1 Properties (java.util.Properties)1 Subject (javax.security.auth.Subject)1 MockHttpSession (net.sourceforge.stripes.mock.MockHttpSession)1 WikiPage (org.apache.wiki.WikiPage)1 WikiSession (org.apache.wiki.WikiSession)1 GroupPrincipal (org.apache.wiki.auth.GroupPrincipal)1