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