2 * Copyright 2005 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.wamblee.security.authorization;
19 import static org.wamblee.security.authorization.AuthorizationResult.DENIED;
20 import static org.wamblee.security.authorization.AuthorizationResult.GRANTED;
22 import org.wamblee.persistence.hibernate.HibernateMappingFiles;
23 import org.wamblee.test.SpringConfigFiles;
24 import org.wamblee.test.SpringTestCase;
25 import org.wamblee.usermgt.UserAccessor;
28 * Tests the authorization service.
30 public class AuthorizationServiceTest extends SpringTestCase {
32 private AuthorizationRule _rule1;
33 private AuthorizationRule _rule2;
34 private AuthorizationRule _rule3;
35 private AuthorizationService _service;
38 public AuthorizationServiceTest() {
39 super(SpringConfigFiles.class, HibernateMappingFiles.class);
42 public AuthorizationServiceTest(Class<? extends SpringConfigFiles>aSpringFiles,
43 Class<? extends HibernateMappingFiles> aMappings) {
44 super(aSpringFiles, aMappings);
47 protected AuthorizationService getService() {
52 * @see junit.framework.TestCase#setUp()
55 protected void setUp() throws Exception {
58 _rule1 = createRule(GRANTED, "users", "/oni/", AllOperation.class);
59 _rule2 = createRule(DENIED, "users", "/abc/", ReadOperation.class);
60 _rule3 = createRule(GRANTED, "users", "/abc/", AllOperation.class);
62 _service = createService();
63 _service.appendRule(_rule1);
64 _service.appendRule(_rule2);
65 _service.appendRule(_rule3);
68 protected void resetTestRules() {
69 ((TestAuthorizationRule)_rule1).reset();
70 ((TestAuthorizationRule)_rule2).reset();
71 ((TestAuthorizationRule)_rule3).reset();
74 protected UserAccessor createUserAccessor() {
75 return new TestUserAccessor();
79 * Creates an authorization service with some rules for testing. .
80 * @return Authorization service.
82 protected AuthorizationService createService() {
83 DefaultAuthorizationService service = new DefaultAuthorizationService() ;
84 service.setUserAccessor(createUserAccessor());
88 protected AuthorizationRule createRule(AuthorizationResult aResult, String aGroup, String aPath, Class<? extends Operation> aOperation) {
89 return new TestAuthorizationRule(aResult, aGroup, aPath, aOperation);
92 protected void checkMatchCount(int aCount, AuthorizationRule aRule) {
93 assertEquals( aCount, ((TestAuthorizationRule)aRule).getMatchCount());
96 protected Object createResource(String aPath) {
97 return new TestResource(aPath);
100 protected void checkRuleCount(int aCount) {
105 * Several checks to verify the outcome of matching against the first rule.
108 public void testFirstRuleGrants() {
109 assertTrue( _service.isAllowed(createResource("/oni/xyz.jpg"), new ReadOperation()));
110 checkMatchCount(1, _rule1);
111 assertTrue(_service.isAllowed(createResource("/oni/xyz.jpg"), new WriteOperation()));
112 checkMatchCount(2, _rule1);
113 assertTrue(_service.isAllowed(createResource("/oni/xyz.jpg"), new DeleteOperation()));
114 checkMatchCount(3, _rule1);
115 assertTrue(_service.isAllowed(createResource("/oni/xyz.jpg"), new CreateOperation()));
116 checkMatchCount(4, _rule1);
117 checkMatchCount(0, _rule2);
118 checkMatchCount(0, _rule3);
122 * Verify that a match with the second rule leads to a denial of authorization.
125 public void testSecondRuleDenies() {
126 assertFalse(_service.isAllowed(createResource("/abc/xyz.jpg"), new ReadOperation()));
127 checkMatchCount(0, _rule1);
128 checkMatchCount(1, _rule2);
129 checkMatchCount(0, _rule3);
133 * Verifies that the third rule is used when appropriate and that it grants access.
136 public void testThirdRuleGrants() {
137 assertTrue(_service.isAllowed(createResource("/abc/xyz.jpg"), new WriteOperation()));
138 checkMatchCount(0, _rule1);
139 checkMatchCount(0, _rule2);
140 checkMatchCount(1, _rule3);
144 * Removes a rule and checks it is removed.
147 public void testRemoveRule() {
149 assertTrue(_service.isAllowed(createResource("/abc/xyz.jpg"), new WriteOperation()));
150 _service.removeRule(2);
151 assertFalse(_service.isAllowed(createResource("/abc/xyz.jpg"), new WriteOperation()));
156 * Inserts a rule and checks it is inserted.
159 public void testInsertRule() {
161 assertFalse(_service.isAllowed(createResource("/janse/xyz.jpg"), new WriteOperation()));
162 _service.appendRule(createRule(GRANTED, "users", "/janse/", WriteOperation.class));
163 assertTrue(_service.isAllowed(createResource("/janse/xyz.jpg"), new WriteOperation()));
169 * Gets the rules. Verifies that all rules are obtained.
172 public void testGetRules() {
173 AuthorizationRule[] rules = _service.getRules();
174 assertEquals(3, rules.length);
178 * Verifies that when no rules match, access is denied.
181 public void testNoRulesSupportResource() {
182 assertFalse(_service.isAllowed(createResource("/xyxyxyxy"), new ReadOperation()));
183 checkMatchCount(0, _rule1);
184 checkMatchCount(0, _rule2);
185 checkMatchCount(0, _rule3);