Search in sources :

Example 1 with TestDocument

use of org.xwiki.security.authorization.testwikis.TestDocument in project xwiki-platform by xwiki.

the class TestDefinitionParserTest method testDefinitionTestParser.

@Test
public void testDefinitionTestParser() throws Exception {
    TestDefinitionParser parser = new DefaultTestDefinitionParser();
    EntityReferenceResolver<String> resolver = componentManager.getInstance(EntityReferenceResolver.TYPE_STRING);
    EntityReferenceSerializer<String> serializer = componentManager.getInstance(EntityReferenceSerializer.TYPE_STRING);
    TestDefinition testDefinition = parser.parse("testwikis" + File.separatorChar + "parserTester.xml", resolver, serializer);
    Collection<TestWiki> testWikis = testDefinition.getWikis();
    assertThat("There should be some wikis", testWikis, notNullValue());
    assertThat("The wikis count should be 3", testWikis.size(), equalTo(3));
    TestWiki mainwiki = testDefinition.getMainWiki();
    assertThat("Main wiki should be defined", mainwiki, notNullValue());
    assertThat("Main wiki should be named 'wiki'", mainwiki.getWikiReference(), equalTo(new WikiReference("wiki")));
    assertThat("Main wiki should be main wiki", mainwiki.isMainWiki(), is(true));
    assertThat("Main wiki owner should be XWiki.Admin", mainwiki.getOwner(), equalTo(new DocumentReference("wiki", "XWiki", "Admin")));
    assertThat("Main wiki should have 4 users (2 groups, and 2 users)", mainwiki.getUsers().size(), equalTo(4));
    assertThat("Main wiki should have 2 groups", mainwiki.getGroups().size(), equalTo(2));
    assertThat("Main wiki should have a groupA", mainwiki.getGroup("groupA"), notNullValue());
    assertThat("Main wiki should have a userA", mainwiki.getUser("userA"), notNullValue());
    Collection<TestGroup> groups = mainwiki.getUser("userA").getGroups();
    assertThat("UserA of Main wiki should be in 2 groups", groups.size(), equalTo(2));
    List<DocumentReference> groupRefs = new ArrayList<DocumentReference>();
    for (TestGroup group : groups) {
        groupRefs.add(group.getGroupReference());
    }
    assertThat("User A is in GroupA of the main wiki and the subwiki", groupRefs, hasItems(new DocumentReference("wiki", "XWiki", "groupA"), new DocumentReference("wiki1", "XWiki", "groupA")));
    Collection<TestAccessRule> rules = mainwiki.getAccessRules();
    assertThat("There must be 26 access rules on main wiki", rules.size(), equalTo(26));
    List<DocumentReference> userRefs = new ArrayList<DocumentReference>();
    List<Right> rights = new ArrayList<Right>();
    List<RuleState> states = new ArrayList<RuleState>();
    for (TestAccessRule rule : rules) {
        userRefs.add(rule.getUser());
        rights.add(rule.getRight());
        states.add(rule.getState());
    }
    assertThat("Users in access rules of main wiki mismatch", userRefs, hasItems(new DocumentReference("wiki", "XWiki", "userA"), new DocumentReference("wiki", "XWiki", "userB"), new DocumentReference("wiki", "XWiki", "groupA"), new DocumentReference("wiki", "XWiki", "groupB")));
    assertThat("Rights in access rules of main wiki mismatch", rights, hasItems(Right.VIEW, Right.LOGIN, Right.EDIT, Right.COMMENT, Right.DELETE, Right.REGISTER, Right.ADMIN, Right.PROGRAM));
    assertThat("State in access rules of main wiki mismatch", states, hasItems(RuleState.ALLOW, RuleState.DENY));
    assertThat("Main wiki should have 3 spaces (2 plus XWiki)", mainwiki.getSpaces().size(), equalTo(3));
    TestSpace space = mainwiki.getSpace("space1");
    assertThat("Main wiki should have a space named 'space1'", space, notNullValue());
    assertThat("'space1' of main wiki should have description 'space 1'", space.getDescription(), equalTo("space 1"));
    rules = space.getAccessRules();
    assertThat("There must be 8 access rules on space 1", rules.size(), equalTo(8));
    userRefs = new ArrayList<DocumentReference>();
    rights = new ArrayList<Right>();
    states = new ArrayList<RuleState>();
    for (TestAccessRule rule : rules) {
        userRefs.add(rule.getUser());
        rights.add(rule.getRight());
        states.add(rule.getState());
    }
    assertThat("Users in access rules of space 1 of main wiki mismatch", userRefs, hasItems(new DocumentReference("wiki", "XWiki", "userA"), new DocumentReference("wiki", "XWiki", "userB"), new DocumentReference("wiki", "XWiki", "groupB")));
    assertThat("Rights in access rules of space 1 of main wiki mismatch", rights, hasItems(Right.VIEW, Right.EDIT, Right.COMMENT, Right.DELETE, Right.ADMIN));
    assertThat("State in access rules of space 1 of main wiki mismatch", states, hasItems(RuleState.DENY));
    assertThat("Space 1 of main wiki should have 2 documents", space.getDocuments().size(), equalTo(2));
    TestDocument document = space.getDocument("document1");
    assertThat("Space 1 of main wiki should have a document named 'document1'", document, notNullValue());
    assertThat("'document1' of 'space1' of main wiki should have description 'Document 1'", document.getDescription(), equalTo("Document 1"));
    rules = document.getAccessRules();
    assertThat("There must be 7 access rules on document 1", rules.size(), equalTo(7));
    userRefs = new ArrayList<DocumentReference>();
    rights = new ArrayList<Right>();
    states = new ArrayList<RuleState>();
    for (TestAccessRule rule : rules) {
        userRefs.add(rule.getUser());
        rights.add(rule.getRight());
        states.add(rule.getState());
    }
    assertThat("Users in access rules of document 1 of space 1 of main wiki mismatch", userRefs, hasItems(new DocumentReference("wiki", "XWiki", "userA"), new DocumentReference("wiki", "XWiki", "userB"), new DocumentReference("wiki", "XWiki", "groupA")));
    assertThat("Rights in access rules of document 1 of space 1 of main wiki mismatch", rights, hasItems(Right.VIEW, Right.EDIT, Right.COMMENT, Right.DELETE));
    assertThat("State in access rules of document 1 of space 1 of main wiki mismatch", states, hasItems(RuleState.ALLOW));
}
Also used : RuleState(org.xwiki.security.authorization.RuleState) TestWiki(org.xwiki.security.authorization.testwikis.TestWiki) ArrayList(java.util.ArrayList) Right(org.xwiki.security.authorization.Right) TestDocument(org.xwiki.security.authorization.testwikis.TestDocument) TestSpace(org.xwiki.security.authorization.testwikis.TestSpace) DocumentReference(org.xwiki.model.reference.DocumentReference) TestDefinition(org.xwiki.security.authorization.testwikis.TestDefinition) TestGroup(org.xwiki.security.authorization.testwikis.TestGroup) TestAccessRule(org.xwiki.security.authorization.testwikis.TestAccessRule) WikiReference(org.xwiki.model.reference.WikiReference) TestDefinitionParser(org.xwiki.security.authorization.testwikis.TestDefinitionParser) Test(org.junit.Test)

