def test_no_banner_when_masquerading_as_staff(self, role_factory,
                                                  mock_get_course_run_details):
        """
        When masquerading as a specific expired user, if that user has a staff role
        the expired course banner will not show up.
        """
        mock_get_course_run_details.return_value = {'weeks_to_complete': 1}

        if role_factory == GlobalStaffFactory:
            expired_staff = role_factory.create(password=TEST_PASSWORD)
        else:
            expired_staff = role_factory.create(password=TEST_PASSWORD,
                                                course_key=self.course.id)

        CourseEnrollmentFactory.create(
            mode=CourseMode.AUDIT,
            course_id=self.course.id,
            user=expired_staff,
        )
        Schedule.objects.update(start_date=self.THREE_YEARS_AGO)
        CourseDurationLimitConfig.objects.create(
            enabled=True,
            course=CourseOverview.get_from_id(self.course.id),
            enabled_as_of=self.course.start,
        )

        staff_user = StaffFactory.create(password=TEST_PASSWORD,
                                         course_key=self.course.id)
        CourseEnrollmentFactory.create(user=staff_user,
                                       course_id=self.course.id,
                                       mode='audit')

        self.client.login(username=staff_user.username, password='******')

        self.update_masquerade(username=expired_staff.username)

        course_home_url = reverse('openedx.course_experience.course_home',
                                  args=[str(self.course.id)])
        response = self.client.get(course_home_url, follow=True)
        assert response.status_code == 200
        self.assertCountEqual(response.redirect_chain, [])
        banner_text = 'This learner does not have access to this course. Their access expired on'
        self.assertNotContains(response, banner_text)
Beispiel #2
0
    def test__has_access_course_can_enroll(self):
        yesterday = datetime.datetime.now(pytz.utc) - datetime.timedelta(days=1)
        tomorrow = datetime.datetime.now(pytz.utc) + datetime.timedelta(days=1)

        # Non-staff can enroll if authenticated and specifically allowed for that course
        # even outside the open enrollment period
        user = UserFactory.create()
        course = Mock(
            enrollment_start=tomorrow, enrollment_end=tomorrow,
            id=CourseLocator('edX', 'test', '2012_Fall'), enrollment_domain=''
        )
        CourseEnrollmentAllowedFactory(email=user.email, course_id=course.id)
        assert access._has_access_course(user, 'enroll', course)

        # Staff can always enroll even outside the open enrollment period
        user = StaffFactory.create(course_key=course.id)
        assert access._has_access_course(user, 'enroll', course)

        # Non-staff cannot enroll if it is between the start and end dates and invitation only
        # and not specifically allowed
        course = Mock(
            enrollment_start=yesterday, enrollment_end=tomorrow,
            id=CourseLocator('edX', 'test', '2012_Fall'), enrollment_domain='',
            invitation_only=True
        )
        user = UserFactory.create()
        assert not access._has_access_course(user, 'enroll', course)

        # Non-staff can enroll if it is between the start and end dates and not invitation only
        course = Mock(
            enrollment_start=yesterday, enrollment_end=tomorrow,
            id=CourseLocator('edX', 'test', '2012_Fall'), enrollment_domain='',
            invitation_only=False
        )
        assert access._has_access_course(user, 'enroll', course)

        # Non-staff cannot enroll outside the open enrollment period if not specifically allowed
        course = Mock(
            enrollment_start=tomorrow, enrollment_end=tomorrow,
            id=CourseLocator('edX', 'test', '2012_Fall'), enrollment_domain='',
            invitation_only=False
        )
        assert not access._has_access_course(user, 'enroll', course)
    def test_no_banner_when_masquerading_as_forum_staff(
            self, role_name, mock_get_course_run_details):
        """
        When masquerading as a specific expired user, if that user has a forum staff role
        the expired course banner will not show up.
        """
        mock_get_course_run_details.return_value = {'weeks_to_complete': 1}

        expired_staff = UserFactory.create()
        role = RoleFactory(name=role_name, course_id=self.course.id)
        role.users.add(expired_staff)

        CourseEnrollmentFactory.create(
            mode=CourseMode.AUDIT,
            course_id=self.course.id,
            user=expired_staff,
        )
        Schedule.objects.update(start_date=self.THREE_YEARS_AGO)

        CourseDurationLimitConfig.objects.create(
            enabled=True,
            course=CourseOverview.get_from_id(self.course.id),
            enabled_as_of=self.course.start,
        )

        staff_user = StaffFactory.create(password=TEST_PASSWORD,
                                         course_key=self.course.id)
        CourseEnrollmentFactory.create(user=staff_user,
                                       course_id=self.course.id,
                                       mode='audit')

        self.client.login(username=staff_user.username, password='******')

        self.update_masquerade(username=expired_staff.username)

        response = self.get_courseware()
        assert response.status_code == 200
        self.assertCountEqual(response.redirect_chain, [])
        banner_text = 'This learner does not have access to this course. Their access expired on'
        self.assertNotContains(response, banner_text)
