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 static org.wamblee.security.authorization.AuthorizationResult.*;
19 import junit.framework.TestCase;
22 * Tests for the {@link org.wamblee.security.authorization.UrlAuthorizationRule}
25 * @author Erik Brakkee
27 public class UrlAuthorizationRuleTest extends TestCase {
29 private TestUserAccessor userAccessor;
32 userAccessor = new TestUserAccessor();
37 * Constructs the rule with a result of UNDECIDED. Verifies that an
38 * IllegalArgumentException is thrown.
40 public void testConstructWithUndecidedResult() {
42 new TestAuthorizationRule(UNDECIDED, "users", "/path",
45 } catch (IllegalArgumentException e) {
51 * Constructs the rule with a result of UNSUPPORTED_RESOURCE. Verifies that
52 * an IllegalArgumentException is thrown.
54 public void testConstructWithUnsupportedResult() {
56 new TestAuthorizationRule(UNSUPPORTED_RESOURCE, "users", "/path",
59 } catch (IllegalArgumentException e) {
65 * Constructs the authorization rule and applies it to an unsupported object
66 * type. Verifies that the result is UNSUPPORTED_RESOURCE.
68 public void testUnsupportedObject() {
69 AuthorizationRule rule = new TestAuthorizationRule(GRANTED, "users",
70 "/path", ReadOperation.class);
71 rule.setUserAdministration(userAccessor.getUserAdmin());
72 assertEquals(UNSUPPORTED_RESOURCE, rule.isAllowed("hello",
73 new ReadOperation(), userAccessor.getCurrentUser()));
76 public void testMatchingScenarios() {
77 AuthorizationRule rule = new TestAuthorizationRule(GRANTED, "users",
78 "/path/", ReadOperation.class);
79 rule.setUserAdministration(userAccessor.getUserAdmin());
80 String user = userAccessor.getCurrentUser();
83 assertEquals(GRANTED, rule.isAllowed(new TestResource("/path/a"),
84 new ReadOperation(), user));
85 assertEquals(GRANTED, rule.isAllowed(new TestResource("/path/"),
86 new ReadOperation(), user));
88 // path does not match.
89 assertEquals(UNDECIDED, rule.isAllowed(new TestResource("/path"),
90 new ReadOperation(), user));
92 // operation does not match.
93 assertEquals(UNDECIDED, rule.isAllowed(new TestResource("/path/"),
94 new WriteOperation(), user));
96 // group does not match.
97 AuthorizationRule rule2 = new TestAuthorizationRule(GRANTED, "users2",
98 "/path/", ReadOperation.class);
99 rule2.setUserAdministration(userAccessor.getUserAdmin());
100 assertEquals(UNDECIDED, rule2.isAllowed(new TestResource("/path/a"),
101 new ReadOperation(), user));