def test_get_user_for_role(self): """ test users_for_role """ role = CourseStaffRole(self.course_key) role.add_users(self.student) assert len(role.users_with_role()) > 0
def remove_all_instructors(course_key): """ Removes all instructor and staff users from the given course. """ staff_role = CourseStaffRole(course_key) staff_role.remove_users(*staff_role.users_with_role()) instructor_role = CourseInstructorRole(course_key) instructor_role.remove_users(*instructor_role.users_with_role())
def test_update_and_assign_or_revoke_staff(self): """ Successfully updates existing enrollments to assign or revoke the CourseStaff role. """ course_staff_role = CourseStaffRole(self.course_id) course_staff_role.add_users(self.student_2) self.create_program_and_course_enrollments('learner-1', user=self.student_1) self.create_program_and_course_enrollments('learner-2', user=self.student_2) self.create_program_and_course_enrollments('learner-3', user=None) learner_4_enrollment = self.create_program_and_course_enrollments( 'learner-4', user=None) learner_5_enrollment = self.create_program_and_course_enrollments( 'learner-5', user=None) CourseAccessRoleAssignment.objects.create( enrollment=learner_4_enrollment, role=ProgramCourseEnrollmentRoles.COURSE_STAFF, ) CourseAccessRoleAssignment.objects.create( enrollment=learner_5_enrollment, role=ProgramCourseEnrollmentRoles.COURSE_STAFF, ) course_enrollment_requests = [ self.course_enrollment_request('learner-1', CourseStatuses.ACTIVE, True), self.course_enrollment_request('learner-2', CourseStatuses.ACTIVE, False), self.course_enrollment_request('learner-3', CourseStatuses.ACTIVE, True), self.course_enrollment_request('learner-4', CourseStatuses.ACTIVE, False), self.course_enrollment_request('learner-5', CourseStatuses.ACTIVE, True), ] write_program_course_enrollments( self.program_uuid, self.course_id, course_enrollment_requests, True, True, ) # Role is revoked for user's with a linked enrollment self.assertListEqual([self.student_1], list(course_staff_role.users_with_role())) # CourseAccessRoleAssignment objects are created/revoked for enrollments with no linked user pending_role_assingments = CourseAccessRoleAssignment.objects.all() assert pending_role_assingments.count() == 2 pending_role_assingments.get( enrollment__program_enrollment__external_user_key='learner-3', enrollment__course_key=self.course_id) pending_role_assingments.get( enrollment__program_enrollment__external_user_key='learner-5', enrollment__course_key=self.course_id)
def test_create_enrollments_and_assign_staff(self, request_user_key_prefix): """ Successfully creates both waiting and linked program course enrollments with the course staff role. """ course_staff_role = CourseStaffRole(self.course_id) course_staff_role.add_users(self.student_1) self.create_program_enrollment('learner-1', user=None) self.create_program_enrollment('learner-2', user=self.student_1) self.create_program_enrollment('learner-3', user=self.student_2) course_enrollment_requests = [ self.course_enrollment_request( '{}-1'.format(request_user_key_prefix), CourseStatuses.ACTIVE, True), self.course_enrollment_request( '{}-2'.format(request_user_key_prefix), CourseStatuses.ACTIVE, True), self.course_enrollment_request( '{}-3'.format(request_user_key_prefix), CourseStatuses.ACTIVE, True), ] write_program_course_enrollments( self.program_uuid, self.course_id, course_enrollment_requests, True, True, ) self.assert_program_course_enrollment('learner-1', CourseStatuses.ACTIVE, False) self.assert_program_course_enrollment('learner-2', CourseStatuses.ACTIVE, True) self.assert_program_course_enrollment('learner-3', CourseStatuses.ACTIVE, True) # Users linked to either enrollment are given the course staff role self.assertListEqual([self.student_1, self.student_2], list(course_staff_role.users_with_role())) # CourseAccessRoleAssignment objects are created for enrollments with no linked user pending_role_assingments = CourseAccessRoleAssignment.objects.all() assert pending_role_assingments.count() == 1 pending_role_assingments.get( enrollment__program_enrollment__external_user_key='learner-1', enrollment__course_key=self.course_id)