Beispiel #4
0
    def test__catalog_visibility(self):
        """
        Tests the catalog visibility tri-states
        """
        user = UserFactory.create()
        course_id = CourseLocator('edX', 'test', '2012_Fall')
        staff = StaffFactory.create(course_key=course_id)

        course = Mock(
            id=course_id,
            catalog_visibility=CATALOG_VISIBILITY_CATALOG_AND_ABOUT
        )
        assert access._has_access_course(user, 'see_in_catalog', course)
        assert access._has_access_course(user, 'see_about_page', course)
        assert access._has_access_course(staff, 'see_in_catalog', course)
        assert access._has_access_course(staff, 'see_about_page', course)

        # Now set visibility to just about page
        course = Mock(
            id=CourseLocator('edX', 'test', '2012_Fall'),
            catalog_visibility=CATALOG_VISIBILITY_ABOUT
        )
        assert not access._has_access_course(user, 'see_in_catalog', course)
        assert access._has_access_course(user, 'see_about_page', course)
        assert access._has_access_course(staff, 'see_in_catalog', course)
        assert access._has_access_course(staff, 'see_about_page', course)

        # Now set visibility to none, which means neither in catalog nor about pages
        course = Mock(
            id=CourseLocator('edX', 'test', '2012_Fall'),
            catalog_visibility=CATALOG_VISIBILITY_NONE
        )
        assert not access._has_access_course(user, 'see_in_catalog', course)
        assert not access._has_access_course(user, 'see_about_page', course)
        assert access._has_access_course(staff, 'see_in_catalog', course)
        assert access._has_access_course(staff, 'see_about_page', course)
Beispiel #5
0
    def setUp(self):
        super().setUp()

        UserPartition.scheme_extensions = ExtensionManager.make_test_instance(
            [
                Extension("memory", USER_PARTITION_SCHEME_NAMESPACE,
                          MemoryUserPartitionScheme(), None),
                Extension("random", USER_PARTITION_SCHEME_NAMESPACE,
                          MemoryUserPartitionScheme(), None)
            ],
            namespace=USER_PARTITION_SCHEME_NAMESPACE)

        self.cat_group = Group(10, 'cats')
        self.dog_group = Group(20, 'dogs')
        self.worm_group = Group(30, 'worms')
        self.animal_partition = UserPartition(
            0,
            'Pet Partition',
            'which animal are you?',
            [self.cat_group, self.dog_group, self.worm_group],
            scheme=UserPartition.get_scheme("memory"),
        )

        self.red_group = Group(1000, 'red')
        self.blue_group = Group(2000, 'blue')
        self.gray_group = Group(3000, 'gray')
        self.color_partition = UserPartition(
            100,
            'Color Partition',
            'what color are you?',
            [self.red_group, self.blue_group, self.gray_group],
            scheme=UserPartition.get_scheme("memory"),
        )

        self.course = CourseFactory.create(
            user_partitions=[self.animal_partition, self.color_partition], )
        with self.store.bulk_operations(self.course.id, emit_signals=False):
            chapter = ItemFactory.create(category='chapter',
                                         parent=self.course)
            section = ItemFactory.create(category='sequential', parent=chapter)
            vertical = ItemFactory.create(category='vertical', parent=section)
            component = ItemFactory.create(category='problem', parent=vertical)

            self.chapter_location = chapter.location
            self.section_location = section.location
            self.vertical_location = vertical.location
            self.component_location = component.location

        self.red_cat = UserFactory()  # student in red and cat groups
        self.set_user_group(self.red_cat, self.animal_partition,
                            self.cat_group)
        self.set_user_group(self.red_cat, self.color_partition, self.red_group)

        self.blue_dog = UserFactory()  # student in blue and dog groups
        self.set_user_group(self.blue_dog, self.animal_partition,
                            self.dog_group)
        self.set_user_group(self.blue_dog, self.color_partition,
                            self.blue_group)

        self.white_mouse = UserFactory()  # student in no group

        self.gray_worm = UserFactory()  # student in deleted group
        self.set_user_group(self.gray_worm, self.animal_partition,
                            self.worm_group)
        self.set_user_group(self.gray_worm, self.color_partition,
                            self.gray_group)
        # delete the gray/worm groups from the partitions now so we can test scenarios
        # for user whose group is missing.
        self.animal_partition.groups.pop()
        self.color_partition.groups.pop()

        # add a staff user, whose access will be unconditional in spite of group access.
        self.staff = StaffFactory.create(course_key=self.course.id)