2 * Copyright 2005-2010 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.
16 package org.wamblee.security.authorization;
18 import junit.framework.TestCase;
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;
23 import org.wamblee.security.authentication.User;
26 * Tests for the {@link org.wamblee.security.authorization.UrlAuthorizationRule}
29 * @author Erik Brakkee
31 public class UrlAuthorizationRuleTest extends TestCase {
33 private TestUserAccessor userAccessor;
36 userAccessor = new TestUserAccessor();
41 * Constructs the rule with a result of UNDECIDED. Verifies that an
42 * IllegalArgumentException is thrown.
44 public void testConstructWithUndecidedResult() {
46 new TestAuthorizationRule(UNDECIDED, "users", "/path",
49 } catch (IllegalArgumentException e) {
55 * Constructs the rule with a result of UNSUPPORTED_RESOURCE. Verifies that
56 * an IllegalArgumentException is thrown.
58 public void testConstructWithUnsupportedResult() {
60 new TestAuthorizationRule(UNSUPPORTED_RESOURCE, "users", "/path",
63 } catch (IllegalArgumentException e) {
69 * Constructs the authorization rule and applies it to an unsupported object
70 * type. Verifies that the result is UNSUPPORTED_RESOURCE.
72 public void testUnsupportedObject() {
73 AuthorizationRule rule = new TestAuthorizationRule(GRANTED, "users",
74 "/path", ReadOperation.class);
75 rule.setUserAdministration(userAccessor.getUserAdmin());
76 assertEquals(UNSUPPORTED_RESOURCE, rule.isAllowed("hello",
77 new ReadOperation(), userAccessor.getCurrentUser()));
80 public void testMatchingScenarios() {
81 AuthorizationRule rule = new TestAuthorizationRule(GRANTED, "users",
82 "/path/", ReadOperation.class);
83 rule.setUserAdministration(userAccessor.getUserAdmin());
84 String user = userAccessor.getCurrentUser();
87 assertEquals(GRANTED, rule.isAllowed(new TestResource("/path/a"),
88 new ReadOperation(), user));
89 assertEquals(GRANTED, rule.isAllowed(new TestResource("/path/"),
90 new ReadOperation(), user));
92 // path does not match.
93 assertEquals(UNDECIDED, rule.isAllowed(new TestResource("/path"),
94 new ReadOperation(), user));
96 // operation does not match.
97 assertEquals(UNDECIDED, rule.isAllowed(new TestResource("/path/"),
98 new WriteOperation(), user));
100 // group does not match.
101 AuthorizationRule rule2 = new TestAuthorizationRule(GRANTED, "users2",
102 "/path/", ReadOperation.class);
103 rule2.setUserAdministration(userAccessor.getUserAdmin());
104 assertEquals(UNDECIDED, rule2.isAllowed(new TestResource("/path/a"),
105 new ReadOperation(), user));