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 assert has_instructor_tab(self.instructor, self.course) staff = StaffFactory(course_key=self.course.id) assert has_instructor_tab(staff, self.course) student = UserFactory.create() assert not 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) assert 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) assert 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) assert roles == {expected_role}
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_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 setUp(self): super(TestGradeUcursosView, self).setUp() self.grade_factory = CourseGradeFactory() with patch('student.models.cc.User.save'): # staff user self.client_instructor = Client() self.client_student = Client() self.client_anonymous = Client() self.user_instructor = UserFactory( username='******', password='******', email='*****@*****.**', is_staff=True) role = CourseInstructorRole(self.course.id) role.add_users(self.user_instructor) self.client_instructor.login( username='******', password='******') self.student = UserFactory( username='******', password='******', email='*****@*****.**') self.student_2 = UserFactory( username='******', password='******', email='*****@*****.**') # Enroll the student in the course CourseEnrollmentFactory( user=self.student, course_id=self.course.id, mode='honor') CourseEnrollmentFactory( user=self.student_2, course_id=self.course.id, mode='honor') self.client_student.login( username='******', password='******') # Create and Enroll data researcher user self.data_researcher_user = UserFactory( username='******', password='******', email='*****@*****.**') CourseEnrollmentFactory( user=self.data_researcher_user, course_id=self.course.id, mode='audit') CourseAccessRoleFactory( course_id=self.course.id, user=self.data_researcher_user, role='data_researcher', org=self.course.id.org ) self.client_data_researcher = Client() self.assertTrue(self.client_data_researcher.login(username='******', password='******'))
def test_data_download(self, access_role, can_access, waffle_status): """ Verify that the Data Download tab only shows up for certain roles """ with override_waffle_flag(DATA_DOWNLOAD_V2, waffle_status): download_section = '<li class="nav-item"><button type="button" class="btn-link data_download" ' \ 'data-section="data_download">Data Download</button></li>' if waffle_status: download_section = '<li class="nav-item"><button type="button" class="btn-link data_download_2" ' \ 'data-section="data_download_2">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_discussion_tab_for_course_staff_role( self, access_role, is_pages_and_resources_enabled, is_legacy_discussion_setting_enabled, is_discussion_tab_available): """ Verify that the Discussion tab is available for course for course staff role. """ discussion_section = ( '<li class="nav-item"><button type="button" class="btn-link discussions_management" ' 'data-section="discussions_management">Discussions</button></li>') with override_waffle_flag(ENABLE_PAGES_AND_RESOURCES_MICROFRONTEND, is_pages_and_resources_enabled): with override_waffle_flag(OVERRIDE_DISCUSSION_LEGACY_SETTINGS_FLAG, is_legacy_discussion_setting_enabled): user = UserFactory.create() CourseAccessRoleFactory(course_id=self.course.id, user=user, role=access_role, org=self.course.id.org) set_course_cohorted(self.course.id, True) self.client.login(username=self.user.username, password='******') response = self.client.get(self.url).content.decode('utf-8') self.assertEqual(discussion_section in response, is_discussion_tab_available)
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 setUp(self): super(TestEolCompletionView, self).setUp() # create a course self.course = CourseFactory.create( org='mss', course='999', display_name='eol_completion_course') # Now give it some content with self.store.bulk_operations(self.course.id, emit_signals=False): chapter = ItemFactory.create( parent_location=self.course.location, category="chapter", ) section = ItemFactory.create( parent_location=chapter.location, category="sequential", ) subsection = ItemFactory.create( parent_location=section.location, category="vertical", ) self.items = [ ItemFactory.create(parent_location=subsection.location, category="problem") for __ in range(USER_COUNT - 1) ] # Create users, enroll self.users = [UserFactory.create() for _ in range(USER_COUNT)] for user in self.users: CourseEnrollmentFactory(user=user, course_id=self.course.id, mode='honor') # create a course without content, only student self.course_no_content = CourseFactory.create( org='mnc', course='777', display_name='eol_completion_course2') for user in self.users: CourseEnrollmentFactory(user=user, course_id=self.course_no_content.id, mode='honor') # create a course without student, only content self.course_no_user = CourseFactory.create( org='mnu', course='888', display_name='eol_completion_course3') # Now give it some content with self.store.bulk_operations(self.course_no_user.id, emit_signals=False): chapter2 = ItemFactory.create( parent_location=self.course_no_user.location, category="chapter", ) section2 = ItemFactory.create( parent_location=chapter2.location, category="sequential", ) subsection2 = ItemFactory.create( parent_location=section2.location, category="vertical", ) self.items2 = [ ItemFactory.create(parent_location=subsection2.location, category="problem") for __ in range(USER_COUNT - 1) ] # create a empty course self.course_empty = CourseFactory.create( org='mem', course='111', display_name='eol_completion_course4') # Patch the comment client user save method so it does not try # to create a new cc user when creating a django user with patch('common.djangoapps.student.models.cc.User.save'): # Create the student self.student = UserFactory(username='******', password='******', email='*****@*****.**') # Enroll the student in the course CourseEnrollmentFactory(user=self.student, course_id=self.course.id, mode='honor') CourseEnrollmentFactory(user=self.student, course_id=self.course_no_content.id, mode='honor') # Create and Enroll staff user self.staff_user = UserFactory(username='******', password='******', email='*****@*****.**') CourseEnrollmentFactory(user=self.staff_user, course_id=self.course.id, mode='audit') CourseStaffRole(self.course.id).add_users(self.staff_user) # Create and Enroll data researcher user self.data_researcher_user = UserFactory( username='******', password='******', email='*****@*****.**') CourseEnrollmentFactory(user=self.data_researcher_user, course_id=self.course.id, mode='audit') CourseAccessRoleFactory(course_id=self.course.id, user=self.data_researcher_user, role='data_researcher', org=self.course.id.org) self.client_data_researcher = Client() self.assertTrue( self.client_data_researcher.login( username='******', password='******')) # Log the student in self.client = Client() self.assertTrue( self.client.login(username='******', password='******')) # Create Super User self.super_user = UserFactory(username='******', password='******', email='*****@*****.**', is_staff=True) self.super_client = Client() self.assertTrue( self.super_client.login(username='******', password='******')) # Log the user staff in self.staff_client = Client() self.assertTrue( self.staff_client.login(username='******', password='******'))
def create_courseaccessrole(**kwargs): """ Create a CourseAccessRole to use in tests. """ return CourseAccessRoleFactory(**kwargs)
def setUp(self): super(TestXblockCompletionView, self).setUp() self.course = CourseFactory.create( org='mss', course='999', display_name='2021', emit_signals=True) aux = CourseOverview.get_from_id(self.course.id) with self.store.bulk_operations(self.course.id, emit_signals=False): self.chapter = ItemFactory.create( parent_location=self.course.location, category="chapter", ) self.section = ItemFactory.create( parent_location=self.chapter.location, category="sequential", ) self.subsection = ItemFactory.create( parent_location=self.section.location, category="vertical", ) self.items = [ ItemFactory.create( parent_location=self.subsection.location, category="problem" ) for __ in range(3) ] with patch('student.models.cc.User.save'): # staff user self.client_instructor = Client() self.client_student = Client() self.user_instructor = UserFactory( username='******', password='******', email='*****@*****.**', is_staff=True) role = CourseInstructorRole(self.course.id) role.add_users(self.user_instructor) self.client_instructor.login( username='******', password='******') self.student = UserFactory( username='******', password='******', email='*****@*****.**') # Enroll the student in the course CourseEnrollmentFactory( user=self.student, course_id=self.course.id, mode='honor') self.client_student.login( username='******', password='******') # Create and Enroll data researcher user self.data_researcher_user = UserFactory( username='******', password='******', email='*****@*****.**') CourseEnrollmentFactory( user=self.data_researcher_user, course_id=self.course.id, mode='audit') CourseAccessRoleFactory( course_id=self.course.id, user=self.data_researcher_user, role='data_researcher', org=self.course.id.org ) self.client_data_researcher = Client() self.assertTrue(self.client_data_researcher.login(username='******', password='******'))