001: /*
002: * Copyright 2006-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.core.document;
017:
018: import java.util.LinkedHashMap;
019:
020: import org.junit.Test;
021: import org.kuali.RicePropertyConstants;
022: import org.kuali.core.document.authorization.DocumentActionFlags;
023: import org.kuali.core.document.authorization.DocumentAuthorizer;
024: import org.kuali.core.document.authorization.DocumentAuthorizerBase;
025: import org.kuali.test.KNSTestBase;
026: import org.kuali.test.KNSWithTestSpringContext;
027:
028: @KNSWithTestSpringContext
029: public class DocumentAuthorizerBaseTest extends KNSTestBase {
030:
031: private static final String SUPERVISOR_USER = "ABNEY";
032: private static final String SUPERVISOR_UNIVERSAL = "2237202707";
033: private static final String NONSUPER_USER = "BARTH";
034: private static final String NONSUPER_UNIVERSAL = "5998202207";
035:
036: private DocumentAuthorizer documentAuthorizer;
037:
038: @Override
039: public void setUp() throws Exception {
040: super .setUp();
041: documentAuthorizer = new DocumentAuthorizerBase();
042: }
043:
044: // following is the Supervisor & Initiator grid
045:
046: // ## Supervisor? UserIsInitiator? ApprovalRequested? canSupervise Result
047: // -----------------------------------------------------------------------------------
048: // A true true true true
049: //
050: // B true true false false
051: //
052: // C true false true true
053: //
054: // D true false false true
055: //
056: // E false * * false
057: //
058: @Test
059: public void testCanSuperviseAsInitiatorA() {
060:
061: DocumentActionFlags flags;
062: Document document;
063:
064: // scenario A
065: document = new MockDocument(getSuperUser(), true);
066: flags = documentAuthorizer.getDocumentActionFlags(document,
067: getSuperUser());
068: assertTrue(flags.getCanSupervise());
069:
070: // scenario B
071: document = new MockDocument(getSuperUser(), false);
072: flags = documentAuthorizer.getDocumentActionFlags(document,
073: getSuperUser());
074: assertFalse(flags.getCanSupervise());
075:
076: // scenario C
077: document = new MockDocument(getNonSuperUser(), true);
078: flags = documentAuthorizer.getDocumentActionFlags(document,
079: getSuperUser());
080: assertTrue(flags.getCanSupervise());
081:
082: // scenario D
083: document = new MockDocument(getNonSuperUser(), false);
084: flags = documentAuthorizer.getDocumentActionFlags(document,
085: getSuperUser());
086: assertTrue(flags.getCanSupervise());
087:
088: }
089:
090: private UniversalUser getSuperUser() {
091: return new UniversalUser(SUPERVISOR_UNIVERSAL, SUPERVISOR_USER,
092: true);
093: }
094:
095: private UniversalUser getNonSuperUser() {
096: return new UniversalUser(NONSUPER_UNIVERSAL, NONSUPER_USER,
097: false);
098: }
099:
100: private class MockDocument extends DocumentBase {
101:
102: private MockDocument() {
103: super ();
104: }
105:
106: public MockDocument(UniversalUser initiator,
107: boolean isApprovalRequested) {
108: this ();
109: this .documentNumber = "1234567890";
110: this .documentHeader
111: .setWorkflowDocument(new MockWorkflowDocument(
112: initiator, isApprovalRequested));
113: }
114:
115: @Override
116: protected LinkedHashMap toStringMapper() {
117: LinkedHashMap map = new LinkedHashMap();
118: map.put("class", "MockDocument");
119: map.put(RicePropertyConstants.DOCUMENT_NUMBER,
120: documentNumber);
121: map.put("initiator", documentHeader.getWorkflowDocument()
122: .getInitiatorNetworkId());
123: return map;
124: }
125:
126: public boolean getAllowsCopy() {
127: return false;
128: }
129:
130: }
131:
132: private class MockWorkflowDocument extends
133: org.kuali.core.impls.MockWorkflowDocument {
134:
135: private UniversalUser initiator;
136: private boolean approvalRequested;
137:
138: private MockWorkflowDocument() {
139: };
140:
141: public MockWorkflowDocument(UniversalUser initiator,
142: boolean isApprovalRequested) {
143: this .initiator = initiator;
144: this .approvalRequested = isApprovalRequested;
145: }
146:
147: public String getInitiatorNetworkId() {
148: return initiator.getPersonUserIdentifier();
149: }
150:
151: public boolean isApprovalRequested() {
152: return approvalRequested;
153: }
154:
155: public boolean userIsInitiator(
156: org.kuali.core.bo.user.UniversalUser user) {
157: return initiator.getPersonUniversalIdentifier()
158: .equalsIgnoreCase(
159: user.getPersonUniversalIdentifier());
160: }
161:
162: }
163:
164: private class UniversalUser extends
165: org.kuali.core.bo.user.UniversalUser {
166: private boolean super visor;
167:
168: private UniversalUser() {
169: };
170:
171: public UniversalUser(String universalId, String userId,
172: boolean super visor) {
173: this .setPersonUniversalIdentifier(universalId);
174: this .setPersonUserIdentifier(userId);
175: this .super visor = super visor;
176: }
177:
178: @Override
179: public boolean isSupervisorUser() {
180: return supervisor;
181: }
182: }
183: }
|