def get(self): """ Substitute requests.get method. """ if self.event == 'no_xml_data': self.return_value = ResponseStub(status_code=200, body='{}') else: self.return_value = ResponseStub(status_code=200, body=self._xml) return lambda x: self.return_value
def api_client_get(self): """ Mock for `api_client` method. """ if self.event == 'fetch_transcripts_exception': self.side_effect = self.mock() elif self.event == 'no_captions_data': self.return_value = ResponseStub(status_code=200, body=json.dumps(self._response)) else: ret = copy(self._response) ret['text_tracks'] = self.transcripts self.return_value = ResponseStub(status_code=200, body=json.dumps(ret)) return self
def test_get_3pm_transcripts_list_success(self, requests_get_mock): """ Test fetching of the list of available 3PlayMedia transcripts (success case). """ # Arrange: test_json = [{"test": "json_string"}] test_message = _("3PlayMedia transcripts fetched successfully.") test_feedback = {'status': Status.success, 'message': test_message} requests_get_mock.return_value = ResponseStub( body=test_json, ok=True ) file_id = 'test_file_id' api_key = 'test_api_key' test_api_url = 'https://static.3playmedia.com/files/test_file_id/transcripts?apikey=test_api_key' # Act: feedback, transcripts_list = self.xblock.get_3pm_transcripts_list(file_id, api_key) # Assert: self.assertTrue(requests_get_mock.ok) self.assertTrue(requests_get_mock.json.assert_called) self.assertEqual(transcripts_list, test_json) self.assertEqual(feedback, test_feedback) requests_get_mock.assert_called_once_with(test_api_url)
def test_wistia_get_default_transcripts_baberlfish(self, requests_get_mock, babel_mock): """ Test Wistia's default transcripts fetching (babelfish fallback). """ # Arrange # ref: https://wistia.com/doc/data-api#captions_index test_api_data = [{'language': 'eng', 'english_name': 'English', }] requests_get_mock.return_value = ResponseStub(status_code=200, body=test_api_data) babel_mock.return_value = lang_code_mock = Mock() type(lang_code_mock).alpha2 = PropertyMock(side_effect=ValueError()) kwargs = { 'video_id': 'test_video_id', 'token': 'test_token' } test_url = 'https://api.wistia.com/v1/medias/test_video_id/captions.json?api_password=test_token' test_download_url = 'http://api.wistia.com/v1/medias/test_video_id/captions/eng.json?api_password=test_token' test_message = _('Success.') test_transcripts = [{ 'lang': babel_mock.fromalpha3b().alpha2, 'label': 'English', 'url': test_download_url, 'source': TranscriptSource.DEFAULT }] with patch.object(self.wistia_player, 'get_transcript_language_parameters') as get_params_mock: get_params_mock.return_value = ('en', 'English') # Act transcripts, message = self.wistia_player.get_default_transcripts(**kwargs) # Assert requests_get_mock.assert_called_once_with(test_url) self.assertEqual(transcripts, test_transcripts) self.assertEqual(message, test_message)
class WistiaAuthMock(RequestsMock): """ Wistia auth mock class. """ return_value = ResponseStub(status_code=200, body='') outcomes = (('not_authorized', { 'auth_data': { 'token': 'some_token' }, 'error_message': 'Authentication failed.' }), ('success', { 'auth_data': { 'token': 'some_token' }, 'error_message': '' })) to_return = ['auth_data', 'error_message'] def get(self): """ Substitute requests.get method. """ if self.event == 'not_authorized': self.return_value = ResponseStub(status_code=401) return lambda x: self.return_value
def get(self): """ Substitute requests.get method. """ if self.event == 'not_authorized': self.return_value = ResponseStub(status_code=401) return lambda x: self.return_value
def test_api_client_get_400(self, requests_get_mock): """ Test Vimeo's API client GET method if status 400 returned. """ # Arrange requests_get_mock.return_value = ResponseStub(status_code=400) # Act & Assert self.assertRaises(vimeo.VimeoApiClientError, self.vimeo_api_client.get, url='test_url')
def test_wistia_download_default_transcript_success(self, requests_get_mock): """ Test Wistia's default transcripts downloading (positive scenario). """ # Arrange test_api_data = {'text': 'test_content'} test_url = "test_url" test_language_code = "test_language_code" requests_get_mock.return_value = ResponseStub(status_code=200, body=test_api_data) # Act content = self.wistia_player.download_default_transcript(test_url, test_language_code) # Assert self.assertEqual(content, 'test_content') requests_get_mock.assert_called_once_with(test_url)
def test_api_client_get_200(self, requests_get_mock): """ Test Vimeo's API client GET method if status Ok returned. """ # Arrange test_body = {'test': 'body'} requests_get_mock.return_value = ResponseStub(status_code=200, body=test_body) # Act response = self.vimeo_api_client.get(url='test_url') # Assert requests_get_mock.assert_called_with('test_url', headers={ 'Accept': 'application/json', 'Authorization': 'Bearer test_token' }) self.assertEqual(response, test_body)
def test_wistia_get_default_transcripts_wrong_video( self, requests_get_mock): """ Test Wistia's default transcripts fetching (not found case). """ # Arrange kwargs = {'video_id': 'test_wrong_video_id', 'token': 'test_token'} test_message = "Wistia video test_wrong_video_id doesn't exist." test_url = 'https://api.wistia.com/v1/medias/test_wrong_video_id/captions.json?api_password=test_token' requests_get_mock.return_value = ResponseStub(status_code=404, body=[]) # Act transcripts, message = self.wistia_player.get_default_transcripts( **kwargs) # Assert requests_get_mock.assert_called_once_with(test_url) self.assertEqual(transcripts, []) self.assertEqual(message, test_message)
def test_wistia_get_default_transcripts_bad_json(self, requests_get_mock): """ Test Wistia's default transcripts fetching (can't parse response JSON). """ # Arrange kwargs = {'video_id': 'test_video_id', 'token': 'test_token'} test_message = "For now, video platform doesn't have any timed transcript for this video." test_url = 'https://api.wistia.com/v1/medias/test_video_id/captions.json?api_password=test_token' requests_get_mock.return_value = response_mock = ResponseStub( status_code=200) response_mock.json = Mock(side_effect=ValueError()) # Act transcripts, message = self.wistia_player.get_default_transcripts( **kwargs) # Assert requests_get_mock.assert_called_once_with(test_url) self.assertEqual(transcripts, []) self.assertEqual(message, test_message)
def test_fetch_single_3pm_translation_success(self, requests_get_mock, player_mock): """ Test single 3PlayMedia transcript fetching (success case). """ # Arrange: test_lang_id = '1' test_transcript_text = 'test_transcript_text' test_format = 51 test_transcript_id = 'test_id' test_video_id = 'test_video_id' test_source = '3play-media' file_id = 'test_file_id' api_key = 'test_api_key' test_transcript_data = {'id': test_transcript_id, 'language_id': test_lang_id} test_lang_code = TPMApiLanguage(test_lang_id) test_api_url = 'https://static.3playmedia.com/files/test_file_id/transcripts/test_id?' \ 'apikey=test_api_key&format_id=51' requests_get_mock.return_value = ResponseStub(body=test_transcript_text) media_id_mock = player_mock.return_value.media_id media_id_mock.return_value = test_video_id self.xblock.threeplaymedia_file_id = file_id self.xblock.threeplaymedia_apikey = api_key test_args = [ test_transcript_id, test_lang_code.name, test_lang_code.iso_639_1_code, test_lang_id, test_transcript_text, test_format, test_video_id, test_source, test_api_url ] # Act: transcript = self.xblock.fetch_single_3pm_translation(test_transcript_data) # Assert: self.assertEqual(transcript, Transcript(*test_args))
def test_brightcove_get_default_transcripts_no_text( self, requests_get_mock): """ Test Brightcove's default transcripts fetching (empty text fetched). """ # Arrange self.bc_player.api_key = 'test_api_key' self.bc_player.api_secret = 'test_api_secret' kwargs = {'account_id': 'test_account_id', 'video_id': 'test_video_id'} test_message = "No timed transcript may be fetched from a video platform." test_url = 'https://cms.api.brightcove.com/v1/accounts/test_account_id/videos/test_video_id' test_headers = {'Authorization': 'Bearer None'} requests_get_mock.return_value = ResponseStub(status_code=200, body='') # Act transcripts, message = self.bc_player.get_default_transcripts(**kwargs) # Assert requests_get_mock.assert_called_once_with(test_url, headers=test_headers) self.assertEqual(transcripts, []) self.assertEqual(message, test_message)
def get(self): """ Substitute requests.get method. """ self.return_value = ResponseStub(status_code=200, body=self._vtt) return lambda x: self.return_value