def test_instructor_tab(self): """ Verify that the instructor tab appears for staff only. """ def has_instructor_tab(user, course): """Returns true if the "Instructor" tab is shown.""" tabs = get_course_tab_list(user, course) return len([tab for tab in tabs if tab.name == 'Instructor']) == 1 self.assertTrue(has_instructor_tab(self.instructor, self.course)) staff = StaffFactory(course_key=self.course.id) self.assertTrue(has_instructor_tab(staff, self.course)) student = UserFactory.create() self.assertFalse(has_instructor_tab(student, self.course)) researcher = UserFactory.create() CourseAccessRoleFactory( course_id=self.course.id, user=researcher, role='data_researcher', org=self.course.id.org ) self.assertTrue(has_instructor_tab(researcher, self.course)) org_researcher = UserFactory.create() CourseAccessRoleFactory( course_id=None, user=org_researcher, role='data_researcher', org=self.course.id.org ) self.assertTrue(has_instructor_tab(org_researcher, self.course))
def create_users_data(): staff_user = UserFactory(last_login=now() - timedelta(days=5)) instructor_user = UserFactory(last_login=now() - timedelta(days=5)) course = CourseOverviewFactory(end=now() + timedelta(days=30)) archived_course = CourseOverviewFactory(end=now() - timedelta(days=30)) course_ids = [course.id, archived_course.id] for course_id in course_ids: CourseAccessRoleFactory.create(course_id=course_id, user=staff_user, role="staff") CourseAccessRoleFactory.create(course_id=course_id, user=instructor_user, role="instructor")
def test_realize_course_access_roles_user_with_existing_course_access_role(self): """ This test asserts that, given a user that already has a staff CourseAccessRole in a course, if that user has a CourseAccessRoleAssignment that describes a staff role in that same course, that we do not mistakenly violate the unique_together constraint on the CourseAccessRole model by creating a duplicate. As of now, this is handled by the CourseStaffRole code itself, which silently ignores such duplicates, but this test is to ensure we do not regress. """ program_enrollment = self._create_waiting_enrollment(self.program, '0001') active_enrollment_1 = self._create_waiting_course_enrollment( program_enrollment, self.fruit_course, status='active' ) # create an CourseAccessRole for the user CourseAccessRoleFactory(user=self.user_1, course_id=self.fruit_course, role=CourseStaffRole.ROLE) # create a corresponding CourseAccessRoleAssignmentFactory that would, theoretically, cause a # duplicate object to be created, violating the CourseAccessRole integrity constraints CourseAccessRoleAssignmentFactory(enrollment=active_enrollment_1) link_program_enrollments(self.program, {'0001': self.user_1.username}) # assert that staff CourseAccessRoles remains fruit_course_staff_role = get_course_access_role( self.user_1, self.fruit_course.org, self.fruit_course, CourseStaffRole.ROLE ) assert fruit_course_staff_role is not None # assert that all CourseAccessRoleAssignment objects are deleted assert not active_enrollment_1.courseaccessroleassignment_set.all().exists()
def test_get_roles(self): """Create a role for a user, then get it""" expected_role = CourseAccessRoleFactory.create( course_id=self.course.id, user=self.user, role="SuperCoolTestRole", ) roles = data.get_user_roles(self.user.username) self.assertEqual(roles, {expected_role})
def setUp(self): super(ForumEventTestCase, self).setUp() self.course = CourseFactory.create() seed_permissions_roles(self.course.id) self.student = UserFactory.create() CourseEnrollmentFactory(user=self.student, course_id=self.course.id) self.student.roles.add(Role.objects.get(name="Student", course_id=self.course.id)) CourseAccessRoleFactory(course_id=self.course.id, user=self.student, role='Wizard')
def test_get_compliance_deadline_for_user(self): """ Test that the proper deadlines get returned for each user scenario * Staff deadline returns STAFF_USER_COMPLIANCE_DEADLINE * CourseAccessRole Users return ELEVATED_PRIVILEGE_USER_COMPLIANCE_DEADLINE * Everyone else gets GENERAL_USER_COMPLIANCE_DEADLINE """ # Staff user returned the STAFF_USER_COMPLIANCE_DEADLINE user = UserFactory(is_staff=True) self.assertEqual(date1, _get_compliance_deadline_for_user(user)) # User with CourseAccessRole returns the ELEVATED_PRIVILEGE_USER_COMPLIANCE_DEADLINE user = UserFactory() CourseAccessRoleFactory.create(user=user) self.assertEqual(date2, _get_compliance_deadline_for_user(user)) user = UserFactory() self.assertEqual(date3, _get_compliance_deadline_for_user(user))
def test_get_compliance_deadline_for_user(self): """ Test that the proper deadlines get returned for each user scenario * Staff deadline returns STAFF_USER_COMPLIANCE_DEADLINE * CourseAccessRole Users return ELEVATED_PRIVILEGE_USER_COMPLIANCE_DEADLINE * Everyone else gets GENERAL_USER_COMPLIANCE_DEADLINE """ # Staff user returned the STAFF_USER_COMPLIANCE_DEADLINE user = UserFactory(is_staff=True) self.assertEqual(date1, _get_compliance_deadline_for_user(user)) # User with CourseAccessRole returns the ELEVATED_PRIVILEGE_USER_COMPLIANCE_DEADLINE user = UserFactory() CourseAccessRoleFactory.create(user=user) self.assertEqual(date2, _get_compliance_deadline_for_user(user)) user = UserFactory() self.assertEqual(date3, _get_compliance_deadline_for_user(user))
def test_user_detail(self): CourseEnrollmentFactory(course_id=self.course1.id, user=self.user2) CourseAccessRoleFactory(course_id=self.course1.id, user=self.user2, role=u'test_role') response = self.client.get(reverse('backoffice:user-detail', args=[self.user2.username])) self.assertEqual(200, response.status_code) self.assertEqual(response.context['enrollments'][0][0], self.course1.display_name) self.assertEqual(response.context['enrollments'][0][1], unicode(self.course1.id)) self.assertEqual(response.context['enrollments'][0][2], False) self.assertEqual(set(response.context['enrollments'][0][4]), set([u'test_role']))
def test_data_download_only(self): """ Verify that only the data download tab is visible for data researchers. """ user = UserFactory.create() CourseAccessRoleFactory(course_id=self.course.id, user=user, role='data_researcher', org=self.course.id.org) self.client.login(username=user.username, password="******") response = self.client.get(self.url) matches = re.findall( rb'<li class="nav-item"><button type="button" class="btn-link .*" data-section=".*">.*', response.content) assert len(matches) == 1
def test_data_download(self, access_role, can_access): """ Verify that the Data Download tab only shows up for certain roles """ download_section = '<li class="nav-item"><button type="button" class="btn-link data_download" '\ 'data-section="data_download">Data Download</button></li>' user = UserFactory.create(is_staff=access_role == 'global_staff') CourseAccessRoleFactory(course_id=self.course.id, user=user, role=access_role, org=self.course.id.org) self.client.login(username=user.username, password="******") response = self.client.get(self.url) if can_access: self.assertContains(response, download_section) else: self.assertNotContains(response, download_section)
def test_get_compliance_deadline_for_user_fallbacks(self): """ Test that when some deadlines aren't specified, we cascade from general to specific. """ staff = UserFactory(is_staff=True) privileged = UserFactory() CourseAccessRoleFactory.create(user=privileged) both = UserFactory(is_staff=True) CourseAccessRoleFactory.create(user=both) user = UserFactory() only_general = {'GENERAL_USER_COMPLIANCE_DEADLINE': date3} with self.settings( PASSWORD_POLICY_COMPLIANCE_ROLLOUT_CONFIG=only_general): self.assertEqual(date3, _get_compliance_deadline_for_user(staff)) self.assertEqual(date3, _get_compliance_deadline_for_user(privileged)) self.assertEqual(date3, _get_compliance_deadline_for_user(both)) no_staff = { 'ELEVATED_PRIVILEGE_USER_COMPLIANCE_DEADLINE': date2, 'GENERAL_USER_COMPLIANCE_DEADLINE': date3 } with self.settings(PASSWORD_POLICY_COMPLIANCE_ROLLOUT_CONFIG=no_staff): self.assertEqual(date2, _get_compliance_deadline_for_user(both)) self.assertEqual(date2, _get_compliance_deadline_for_user(staff)) no_privileged = { 'STAFF_USER_COMPLIANCE_DEADLINE': date1, 'GENERAL_USER_COMPLIANCE_DEADLINE': date3 } with self.settings( PASSWORD_POLICY_COMPLIANCE_ROLLOUT_CONFIG=no_privileged): self.assertEqual(date1, _get_compliance_deadline_for_user(both)) self.assertEqual(date3, _get_compliance_deadline_for_user(privileged)) only_privileged = { 'ELEVATED_PRIVILEGE_USER_COMPLIANCE_DEADLINE': date2, } with self.settings( PASSWORD_POLICY_COMPLIANCE_ROLLOUT_CONFIG=only_privileged): self.assertEqual(date2, _get_compliance_deadline_for_user(both)) self.assertEqual(date2, _get_compliance_deadline_for_user(staff)) self.assertIsNone(_get_compliance_deadline_for_user(user)) early_elevated = { 'STAFF_USER_COMPLIANCE_DEADLINE': date2, 'ELEVATED_PRIVILEGE_USER_COMPLIANCE_DEADLINE': date1, } with self.settings( PASSWORD_POLICY_COMPLIANCE_ROLLOUT_CONFIG=early_elevated): self.assertEqual(date1, _get_compliance_deadline_for_user(both)) self.assertEqual(date2, _get_compliance_deadline_for_user(staff)) self.assertEqual(date1, _get_compliance_deadline_for_user(privileged)) self.assertIsNone(_get_compliance_deadline_for_user(user)) early_general = { 'STAFF_USER_COMPLIANCE_DEADLINE': date3, 'ELEVATED_PRIVILEGE_USER_COMPLIANCE_DEADLINE': date2, 'GENERAL_USER_COMPLIANCE_DEADLINE': date1, } with self.settings( PASSWORD_POLICY_COMPLIANCE_ROLLOUT_CONFIG=early_general): self.assertEqual(date1, _get_compliance_deadline_for_user(both)) self.assertEqual(date1, _get_compliance_deadline_for_user(staff)) self.assertEqual(date1, _get_compliance_deadline_for_user(privileged)) self.assertEqual(date1, _get_compliance_deadline_for_user(user))
def test_get_compliance_deadline_for_user_fallbacks(self): """ Test that when some deadlines aren't specified, we cascade from general to specific. """ staff = UserFactory(is_staff=True) privileged = UserFactory() CourseAccessRoleFactory.create(user=privileged) both = UserFactory(is_staff=True) CourseAccessRoleFactory.create(user=both) user = UserFactory() only_general = { 'GENERAL_USER_COMPLIANCE_DEADLINE': date3 } with self.settings(PASSWORD_POLICY_COMPLIANCE_ROLLOUT_CONFIG=only_general): self.assertEqual(date3, _get_compliance_deadline_for_user(staff)) self.assertEqual(date3, _get_compliance_deadline_for_user(privileged)) self.assertEqual(date3, _get_compliance_deadline_for_user(both)) no_staff = { 'ELEVATED_PRIVILEGE_USER_COMPLIANCE_DEADLINE': date2, 'GENERAL_USER_COMPLIANCE_DEADLINE': date3 } with self.settings(PASSWORD_POLICY_COMPLIANCE_ROLLOUT_CONFIG=no_staff): self.assertEqual(date2, _get_compliance_deadline_for_user(both)) self.assertEqual(date2, _get_compliance_deadline_for_user(staff)) no_privileged = { 'STAFF_USER_COMPLIANCE_DEADLINE': date1, 'GENERAL_USER_COMPLIANCE_DEADLINE': date3 } with self.settings(PASSWORD_POLICY_COMPLIANCE_ROLLOUT_CONFIG=no_privileged): self.assertEqual(date1, _get_compliance_deadline_for_user(both)) self.assertEqual(date3, _get_compliance_deadline_for_user(privileged)) only_privileged = { 'ELEVATED_PRIVILEGE_USER_COMPLIANCE_DEADLINE': date2, } with self.settings(PASSWORD_POLICY_COMPLIANCE_ROLLOUT_CONFIG=only_privileged): self.assertEqual(date2, _get_compliance_deadline_for_user(both)) self.assertEqual(date2, _get_compliance_deadline_for_user(staff)) self.assertIsNone(_get_compliance_deadline_for_user(user)) early_elevated = { 'STAFF_USER_COMPLIANCE_DEADLINE': date2, 'ELEVATED_PRIVILEGE_USER_COMPLIANCE_DEADLINE': date1, } with self.settings(PASSWORD_POLICY_COMPLIANCE_ROLLOUT_CONFIG=early_elevated): self.assertEqual(date1, _get_compliance_deadline_for_user(both)) self.assertEqual(date2, _get_compliance_deadline_for_user(staff)) self.assertEqual(date1, _get_compliance_deadline_for_user(privileged)) self.assertIsNone(_get_compliance_deadline_for_user(user)) early_general = { 'STAFF_USER_COMPLIANCE_DEADLINE': date3, 'ELEVATED_PRIVILEGE_USER_COMPLIANCE_DEADLINE': date2, 'GENERAL_USER_COMPLIANCE_DEADLINE': date1, } with self.settings(PASSWORD_POLICY_COMPLIANCE_ROLLOUT_CONFIG=early_general): self.assertEqual(date1, _get_compliance_deadline_for_user(both)) self.assertEqual(date1, _get_compliance_deadline_for_user(staff)) self.assertEqual(date1, _get_compliance_deadline_for_user(privileged)) self.assertEqual(date1, _get_compliance_deadline_for_user(user))