Exemplo n.º 1
0
    def test_cea_enrolls_only_one_user(self):
        """
        Tests that a CourseEnrollmentAllowed can be used by just one user.
        If the user changes e-mail and then a second user tries to enroll with the same accepted e-mail,
        the second enrollment should fail.
        However, the original user can reuse the CEA many times.
        """

        cea = CourseEnrollmentAllowedFactory(
            email='*****@*****.**',
            course_id=self.course.id,
            auto_enroll=False,
        )
        # Still unlinked
        self.assertIsNone(cea.user)

        user1 = UserFactory.create(username="******", email="*****@*****.**", password="******")
        user2 = UserFactory.create(username="******", email="*****@*****.**", password="******")

        self.assertFalse(
            CourseEnrollment.objects.filter(course_id=self.course.id, user=user1).exists()
        )

        user1.email = '*****@*****.**'
        user1.save()

        CourseEnrollment.enroll(user1, self.course.id, check_access=True)

        self.assertTrue(
            CourseEnrollment.objects.filter(course_id=self.course.id, user=user1).exists()
        )

        # The CEA is now linked
        cea.refresh_from_db()
        self.assertEqual(cea.user, user1)

        # user2 wants to enroll too, (ab)using the same allowed e-mail, but cannot
        user1.email = '*****@*****.**'
        user1.save()
        user2.email = '*****@*****.**'
        user2.save()
        with self.assertRaises(EnrollmentClosedError):
            CourseEnrollment.enroll(user2, self.course.id, check_access=True)

        # CEA still linked to user1. Also after unenrolling
        cea.refresh_from_db()
        self.assertEqual(cea.user, user1)

        CourseEnrollment.unenroll(user1, self.course.id)

        cea.refresh_from_db()
        self.assertEqual(cea.user, user1)

        # Enroll user1 again. Because it's the original owner of the CEA, the enrollment is allowed
        CourseEnrollment.enroll(user1, self.course.id, check_access=True)

        # Still same
        cea.refresh_from_db()
        self.assertEqual(cea.user, user1)
Exemplo n.º 2
0
    def test_cea_enrolls_only_one_user(self):
        """
        Tests that a CourseEnrollmentAllowed can be used by just one user.
        If the user changes e-mail and then a second user tries to enroll with the same accepted e-mail,
        the second enrollment should fail.
        However, the original user can reuse the CEA many times.
        """

        cea = CourseEnrollmentAllowedFactory(
            email='*****@*****.**',
            course_id=self.course.id,
            auto_enroll=False,
        )
        # Still unlinked
        self.assertIsNone(cea.user)

        user1 = UserFactory.create(username="******", email="*****@*****.**", password="******")
        user2 = UserFactory.create(username="******", email="*****@*****.**", password="******")

        self.assertFalse(
            CourseEnrollment.objects.filter(course_id=self.course.id, user=user1).exists()
        )

        user1.email = '*****@*****.**'
        user1.save()

        CourseEnrollment.enroll(user1, self.course.id, check_access=True)

        self.assertTrue(
            CourseEnrollment.objects.filter(course_id=self.course.id, user=user1).exists()
        )

        # The CEA is now linked
        cea.refresh_from_db()
        self.assertEqual(cea.user, user1)

        # user2 wants to enroll too, (ab)using the same allowed e-mail, but cannot
        user1.email = '*****@*****.**'
        user1.save()
        user2.email = '*****@*****.**'
        user2.save()
        with self.assertRaises(EnrollmentClosedError):
            CourseEnrollment.enroll(user2, self.course.id, check_access=True)

        # CEA still linked to user1. Also after unenrolling
        cea.refresh_from_db()
        self.assertEqual(cea.user, user1)

        CourseEnrollment.unenroll(user1, self.course.id)

        cea.refresh_from_db()
        self.assertEqual(cea.user, user1)

        # Enroll user1 again. Because it's the original owner of the CEA, the enrollment is allowed
        CourseEnrollment.enroll(user1, self.course.id, check_access=True)

        # Still same
        cea.refresh_from_db()
        self.assertEqual(cea.user, user1)
Exemplo n.º 3
0
    def test_enrolled_after_email_change(self):
        """
        Test that when a user's email changes, the user is enrolled in pending courses.
        """
        pending_enrollment = CourseEnrollmentAllowedFactory(auto_enroll=True)

        # the e-mail will change to [email protected] (from something else)
        self.assertNotEquals(self.user.email, '*****@*****.**')

        # there's a CEA for the new e-mail
        self.assertEquals(CourseEnrollmentAllowed.objects.count(), 1)
        self.assertEquals(
            CourseEnrollmentAllowed.objects.filter(
                email='*****@*****.**').count(), 1)

        # Changing the e-mail to the enrollment-allowed e-mail should enroll
        self.user.email = '*****@*****.**'
        self.user.save()
        self.assert_user_enrollment_occurred('edX/toy/2012_Fall')

        # CEAs shouldn't have been affected
        self.assertEquals(CourseEnrollmentAllowed.objects.count(), 1)
        self.assertEquals(
            CourseEnrollmentAllowed.objects.filter(
                email='*****@*****.**').count(), 1)
