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.usermgt.User;
26 * Tests for the {@link org.wamblee.security.authorization.UrlAuthorizationRule}
29 * @author Erik Brakkee
31 public class UrlAuthorizationRuleTest extends TestCase {
33 * Constructs the rule with a result of UNDECIDED. Verifies that an
34 * IllegalArgumentException is thrown.
36 public void testConstructWithUndecidedResult() {
38 new TestAuthorizationRule(UNDECIDED, "users", "/path",
41 } catch (IllegalArgumentException e) {
47 * Constructs the rule with a result of UNSUPPORTED_RESOURCE. Verifies that
48 * an IllegalArgumentException is thrown.
50 public void testConstructWithUnsupportedResult() {
52 new TestAuthorizationRule(UNSUPPORTED_RESOURCE, "users", "/path",
55 } catch (IllegalArgumentException e) {
61 * Constructs the authorization rule and applies it to an unsupported object
62 * type. Verifies that the result is UNSUPPORTED_RESOURCE.
64 public void testUnsupportedObject() {
65 AuthorizationRule rule = new TestAuthorizationRule(GRANTED, "users",
66 "/path", ReadOperation.class);
67 assertEquals(UNSUPPORTED_RESOURCE, rule.isAllowed("hello",
68 new ReadOperation(), new TestUserAccessor().getCurrentUser()));
71 public void testMatchingScenarios() {
72 AuthorizationRule rule = new TestAuthorizationRule(GRANTED, "users",
73 "/path/", ReadOperation.class);
74 User user = new TestUserAccessor().getCurrentUser();
77 assertEquals(GRANTED, rule.isAllowed(new TestResource("/path/a"),
78 new ReadOperation(), user));
79 assertEquals(GRANTED, rule.isAllowed(new TestResource("/path/"),
80 new ReadOperation(), user));
82 // path does not match.
83 assertEquals(UNDECIDED, rule.isAllowed(new TestResource("/path"),
84 new ReadOperation(), user));
86 // operation does not match.
87 assertEquals(UNDECIDED, rule.isAllowed(new TestResource("/path/"),
88 new WriteOperation(), user));
90 // group does not match.
91 AuthorizationRule rule2 = new TestAuthorizationRule(GRANTED, "users2",
92 "/path/", ReadOperation.class);
93 assertEquals(UNDECIDED, rule2.isAllowed(new TestResource("/path/a"),
94 new ReadOperation(), user));