Example 2 with TestDocument

use of org.xwiki.security.authorization.testwikis.TestDocument in project xwiki-platform by xwiki.

the class AbstractAuthorizationTestCase method getUserReadableName.

/**
 * @param user user reference.
 * @return a pretty name for the user based on entities "alt" attributes or their names.
 */
protected String getUserReadableName(DocumentReference user) {
    if (user == null) {
        return "Public";
    }
    TestEntity userEntity = testDefinition.searchEntity(user);
    String result = (userEntity != null && userEntity instanceof TestDocument) ? ((TestDocument) userEntity).getDescription() : null;
    result = (result != null) ? result : user.getName();
    TestWiki wiki = testDefinition.getWiki(user.getWikiReference());
    String name = (wiki != null) ? wiki.getDescription() : null;
    name = (name != null) ? name : user.getWikiReference().getName();
    if (!name.startsWith("any")) {
        result += " from " + name;
    }
    return result;
}
Also used : TestEntity(org.xwiki.security.authorization.testwikis.TestEntity) TestDocument(org.xwiki.security.authorization.testwikis.TestDocument) TestWiki(org.xwiki.security.authorization.testwikis.TestWiki)

Example 3 with TestDocument

use of org.xwiki.security.authorization.testwikis.TestDocument in project xwiki-platform by xwiki.

the class AbstractAuthorizationTestCase method getEntityReadableName.

/**
 * @param entity user reference.
 * @return a pretty name for the user based on entities "alt" attributes or their names.
 */
