View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  /**
17   * 
18   */
19  package org.kuali.rice.student.permission;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import org.junit.Test;
25  import org.kuali.rice.kew.service.WorkflowDocument;
26  import org.kuali.rice.kew.util.KEWConstants;
27  import org.kuali.rice.kim.bo.impl.KimAttributes;
28  import org.kuali.rice.kim.bo.types.dto.AttributeSet;
29  import org.kuali.rice.kim.service.KIMServiceLocator;
30  import org.kuali.rice.kim.service.PermissionService;
31  import org.kuali.rice.student.StudentStandaloneTestBase;
32  
33  /**
34   * Test case to verify permissions work properly
35   * 
36   * @author delyea
37   */
38  public class BasicPermissionsTest extends StudentStandaloneTestBase {
39  
40  	protected static final String PERMISSIONS_NAME_OPEN_DOCUMENT = "Open Document";
41  	protected static final String PERMISSIONS_NAME_COMMENT_ON_DOCUMENT = "Comment on Document";
42  	protected static final String PERMISSIONS_NAME_EDIT_DOCUMENT = "Edit Document";
43  
44  	@Override
45  	public void setUp() throws Exception {
46  		super.setUp();
47  		loadXmlFile("BasicPermissionsTestConfig.xml");
48  	}
49  
50  	protected void verifyPermissions(String principalId, String documentId, Map<String,Boolean> existingPermissions) {
51  		String permissionNamespace = "KS-LUM";
52  		PermissionService permService = KIMServiceLocator.getPermissionService();
53  		for (Map.Entry<String, Boolean> entry : existingPermissions.entrySet()) {
54  			if ( (entry.getValue() != null) && (entry.getValue().booleanValue()) ) {
55  				assertTrue("Principal Id '" + principalId + "' should have permission '" + entry.getKey() + "'", permService.isAuthorized(principalId, permissionNamespace, entry.getKey(), null, new AttributeSet(KimAttributes.DOCUMENT_NUMBER, ""+documentId)));
56  			}
57  			else {
58  				assertFalse("Principal Id '" + principalId + "' should not have permission '" + entry.getKey() + "'", permService.isAuthorized(principalId, permissionNamespace, entry.getKey(), null, new AttributeSet(KimAttributes.DOCUMENT_NUMBER, ""+documentId)));
59  			}
60  		}
61  	}
62  
63  	@Test public void testOpenPermission() throws Exception {
64  		String documentTypeName = "BasicPermissionsTestDocument";
65  		Map<String,Boolean> hasPermissionByPermissionName = new HashMap<String,Boolean>();
66  
67  		String principalId = "testuser1";
68  		WorkflowDocument doc = new WorkflowDocument(principalId, documentTypeName);
69  		doc.saveDocument("");
70  
71  		// verify testuser1 has correct permissions as initiator
72  		principalId = "testuser1";
73  		doc = new WorkflowDocument(principalId, doc.getRouteHeaderId());
74  		hasPermissionByPermissionName.put(PERMISSIONS_NAME_OPEN_DOCUMENT, Boolean.TRUE);
75  		hasPermissionByPermissionName.put(PERMISSIONS_NAME_COMMENT_ON_DOCUMENT, Boolean.FALSE);
76  		hasPermissionByPermissionName.put(PERMISSIONS_NAME_EDIT_DOCUMENT, Boolean.FALSE);
77  		verifyPermissions(principalId, ""+doc.getRouteHeaderId(), hasPermissionByPermissionName);
78  
79  		// send adhoc approve to 'testuser3'
80  		doc.adHocRouteDocumentToPrincipal(KEWConstants.ACTION_REQUEST_APPROVE_REQ, "", "testuser3", "", true);
81  		
82  		// verify testuser2 has no permissions
83  		principalId = "testuser2";
84  		doc = new WorkflowDocument(principalId, doc.getRouteHeaderId());
85  		hasPermissionByPermissionName.put(PERMISSIONS_NAME_OPEN_DOCUMENT, Boolean.FALSE);
86  		hasPermissionByPermissionName.put(PERMISSIONS_NAME_COMMENT_ON_DOCUMENT, Boolean.FALSE);
87  		hasPermissionByPermissionName.put(PERMISSIONS_NAME_EDIT_DOCUMENT, Boolean.FALSE);
88  		verifyPermissions(principalId, ""+doc.getRouteHeaderId(), hasPermissionByPermissionName);
89  
90  		// verify testuser3 has no permissions
91  		principalId = "testuser3";
92  		doc = new WorkflowDocument(principalId, doc.getRouteHeaderId());
93  		hasPermissionByPermissionName.put(PERMISSIONS_NAME_OPEN_DOCUMENT, Boolean.TRUE);
94  		hasPermissionByPermissionName.put(PERMISSIONS_NAME_COMMENT_ON_DOCUMENT, Boolean.TRUE);
95  		hasPermissionByPermissionName.put(PERMISSIONS_NAME_EDIT_DOCUMENT, Boolean.TRUE);
96  		verifyPermissions(principalId, ""+doc.getRouteHeaderId(), hasPermissionByPermissionName);
97  
98  		doc = new WorkflowDocument(principalId, doc.getRouteHeaderId());
99  		doc.routeDocument("");
100 		
101 		// verify testuser1 has correct permissions as initiator
102 		principalId = "testuser1";
103 		hasPermissionByPermissionName.put(PERMISSIONS_NAME_OPEN_DOCUMENT, Boolean.TRUE);
104 		hasPermissionByPermissionName.put(PERMISSIONS_NAME_COMMENT_ON_DOCUMENT, Boolean.FALSE);
105 		hasPermissionByPermissionName.put(PERMISSIONS_NAME_EDIT_DOCUMENT, Boolean.FALSE);
106 		verifyPermissions(principalId, ""+doc.getRouteHeaderId(), hasPermissionByPermissionName);
107 		
108 		// verify testuser3 has correct permissions as router
109 		principalId = "testuser3";
110 		hasPermissionByPermissionName.put(PERMISSIONS_NAME_OPEN_DOCUMENT, Boolean.TRUE);
111 		hasPermissionByPermissionName.put(PERMISSIONS_NAME_COMMENT_ON_DOCUMENT, Boolean.FALSE);
112 		hasPermissionByPermissionName.put(PERMISSIONS_NAME_EDIT_DOCUMENT, Boolean.FALSE);
113 		verifyPermissions(principalId, ""+doc.getRouteHeaderId(), hasPermissionByPermissionName);
114 		
115 		// verify fred has request for approval and correct permissions
116 		principalId = "fred";
117 		doc = new WorkflowDocument(principalId, doc.getRouteHeaderId());
118 		assertTrue("Approval should be requested of user '" + principalId + "'", doc.isApprovalRequested());
119 		hasPermissionByPermissionName.put(PERMISSIONS_NAME_OPEN_DOCUMENT, Boolean.TRUE);
120 		hasPermissionByPermissionName.put(PERMISSIONS_NAME_COMMENT_ON_DOCUMENT, Boolean.TRUE);
121 		hasPermissionByPermissionName.put(PERMISSIONS_NAME_EDIT_DOCUMENT, Boolean.TRUE);
122 		verifyPermissions(principalId, ""+doc.getRouteHeaderId(), hasPermissionByPermissionName);
123 
124 		// verify doug has request for approval and correct permissions
125 		principalId = "doug";
126 		doc = new WorkflowDocument(principalId, doc.getRouteHeaderId());
127 		assertTrue("FYI should be requested of user '" + principalId + "'", doc.isFYIRequested());
128 		hasPermissionByPermissionName.put(PERMISSIONS_NAME_OPEN_DOCUMENT, Boolean.TRUE);
129 		hasPermissionByPermissionName.put(PERMISSIONS_NAME_COMMENT_ON_DOCUMENT, Boolean.TRUE);
130 		hasPermissionByPermissionName.put(PERMISSIONS_NAME_EDIT_DOCUMENT, Boolean.FALSE);
131 		verifyPermissions(principalId, ""+doc.getRouteHeaderId(), hasPermissionByPermissionName);
132 		
133 		// appprove the document as fred and re-verify his and doug's permissions
134 		principalId = "fred";
135 		doc = new WorkflowDocument(principalId, doc.getRouteHeaderId());
136 		doc.approve("");
137 
138 		// verify fred has no request for approval and correct permissions
139 		principalId = "fred";
140 		doc = new WorkflowDocument(principalId, doc.getRouteHeaderId());
141 		assertFalse("Approval should be requested of user '" + principalId + "'", doc.isApprovalRequested());
142 		hasPermissionByPermissionName.put(PERMISSIONS_NAME_OPEN_DOCUMENT, Boolean.TRUE);
143 		hasPermissionByPermissionName.put(PERMISSIONS_NAME_COMMENT_ON_DOCUMENT, Boolean.FALSE);
144 		hasPermissionByPermissionName.put(PERMISSIONS_NAME_EDIT_DOCUMENT, Boolean.FALSE);
145 		verifyPermissions(principalId, ""+doc.getRouteHeaderId(), hasPermissionByPermissionName);
146 
147 		// verify doug still has request for FYI and correct permissions
148 		principalId = "doug";
149 		doc = new WorkflowDocument(principalId, doc.getRouteHeaderId());
150 		assertTrue("FYI should be requested of user '" + principalId + "'", doc.isFYIRequested());
151 		hasPermissionByPermissionName.put(PERMISSIONS_NAME_OPEN_DOCUMENT, Boolean.TRUE);
152 		hasPermissionByPermissionName.put(PERMISSIONS_NAME_COMMENT_ON_DOCUMENT, Boolean.TRUE);
153 		hasPermissionByPermissionName.put(PERMISSIONS_NAME_EDIT_DOCUMENT, Boolean.FALSE);
154 		verifyPermissions(principalId, ""+doc.getRouteHeaderId(), hasPermissionByPermissionName);
155 		
156 		// verify edna has request for Acknoweldge and correct permissions
157 		principalId = "edna";
158 		doc = new WorkflowDocument(principalId, doc.getRouteHeaderId());
159 		assertTrue("Acknowledge should be requested of user '" + principalId + "'", doc.isAcknowledgeRequested());
160 		hasPermissionByPermissionName.put(PERMISSIONS_NAME_OPEN_DOCUMENT, Boolean.TRUE);
161 		hasPermissionByPermissionName.put(PERMISSIONS_NAME_COMMENT_ON_DOCUMENT, Boolean.TRUE);
162 		hasPermissionByPermissionName.put(PERMISSIONS_NAME_EDIT_DOCUMENT, Boolean.FALSE);
163 		verifyPermissions(principalId, ""+doc.getRouteHeaderId(), hasPermissionByPermissionName);
164 
165 		// route the document to PROCESSED
166 		principalId = "fran";
167 		doc = new WorkflowDocument(principalId, doc.getRouteHeaderId());
168 		doc.approve("");
169 		principalId = "user1";
170 		doc = new WorkflowDocument(principalId, doc.getRouteHeaderId());
171 		doc.approve("");
172 
173 		// verify edna still has request for Acknoweldge and correct permissions
174 		principalId = "edna";
175 		doc = new WorkflowDocument(principalId, doc.getRouteHeaderId());
176 		assertTrue("", doc.stateIsProcessed());
177 		assertTrue("Acknowledge should be requested of user '" + principalId + "'", doc.isAcknowledgeRequested());
178 		hasPermissionByPermissionName.put(PERMISSIONS_NAME_OPEN_DOCUMENT, Boolean.TRUE);
179 		hasPermissionByPermissionName.put(PERMISSIONS_NAME_COMMENT_ON_DOCUMENT, Boolean.TRUE);
180 		hasPermissionByPermissionName.put(PERMISSIONS_NAME_EDIT_DOCUMENT, Boolean.FALSE);
181 		verifyPermissions(principalId, ""+doc.getRouteHeaderId(), hasPermissionByPermissionName);
182 
183 		// move document to FINAL
184 		doc.acknowledge("");
185 
186 		// verify edna has no request and correct permissions
187 		principalId = "edna";
188 		doc = new WorkflowDocument(principalId, doc.getRouteHeaderId());
189 		assertTrue("Doc should be FINAL", doc.stateIsFinal());
190 		hasPermissionByPermissionName.put(PERMISSIONS_NAME_OPEN_DOCUMENT, Boolean.TRUE);
191 		hasPermissionByPermissionName.put(PERMISSIONS_NAME_COMMENT_ON_DOCUMENT, Boolean.FALSE);
192 		hasPermissionByPermissionName.put(PERMISSIONS_NAME_EDIT_DOCUMENT, Boolean.FALSE);
193 		verifyPermissions(principalId, ""+doc.getRouteHeaderId(), hasPermissionByPermissionName);
194 
195 		// verify doug still has request for FYI and correct permissions
196 		principalId = "doug";
197 		doc = new WorkflowDocument(principalId, doc.getRouteHeaderId());
198 		assertTrue("FYI should be requested of user '" + principalId + "'", doc.isFYIRequested());
199 		hasPermissionByPermissionName.put(PERMISSIONS_NAME_OPEN_DOCUMENT, Boolean.TRUE);
200 		hasPermissionByPermissionName.put(PERMISSIONS_NAME_COMMENT_ON_DOCUMENT, Boolean.TRUE);
201 		hasPermissionByPermissionName.put(PERMISSIONS_NAME_EDIT_DOCUMENT, Boolean.FALSE);
202 		verifyPermissions(principalId, ""+doc.getRouteHeaderId(), hasPermissionByPermissionName);
203 
204 	}
205 }