def fetch_videos_from_course_blocks(self, course_id):
        logger.info("Fetching video ids from Course Blocks API...")
        try:
            api_base_url = settings.LMS_BASE_URL + 'api/courses/v1/'
        except AttributeError:
            logger.warning("LMS_BASE_URL is not configured! Cannot get video ids.")
            return None
        logger.info("Assuming the Course Blocks API is hosted at: %s", api_base_url)

        blocks_api = CourseBlocksApiClient(api_base_url, settings.COURSE_BLOCK_API_AUTH_TOKEN, timeout=5)
        return blocks_api.all_videos(course_id)
    def fetch_videos_from_course_blocks(self, course_id):
        logger.info("Fetching video ids from Course Blocks API...")
        try:
            api_base_url = settings.LMS_BASE_URL + 'api/courses/v1/'
        except AttributeError:
            logger.warning(
                "LMS_BASE_URL is not configured! Cannot get video ids.")
            return None
        logger.info("Assuming the Course Blocks API is hosted at: %s",
                    api_base_url)

        blocks_api = CourseBlocksApiClient(
            api_base_url, settings.COURSE_BLOCK_API_AUTH_TOKEN, timeout=5)
        return blocks_api.all_videos(course_id)
 def setUp(self, *args, **kwargs):  # pylint: disable=unused-argument
     self.client = CourseBlocksApiClient('http://example.com/', 'token', 5)
class ClientTests(TestCase):
    @mock.patch('analyticsdataserver.clients.EdxRestApiClient')
    def setUp(self, *args, **kwargs):  # pylint: disable=unused-argument
        self.client = CourseBlocksApiClient('http://example.com/', 'token', 5)

    @responses.activate
    def test_all_videos(self):
        responses.add(
            responses.GET,
            'http://example.com/blocks/',
            body=json.dumps({
                'blocks': {
                    'block-v1:edX+DemoX+Demo_Course+type@video+block@5c90cffecd9b48b188cbfea176bf7fe9':
                    {
                        'id':
                        'block-v1:edX+DemoX+Demo_Course+type@video+block@5c90cffecd9b48b188cbfea176bf7fe9'
                    },
                    'block-v1:edX+DemoX+Demo_Course+type@video+block@7e9b434e6de3435ab99bd3fb25bde807':
                    {
                        'id':
                        'block-v1:edX+DemoX+Demo_Course+type@video+block@7e9b434e6de3435ab99bd3fb25bde807'
                    }
                }
            }),
            status=200,
            content_type='application/json')
        videos = self.client.all_videos('course_id')
        self.assertListEqual(
            videos, [{
                'video_id': 'course_id|5c90cffecd9b48b188cbfea176bf7fe9',
                'video_module_id': '5c90cffecd9b48b188cbfea176bf7fe9'
            }, {
                'video_id': 'course_id|7e9b434e6de3435ab99bd3fb25bde807',
                'video_module_id': '7e9b434e6de3435ab99bd3fb25bde807'
            }])

    @responses.activate
    @mock.patch('analyticsdataserver.clients.logger')
    def test_all_videos_401(self, logger):
        responses.add(responses.GET,
                      'http://example.com/blocks/',
                      status=401,
                      content_type='application/json')
        videos = self.client.all_videos('course_id')
        logger.warning.assert_called_with(
            'Course Blocks API failed to return video ids (%s). ' +
            'See README for instructions on how to authenticate the API with your local LMS.',
            401)
        self.assertEqual(videos, None)

    @responses.activate
    @mock.patch('analyticsdataserver.clients.logger')
    def test_all_videos_404(self, logger):
        responses.add(responses.GET,
                      'http://example.com/blocks/',
                      status=404,
                      content_type='application/json')
        videos = self.client.all_videos('course_id')
        logger.warning.assert_called_with(
            'Course Blocks API failed to return video ids (%s). ' +
            'Does the course exist in the LMS?', 404)
        self.assertEqual(videos, None)

    @responses.activate
    @mock.patch('analyticsdataserver.clients.logger')
    def test_all_videos_500(self, logger):
        responses.add(responses.GET,
                      'http://example.com/blocks/',
                      status=418,
                      content_type='application/json')
        videos = self.client.all_videos('course_id')
        logger.warning.assert_called_with(
            'Course Blocks API failed to return video ids (%s).', 418)
        self.assertEqual(videos, None)

    @responses.activate
    @mock.patch('analyticsdataserver.clients.logger')
    def test_all_videos_connection_error(self, logger):
        exception = ConnectionError('LMS is dead')
        responses.add(responses.GET,
                      'http://example.com/blocks/',
                      body=exception)
        videos = self.client.all_videos('course_id')
        logger.warning.assert_called_with(
            'Course Blocks API request failed. Is the LMS running?: ' +
            str(exception))
        self.assertEqual(videos, None)

    @responses.activate
    def test_all_videos_pass_through_bad_id(self):
        responses.add(
            responses.GET,
            'http://example.com/blocks/',
            body=json.dumps({
                'blocks': {
                    'block-v1:edX+DemoX+Demo_Course+type@video+block@5c90cffecd9b48b188cbfea176bf7fe9':
                    {
                        'id': 'bad_key'
                    },
                    'block-v1:edX+DemoX+Demo_Course+type@video+block@7e9b434e6de3435ab99bd3fb25bde807':
                    {
                        'id': 'bad_key'
                    }
                }
            }),
            status=200,
            content_type='application/json')
        responses.add(responses.GET,
                      'http://example.com/blocks/',
                      status=200,
                      content_type='application/json')
        videos = self.client.all_videos('course_id')
        self.assertListEqual(videos, [{
            'video_id': 'course_id|bad_key',
            'video_module_id': 'bad_key'
        }, {
            'video_id': 'course_id|bad_key',
            'video_module_id': 'bad_key'
        }])