protected String getEntityReadableName(EntityReference entity) {
    if (entity == null) {
        return "Main Wiki";
    }
    StringBuilder result = null;
    if (entity.getType() == EntityType.DOCUMENT) {
        TestEntity docEntity = testDefinition.searchEntity(entity);
        String name = (docEntity != null && docEntity instanceof TestDocument) ? ((TestDocument) docEntity).getDescription() : null;
        name = (name != null) ? name : entity.getName();
        result = new StringBuilder(name);
        entity = entity.getParent();
    }
    if (entity.getType() == EntityType.SPACE) {
        TestEntity spaceEntity = testDefinition.searchEntity(entity);
        String name = (spaceEntity != null && spaceEntity instanceof TestSpace) ? ((TestSpace) spaceEntity).getDescription() : null;
        name = (name != null) ? name : entity.getName();
        if (result == null || !name.startsWith("any")) {
            if (result != null) {
                result.append(" in ");
                result.append(name);
            } else {
                result = new StringBuilder(name);
            }
        }
        entity = entity.getParent();
    }
    if (entity.getType() == EntityType.WIKI) {
        TestEntity wikiEntity = testDefinition.getWiki(new WikiReference(entity));
        String name = (wikiEntity != null) ? ((TestWiki) wikiEntity).getDescription() : null;
        name = (name != null) ? name : entity.getName();
        if (result == null || !name.startsWith("any")) {
            if (result != null) {
                result.append(" from ");
                result.append(name);
            } else {
                result = new StringBuilder(name);
            }
        }
    }
    return result.toString();
}
Also used : TestEntity(org.xwiki.security.authorization.testwikis.TestEntity) TestDocument(org.xwiki.security.authorization.testwikis.TestDocument) TestSpace(org.xwiki.security.authorization.testwikis.TestSpace) WikiReference(org.xwiki.model.reference.WikiReference)

Example 4 with TestDocument

use of org.xwiki.security.authorization.testwikis.TestDocument in project xwiki-platform by xwiki.

the class DefaultAuthorizationManagerIntegrationTest method initialiseWikiMock.

@Override
public TestDefinition initialiseWikiMock(String filename) throws Exception {
    super.initialiseWikiMock(filename);
    when(xWikiBridge.getMainWikiReference()).thenReturn(testDefinition.getMainWiki().getWikiReference());
    when(xWikiBridge.isWikiReadOnly()).thenReturn(false);
    when(userBridge.getAllGroupsFor(any(UserSecurityReference.class), any(WikiReference.class))).thenAnswer(new Answer<Collection<GroupSecurityReference>>() {

        @Override
        public Collection<GroupSecurityReference> answer(InvocationOnMock invocationOnMock) throws Throwable {
            UserSecurityReference userReference = (UserSecurityReference) invocationOnMock.getArguments()[0];
            WikiReference wikiReference = (WikiReference) invocationOnMock.getArguments()[1];
            if (userReference.getOriginalReference() == null) {
                // Public users (not logged in) may not appears in any group
                return Collections.emptyList();
            }
            TestWiki wiki = testDefinition.getWiki(userReference.getOriginalReference().getWikiReference());
            if (wiki == null) {
                throw new AuthorizationException(String.format("Failed to get groups for user or group [%s] in wiki [%s]. Unknown wiki.", userReference, wikiReference), null);
            }
            TestUserDocument user = wiki.getUser(userReference.getName());
            if (user == null) {
                return Collections.emptyList();
            }
            Collection<GroupSecurityReference> groups = new ArrayList<GroupSecurityReference>();
            for (TestGroup group : user.getGroups()) {
                // Ensure we return only group of the requested wiki
                if (group.getGroupReference().getWikiReference().equals(wikiReference)) {
                    groups.add(securityReferenceFactory.newGroupReference(group.getGroupReference()));
                }
            }
            return groups;
        }
    });
    when(securityEntryReader.read(any(SecurityReference.class))).thenAnswer(new Answer<SecurityRuleEntry>() {

        @Override
        public SecurityRuleEntry answer(InvocationOnMock invocationOnMock) throws Throwable {
            final SecurityReference reference = (SecurityReference) invocationOnMock.getArguments()[0];
            TestEntity entity = testDefinition.searchEntity(reference);
            Collection<TestAccessRule> rules = (entity != null && entity instanceof SecureTestEntity) ? ((SecureTestEntity) entity).getAccessRules() : Collections.<TestAccessRule>emptyList();
            final Collection<SecurityRule> mockedRules = new ArrayList<SecurityRule>();
            for (final TestAccessRule rule : rules) {
                mockedRules.add(mockSecurityRule(reference, rule.getRight(), rule.getState(), rule.getUser(), rule.isUser()));
            }
            if (entity instanceof TestWiki) {
                TestWiki wiki = (TestWiki) entity;
                if (wiki.getOwner() != null) {
                    mockedRules.add(mockSecurityRule(reference, Right.ADMIN, RuleState.ALLOW, wiki.getOwner(), true));
                }
            }
            if (entity instanceof TestDocument) {
                TestDocument document = (TestDocument) entity;
                if (document.getCreator() != null) {
                    mockedRules.add(mockSecurityRule(reference, Right.CREATOR, RuleState.ALLOW, document.getCreator(), true));
                }
            }
            return new AbstractSecurityRuleEntry() {

                @Override
                public Collection<SecurityRule> getRules() {
                    return mockedRules;
                }

                @Override
                public SecurityReference getReference() {
                    return reference;
                }

                @Override
                public String toString() {
                    return String.format("Rule entry for %s containing %d rules", reference.toString(), mockedRules.size());
                }

                @Override
                public boolean equals(Object object) {
                    if (object == this) {
                        return true;
                    }
                    if (!(object instanceof SecurityRuleEntry)) {
                        return false;
                    }
                    SecurityRuleEntry other = (SecurityRuleEntry) object;
                    return compareReferenceNullSafe(other.getReference(), reference) && other.getRules().size() == mockedRules.size();
                }
            };
        }
    });
    return testDefinition;
}
Also used : TestWiki(org.xwiki.security.authorization.testwikis.TestWiki) TestUserDocument(org.xwiki.security.authorization.testwikis.TestUserDocument) AbstractSecurityRuleEntry(org.xwiki.security.authorization.internal.AbstractSecurityRuleEntry) GroupSecurityReference(org.xwiki.security.GroupSecurityReference) TestDocument(org.xwiki.security.authorization.testwikis.TestDocument) UserSecurityReference(org.xwiki.security.UserSecurityReference) SecureTestEntity(org.xwiki.security.authorization.testwikis.SecureTestEntity) TestEntity(org.xwiki.security.authorization.testwikis.TestEntity) SecureTestEntity(org.xwiki.security.authorization.testwikis.SecureTestEntity) AbstractSecurityRuleEntry(org.xwiki.security.authorization.internal.AbstractSecurityRuleEntry) TestGroup(org.xwiki.security.authorization.testwikis.TestGroup) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TestAccessRule(org.xwiki.security.authorization.testwikis.TestAccessRule) Collection(java.util.Collection) SecurityReference(org.xwiki.security.SecurityReference) GroupSecurityReference(org.xwiki.security.GroupSecurityReference) UserSecurityReference(org.xwiki.security.UserSecurityReference) WikiReference(org.xwiki.model.reference.WikiReference)

