Пример #1
0
    def test_lms_edx_get_enrollment_success(self):
        """Getting an enrollment with success."""
        expected_object = {"is_active": random.choice([True, False])}

        responses.add(
            responses.GET,
            (
                "http://edx:8073/api/enrollment/v1/enrollment/"
                "teacher,course-v1:edX+DemoX+Demo_Course"
            ),
            status=200,
            json=expected_object,
        )

        user = UserFactory(username="******")
        enrollment = LMSHandler.get_enrollment(
            user, "http://edx:8073/courses/course-v1:edX+DemoX+Demo_Course/course/"
        )

        self.assertEqual(len(responses.calls), 1)

        # Check API call to get enrollment
        self.assertEqual(
            responses.calls[0].request.url,
            (
                "http://edx:8073/api/enrollment/v1/enrollment/"
                "teacher,course-v1:edX+DemoX+Demo_Course"
            ),
        )
        self.assertEqual(
            responses.calls[0].request.headers["X-Edx-Api-Key"], "fakesecret"
        )
        self.assertEqual(responses.calls[0].request.body, None)

        self.assertEqual(enrollment, expected_object)
Пример #2
0
    def test_lms_edx_set_enrollment_active(self):
        """Setting an enrollment with success."""
        user = UserFactory(username="******")

        responses.add(
            responses.POST,
            "http://edx:8073/api/enrollment/v1/enrollment",
            status=200,
            json={"is_active": True},
        )

        is_enrolled = LMSHandler.set_enrollment(
            user, "http://edx:8073/courses/course-v1:edX+DemoX+Demo_Course/course/"
        )

        self.assertEqual(len(responses.calls), 1)

        # Check API call to set enrollment
        self.assertEqual(
            responses.calls[0].request.url,
            "http://edx:8073/api/enrollment/v1/enrollment",
        )
        self.assertEqual(
            responses.calls[0].request.headers["X-Edx-Api-Key"], "fakesecret"
        )
        self.assertEqual(
            json.loads(responses.calls[0].request.body),
            {
                "user": user.username,
                "course_details": {"course_id": "course-v1:edX+DemoX+Demo_Course"},
            },
        )

        self.assertTrue(is_enrolled)
Пример #3
0
    def test_lms_select(self):
        """
        The "select_lms" util function should return a backend instance with its configuration
        or raise an ImproperlyConfigurer error if the url does not match any backend or is blank.
        """
        backend = LMSHandler.select_lms("https://www.example.com/course/123")
        self.assertEqual(type(backend), TokenEdXLMSBackend)
        self.assertEqual(backend.configuration["BASE_URL"], "https://www.example.com")

        backend = LMSHandler.select_lms("https://edx.org/course/123")
        self.assertEqual(type(backend), BaseLMSBackend)
        self.assertEqual(backend.configuration["BASE_URL"], "https://edx.org")

        self.assertIsNone(LMSHandler.select_lms("https://unknown.io/course/123"))

        self.assertIsNone(LMSHandler.select_lms(None))
Пример #4
0
    def test_lms_edx_set_enrollment_error(self):
        """The enrollment attempt should return False if the server returns an error."""
        user = UserFactory(username="******")

        responses.add(
            responses.POST, "http://edx:8073/api/enrollment/v1/enrollment", status=404
        )
        is_enrolled = LMSHandler.set_enrollment(
            user, "http://edx:8073/courses/course-v1:edX+DemoX+Demo_Course/course/"
        )

        self.assertEqual(len(responses.calls), 1)
        self.assertFalse(is_enrolled)
Пример #5
0
    def test_lms_edx_get_enrollment_error(self):
        """Getting an enrollment should return None if the server returns an error."""
        responses.add(
            responses.GET,
            (
                "http://edx:8073/api/enrollment/v1/enrollment/"
                "teacher,course-v1:edX+DemoX+Demo_Course"
            ),
            status=404,
        )

        user = UserFactory(username="******")
        enrollment = LMSHandler.get_enrollment(
            user, "http://edx:8073/courses/course-v1:edX+DemoX+Demo_Course/course/"
        )

        self.assertEqual(len(responses.calls), 1)
        self.assertIsNone(enrollment)