| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
package org.xwiki.security.authorization.testwikis.internal.parser; |
| 22 |
|
|
| 23 |
|
import java.io.File; |
| 24 |
|
import java.util.ArrayList; |
| 25 |
|
import java.util.Collection; |
| 26 |
|
import java.util.List; |
| 27 |
|
|
| 28 |
|
import org.junit.Rule; |
| 29 |
|
import org.junit.Test; |
| 30 |
|
import org.xwiki.model.internal.DefaultModelConfiguration; |
| 31 |
|
import org.xwiki.model.internal.reference.DefaultEntityReferenceProvider; |
| 32 |
|
import org.xwiki.model.internal.reference.DefaultStringEntityReferenceResolver; |
| 33 |
|
import org.xwiki.model.internal.reference.DefaultStringEntityReferenceSerializer; |
| 34 |
|
import org.xwiki.model.internal.reference.DefaultSymbolScheme; |
| 35 |
|
import org.xwiki.model.reference.DocumentReference; |
| 36 |
|
import org.xwiki.model.reference.EntityReferenceResolver; |
| 37 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
| 38 |
|
import org.xwiki.model.reference.WikiReference; |
| 39 |
|
import org.xwiki.security.authorization.Right; |
| 40 |
|
import org.xwiki.security.authorization.RuleState; |
| 41 |
|
import org.xwiki.security.authorization.testwikis.TestAccessRule; |
| 42 |
|
import org.xwiki.security.authorization.testwikis.TestDefinition; |
| 43 |
|
import org.xwiki.security.authorization.testwikis.TestDefinitionParser; |
| 44 |
|
import org.xwiki.security.authorization.testwikis.TestDocument; |
| 45 |
|
import org.xwiki.security.authorization.testwikis.TestGroup; |
| 46 |
|
import org.xwiki.security.authorization.testwikis.TestSpace; |
| 47 |
|
import org.xwiki.security.authorization.testwikis.TestWiki; |
| 48 |
|
import org.xwiki.test.ComponentManagerRule; |
| 49 |
|
import org.xwiki.test.annotation.ComponentList; |
| 50 |
|
|
| 51 |
|
import static org.hamcrest.CoreMatchers.equalTo; |
| 52 |
|
import static org.hamcrest.CoreMatchers.hasItems; |
| 53 |
|
import static org.hamcrest.CoreMatchers.is; |
| 54 |
|
import static org.hamcrest.CoreMatchers.notNullValue; |
| 55 |
|
import static org.junit.Assert.assertThat; |
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
@version |
| 61 |
|
@since |
| 62 |
|
|
| 63 |
|
@ComponentList({ |
| 64 |
|
DefaultStringEntityReferenceResolver.class, |
| 65 |
|
DefaultStringEntityReferenceSerializer.class, |
| 66 |
|
DefaultEntityReferenceProvider.class, |
| 67 |
|
DefaultModelConfiguration.class, |
| 68 |
|
DefaultSymbolScheme.class |
| 69 |
|
}) |
| |
|
| 100% |
Uncovered Elements: 0 (67) |
Complexity: 1 |
Complexity Density: 0.02 |
|
| 70 |
|
public class TestDefinitionParserTest |
| 71 |
|
{ |
| 72 |
|
@Rule |
| 73 |
|
public final ComponentManagerRule componentManager = new ComponentManagerRule(); |
| 74 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (66) |
Complexity: 1 |
Complexity Density: 0.02 |
1PASS
|
|
| 75 |
1 |
@Test... |
| 76 |
|
public void testDefinitionTestParser() throws Exception |
| 77 |
|
{ |
| 78 |
1 |
TestDefinitionParser parser = new DefaultTestDefinitionParser(); |
| 79 |
|
|
| 80 |
1 |
EntityReferenceResolver<String> resolver = |
| 81 |
|
componentManager.getInstance(EntityReferenceResolver.TYPE_STRING); |
| 82 |
1 |
EntityReferenceSerializer<String> serializer = |
| 83 |
|
componentManager.getInstance(EntityReferenceSerializer.TYPE_STRING); |
| 84 |
|
|
| 85 |
1 |
TestDefinition testDefinition = |
| 86 |
|
parser.parse("testwikis" + File.separatorChar + "parserTester.xml", resolver, serializer); |
| 87 |
|
|
| 88 |
1 |
Collection<TestWiki> testWikis = testDefinition.getWikis(); |
| 89 |
|
|
| 90 |
1 |
assertThat("There should be some wikis", testWikis, notNullValue()); |
| 91 |
1 |
assertThat("The wikis count should be 3", testWikis.size(), equalTo(3)); |
| 92 |
|
|
| 93 |
1 |
TestWiki mainwiki = testDefinition.getMainWiki(); |
| 94 |
|
|
| 95 |
1 |
assertThat("Main wiki should be defined", mainwiki, notNullValue()); |
| 96 |
1 |
assertThat("Main wiki should be named 'wiki'", mainwiki.getWikiReference(), equalTo(new WikiReference("wiki"))); |
| 97 |
1 |
assertThat("Main wiki should be main wiki", mainwiki.isMainWiki(), is(true)); |
| 98 |
1 |
assertThat("Main wiki owner should be XWiki.Admin", mainwiki.getOwner(), equalTo( |
| 99 |
|
new DocumentReference("wiki", "XWiki", "Admin"))); |
| 100 |
1 |
assertThat("Main wiki should have 4 users (2 groups, and 2 users)", mainwiki.getUsers().size(), equalTo(4)); |
| 101 |
1 |
assertThat("Main wiki should have 2 groups", mainwiki.getGroups().size(), equalTo(2)); |
| 102 |
1 |
assertThat("Main wiki should have a groupA", mainwiki.getGroup("groupA"), notNullValue()); |
| 103 |
1 |
assertThat("Main wiki should have a userA", mainwiki.getUser("userA"), notNullValue()); |
| 104 |
|
|
| 105 |
1 |
Collection<TestGroup> groups = mainwiki.getUser("userA").getGroups(); |
| 106 |
|
|
| 107 |
1 |
assertThat("UserA of Main wiki should be in 2 groups", groups.size(), equalTo(2)); |
| 108 |
|
|
| 109 |
1 |
List<DocumentReference> groupRefs = new ArrayList<DocumentReference>(); |
| 110 |
1 |
for (TestGroup group : groups) { |
| 111 |
2 |
groupRefs.add(group.getGroupReference()); |
| 112 |
|
} |
| 113 |
|
|
| 114 |
1 |
assertThat("User A is in GroupA of the main wiki and the subwiki", groupRefs, |
| 115 |
|
hasItems(new DocumentReference("wiki", "XWiki", "groupA"), |
| 116 |
|
new DocumentReference("wiki1", "XWiki", "groupA"))); |
| 117 |
|
|
| 118 |
1 |
Collection<TestAccessRule> rules = mainwiki.getAccessRules(); |
| 119 |
|
|
| 120 |
1 |
assertThat("There must be 26 access rules on main wiki", rules.size(), equalTo(26)); |
| 121 |
|
|
| 122 |
1 |
List<DocumentReference> userRefs = new ArrayList<DocumentReference>(); |
| 123 |
1 |
List<Right> rights = new ArrayList<Right>(); |
| 124 |
1 |
List<RuleState> states = new ArrayList<RuleState>(); |
| 125 |
|
|
| 126 |
1 |
for (TestAccessRule rule : rules) { |
| 127 |
26 |
userRefs.add(rule.getUser()); |
| 128 |
26 |
rights.add(rule.getRight()); |
| 129 |
26 |
states.add(rule.getState()); |
| 130 |
|
} |
| 131 |
|
|
| 132 |
1 |
assertThat("Users in access rules of main wiki mismatch", userRefs, hasItems( |
| 133 |
|
new DocumentReference("wiki", "XWiki", "userA"), |
| 134 |
|
new DocumentReference("wiki", "XWiki", "userB"), |
| 135 |
|
new DocumentReference("wiki", "XWiki", "groupA"), |
| 136 |
|
new DocumentReference("wiki", "XWiki", "groupB") |
| 137 |
|
)); |
| 138 |
1 |
assertThat("Rights in access rules of main wiki mismatch", rights, hasItems( |
| 139 |
|
Right.VIEW, Right.LOGIN, Right.EDIT, Right.COMMENT, Right.DELETE, Right.REGISTER, Right.ADMIN, Right.PROGRAM |
| 140 |
|
)); |
| 141 |
1 |
assertThat("State in access rules of main wiki mismatch", states, hasItems( |
| 142 |
|
RuleState.ALLOW, RuleState.DENY |
| 143 |
|
)); |
| 144 |
|
|
| 145 |
1 |
assertThat("Main wiki should have 3 spaces (2 plus XWiki)", mainwiki.getSpaces().size(), equalTo(3)); |
| 146 |
|
|
| 147 |
1 |
TestSpace space = mainwiki.getSpace("space1"); |
| 148 |
|
|
| 149 |
1 |
assertThat("Main wiki should have a space named 'space1'", space, notNullValue()); |
| 150 |
1 |
assertThat("'space1' of main wiki should have description 'space 1'", space.getDescription(), equalTo("space 1")); |
| 151 |
|
|
| 152 |
1 |
rules = space.getAccessRules(); |
| 153 |
|
|
| 154 |
1 |
assertThat("There must be 8 access rules on space 1", rules.size(), equalTo(8)); |
| 155 |
|
|
| 156 |
1 |
userRefs = new ArrayList<DocumentReference>(); |
| 157 |
1 |
rights = new ArrayList<Right>(); |
| 158 |
1 |
states = new ArrayList<RuleState>(); |
| 159 |
|
|
| 160 |
1 |
for (TestAccessRule rule : rules) { |
| 161 |
8 |
userRefs.add(rule.getUser()); |
| 162 |
8 |
rights.add(rule.getRight()); |
| 163 |
8 |
states.add(rule.getState()); |
| 164 |
|
} |
| 165 |
|
|
| 166 |
1 |
assertThat("Users in access rules of space 1 of main wiki mismatch", userRefs, hasItems( |
| 167 |
|
new DocumentReference("wiki", "XWiki", "userA"), |
| 168 |
|
new DocumentReference("wiki", "XWiki", "userB"), |
| 169 |
|
new DocumentReference("wiki", "XWiki", "groupB") |
| 170 |
|
)); |
| 171 |
1 |
assertThat("Rights in access rules of space 1 of main wiki mismatch", rights, hasItems( |
| 172 |
|
Right.VIEW, Right.EDIT, Right.COMMENT, Right.DELETE, Right.ADMIN |
| 173 |
|
)); |
| 174 |
1 |
assertThat("State in access rules of space 1 of main wiki mismatch", states, hasItems( |
| 175 |
|
RuleState.DENY |
| 176 |
|
)); |
| 177 |
|
|
| 178 |
1 |
assertThat("Space 1 of main wiki should have 2 documents", space.getDocuments().size(), equalTo(2)); |
| 179 |
|
|
| 180 |
1 |
TestDocument document = space.getDocument("document1"); |
| 181 |
|
|
| 182 |
1 |
assertThat("Space 1 of main wiki should have a document named 'document1'", document, notNullValue()); |
| 183 |
1 |
assertThat("'document1' of 'space1' of main wiki should have description 'Document 1'", document.getDescription(), equalTo("Document 1")); |
| 184 |
|
|
| 185 |
1 |
rules = document.getAccessRules(); |
| 186 |
|
|
| 187 |
1 |
assertThat("There must be 7 access rules on document 1", rules.size(), equalTo(7)); |
| 188 |
|
|
| 189 |
1 |
userRefs = new ArrayList<DocumentReference>(); |
| 190 |
1 |
rights = new ArrayList<Right>(); |
| 191 |
1 |
states = new ArrayList<RuleState>(); |
| 192 |
|
|
| 193 |
1 |
for (TestAccessRule rule : rules) { |
| 194 |
7 |
userRefs.add(rule.getUser()); |
| 195 |
7 |
rights.add(rule.getRight()); |
| 196 |
7 |
states.add(rule.getState()); |
| 197 |
|
} |
| 198 |
|
|
| 199 |
1 |
assertThat("Users in access rules of document 1 of space 1 of main wiki mismatch", userRefs, hasItems( |
| 200 |
|
new DocumentReference("wiki", "XWiki", "userA"), |
| 201 |
|
new DocumentReference("wiki", "XWiki", "userB"), |
| 202 |
|
new DocumentReference("wiki", "XWiki", "groupA") |
| 203 |
|
)); |
| 204 |
1 |
assertThat("Rights in access rules of document 1 of space 1 of main wiki mismatch", rights, hasItems( |
| 205 |
|
Right.VIEW, Right.EDIT, Right.COMMENT, Right.DELETE |
| 206 |
|
)); |
| 207 |
1 |
assertThat("State in access rules of document 1 of space 1 of main wiki mismatch", states, hasItems( |
| 208 |
|
RuleState.ALLOW |
| 209 |
|
)); |
| 210 |
|
|
| 211 |
|
} |
| 212 |
|
} |
| 213 |
|
|