def test_good_response(self, mock_eme, mock_request, mock_file): mock_request.return_value = mock.MagicMock(ok=True) core.save_to_file_or_error('test', 'test') assert_false(mock_eme.called) mock_request.assert_called_once_with('test', stream=True)
def test_bad_response_raises(self, mock_request, mock_file): mock_request.return_value = mock.Mock(ok=False, status_code=418) with assert_raises(exceptions.RenderFailureException): core.save_to_file_or_error('test', 'test') mock_request.assert_called_once_with('test', stream=True)
def test_bad_response(self, mock_eme, mock_request, mock_file): mock_request.return_value = mock.Mock(ok=False, status_code=418) core.save_to_file_or_error('test', 'test') mock_request.assert_called_once_with('test', stream=True) mock_eme.assert_called_once_with(418, dest_path='test', download_url='test')
def _build_rendered_html(download_url, cache_path, temp_path, public_download_url): """ :param str download_url: The url to download the file to be rendered :param str cache_path: Location to cache the rendered file :param str temp_path: Where the downloaded file will be cached """ if render_is_done_or_happening(cache_path, temp_path): return # Ensure our paths exists # Note: Ensures that cache directories have the same owner # as the files inside them ensure_path(os.path.split(temp_path)[0]) ensure_path(os.path.split(cache_path)[0]) rendered = None try: save_to_file_or_error(download_url, temp_path) except exceptions.RenderNotPossibleException as e: # Write out unavoidable errors rendered = e.renderable_error else: encoding = None # Workaround for https://github.com/CenterForOpenScience/osf.io/issues/2389 # Open text files as utf-8 # Don't specify an encoding for other filetypes. Otherwise filetypes # such as docx will break if get_file_extension(temp_path) in CODE_EXTENSIONS: encoding = 'utf-8' with codecs.open(temp_path, encoding=encoding) as temp_file: try: render_result = mfr.render(temp_file, src=public_download_url) # Rendered result rendered = _build_html(render_result) except MFRError as err: # Rendered MFR error rendered = render_mfr_error(err) # Cache rendered content with codecs.open(cache_path, 'w', 'utf-8') as render_result_cache: render_result_cache.write(rendered) # Cleanup when we're done os.remove(temp_path)
def _build_rendered_html(download_url, cache_path, temp_path, public_download_url): """ :param str download_url: The url to download the file to be rendered :param str cache_path: Location to cache the rendered file :param str temp_path: Where the downloaded file will be cached """ if render_is_done_or_happening(cache_path, temp_path): return # Ensure our paths exists # Note: Ensures that cache directories have the same owner # as the files inside them ensure_path(os.path.split(temp_path)[0]) ensure_path(os.path.split(cache_path)[0]) rendered = None try: save_to_file_or_error(download_url, temp_path) except exceptions.RenderNotPossibleException as e: # Write out unavoidable errors rendered = e.renderable_error else: with codecs.open(temp_path) as temp_file: # Try to render file try: render_result = mfr.render(temp_file, src=public_download_url) # Rendered result rendered = _build_html(render_result) except MFRError as err: # Rendered MFR error rendered = render_mfr_error(err) # Cache rendered content with codecs.open(cache_path, 'w', 'utf-8') as render_result_cache: render_result_cache.write(rendered) # Cleanup when we're done os.remove(temp_path)