001    /**
002     * Copyright 2010 The Kuali Foundation Licensed under the
003     * Educational Community License, Version 2.0 (the "License"); you may
004     * not use this file except in compliance with the License. You may
005     * obtain a copy of the License at
006     *
007     * http://www.osedu.org/licenses/ECL-2.0
008     *
009     * Unless required by applicable law or agreed to in writing,
010     * software distributed under the License is distributed on an "AS IS"
011     * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
012     * or implied. See the License for the specific language governing
013     * permissions and limitations under the License.
014     */
015    
016    package org.kuali.rice.student.permission;
017    
018    import static org.junit.Assert.*;
019    
020    import org.junit.Ignore;
021    import org.junit.Test;
022    import org.kuali.rice.kew.api.WorkflowDocument;
023    import org.kuali.rice.kew.api.WorkflowDocumentFactory;
024    import org.kuali.rice.kew.api.action.ActionRequestType;
025    import org.kuali.rice.kew.api.document.DocumentStatus;
026    import org.kuali.rice.kim.api.KimConstants;
027    import org.kuali.rice.kim.api.permission.PermissionService;
028    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
029    import org.kuali.rice.student.StudentStandaloneTestBase;
030    
031    import java.util.HashMap;
032    import java.util.LinkedHashMap;
033    import java.util.Map;
034    
035    /**
036     * Test case to verify permissions work properly
037     *
038     * @author delyea
039     */
040    @Ignore
041    //FIXME(kscm-713)
042    public class BasicPermissionsTest extends StudentStandaloneTestBase {
043    
044            protected static final String PERMISSIONS_NAME_OPEN_DOCUMENT = "Open Document";
045            protected static final String PERMISSIONS_NAME_COMMENT_ON_DOCUMENT = "Comment on Document";
046            protected static final String PERMISSIONS_NAME_EDIT_DOCUMENT = "Edit Document";
047    
048            @Override
049            public void setUp() throws Exception {
050                    super.setUp();
051                    loadXmlFile("BasicPermissionsTestConfig.xml");
052            }
053    
054            protected void verifyPermissions(String principalId, String documentId, Map<String,Boolean> existingPermissions) {
055                    String permissionNamespace = "KS-CM";
056                    PermissionService permService = KimApiServiceLocator.getPermissionService();
057                    for (Map.Entry<String, Boolean> entry : existingPermissions.entrySet()) {
058                            if ( (entry.getValue() != null) && (entry.getValue().booleanValue()) ) {
059                                    Map<String,String> result = new LinkedHashMap<String,String> ();
060                                    result.put(KimConstants.AttributeConstants.DOCUMENT_NUMBER, ""+documentId);
061                                    assertTrue("Principal Id '" + principalId + "' should have permission '" + entry.getKey() + "'", permService.isAuthorized(principalId, permissionNamespace, entry.getKey(), result));
062                            }
063                            else {
064                                    Map<String,String> result = new LinkedHashMap<String,String> ();
065                                    result.put(KimConstants.AttributeConstants.DOCUMENT_NUMBER, ""+documentId);
066                                    assertFalse("Principal Id '" + principalId + "' should not have permission '" + entry.getKey() + "'", permService.isAuthorized(principalId, permissionNamespace, entry.getKey(), result));
067                            }
068                    }
069            }
070    
071            @Test public void testOpenPermission() throws Exception {
072                    String documentTypeName = "BasicPermissionsTestDocument";
073                    Map<String,Boolean> hasPermissionByPermissionName = new HashMap<String,Boolean>();
074    
075                    String principalId = "testuser1";
076                    WorkflowDocument doc = WorkflowDocumentFactory.createDocument(principalId, documentTypeName);
077                    doc.saveDocument("");
078    
079                    // verify testuser1 has correct permissions as initiator
080                    principalId = "testuser1";
081                    doc = WorkflowDocumentFactory.createDocument(principalId, doc.getDocumentId());
082                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_OPEN_DOCUMENT, Boolean.TRUE);
083                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_COMMENT_ON_DOCUMENT, Boolean.FALSE);
084                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_EDIT_DOCUMENT, Boolean.FALSE);
085                    verifyPermissions(principalId, ""+doc.getDocumentId(), hasPermissionByPermissionName);
086    
087                    // send adhoc approve to 'testuser3'
088                    doc.adHocToPrincipal(ActionRequestType.APPROVE, "", "testuser3", "", true);
089    
090                    // verify testuser2 has no permissions
091                    principalId = "testuser2";
092                    doc = WorkflowDocumentFactory.createDocument(principalId, doc.getDocumentId());
093                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_OPEN_DOCUMENT, Boolean.FALSE);
094                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_COMMENT_ON_DOCUMENT, Boolean.FALSE);
095                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_EDIT_DOCUMENT, Boolean.FALSE);
096                    verifyPermissions(principalId, ""+doc.getDocumentId(), hasPermissionByPermissionName);
097    
098                    // verify testuser3 has no permissions
099                    principalId = "testuser3";
100                    doc = WorkflowDocumentFactory.createDocument(principalId, doc.getDocumentId());
101                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_OPEN_DOCUMENT, Boolean.TRUE);
102                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_COMMENT_ON_DOCUMENT, Boolean.TRUE);
103                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_EDIT_DOCUMENT, Boolean.TRUE);
104                    verifyPermissions(principalId, ""+doc.getDocumentId(), hasPermissionByPermissionName);
105    
106                    doc = WorkflowDocumentFactory.createDocument(principalId, doc.getDocumentId());
107                    doc.route("");
108    
109                    // verify testuser1 has correct permissions as initiator
110                    principalId = "testuser1";
111                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_OPEN_DOCUMENT, Boolean.TRUE);
112                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_COMMENT_ON_DOCUMENT, Boolean.FALSE);
113                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_EDIT_DOCUMENT, Boolean.FALSE);
114                    verifyPermissions(principalId, ""+doc.getDocumentId(), hasPermissionByPermissionName);
115    
116                    // verify testuser3 has correct permissions as router
117                    principalId = "testuser3";
118                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_OPEN_DOCUMENT, Boolean.TRUE);
119                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_COMMENT_ON_DOCUMENT, Boolean.FALSE);
120                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_EDIT_DOCUMENT, Boolean.FALSE);
121                    verifyPermissions(principalId, ""+doc.getDocumentId(), hasPermissionByPermissionName);
122    
123                    // verify fred has request for approval and correct permissions
124                    principalId = "fred";
125                    doc = WorkflowDocumentFactory.createDocument(principalId, doc.getDocumentId());
126                    assertTrue("Approval should be requested of user '" + principalId + "'", doc.isApprovalRequested());
127                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_OPEN_DOCUMENT, Boolean.TRUE);
128                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_COMMENT_ON_DOCUMENT, Boolean.TRUE);
129                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_EDIT_DOCUMENT, Boolean.TRUE);
130                    verifyPermissions(principalId, ""+doc.getDocumentId(), hasPermissionByPermissionName);
131    
132                    // verify doug has request for approval and correct permissions
133                    principalId = "doug";
134                    doc = WorkflowDocumentFactory.createDocument(principalId, doc.getDocumentId());
135                    assertTrue("FYI should be requested of user '" + principalId + "'", doc.isFYIRequested());
136                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_OPEN_DOCUMENT, Boolean.TRUE);
137                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_COMMENT_ON_DOCUMENT, Boolean.TRUE);
138                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_EDIT_DOCUMENT, Boolean.FALSE);
139                    verifyPermissions(principalId, ""+doc.getDocumentId(), hasPermissionByPermissionName);
140    
141                    // appprove the document as fred and re-verify his and doug's permissions
142                    principalId = "fred";
143                    doc = WorkflowDocumentFactory.createDocument(principalId, doc.getDocumentId());
144                    doc.approve("");
145    
146                    // verify fred has no request for approval and correct permissions
147                    principalId = "fred";
148                    doc = WorkflowDocumentFactory.createDocument(principalId, doc.getDocumentId());
149                    assertFalse("Approval should be requested of user '" + principalId + "'", doc.isApprovalRequested());
150                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_OPEN_DOCUMENT, Boolean.TRUE);
151                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_COMMENT_ON_DOCUMENT, Boolean.FALSE);
152                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_EDIT_DOCUMENT, Boolean.FALSE);
153                    verifyPermissions(principalId, ""+doc.getDocumentId(), hasPermissionByPermissionName);
154    
155                    // verify doug still has request for FYI and correct permissions
156                    principalId = "doug";
157                    doc = WorkflowDocumentFactory.createDocument(principalId, doc.getDocumentId());
158                    assertTrue("FYI should be requested of user '" + principalId + "'", doc.isFYIRequested());
159                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_OPEN_DOCUMENT, Boolean.TRUE);
160                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_COMMENT_ON_DOCUMENT, Boolean.TRUE);
161                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_EDIT_DOCUMENT, Boolean.FALSE);
162                    verifyPermissions(principalId, ""+doc.getDocumentId(), hasPermissionByPermissionName);
163    
164                    // verify edna has request for Acknoweldge and correct permissions
165                    principalId = "edna";
166                    doc = WorkflowDocumentFactory.createDocument(principalId, doc.getDocumentId());
167                    assertTrue("Acknowledge should be requested of user '" + principalId + "'", doc.isAcknowledgeRequested());
168                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_OPEN_DOCUMENT, Boolean.TRUE);
169                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_COMMENT_ON_DOCUMENT, Boolean.TRUE);
170                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_EDIT_DOCUMENT, Boolean.FALSE);
171                    verifyPermissions(principalId, ""+doc.getDocumentId(), hasPermissionByPermissionName);
172    
173                    // route the document to PROCESSED
174                    principalId = "fran";
175                    doc = WorkflowDocumentFactory.createDocument(principalId, doc.getDocumentId());
176                    doc.approve("");
177                    principalId = "user1";
178                    doc = WorkflowDocumentFactory.createDocument(principalId, doc.getDocumentId());
179                    doc.approve("");
180    
181                    // verify edna still has request for Acknoweldge and correct permissions
182                    principalId = "edna";
183                    doc = WorkflowDocumentFactory.createDocument(principalId, doc.getDocumentId());
184                    assertEquals (doc.getStatus(), DocumentStatus.PROCESSED);
185                    assertTrue("Acknowledge should be requested of user '" + principalId + "'", doc.isAcknowledgeRequested());
186                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_OPEN_DOCUMENT, Boolean.TRUE);
187                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_COMMENT_ON_DOCUMENT, Boolean.TRUE);
188                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_EDIT_DOCUMENT, Boolean.FALSE);
189                    verifyPermissions(principalId, ""+doc.getDocumentId(), hasPermissionByPermissionName);
190    
191                    // move document to FINAL
192                    doc.acknowledge("");
193    
194                    // verify edna has no request and correct permissions
195                    principalId = "edna";
196                    doc = WorkflowDocumentFactory.createDocument(principalId, doc.getDocumentId());
197                    assertEquals (doc.getStatus(), DocumentStatus.FINAL);
198                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_OPEN_DOCUMENT, Boolean.TRUE);
199                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_COMMENT_ON_DOCUMENT, Boolean.FALSE);
200                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_EDIT_DOCUMENT, Boolean.FALSE);
201                    verifyPermissions(principalId, ""+doc.getDocumentId(), hasPermissionByPermissionName);
202    
203                    // verify doug still has request for FYI and correct permissions
204                    principalId = "doug";
205                    doc = WorkflowDocumentFactory.createDocument(principalId, doc.getDocumentId());
206                    assertTrue("FYI should be requested of user '" + principalId + "'", doc.isFYIRequested());
207                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_OPEN_DOCUMENT, Boolean.TRUE);
208                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_COMMENT_ON_DOCUMENT, Boolean.TRUE);
209                    hasPermissionByPermissionName.put(PERMISSIONS_NAME_EDIT_DOCUMENT, Boolean.FALSE);
210                    verifyPermissions(principalId, ""+doc.getDocumentId(), hasPermissionByPermissionName);
211    
212            }
213    }