Exemplo n.º 4
0
    def test_invitation_only_but_allowed(self):
        """
        Test for user logged in and allowed to enroll in invitation only course.
        """

        # Course is invitation only, student is allowed to enroll and logged in
        user = UserFactory.create(username='******', password='******', email='*****@*****.**')
        CourseEnrollmentAllowedFactory(email=user.email, course_id=self.course.id)
        self.client.login(username=user.username, password='******')

        url = reverse('about_course', args=[text_type(self.course.id)])
        resp = self.client.get(url)
        self.assertContains(resp, u"Enroll Now")

        # Check that registration button is present
        self.assertContains(resp, REG_STR)
Exemplo n.º 5
0
    def test_invitation_only_but_allowed(self):
        """
        Test for user logged in and allowed to enroll in invitation only course.
        """

        # Course is invitation only, student is allowed to enroll and logged in
        user = UserFactory.create(username='******', password='******', email='*****@*****.**')
        CourseEnrollmentAllowedFactory(email=user.email, course_id=self.course.id)
        self.client.login(username=user.username, password='******')

        url = reverse('about_course', args=[text_type(self.course.id)])
        resp = self.client.get(url)
        self.assertEqual(resp.status_code, 200)
        self.assertIn(u"Enroll in {}".format(self.course.id.course), resp.content.decode('utf-8'))

        # Check that registration button is present
        self.assertIn(REG_STR, resp.content)
Exemplo n.º 6
0
    def test__has_access_course_desc_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=SlashSeparatedCourseKey('edX', 'test', '2012_Fall'),
                      enrollment_domain='')
        CourseEnrollmentAllowedFactory(email=user.email, course_id=course.id)
        self.assertTrue(access._has_access_course_desc(user, 'enroll', course))

        # Staff can always enroll even outside the open enrollment period
        user = StaffFactory.create(course_key=course.id)
        self.assertTrue(access._has_access_course_desc(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=SlashSeparatedCourseKey('edX', 'test', '2012_Fall'),
                      enrollment_domain='',
                      invitation_only=True)
        user = UserFactory.create()
        self.assertFalse(access._has_access_course_desc(
            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=SlashSeparatedCourseKey('edX', 'test', '2012_Fall'),
                      enrollment_domain='',
                      invitation_only=False)
        self.assertTrue(access._has_access_course_desc(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=SlashSeparatedCourseKey('edX', 'test', '2012_Fall'),
                      enrollment_domain='',
                      invitation_only=False)
        self.assertFalse(access._has_access_course_desc(
            user, 'enroll', course))
Exemplo n.º 7
0
    def test__has_access_course_desc_can_enroll(self):
        user = Mock()
        yesterday = datetime.datetime.now(
            pytz.utc) - datetime.timedelta(days=1)
        tomorrow = datetime.datetime.now(pytz.utc) + datetime.timedelta(days=1)
        course = Mock(enrollment_start=yesterday,
                      enrollment_end=tomorrow,
                      enrollment_domain='')

        # User can enroll if it is between the start and end dates
        self.assertTrue(access._has_access_course_desc(user, 'enroll', course))

        # User can enroll if authenticated and specifically allowed for that course
        # even outside the open enrollment period
        user = Mock(email='*****@*****.**', is_staff=False)
        user.is_authenticated.return_value = True

        course = Mock(enrollment_start=tomorrow,
                      enrollment_end=tomorrow,
                      id=SlashSeparatedCourseKey('edX', 'test', '2012_Fall'),
                      enrollment_domain='')

        CourseEnrollmentAllowedFactory(email=user.email, course_id=course.id)

        self.assertTrue(access._has_access_course_desc(user, 'enroll', course))

        # Staff can always enroll even outside the open enrollment period
        user = Mock(email='*****@*****.**', is_staff=True)
        user.is_authenticated.return_value = True

        course = Mock(
            enrollment_start=tomorrow,
            enrollment_end=tomorrow,
            id=SlashSeparatedCourseKey('edX', 'test', 'Whenever'),
            enrollment_domain='',
        )
        self.assertTrue(access._has_access_course_desc(user, 'enroll', course))