(no commit message)
[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 static org.wamblee.security.authorization.AuthorizationResult.*;
19 import junit.framework.TestCase;
20
21 /**
22  * Tests for the {@link org.wamblee.security.authorization.UrlAuthorizationRule}
23  * .
24  * 
25  * @author Erik Brakkee
26  */
27 public class UrlAuthorizationRuleTest extends TestCase {
28     
29     private TestUserAccessor userAccessor; 
30     
31     public void setUp() { 
32         userAccessor = new TestUserAccessor(); 
33         
34     }
35     
36     /**
37      * Constructs the rule with a result of UNDECIDED. Verifies that an
38      * IllegalArgumentException is thrown.
39      */
40     public void testConstructWithUndecidedResult() {
41         try {
42             new TestAuthorizationRule(UNDECIDED, "users", "/path",
43                 ReadOperation.class);
44             fail();
45         } catch (IllegalArgumentException e) {
46             // ok
47         }
48     }
49
50     /**
51      * Constructs the rule with a result of UNSUPPORTED_RESOURCE. Verifies that
52      * an IllegalArgumentException is thrown.
53      */
54     public void testConstructWithUnsupportedResult() {
55         try {
56             new TestAuthorizationRule(UNSUPPORTED_RESOURCE, "users", "/path",
57                 ReadOperation.class);
58             fail();
59         } catch (IllegalArgumentException e) {
60             // ok
61         }
62     }
63
64     /**
65      * Constructs the authorization rule and applies it to an unsupported object
66      * type. Verifies that the result is UNSUPPORTED_RESOURCE.
67      */
68     public void testUnsupportedObject() {
69         AuthorizationRule rule = new TestAuthorizationRule(GRANTED, "users",
70             "/path", ReadOperation.class);
71         rule.setUserAdministration(userAccessor.getUserAdmin());
72         assertEquals(UNSUPPORTED_RESOURCE, rule.isAllowed("hello",
73             new ReadOperation(), userAccessor.getCurrentUser()));
74     }
75
76     public void testMatchingScenarios() {
77         AuthorizationRule rule = new TestAuthorizationRule(GRANTED, "users",
78             "/path/", ReadOperation.class);
79         rule.setUserAdministration(userAccessor.getUserAdmin());
80         String user = userAccessor.getCurrentUser();
81
82         // everything matches
83         assertEquals(GRANTED, rule.isAllowed(new TestResource("/path/a"),
84             new ReadOperation(), user));
85         assertEquals(GRANTED, rule.isAllowed(new TestResource("/path/"),
86             new ReadOperation(), user));
87
88         // path does not match.
89         assertEquals(UNDECIDED, rule.isAllowed(new TestResource("/path"),
90             new ReadOperation(), user));
91
92         // operation does not match.
93         assertEquals(UNDECIDED, rule.isAllowed(new TestResource("/path/"),
94             new WriteOperation(), user));
95
96         // group does not match.
97         AuthorizationRule rule2 = new TestAuthorizationRule(GRANTED, "users2",
98             "/path/", ReadOperation.class);
99         rule2.setUserAdministration(userAccessor.getUserAdmin());
100         assertEquals(UNDECIDED, rule2.isAllowed(new TestResource("/path/a"),
101             new ReadOperation(), user));
102     }
103 }