diff --git a/connect/reports/renderers/pdf.py b/connect/reports/renderers/pdf.py index 8669b5f..05d89aa 100644 --- a/connect/reports/renderers/pdf.py +++ b/connect/reports/renderers/pdf.py @@ -1,5 +1,5 @@ # Copyright © 2021 CloudBlue. All rights reserved. - +import pathlib import os from datetime import datetime from functools import partial @@ -14,6 +14,8 @@ def local_fetcher(url, root_dir=None, template_dir=None, cwd=None): + tpl_dir_path = pathlib.Path(os.path.abspath(root_dir)) / pathlib.Path(template_dir) + tpl_dir_url = tpl_dir_path.as_uri() if url.startswith(f'file://{cwd}'): rel_path = os.path.relpath(url[7:], cwd) new_path = os.path.join( @@ -21,7 +23,7 @@ def local_fetcher(url, root_dir=None, template_dir=None, cwd=None): rel_path, ) url = f'file://{new_path}' - elif url.startswith(f'file://{os.path.abspath(os.path.join(root_dir, template_dir))}'): + elif url.startswith(tpl_dir_url): count = url.count(template_dir) if url.count(template_dir) > 1: url = url.replace(f'{template_dir}/', '', count - 1) diff --git a/tests/reports/renderers/test_pdf.py b/tests/reports/renderers/test_pdf.py index b6406c6..ab3576e 100644 --- a/tests/reports/renderers/test_pdf.py +++ b/tests/reports/renderers/test_pdf.py @@ -66,6 +66,16 @@ def test_local_fetcher(mocker, url, expected_url): def_fetcher.assert_called_once_with(expected_url) +def test_local_fetcher_root_with_spaces(mocker): + url = 'file:///root%20dir/template_dir/template_dir/image.png' + expected_url = 'file:///root%20dir/template_dir/image.png' + def_fetcher = mocker.patch('connect.reports.renderers.pdf.default_url_fetcher') + + local_fetcher(url, root_dir='/root dir', template_dir='template_dir', cwd='/tmp/my_temp_dir') + + def_fetcher.assert_called_once_with(expected_url) + + def test_generate_report(mocker, account_factory, report_factory, report_data): mocker.patch( 'connect.reports.renderers.pdf.Jinja2Renderer.generate_report',