001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sections/tags/sakai_2-4-1/sections-app/src/test/org/sakaiproject/test/section/AuthzTest.java $
003: * $Id: AuthzTest.java 18134 2006-11-14 18:59:25Z jholtzman@berkeley.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 The Regents of the University of California and The Regents of the University of Michigan
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.test.section;
021:
022: import java.util.List;
023:
024: import junit.framework.Assert;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.sakaiproject.section.api.CourseManager;
029: import org.sakaiproject.section.api.SectionManager;
030: import org.sakaiproject.section.api.coursemanagement.Course;
031: import org.sakaiproject.section.api.coursemanagement.CourseSection;
032: import org.sakaiproject.section.api.coursemanagement.User;
033: import org.sakaiproject.section.api.facade.Role;
034: import org.sakaiproject.section.api.facade.manager.Authz;
035: import org.sakaiproject.section.api.facade.manager.Context;
036: import org.sakaiproject.component.section.support.UserManager;
037:
038: /**
039: * Each test method is isolated in its own transaction, which is rolled back when
040: * the method exits. Since we can not assume that data will exist, we need to use
041: * the SectionManager api to insert data before retrieving it with SectionAwareness.
042: *
043: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman</a>
044: *
045: */
046: public class AuthzTest extends SectionsTestBase {
047: private static final Log log = LogFactory.getLog(AuthzTest.class);
048:
049: private Authz authz;
050: private Context context;
051: private SectionManager secMgr;
052: private CourseManager courseMgr;
053: private UserManager userMgr;
054:
055: protected void onSetUpInTransaction() throws Exception {
056: authz = (Authz) applicationContext
057: .getBean("org.sakaiproject.section.api.facade.manager.Authz");
058: context = (Context) applicationContext
059: .getBean("org.sakaiproject.section.api.facade.manager.Context");
060: secMgr = (SectionManager) applicationContext
061: .getBean("org.sakaiproject.section.api.SectionManager");
062: courseMgr = (CourseManager) applicationContext
063: .getBean("org.sakaiproject.section.api.CourseManager");
064: userMgr = (UserManager) applicationContext
065: .getBean("org.sakaiproject.component.section.support.UserManager");
066: }
067:
068: public void testSectionMembership() throws Exception {
069: String siteContext = context.getContext(null);
070: List categories = secMgr.getSectionCategories(siteContext);
071:
072: // Add a course and a section to work from
073: Course course = courseMgr.createCourse(siteContext, "A course",
074: false, false, false);
075:
076: CourseSection sec1 = secMgr.addSection(course.getUuid(),
077: "A section", (String) categories.get(0),
078: new Integer(10), null, null, null, false, false, false,
079: false, false, false, false);
080: CourseSection sec2 = secMgr.addSection(course.getUuid(),
081: "Another section", (String) categories.get(1),
082: new Integer(10), null, null, null, false, false, false,
083: false, false, false, false);
084:
085: // Load students
086: User student1 = userMgr.createUser("student1", "Joe Student",
087: "Student, Joe", "jstudent");
088: User student2 = userMgr.createUser("student2",
089: "Jane Undergrad", "Undergrad, Jane", "jundergrad");
090:
091: // Load TAs
092: User ta1 = userMgr.createUser("ta1", "Mike Grad", "Grad, Mike",
093: "mgrad");
094: User ta2 = userMgr.createUser("ta2", "Sara Postdoc",
095: "Postdoc, Sara", "spostdoc");
096:
097: // Load instructors
098: User instructor1 = userMgr.createUser("instructor1",
099: "Bill Economist", "Economist, Bill", "beconomist");
100:
101: // Load enrollments into the course
102: courseMgr.addEnrollment(student1, course);
103: courseMgr.addEnrollment(student2, course);
104:
105: // Load enrollments into sections
106: secMgr.addSectionMembership("student1", Role.STUDENT, sec1
107: .getUuid());
108: secMgr.addSectionMembership("student2", Role.STUDENT, sec2
109: .getUuid());
110:
111: // Load TAs into the course
112: courseMgr.addTA(ta1, course);
113: courseMgr.addTA(ta2, course);
114:
115: // Load TAs into the sections
116: secMgr.addSectionMembership("ta1", Role.TA, sec1.getUuid());
117: secMgr.addSectionMembership("ta2", Role.TA, sec2.getUuid());
118:
119: // Load instructor into the courses
120: courseMgr.addInstructor(instructor1, course);
121:
122: Assert.assertTrue(authz.isViewOwnSectionsAllowed(student1
123: .getUserUid(), siteContext));
124: Assert.assertTrue(authz.isViewOwnSectionsAllowed(student2
125: .getUserUid(), siteContext));
126: Assert.assertTrue(!authz.isViewOwnSectionsAllowed(ta1
127: .getUserUid(), siteContext));
128: Assert.assertTrue(!authz.isViewOwnSectionsAllowed(ta2
129: .getUserUid(), siteContext));
130: Assert.assertTrue(!authz.isViewOwnSectionsAllowed(instructor1
131: .getUserUid(), siteContext));
132:
133: Assert.assertTrue(authz.isSectionEnrollmentMangementAllowed(
134: instructor1.getUserUid(), siteContext));
135: Assert.assertTrue(authz.isSectionEnrollmentMangementAllowed(ta1
136: .getUserUid(), siteContext));
137: Assert.assertTrue(authz.isSectionEnrollmentMangementAllowed(ta2
138: .getUserUid(), siteContext));
139: Assert.assertTrue(!authz.isSectionEnrollmentMangementAllowed(
140: student1.getUserUid(), siteContext));
141: Assert.assertTrue(!authz.isSectionEnrollmentMangementAllowed(
142: student2.getUserUid(), siteContext));
143:
144: Assert.assertTrue(authz.isSectionManagementAllowed(instructor1
145: .getUserUid(), siteContext));
146: Assert.assertTrue(!authz.isSectionManagementAllowed(ta1
147: .getUserUid(), siteContext));
148: Assert.assertTrue(!authz.isSectionManagementAllowed(ta2
149: .getUserUid(), siteContext));
150: Assert.assertTrue(!authz.isSectionManagementAllowed(student1
151: .getUserUid(), siteContext));
152: Assert.assertTrue(!authz.isSectionManagementAllowed(student2
153: .getUserUid(), siteContext));
154:
155: Assert.assertTrue(authz.isSectionOptionsManagementAllowed(
156: instructor1.getUserUid(), siteContext));
157: Assert.assertTrue(!authz.isSectionOptionsManagementAllowed(ta1
158: .getUserUid(), siteContext));
159: Assert.assertTrue(!authz.isSectionOptionsManagementAllowed(ta2
160: .getUserUid(), siteContext));
161: Assert.assertTrue(!authz.isSectionOptionsManagementAllowed(
162: student1.getUserUid(), siteContext));
163: Assert.assertTrue(!authz.isSectionOptionsManagementAllowed(
164: student2.getUserUid(), siteContext));
165:
166: }
167: }
|