This repository has been archived by the owner on May 9, 2020. It is now read-only.
/
AuthorizationTests.java
113 lines (94 loc) · 5.81 KB
/
AuthorizationTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*******************************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*******************************************************************************/
package org.ofbiz.securityext.test;
import java.util.Map;
import org.ofbiz.base.util.Debug;
import org.ofbiz.security.SecurityConfigurationException;
import org.ofbiz.security.authz.AbstractAuthorization;
import org.ofbiz.security.authz.Authorization;
import org.ofbiz.security.authz.AuthorizationFactory;
import org.ofbiz.service.testtools.OFBizTestCase;
public class AuthorizationTests extends OFBizTestCase {
private static final String module = AuthorizationTests.class.getName();
protected Authorization security = null;
public AuthorizationTests(String name) {
super(name);
}
@Override
public void setUp() throws SecurityConfigurationException {
if (security == null) {
security = AuthorizationFactory.getInstance(delegator);
}
AbstractAuthorization.clearThreadLocal();
}
public void testBasicAdminPermission() throws Exception {
Debug.logInfo("Running testBasicAdminPermission()", module);
assertTrue("User was not granted permission as expected", security.hasPermission("system", "access:foo:bar", null));
}
public void testBasePermissionFailure() throws Exception {
Debug.logInfo("Running testBasePermissionFailure()", module);
assertFalse("Permission did not fail as expected", security.hasPermission("system", "no:permission", null));
}
public void testDynamicAccessFromClasspath() throws Exception {
Debug.logInfo("Running testDynamicAccessFromClasspath()", module);
assertTrue("User was not granted dynamic access as expected", security.hasPermission("system", "test:groovy2:2000", null));
}
public void testDynamicAccessService() throws Exception {
Debug.logInfo("Running testDynamicAccessService()", module);
assertTrue("User was not granted dynamic access as expected", security.hasPermission("system", "test:service:2000", null));
}
public void testDynamicAccessFailure() throws Exception {
Debug.logInfo("Running testDynamicAccessFailure()", module);
assertFalse("Dynamic access did not fail as expected", security.hasPermission("system", "test:groovy1:2000", null));
}
public void testAutoGrantPermissions() throws Exception {
Debug.logInfo("Running testDynamicAccessFailure()", module);
// first verify the user does not have the initial permission
assertFalse("User already has the auto-granted permission", security.hasPermission("system", "test:autogranted", null));
// next run security check to setup the auto-grant
assertTrue("User was not granted dynamic access as expected", security.hasPermission("system", "test:groovy1:1000", null));
// as long as this runs in the same thread (and it should) access should now be granted
assertTrue("User was not auto-granted expected permission", security.hasPermission("system", "test:autogranted", null));
}
public void testAutoGrantCleanup() throws Exception {
Debug.logInfo("Running testAutoGrantCleanup()", module);
assertFalse("User was auto-granted an unexpected permission", security.hasPermission("user", "test:autogranted", null));
}
public void testDynamicAccessRecursion() throws Exception {
Debug.logInfo("Running testDynamicAccessRecursion()", module);
assertFalse("User was granted an unexpected permission", security.hasPermission("user", "test:recursion", null));
}
public void testFindAllPermissionRegexp() throws Exception {
Debug.logInfo("Running testFindAllPermissionRegexp()", module);
Map<String,Boolean> permResultMap = security.findMatchingPermission("system", ".*:example", null);
assertEquals("Invalid result map size; should be 5", 5, permResultMap.size());
assertTrue("User was not granted expected permission {access:example}", permResultMap.get("access:example"));
assertTrue("User was not granted expected permission {create:example}", permResultMap.get("create:example"));
assertTrue("User was not granted expected permission {read:example}", permResultMap.get("read:example"));
assertTrue("User was not granted expected permission {update:example}", permResultMap.get("update:example"));
assertTrue("User was not granted expected permission {delete:example}", permResultMap.get("delete:example"));
}
public void testFindLimitedPermissionRegexp() throws Exception {
Debug.logInfo("Running testFindLimitedPermissionRegexp()", module);
Map<String,Boolean> permResultMap = security.findMatchingPermission("user", "(access|read):example", null);
assertEquals("Invalid result map size; should be 2", 2, permResultMap.size());
assertFalse("User was granted an unexpected permission {access:example}", permResultMap.get("access:example"));
assertFalse("User was granted an unexpected permission {read:example}", permResultMap.get("read:example"));
}
}