Example 5 with TestDocument

use of org.xwiki.security.authorization.testwikis.TestDocument in project xwiki-platform by xwiki.

the class DefaultTestWiki method getUser.

@Override
public TestUserDocument getUser(String name) {
    SpaceReference spaceRef = new SpaceReference(XWikiConstants.XWIKI_SPACE, getWikiReference());
    TestSpace space = getSpace(spaceRef);
    if (space == null) {
        return null;
    }
    TestDocument user = space.getDocument(new DocumentReference(name, spaceRef));
    return (user instanceof TestUserDocument) ? (TestUserDocument) user : null;
}
Also used : TestDocument(org.xwiki.security.authorization.testwikis.TestDocument) SpaceReference(org.xwiki.model.reference.SpaceReference) TestUserDocument(org.xwiki.security.authorization.testwikis.TestUserDocument) TestSpace(org.xwiki.security.authorization.testwikis.TestSpace) DocumentReference(org.xwiki.model.reference.DocumentReference)

Aggregations

TestDocument (org.xwiki.security.authorization.testwikis.TestDocument)5 WikiReference (org.xwiki.model.reference.WikiReference)3 TestEntity (org.xwiki.security.authorization.testwikis.TestEntity)3 TestSpace (org.xwiki.security.authorization.testwikis.TestSpace)3 TestWiki (org.xwiki.security.authorization.testwikis.TestWiki)3 DocumentReference (org.xwiki.model.reference.DocumentReference)2 TestAccessRule (org.xwiki.security.authorization.testwikis.TestAccessRule)2 TestGroup (org.xwiki.security.authorization.testwikis.TestGroup)2 TestUserDocument (org.xwiki.security.authorization.testwikis.TestUserDocument)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Test (org.junit.Test)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 SpaceReference (org.xwiki.model.reference.SpaceReference)1 GroupSecurityReference (org.xwiki.security.GroupSecurityReference)1 SecurityReference (org.xwiki.security.SecurityReference)1 UserSecurityReference (org.xwiki.security.UserSecurityReference)1 Right (org.xwiki.security.authorization.Right)1 RuleState (org.xwiki.security.authorization.RuleState)1 AbstractSecurityRuleEntry (org.xwiki.security.authorization.internal.AbstractSecurityRuleEntry)1