now using the simplified user management interface.
[utils] / security / impl / src / test / java / org / wamblee / security / authorization / UrlAuthorizationRuleTest.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */ 
16 package org.wamblee.security.authorization;
17
18 import junit.framework.TestCase;
19 import static org.wamblee.security.authorization.AuthorizationResult.GRANTED;
20 import static org.wamblee.security.authorization.AuthorizationResult.UNDECIDED;
21 import static org.wamblee.security.authorization.AuthorizationResult.UNSUPPORTED_RESOURCE;
22
23 import org.wamblee.security.authentication.User;
24
25 /**
26  * Tests for the {@link org.wamblee.security.authorization.UrlAuthorizationRule}
27  * .
28  * 
29  * @author Erik Brakkee
30  */
31 public class UrlAuthorizationRuleTest extends TestCase {
32     
33     private TestUserAccessor userAccessor; 
34     
35     public void setUp() { 
36         userAccessor = new TestUserAccessor(); 
37         
38     }
39     
40     /**
41      * Constructs the rule with a result of UNDECIDED. Verifies that an
42      * IllegalArgumentException is thrown.
43      */
44     public void testConstructWithUndecidedResult() {
45         try {
46             new TestAuthorizationRule(UNDECIDED, "users", "/path",
47                 ReadOperation.class);
48             fail();
49         } catch (IllegalArgumentException e) {
50             // ok
51         }
52     }
53
54     /**
55      * Constructs the rule with a result of UNSUPPORTED_RESOURCE. Verifies that
56      * an IllegalArgumentException is thrown.
57      */
58     public void testConstructWithUnsupportedResult() {
59         try {
60             new TestAuthorizationRule(UNSUPPORTED_RESOURCE, "users", "/path",
61                 ReadOperation.class);
62             fail();
63         } catch (IllegalArgumentException e) {
64             // ok
65         }
66     }
67
68     /**
69      * Constructs the authorization rule and applies it to an unsupported object
70      * type. Verifies that the result is UNSUPPORTED_RESOURCE.
71      */
72     public void testUnsupportedObject() {
73         AuthorizationRule rule = new TestAuthorizationRule(GRANTED, "users",
74             "/path", ReadOperation.class);
75         rule.setUserAdministration(userAccessor.getUserAdmin());
76         assertEquals(UNSUPPORTED_RESOURCE, rule.isAllowed("hello",
77             new ReadOperation(), userAccessor.getCurrentUser()));
78     }
79
80     public void testMatchingScenarios() {
81         AuthorizationRule rule = new TestAuthorizationRule(GRANTED, "users",
82             "/path/", ReadOperation.class);
83         rule.setUserAdministration(userAccessor.getUserAdmin());
84         String user = userAccessor.getCurrentUser();
85
86         // everything matches
87         assertEquals(GRANTED, rule.isAllowed(new TestResource("/path/a"),
88             new ReadOperation(), user));
89         assertEquals(GRANTED, rule.isAllowed(new TestResource("/path/"),
90             new ReadOperation(), user));
91
92         // path does not match.
93         assertEquals(UNDECIDED, rule.isAllowed(new TestResource("/path"),
94             new ReadOperation(), user));
95
96         // operation does not match.
97         assertEquals(UNDECIDED, rule.isAllowed(new TestResource("/path/"),
98             new WriteOperation(), user));
99
100         // group does not match.
101         AuthorizationRule rule2 = new TestAuthorizationRule(GRANTED, "users2",
102             "/path/", ReadOperation.class);
103         rule2.setUserAdministration(userAccessor.getUserAdmin());
104         assertEquals(UNDECIDED, rule2.isAllowed(new TestResource("/path/a"),
105             new ReadOperation(), user));
106     }
107 }