Search in sources :

Example 16 with WikiSession

use of org.apache.wiki.WikiSession in project jspwiki by apache.

the class UserManagerTest method testSetUserProfile.

@Test
public void testSetUserProfile() throws Exception {
    // First, count the number of users in the db now.
    int oldUserCount = m_db.getWikiNames().length;
    // Create a new user with random name
    WikiSession session = m_engine.guestSession();
    String loginName = "TestUser" + String.valueOf(System.currentTimeMillis());
    UserProfile profile = m_db.newProfile();
    profile.setEmail("jspwiki.tests@mailinator.com");
    profile.setLoginName(loginName);
    profile.setFullname("FullName" + loginName);
    profile.setPassword("password");
    m_mgr.setUserProfile(session, profile);
    // Make sure the profile saved successfully
    profile = m_mgr.getUserProfile(session);
    Assert.assertEquals(loginName, profile.getLoginName());
    Assert.assertEquals(oldUserCount + 1, m_db.getWikiNames().length);
    // Now delete the profile; should be back to old count
    m_db.deleteByLoginName(loginName);
    Assert.assertEquals(oldUserCount, m_db.getWikiNames().length);
}
Also used : WikiSession(org.apache.wiki.WikiSession) UserProfile(org.apache.wiki.auth.user.UserProfile) WikiSessionTest(org.apache.wiki.WikiSessionTest) Test(org.junit.Test)

Example 17 with WikiSession

use of org.apache.wiki.WikiSession in project jspwiki by apache.

the class UserManagerTest method testSetUserProfileWithDenial.

@Test
public void testSetUserProfileWithDenial() throws Exception {
    setUpWithWorkflow();
    // First, count the number of users in the db now.
    int oldUserCount = m_db.getWikiNames().length;
    // Create a new user with random name
    WikiSession session = m_engine.guestSession();
    String loginName = "TestUser" + String.valueOf(System.currentTimeMillis());
    UserProfile profile = m_db.newProfile();
    profile.setEmail("jspwiki.tests@mailinator.com");
    profile.setLoginName(loginName);
    profile.setFullname("FullName" + loginName);
    profile.setPassword("password");
    // Because user profile saves require approvals, we will catch a Redirect
    try {
        m_mgr.setUserProfile(session, profile);
        Assert.fail("We should have caught a DecisionRequiredException caused by approval!");
    } catch (DecisionRequiredException e) {
    }
    // The user should NOT be saved yet
    Assert.assertEquals(oldUserCount, m_db.getWikiNames().length);
    // Now, look in Admin's queue, and verify there's a pending Decision there
    DecisionQueue dq = m_engine.getWorkflowManager().getDecisionQueue();
    Collection decisions = dq.getActorDecisions(m_engine.adminSession());
    Assert.assertEquals(1, decisions.size());
    // Verify that the Decision has all the facts and attributes we need
    Decision d = (Decision) decisions.iterator().next();
    List facts = d.getFacts();
    Assert.assertEquals(new Fact(UserManager.PREFS_FULL_NAME, profile.getFullname()), facts.get(0));
    Assert.assertEquals(new Fact(UserManager.PREFS_LOGIN_NAME, profile.getLoginName()), facts.get(1));
    Assert.assertEquals(new Fact(UserManager.FACT_SUBMITTER, session.getUserPrincipal().getName()), facts.get(2));
    Assert.assertEquals(new Fact(UserManager.PREFS_EMAIL, profile.getEmail()), facts.get(3));
    Assert.assertEquals(profile, d.getWorkflow().getAttribute(UserManager.SAVED_PROFILE));
    // Approve the profile
    d.decide(Outcome.DECISION_DENY);
    // Make sure the profile did NOT save
    Assert.assertEquals(oldUserCount, m_db.getWikiNames().length);
}
Also used : WikiSession(org.apache.wiki.WikiSession) UserProfile(org.apache.wiki.auth.user.UserProfile) DecisionRequiredException(org.apache.wiki.workflow.DecisionRequiredException) DecisionQueue(org.apache.wiki.workflow.DecisionQueue) Collection(java.util.Collection) List(java.util.List) Fact(org.apache.wiki.workflow.Fact) Decision(org.apache.wiki.workflow.Decision) WikiSessionTest(org.apache.wiki.WikiSessionTest) Test(org.junit.Test)

