1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.security.authorization.internal

File SecurityAccessTest.java

 

Code metrics

2
82
3
1
161
109
4
0.05
27.33
3
1.33

Classes

Class Line # Actions
SecurityAccessTest 39 82 0% 4 1
0.988505798.9%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.security.authorization.internal;
21   
22    import org.junit.Test;
23    import org.xwiki.security.authorization.Right;
24    import org.xwiki.security.authorization.RuleState;
25    import org.xwiki.security.authorization.SecurityAccess;
26   
27    import static org.hamcrest.CoreMatchers.equalTo;
28    import static org.hamcrest.CoreMatchers.not;
29    import static org.hamcrest.CoreMatchers.sameInstance;
30    import static org.junit.Assert.assertThat;
31    import static org.xwiki.security.authorization.RuleState.UNDETERMINED;
32   
33    /**
34    * Tests for asserting the correctness of the {@link SecurityAccess} data structure.
35    *
36    * @version $Id: cfab6476a201d51ed035777bce7d03a3bfae1a24 $
37    * @since 4.0M2
38    */
 
39    public class SecurityAccessTest
40    {
41   
42    /**
43    * Assert that the default security access instance defines an access level for each defined right, and that the
44    * access level is the same as the defined default for the right.
45    */
 
46  3 toggle private void assertDefaultAccessLevel()
47    {
48  3 SecurityAccess access = XWikiSecurityAccess.getDefaultAccess();
49  3 for (Right right : Right.values()) {
50  36 if (access.get(right) != UNDETERMINED) {
51  36 assertThat("Right(" + right.getName() + ")",
52    access.get(right), equalTo(right.getDefaultState()));
53    }
54    }
55    }
56   
57    /**
58    * Assert that access levels can be cleared and set on a SecurityAccess instance.
59    */
 
60  1 toggle @Test
61    public void testAccessLevel() throws Exception
62    {
63  1 assertDefaultAccessLevel();
64   
65  1 XWikiSecurityAccess l = XWikiSecurityAccess.getDefaultAccess().clone();
66   
67  1 assertThat(RuleState.ALLOW, equalTo(l.get(Right.VIEW)));
68  1 assertThat(RuleState.ALLOW, equalTo(l.get(Right.EDIT)));
69  1 assertThat(RuleState.ALLOW, equalTo(l.get(Right.COMMENT)));
70  1 assertThat(RuleState.ALLOW, equalTo(l.get(Right.LOGIN)));
71  1 assertThat(RuleState.ALLOW, equalTo(l.get(Right.REGISTER)));
72  1 assertThat(RuleState.DENY, equalTo(l.get(Right.DELETE)));
73  1 assertThat(RuleState.DENY, equalTo(l.get(Right.ADMIN)));
74  1 assertThat(RuleState.DENY, equalTo(l.get(Right.PROGRAM)));
75  1 assertThat(RuleState.DENY, equalTo(l.get(Right.ILLEGAL)));
76   
77  1 l.clear(Right.VIEW);
78  1 l.clear(Right.COMMENT);
79  1 l.clear(Right.LOGIN);
80  1 l.clear(Right.ADMIN);
81   
82  1 assertThat(RuleState.UNDETERMINED, equalTo(l.get(Right.VIEW)));
83  1 assertThat(RuleState.ALLOW, equalTo(l.get(Right.EDIT)));
84  1 assertThat(RuleState.UNDETERMINED, equalTo(l.get(Right.COMMENT)));
85  1 assertThat(RuleState.UNDETERMINED, equalTo(l.get(Right.LOGIN)));
86  1 assertThat(RuleState.ALLOW, equalTo(l.get(Right.REGISTER)));
87  1 assertThat(RuleState.DENY, equalTo(l.get(Right.DELETE)));
88  1 assertThat(RuleState.UNDETERMINED, equalTo(l.get(Right.ADMIN)));
89  1 assertThat(RuleState.DENY, equalTo(l.get(Right.PROGRAM)));
90  1 assertThat(RuleState.DENY, equalTo(l.get(Right.ILLEGAL)));
91   
92  1 l.deny(Right.VIEW);
93  1 l.deny(Right.LOGIN);
94  1 l.allow(Right.ADMIN);
95   
96  1 assertThat(RuleState.DENY, equalTo(l.get(Right.VIEW)));
97  1 assertThat(RuleState.ALLOW, equalTo(l.get(Right.EDIT)));
98  1 assertThat(RuleState.UNDETERMINED, equalTo(l.get(Right.COMMENT)));
99  1 assertThat(RuleState.DENY, equalTo(l.get(Right.LOGIN)));
100  1 assertThat(RuleState.ALLOW, equalTo(l.get(Right.REGISTER)));
101  1 assertThat(RuleState.DENY, equalTo(l.get(Right.DELETE)));
102  1 assertThat(RuleState.ALLOW, equalTo(l.get(Right.ADMIN)));
103  1 assertThat(RuleState.DENY, equalTo(l.get(Right.PROGRAM)));
104  1 assertThat(RuleState.DENY, equalTo(l.get(Right.ILLEGAL)));
105   
106  1 l.clear(Right.VIEW);
107  1 l.clear(Right.EDIT);
108  1 l.clear(Right.COMMENT);
109  1 l.clear(Right.LOGIN);
110  1 l.clear(Right.REGISTER);
111  1 l.clear(Right.DELETE);
112  1 l.clear(Right.ADMIN);
113  1 l.clear(Right.PROGRAM);
114  1 l.clear(Right.ILLEGAL);
115   
116  1 assertThat(RuleState.UNDETERMINED, equalTo(l.get(Right.VIEW)));
117  1 assertThat(RuleState.UNDETERMINED, equalTo(l.get(Right.EDIT)));
118  1 assertThat(RuleState.UNDETERMINED, equalTo(l.get(Right.COMMENT)));
119  1 assertThat(RuleState.UNDETERMINED, equalTo(l.get(Right.LOGIN)));
120  1 assertThat(RuleState.UNDETERMINED, equalTo(l.get(Right.REGISTER)));
121  1 assertThat(RuleState.UNDETERMINED, equalTo(l.get(Right.DELETE)));
122  1 assertThat(RuleState.UNDETERMINED, equalTo(l.get(Right.ADMIN)));
123  1 assertThat(RuleState.UNDETERMINED, equalTo(l.get(Right.PROGRAM)));
124  1 assertThat(RuleState.UNDETERMINED, equalTo(l.get(Right.ILLEGAL)));
125   
126  1 l.allow(Right.VIEW);
127  1 l.allow(Right.EDIT);
128  1 l.allow(Right.COMMENT);
129  1 l.allow(Right.LOGIN);
130  1 l.allow(Right.REGISTER);
131  1 l.allow(Right.DELETE);
132  1 l.allow(Right.ADMIN);
133  1 l.allow(Right.PROGRAM);
134  1 l.allow(Right.ILLEGAL);
135   
136  1 assertThat(RuleState.ALLOW, equalTo(l.get(Right.VIEW)));
137  1 assertThat(RuleState.ALLOW, equalTo(l.get(Right.EDIT)));
138  1 assertThat(RuleState.ALLOW, equalTo(l.get(Right.COMMENT)));
139  1 assertThat(RuleState.ALLOW, equalTo(l.get(Right.LOGIN)));
140  1 assertThat(RuleState.ALLOW, equalTo(l.get(Right.REGISTER)));
141  1 assertThat(RuleState.ALLOW, equalTo(l.get(Right.DELETE)));
142  1 assertThat(RuleState.ALLOW, equalTo(l.get(Right.ADMIN)));
143  1 assertThat(RuleState.ALLOW, equalTo(l.get(Right.PROGRAM)));
144  1 assertThat(RuleState.ALLOW, equalTo(l.get(Right.ILLEGAL)));
145   
146  1 assertDefaultAccessLevel();
147    }
148   
149    /**
150    * Assert that the clone method works.
151    */
 
152  1 toggle @Test
153    public void testClone() throws Exception
154    {
155  1 XWikiSecurityAccess l = XWikiSecurityAccess.getDefaultAccess().clone();
156  1 XWikiSecurityAccess k = l.clone();
157  1 assertThat(l, equalTo(k));
158  1 assertThat(l, not(sameInstance(k)));
159  1 assertDefaultAccessLevel();
160    }
161    }