Search in sources :

Example 16 with CustomStub

use of org.jmock.core.stub.CustomStub in project xwiki-platform by xwiki.

the class XWikiRightServiceImplTest method testHasAccessLevelWhithUserFromAnotherWiki.

public void testHasAccessLevelWhithUserFromAnotherWiki() throws XWikiException {
    final XWikiDocument doc = new XWikiDocument(new DocumentReference(this.group2.getWikiName(), "Space", "Page"));
    final XWikiDocument preferences = new XWikiDocument(new DocumentReference("wiki2", "XWiki", "XWikiPreference"));
    BaseObject preferencesObject = new BaseObject();
    preferencesObject.setClassName("XWiki.XWikiGlobalRights");
    preferencesObject.setStringValue("levels", "view");
    preferencesObject.setIntValue("allow", 1);
    preferences.addXObject(preferencesObject);
    preferences.setNew(false);
    this.mockXWiki.stubs().method("getDocument").with(eq(XWIKIPREFERENCES_REFERENCE), ANYTHING).will(new CustomStub("Implements XWiki.getDocument") {

        @Override
        public Object invoke(Invocation invocation) throws Throwable {
            if (!getContext().getWikiId().equals("wiki2")) {
                new XWikiDocument(new DocumentReference(getContext().getWikiId(), "XWiki", "XWikiPreference"));
            }
            return preferences;
        }
    });
    this.mockXWiki.stubs().method("getDocument").with(eq(doc.getPrefixedFullName()), ANYTHING).will(returnValue(doc));
    getContext().setWikiId("wiki");
    assertFalse("User from another wiki has right on a local wiki", this.rightService.hasAccessLevel("view", this.user.getPrefixedFullName(), doc.getPrefixedFullName(), true, getContext()));
    // direct user rights
    preferencesObject.setStringValue("users", this.user.getPrefixedFullName());
    getContext().setWikiId(this.user.getWikiName());
    assertTrue("User from another wiki does not have right on a local wiki when tested from user wiki", this.rightService.hasAccessLevel("view", this.user.getPrefixedFullName(), doc.getPrefixedFullName(), true, getContext()));
    assertTrue("User from another wiki does not have right on a local wiki when tested from user wiki", this.rightService.hasAccessLevel("view", this.user.getFullName(), doc.getPrefixedFullName(), true, getContext()));
    getContext().setWikiId(doc.getWikiName());
    assertTrue("User from another wiki does not have right on a local wiki when tested from local wiki", this.rightService.hasAccessLevel("view", this.user.getPrefixedFullName(), doc.getPrefixedFullName(), true, getContext()));
    assertTrue("User from another wiki does not have right on a local wiki when tested from local wiki", this.rightService.hasAccessLevel("view", this.user.getPrefixedFullName(), doc.getFullName(), true, getContext()));
    // user group rights
    preferencesObject.removeField("users");
    // group from user's wiki
    preferencesObject.setStringValue("groups", this.group.getPrefixedFullName());
    getContext().setWikiId(this.user.getWikiName());
    assertTrue("User group from another wiki does not have right on a local wiki when tested from user wiki", this.rightService.hasAccessLevel("view", this.user.getPrefixedFullName(), doc.getPrefixedFullName(), true, getContext()));
    assertTrue("User group from another wiki does not have right on a local wiki when tested from user wiki", this.rightService.hasAccessLevel("view", this.user.getFullName(), doc.getPrefixedFullName(), true, getContext()));
    getContext().setWikiId(doc.getWikiName());
    assertTrue("User group from another wiki does not have right on a local wiki when tested from local wiki", this.rightService.hasAccessLevel("view", this.user.getPrefixedFullName(), doc.getPrefixedFullName(), true, getContext()));
    assertTrue("User group from another wiki does not have right on a local wiki when tested from local wiki", this.rightService.hasAccessLevel("view", this.user.getPrefixedFullName(), doc.getFullName(), true, getContext()));
    // group from document's wiki
    preferencesObject.setStringValue("groups", this.group2.getFullName());
    getContext().setWikiId(this.user.getWikiName());
    assertTrue("User group from another wiki does not have right on a local wiki when tested from user wiki", this.rightService.hasAccessLevel("view", this.user.getPrefixedFullName(), doc.getPrefixedFullName(), true, getContext()));
    assertTrue("User group from another wiki does not have right on a local wiki when tested from user wiki", this.rightService.hasAccessLevel("view", this.user.getFullName(), doc.getPrefixedFullName(), true, getContext()));
    getContext().setWikiId(doc.getWikiName());
    assertTrue("User group from another wiki does not have right on a local wiki when tested from local wiki", this.rightService.hasAccessLevel("view", this.user.getPrefixedFullName(), doc.getPrefixedFullName(), true, getContext()));
    assertTrue("User group from another wiki does not have right on a local wiki when tested from local wiki", this.rightService.hasAccessLevel("view", this.user.getPrefixedFullName(), doc.getFullName(), true, getContext()));
    // user is wiki owner
    preferencesObject.removeField("groups");
    this.mockXWiki.stubs().method("getWikiOwner").with(eq(doc.getWikiName()), ANYTHING).will(returnValue(this.user.getPrefixedFullName()));
    getContext().setWikiId(this.user.getWikiName());
    assertTrue("Wiki owner from another wiki does not have right on a local wiki when tested from user wiki", this.rightService.hasAccessLevel("view", this.user.getPrefixedFullName(), doc.getPrefixedFullName(), true, getContext()));
    assertTrue("Wiki owner group from another wiki does not have right on a local wiki when tested from user wiki", this.rightService.hasAccessLevel("view", this.user.getFullName(), doc.getPrefixedFullName(), true, getContext()));
    getContext().setWikiId(doc.getWikiName());
    assertTrue("Wiki owner group from another wiki does not have right on a local wiki when tested from local wiki", this.rightService.hasAccessLevel("view", this.user.getPrefixedFullName(), doc.getPrefixedFullName(), true, getContext()));
    assertTrue("Wiki owner group from another wiki does not have right on a local wiki when tested from local wiki", this.rightService.hasAccessLevel("view", this.user.getPrefixedFullName(), doc.getFullName(), true, getContext()));
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Invocation(org.jmock.core.Invocation) CustomStub(org.jmock.core.stub.CustomStub) BaseObject(com.xpn.xwiki.objects.BaseObject) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 17 with CustomStub

use of org.jmock.core.stub.CustomStub in project xwiki-platform by xwiki.

the class XWikiRightServiceImplTest method testHasAccessLevelAdminOnDocument.

public void testHasAccessLevelAdminOnDocument() throws Exception {
    final String wikiName = this.user.getWikiName();
    final XWikiDocument doc = new XWikiDocument(new DocumentReference(wikiName, "Space", "Page"));
    final XWikiDocument preferences = new XWikiDocument(new DocumentReference(wikiName, "XWiki", "XWikiPreference"));
    BaseObject rightsObject = new BaseObject();
    rightsObject.setClassName("XWiki.XWikiRights");
    rightsObject.setStringValue("levels", "admin");
    rightsObject.setStringValue("users", this.user.getPrefixedFullName());
    rightsObject.setIntValue("allow", 1);
    doc.addXObject(rightsObject);
    BaseObject preferencesObject = new BaseObject();
    preferencesObject.setClassName("XWiki.XWikiGlobalRights");
    preferencesObject.setStringValue("levels", "admin");
    preferencesObject.setIntValue("allow", 0);
    preferencesObject.setStringValue("users", this.user.getPrefixedFullName());
    preferences.addXObject(preferencesObject);
    preferences.setNew(false);
    this.mockXWiki.stubs().method("getDocument").with(eq(XWIKIPREFERENCES_REFERENCE), ANYTHING).will(new CustomStub("Implements XWiki.getDocument") {

        @Override
        public Object invoke(Invocation invocation) throws Throwable {
            if (!getContext().getWikiId().equals(wikiName)) {
                new XWikiDocument(new DocumentReference(getContext().getWikiId(), "XWiki", "XWikiPreference"));
            }
            return preferences;
        }
    });
    this.mockXWiki.stubs().method("getDocument").with(eq(doc.getPrefixedFullName()), ANYTHING).will(returnValue(doc));
    getContext().setWikiId(wikiName);
    assertFalse("Admin rights must not be considered when set on document level.", this.rightService.hasAccessLevel("admin", this.user.getPrefixedFullName(), doc.getPrefixedFullName(), true, getContext()));
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Invocation(org.jmock.core.Invocation) CustomStub(org.jmock.core.stub.CustomStub) BaseObject(com.xpn.xwiki.objects.BaseObject) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 18 with CustomStub

use of org.jmock.core.stub.CustomStub in project xwiki-platform by xwiki.

the class XWikiRightServiceImplTest method testEditRightsOnWebPreferencesDocuments.

/**
 * Verify that edit rights is not sufficient for editing
 * *.WebPreferences and XWiki.XWikiPreferences, since that can be
 * used to elevate the privileges to admin.
 */
public void testEditRightsOnWebPreferencesDocuments() throws Exception {
    this.mockGroupService.stubs().method("getAllGroupsReferencesForMember").with(ANYTHING, ANYTHING, ANYTHING, ANYTHING).will(returnValue(Collections.emptyList()));
    this.user = new XWikiDocument(new DocumentReference("wiki", "XWiki", "user"));
    this.user.setNew(false);
    getContext().setWikiId(this.user.getWikiName());
    BaseObject userObject = new BaseObject();
    userObject.setClassName("XWiki.XWikiUser");
    this.user.addXObject(userObject);
    this.mockXWiki.stubs().method("getDocument").with(eq(this.user.getPrefixedFullName()), ANYTHING).will(returnValue(this.user));
    getContext().setWikiId(this.user.getWikiName());
    final XWikiDocument doc = new XWikiDocument(new DocumentReference("wiki", "Space", "Document"));
    this.mockXWiki.stubs().method("getDocument").with(eq(doc.getPrefixedFullName()), ANYTHING).will(returnValue(doc));
    final XWikiDocument preferences = new XWikiDocument(new DocumentReference("wiki", "XWiki", "XWikiPreference"));
    BaseObject preferencesObject = new BaseObject();
    preferencesObject.setClassName("XWiki.XWikiGlobalRights");
    preferencesObject.setStringValue("levels", "admin");
    preferencesObject.setIntValue("allow", 0);
    preferencesObject.setStringValue("users", "xwiki:XWiki.UserA");
    preferences.addXObject(preferencesObject);
    this.mockXWiki.stubs().method("getDocument").with(eq("wiki:Space.WebPreferences"), ANYTHING).will(returnValue(new XWikiDocument(new DocumentReference("wiki", "Space", "WebPreferences"))));
    this.mockXWiki.stubs().method("getDocument").with(eq("wiki:XWiki.XWikiPreferences"), ANYTHING).will(returnValue(new XWikiDocument(new DocumentReference("wiki", "XWiki", "XWikiPreferences"))));
    this.mockXWiki.stubs().method("getDocument").with(eq("wiki:Space.XWikiPreferences"), ANYTHING).will(returnValue(new XWikiDocument(new DocumentReference("wiki", "Space", "XWikiPreferences"))));
    this.mockXWiki.stubs().method("getDocument").with(eq(XWIKIPREFERENCES_REFERENCE), ANYTHING).will(new CustomStub("Implements XWiki.getDocument") {

        @Override
        public Object invoke(Invocation invocation) throws Throwable {
            if (!getContext().getWikiId().equals("wiki")) {
                new XWikiDocument(new DocumentReference(getContext().getWikiId(), "XWiki", "XWikiPreference"));
            }
            return preferences;
        }
    });
    assertFalse("Programming rights have not been configured.", rightService.hasAccessLevel("programming", "xwiki:XWiki.UserA", "wiki:Space.WebPreferences", getContext()));
    assertFalse("Admin rights have not been configured.", rightService.hasAccessLevel("admin", "xwiki:XWiki.UserA", "wiki:Space.WebPreferences", getContext()));
    assertFalse("Shouldn't allow edit rights by default on WebPreferences documents.", rightService.hasAccessLevel("edit", "xwiki:XWiki.UserA", "wiki:Space.WebPreferences", getContext()));
    assertFalse("Edit rights should be denied by default on XWiki.XWikiPreferences", rightService.hasAccessLevel("edit", "xwiki:XWiki.UserA", "wiki:XWiki.XWikiPreferences", getContext()));
    assertTrue("Other documents named XWikiPreferences should be unaffected.", rightService.hasAccessLevel("edit", "xwiki:XWiki.UserA", "wiki:Space.XWikiPreferences", getContext()));
    preferencesObject = new BaseObject();
    preferencesObject.setClassName("XWiki.XWikiGlobalRights");
    preferencesObject.setStringValue("levels", "edit");
    preferencesObject.setIntValue("allow", 1);
    preferencesObject.setStringValue("users", "xwiki:XWiki.UserA");
    preferences.addXObject(preferencesObject);
    assertTrue("Edit rights have been configured.", rightService.hasAccessLevel("edit", "xwiki:XWiki.UserA", "wiki:Space.Document", getContext()));
    assertFalse("No admin rights have been configured.", rightService.hasAccessLevel("admin", "xwiki:XWiki.UserA", "wiki:Space.Document", getContext()));
    assertFalse("Edit rights should be denied WebPreferences document for non-admin users.", rightService.hasAccessLevel("edit", "xwiki:XWiki.UserA", "wiki:Space.WebPreferences", getContext()));
    assertFalse("Edit rights should be denied XWiki.XWikiPreferences document for non-admin users.", rightService.hasAccessLevel("edit", "xwiki:XWiki.UserA", "wiki:XWiki.XWikiPreferences", getContext()));
    preferencesObject = new BaseObject();
    preferencesObject.setClassName("XWiki.XWikiGlobalRights");
    preferencesObject.setStringValue("levels", "admin");
    preferencesObject.setIntValue("allow", 1);
    preferencesObject.setStringValue("users", "xwiki:XWiki.UserA");
    preferences.addXObject(preferencesObject);
    assertTrue("Admin rights have been configured.", rightService.hasAccessLevel("admin", "xwiki:XWiki.UserA", "wiki:Space.Document", getContext()));
    assertTrue("Edit rights should be granted on WebPreferences document for admin users.", rightService.hasAccessLevel("edit", "xwiki:XWiki.UserA", "wiki:Space.WebPreferences", getContext()));
    assertTrue("Edit rights should be granted on XWiki.XWikiPreferences document for non-admin users.", rightService.hasAccessLevel("edit", "xwiki:XWiki.UserA", "wiki:XWiki.XWikiPreferences", getContext()));
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Invocation(org.jmock.core.Invocation) CustomStub(org.jmock.core.stub.CustomStub) BaseObject(com.xpn.xwiki.objects.BaseObject) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject)

Aggregations

Invocation (org.jmock.core.Invocation)18 CustomStub (org.jmock.core.stub.CustomStub)18 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)11 BaseObject (com.xpn.xwiki.objects.BaseObject)11 Mock (org.jmock.Mock)9 DocumentReference (org.xwiki.model.reference.DocumentReference)9 XWikiContext (com.xpn.xwiki.XWikiContext)6 XWikiStoreInterface (com.xpn.xwiki.store.XWikiStoreInterface)4 HashMap (java.util.HashMap)4 XWiki (com.xpn.xwiki.XWiki)3 XWikiConfig (com.xpn.xwiki.XWikiConfig)3 XWikiHibernateStore (com.xpn.xwiki.store.XWikiHibernateStore)3 XWikiHibernateVersioningStore (com.xpn.xwiki.store.XWikiHibernateVersioningStore)3 XWikiServletRequestStub (com.xpn.xwiki.web.XWikiServletRequestStub)2 URL (java.net.URL)2 Date (java.util.Date)2 VelocityContext (org.apache.velocity.VelocityContext)2 Execution (org.xwiki.context.Execution)2 VelocityEngine (org.xwiki.velocity.VelocityEngine)2 VelocityManager (org.xwiki.velocity.VelocityManager)2