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