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;
21 import junit.framework.TestCase;
23 import org.wamblee.persistence.hibernate.HibernateMappingFiles;
24 import org.wamblee.test.spring.SpringTestCase;
25 import org.wamblee.test.spring.SpringConfigFiles;
26 import org.wamblee.usermgt.UserAccessor;
29 * Tests the authorization service.
31 * @author Erik Brakkee
33 public class AuthorizationServiceTest extends TestCase {
35 private AuthorizationRule _rule1;
36 private AuthorizationRule _rule2;
37 private AuthorizationRule _rule3;
38 private AuthorizationService _service;
40 protected AuthorizationService getService() {
45 * @see junit.framework.TestCase#setUp()
48 protected void setUp() throws Exception {
51 _rule1 = createRule(GRANTED, "users", "/oni/", AllOperation.class);
52 _rule2 = createRule(DENIED, "users", "/abc/", ReadOperation.class);
53 _rule3 = createRule(GRANTED, "users", "/abc/", AllOperation.class);
55 _service = createService();
56 _service.appendRule(_rule1);
57 _service.appendRule(_rule2);
58 _service.appendRule(_rule3);
61 protected void resetTestRules() {
62 ((TestAuthorizationRule)_rule1).reset();
63 ((TestAuthorizationRule)_rule2).reset();
64 ((TestAuthorizationRule)_rule3).reset();
67 protected UserAccessor createUserAccessor() {
68 return new TestUserAccessor();
72 * Creates an authorization service with some rules for testing. .
73 * @return Authorization service.
75 protected AuthorizationService createService() {
76 DefaultAuthorizationService service = new DefaultAuthorizationService() ;
77 service.setUserAccessor(createUserAccessor());
81 protected AuthorizationRule createRule(AuthorizationResult aResult, String aGroup, String aPath, Class<? extends Operation> aOperation) {
82 return new TestAuthorizationRule(aResult, aGroup, aPath, aOperation);
85 protected void checkMatchCount(int aCount, AuthorizationRule aRule) {
86 assertEquals( aCount, ((TestAuthorizationRule)aRule).getMatchCount());
89 protected Object createResource(String aPath) {
90 return new TestResource(aPath);
93 protected void checkRuleCount(int aCount) {
98 * Several checks to verify the outcome of matching against the first rule.
101 public void testFirstRuleGrants() {
102 assertTrue( _service.isAllowed(createResource("/oni/xyz.jpg"), new ReadOperation()));
103 checkMatchCount(1, _rule1);
104 assertTrue(_service.isAllowed(createResource("/oni/xyz.jpg"), new WriteOperation()));
105 checkMatchCount(2, _rule1);
106 assertTrue(_service.isAllowed(createResource("/oni/xyz.jpg"), new DeleteOperation()));
107 checkMatchCount(3, _rule1);
108 assertTrue(_service.isAllowed(createResource("/oni/xyz.jpg"), new CreateOperation()));
109 checkMatchCount(4, _rule1);
110 checkMatchCount(0, _rule2);
111 checkMatchCount(0, _rule3);
115 * Verify that a match with the second rule leads to a denial of authorization.
118 public void testSecondRuleDenies() {
119 assertFalse(_service.isAllowed(createResource("/abc/xyz.jpg"), new ReadOperation()));
120 checkMatchCount(0, _rule1);
121 checkMatchCount(1, _rule2);
122 checkMatchCount(0, _rule3);
126 * Verifies that the third rule is used when appropriate and that it grants access.
129 public void testThirdRuleGrants() {
130 assertTrue(_service.isAllowed(createResource("/abc/xyz.jpg"), new WriteOperation()));
131 checkMatchCount(0, _rule1);
132 checkMatchCount(0, _rule2);
133 checkMatchCount(1, _rule3);
137 * Removes a rule and checks it is removed.
140 public void testRemoveRule() {
142 assertTrue(_service.isAllowed(createResource("/abc/xyz.jpg"), new WriteOperation()));
143 _service.removeRule(2);
144 assertFalse(_service.isAllowed(createResource("/abc/xyz.jpg"), new WriteOperation()));
149 * Inserts a rule and checks it is inserted.
152 public void testInsertRule() {
154 assertFalse(_service.isAllowed(createResource("/janse/xyz.jpg"), new WriteOperation()));
155 _service.appendRule(createRule(GRANTED, "users", "/janse/", WriteOperation.class));
156 assertTrue(_service.isAllowed(createResource("/janse/xyz.jpg"), new WriteOperation()));
162 * Gets the rules. Verifies that all rules are obtained.
165 public void testGetRules() {
166 AuthorizationRule[] rules = _service.getRules();
167 assertEquals(3, rules.length);
171 * Verifies that when no rules match, access is denied.
174 public void testNoRulesSupportResource() {
175 assertFalse(_service.isAllowed(createResource("/xyxyxyxy"), new ReadOperation()));
176 checkMatchCount(0, _rule1);
177 checkMatchCount(0, _rule2);
178 checkMatchCount(0, _rule3);