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.usermgt.UserAccessor;
26 * Tests the authorization service.
28 * @author Erik Brakkee
30 public class AuthorizationServiceTest extends TestCase {
32 private AuthorizationRule _rule1;
33 private AuthorizationRule _rule2;
34 private AuthorizationRule _rule3;
35 private AuthorizationService _service;
37 protected AuthorizationService getService() {
42 * @see junit.framework.TestCase#setUp()
45 protected void setUp() throws Exception {
48 _rule1 = createRule(GRANTED, "users", "/oni/", AllOperation.class);
49 _rule2 = createRule(DENIED, "users", "/abc/", ReadOperation.class);
50 _rule3 = createRule(GRANTED, "users", "/abc/", AllOperation.class);
52 _service = createService();
53 _service.appendRule(_rule1);
54 _service.appendRule(_rule2);
55 _service.appendRule(_rule3);
58 protected void resetTestRules() {
59 ((TestAuthorizationRule)_rule1).reset();
60 ((TestAuthorizationRule)_rule2).reset();
61 ((TestAuthorizationRule)_rule3).reset();
64 protected UserAccessor createUserAccessor() {
65 return new TestUserAccessor();
69 * Creates an authorization service with some rules for testing. .
70 * @return Authorization service.
72 protected AuthorizationService createService() {
73 DefaultAuthorizationService service = new DefaultAuthorizationService() ;
74 service.setUserAccessor(createUserAccessor());
78 protected AuthorizationRule createRule(AuthorizationResult aResult, String aGroup, String aPath, Class<? extends Operation> aOperation) {
79 return new TestAuthorizationRule(aResult, aGroup, aPath, aOperation);
82 protected void checkMatchCount(int aCount, AuthorizationRule aRule) {
83 assertEquals( aCount, ((TestAuthorizationRule)aRule).getMatchCount());
86 protected Object createResource(String aPath) {
87 return new TestResource(aPath);
90 protected void checkRuleCount(int aCount) {
95 * Several checks to verify the outcome of matching against the first rule.
98 public void testFirstRuleGrants() {
99 assertTrue( _service.isAllowed(createResource("/oni/xyz.jpg"), new ReadOperation()));
100 checkMatchCount(1, _rule1);
101 assertTrue(_service.isAllowed(createResource("/oni/xyz.jpg"), new WriteOperation()));
102 checkMatchCount(2, _rule1);
103 assertTrue(_service.isAllowed(createResource("/oni/xyz.jpg"), new DeleteOperation()));
104 checkMatchCount(3, _rule1);
105 assertTrue(_service.isAllowed(createResource("/oni/xyz.jpg"), new CreateOperation()));
106 checkMatchCount(4, _rule1);
107 checkMatchCount(0, _rule2);
108 checkMatchCount(0, _rule3);
112 * Verify that a match with the second rule leads to a denial of authorization.
115 public void testSecondRuleDenies() {
116 assertFalse(_service.isAllowed(createResource("/abc/xyz.jpg"), new ReadOperation()));
117 checkMatchCount(0, _rule1);
118 checkMatchCount(1, _rule2);
119 checkMatchCount(0, _rule3);
123 * Verifies that the third rule is used when appropriate and that it grants access.
126 public void testThirdRuleGrants() {
127 assertTrue(_service.isAllowed(createResource("/abc/xyz.jpg"), new WriteOperation()));
128 checkMatchCount(0, _rule1);
129 checkMatchCount(0, _rule2);
130 checkMatchCount(1, _rule3);
134 * Removes a rule and checks it is removed.
137 public void testRemoveRule() {
139 assertTrue(_service.isAllowed(createResource("/abc/xyz.jpg"), new WriteOperation()));
140 _service.removeRule(2);
141 assertFalse(_service.isAllowed(createResource("/abc/xyz.jpg"), new WriteOperation()));
146 * Inserts a rule and checks it is inserted.
149 public void testInsertRule() {
151 assertFalse(_service.isAllowed(createResource("/janse/xyz.jpg"), new WriteOperation()));
152 _service.appendRule(createRule(GRANTED, "users", "/janse/", WriteOperation.class));
153 assertTrue(_service.isAllowed(createResource("/janse/xyz.jpg"), new WriteOperation()));
159 * Gets the rules. Verifies that all rules are obtained.
162 public void testGetRules() {
163 AuthorizationRule[] rules = _service.getRules();
164 assertEquals(3, rules.length);
168 * Verifies that when no rules match, access is denied.
171 public void testNoRulesSupportResource() {
172 assertFalse(_service.isAllowed(createResource("/xyxyxyxy"), new ReadOperation()));
173 checkMatchCount(0, _rule1);
174 checkMatchCount(0, _rule2);
175 checkMatchCount(0, _rule3);