Example 18 with WikiSession

use of org.apache.wiki.WikiSession in project jspwiki by apache.

the class UserManagerTest method testSetUserProfileWithApproval.

@Test
public void testSetUserProfileWithApproval() throws Exception {
    setUpWithWorkflow();
    // First, count the number of users in the db now.
    int oldUserCount = m_db.getWikiNames().length;
    // Create a new user with random name
    WikiSession session = m_engine.guestSession();
    String loginName = "TestUser" + String.valueOf(System.currentTimeMillis());
    UserProfile profile = m_db.newProfile();
    profile.setEmail("jspwiki.tests@mailinator.com");
    profile.setLoginName(loginName);
    profile.setFullname("FullName" + loginName);
    profile.setPassword("password");
    // Because user profile saves require approvals, we will catch a Redirect
    try {
        m_mgr.setUserProfile(session, profile);
        Assert.fail("We should have caught a DecisionRequiredException caused by approval!");
    } catch (DecisionRequiredException e) {
    }
    // The user should NOT be saved yet
    Assert.assertEquals(oldUserCount, m_db.getWikiNames().length);
    // Now, look in Admin's queue, and verify there's a pending Decision there
    DecisionQueue dq = m_engine.getWorkflowManager().getDecisionQueue();
    Collection decisions = dq.getActorDecisions(m_engine.adminSession());
    Assert.assertEquals(1, decisions.size());
    // Verify that the Decision has all the facts and attributes we need
    Decision d = (Decision) decisions.iterator().next();
    List facts = d.getFacts();
    Assert.assertEquals(new Fact(UserManager.PREFS_FULL_NAME, profile.getFullname()), facts.get(0));
    Assert.assertEquals(new Fact(UserManager.PREFS_LOGIN_NAME, profile.getLoginName()), facts.get(1));
    Assert.assertEquals(new Fact(UserManager.FACT_SUBMITTER, session.getUserPrincipal().getName()), facts.get(2));
    Assert.assertEquals(new Fact(UserManager.PREFS_EMAIL, profile.getEmail()), facts.get(3));
    Assert.assertEquals(profile, d.getWorkflow().getAttribute(UserManager.SAVED_PROFILE));
    // Approve the profile
    d.decide(Outcome.DECISION_APPROVE);
    // Make sure the profile saved successfully
    Assert.assertEquals(oldUserCount + 1, m_db.getWikiNames().length);
    // Now delete the profile; should be back to old count
    m_db.deleteByLoginName(loginName);
    Assert.assertEquals(oldUserCount, m_db.getWikiNames().length);
}
Also used : WikiSession(org.apache.wiki.WikiSession) UserProfile(org.apache.wiki.auth.user.UserProfile) DecisionRequiredException(org.apache.wiki.workflow.DecisionRequiredException) DecisionQueue(org.apache.wiki.workflow.DecisionQueue) Collection(java.util.Collection) List(java.util.List) Fact(org.apache.wiki.workflow.Fact) Decision(org.apache.wiki.workflow.Decision) WikiSessionTest(org.apache.wiki.WikiSessionTest) Test(org.junit.Test)

Example 19 with WikiSession

use of org.apache.wiki.WikiSession in project jspwiki by apache.

the class IfPluginTest method getJanneBasedWikiContextFor.

/**
 * Returns a {@link WikiContext} for the given page, with user {@link Users#JANNE} logged in.
 *
 * @param page given {@link WikiPage}.
 * @return {@link WikiContext} associated to given {@link WikiPage}.
 * @throws WikiException problems while logging in.
 */
WikiContext getJanneBasedWikiContextFor(WikiPage page) throws WikiException {
    MockHttpServletRequest request = testEngine.newHttpRequest();
    WikiSession session = WikiSession.getWikiSession(testEngine, request);
    testEngine.getAuthenticationManager().login(session, request, Users.JANNE, Users.JANNE_PASS);
    return new WikiContext(testEngine, request, page);
}
Also used : WikiSession(org.apache.wiki.WikiSession) WikiContext(org.apache.wiki.WikiContext) MockHttpServletRequest(net.sourceforge.stripes.mock.MockHttpServletRequest)

Example 20 with WikiSession

use of org.apache.wiki.WikiSession in project jspwiki by apache.

