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