Exemplo n.º 5
0
 def setUp(self, restApiClientMock):
     self.client = CourseBlocksApiClient('http://example.com/', 'token', 5)
Exemplo n.º 6
0
 def setUp(self, *args, **kwargs):  # pylint: disable=unused-argument
     self.client = CourseBlocksApiClient('http://example.com/', 'token', 5)
Exemplo n.º 7
0
class ClientTests(TestCase):
    @mock.patch('analyticsdataserver.clients.EdxRestApiClient')
    def setUp(self, *args, **kwargs):  # pylint: disable=unused-argument
        self.client = CourseBlocksApiClient('http://example.com/', 'token', 5)

    @responses.activate
    def test_all_videos(self):
        responses.add(responses.GET, 'http://example.com/blocks/', body=json.dumps({'blocks': {
            'block-v1:edX+DemoX+Demo_Course+type@video+block@5c90cffecd9b48b188cbfea176bf7fe9': {
                'id': 'block-v1:edX+DemoX+Demo_Course+type@video+block@5c90cffecd9b48b188cbfea176bf7fe9'
            },
            'block-v1:edX+DemoX+Demo_Course+type@video+block@7e9b434e6de3435ab99bd3fb25bde807': {
                'id': 'block-v1:edX+DemoX+Demo_Course+type@video+block@7e9b434e6de3435ab99bd3fb25bde807'
            }
        }}), status=200, content_type='application/json')
        videos = self.client.all_videos('course_id')
        self.assertListEqual(videos, [
            {
                'video_id': 'course_id|5c90cffecd9b48b188cbfea176bf7fe9',
                'video_module_id': '5c90cffecd9b48b188cbfea176bf7fe9'
            },
            {
                'video_id': 'course_id|7e9b434e6de3435ab99bd3fb25bde807',
                'video_module_id': '7e9b434e6de3435ab99bd3fb25bde807'
            }
        ])

    @responses.activate
    @mock.patch('analyticsdataserver.clients.logger')
    def test_all_videos_401(self, logger):
        responses.add(responses.GET, 'http://example.com/blocks/', status=401, content_type='application/json')
        videos = self.client.all_videos('course_id')
        logger.warning.assert_called_with(
            'Course Blocks API failed to return video ids (%s). ' +
            'See README for instructions on how to authenticate the API with your local LMS.', 401)
        self.assertEqual(videos, None)

    @responses.activate
    @mock.patch('analyticsdataserver.clients.logger')
    def test_all_videos_404(self, logger):
        responses.add(responses.GET, 'http://example.com/blocks/', status=404, content_type='application/json')
        videos = self.client.all_videos('course_id')
        logger.warning.assert_called_with('Course Blocks API failed to return video ids (%s). ' +
                                          'Does the course exist in the LMS?', 404)
        self.assertEqual(videos, None)

    @responses.activate
    @mock.patch('analyticsdataserver.clients.logger')
    def test_all_videos_500(self, logger):
        responses.add(responses.GET, 'http://example.com/blocks/', status=418, content_type='application/json')
        videos = self.client.all_videos('course_id')
        logger.warning.assert_called_with('Course Blocks API failed to return video ids (%s).', 418)
        self.assertEqual(videos, None)

    @responses.activate
    @mock.patch('analyticsdataserver.clients.logger')
    def test_all_videos_connection_error(self, logger):
        exception = ConnectionError('LMS is dead')
        responses.add(responses.GET, 'http://example.com/blocks/', body=exception)
        videos = self.client.all_videos('course_id')
        logger.warning.assert_called_with('Course Blocks API request failed. Is the LMS running?: ' + str(exception))
        self.assertEqual(videos, None)

    @responses.activate
    def test_all_videos_pass_through_bad_id(self):
        responses.add(responses.GET, 'http://example.com/blocks/', body=json.dumps({'blocks': {
            'block-v1:edX+DemoX+Demo_Course+type@video+block@5c90cffecd9b48b188cbfea176bf7fe9': {
                'id': 'bad_key'
            },
            'block-v1:edX+DemoX+Demo_Course+type@video+block@7e9b434e6de3435ab99bd3fb25bde807': {
                'id': 'bad_key'
            }
        }}), status=200, content_type='application/json')
        responses.add(responses.GET, 'http://example.com/blocks/', status=200, content_type='application/json')
        videos = self.client.all_videos('course_id')
        self.assertListEqual(videos, [
            {
                'video_id': 'course_id|bad_key',
                'video_module_id': 'bad_key'
            },
            {
                'video_id': 'course_id|bad_key',
                'video_module_id': 'bad_key'
            }
        ])