the class AuthenticationManagerTest method testCustomAuthorizer.

/**
 * Tests a dummy WebAuthorizer that is guaranteed to return true for one
 * role for each of the two <code>isInRole</code> methods.
 *
 * @throws Exception
 */
@Test
public void testCustomAuthorizer() throws Exception {
    Properties props = TestEngine.getTestProperties();
    props.put(AuthorizationManager.PROP_AUTHORIZER, "org.apache.wiki.auth.AuthenticationManagerTest$DummyAuthorizer");
    m_engine = new TestEngine(props);
    // Start a session without any container roles: DummyAuthorizer should ALWAYS allow AuthorizerRole
    WikiSession session = WikiSessionTest.authenticatedSession(m_engine, Users.JANNE, Users.JANNE_PASS);
    Assert.assertTrue(session.hasPrincipal(Role.ALL));
    Assert.assertTrue(session.hasPrincipal(Role.AUTHENTICATED));
    Assert.assertTrue(session.hasPrincipal(new WikiPrincipal(Users.JANNE, WikiPrincipal.LOGIN_NAME)));
    Assert.assertTrue(session.hasPrincipal(new WikiPrincipal("JanneJalkanen", WikiPrincipal.WIKI_NAME)));
    Assert.assertTrue(session.hasPrincipal(new WikiPrincipal("Janne Jalkanen", WikiPrincipal.FULL_NAME)));
    Assert.assertTrue(session.hasPrincipal(new Role("AuthorizerRole")));
    Assert.assertFalse(session.hasPrincipal(new Role("ContainerRole")));
    Assert.assertFalse(session.hasPrincipal(new Role("DummyRole")));
    // Try again with a container-authenticated session: DummyAuthorizer should ALSO allow ContainerRole
    session = WikiSessionTest.containerAuthenticatedSession(m_engine, Users.JANNE, new Principal[0]);
    Assert.assertTrue(session.hasPrincipal(Role.ALL));
    Assert.assertTrue(session.hasPrincipal(Role.AUTHENTICATED));
    Assert.assertTrue(session.hasPrincipal(new WikiPrincipal(Users.JANNE, WikiPrincipal.LOGIN_NAME)));
    Assert.assertTrue(session.hasPrincipal(new WikiPrincipal("JanneJalkanen", WikiPrincipal.WIKI_NAME)));
    Assert.assertTrue(session.hasPrincipal(new WikiPrincipal("Janne Jalkanen", WikiPrincipal.FULL_NAME)));
    Assert.assertTrue(session.hasPrincipal(new Role("AuthorizerRole")));
    Assert.assertTrue(session.hasPrincipal(new Role("ContainerRole")));
    Assert.assertFalse(session.hasPrincipal(new Role("DummyRole")));
}
Also used : Role(org.apache.wiki.auth.authorize.Role) WikiSession(org.apache.wiki.WikiSession) TestEngine(org.apache.wiki.TestEngine) Properties(java.util.Properties) Principal(java.security.Principal) WikiSessionTest(org.apache.wiki.WikiSessionTest) Test(org.junit.Test)

Aggregations

WikiSession (org.apache.wiki.WikiSession)40 WikiSessionTest (org.apache.wiki.WikiSessionTest)23 Test (org.junit.Test)23 Principal (java.security.Principal)15 UserProfile (org.apache.wiki.auth.user.UserProfile)9 AllPermission (org.apache.wiki.auth.permissions.AllPermission)8 PagePermission (org.apache.wiki.auth.permissions.PagePermission)8 Group (org.apache.wiki.auth.authorize.Group)7 Permission (java.security.Permission)6 UnresolvedPrincipal (org.apache.wiki.auth.acl.UnresolvedPrincipal)6 WikiPermission (org.apache.wiki.auth.permissions.WikiPermission)6 WikiPage (org.apache.wiki.WikiPage)5 Role (org.apache.wiki.auth.authorize.Role)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 Collection (java.util.Collection)3 HttpSession (javax.servlet.http.HttpSession)3 ProviderException (org.apache.wiki.api.exceptions.ProviderException)3 Attachment (org.apache.wiki.attachment.Attachment)3 GroupPrincipal (org.apache.wiki.auth.GroupPrincipal)3 File (java.io.File)2