# Path: mfr/core/provider.py
# class ProviderMetadata:
# 
#     def __init__(self, name, ext, content_type, unique_key, download_url, stable_id=None):
#         self.name = name
#         self.ext = ext
#         self.content_type = content_type
#         self.unique_key = unique_key
#         self.download_url = download_url
#         self.stable_id = stable_id
# 
#     def serialize(self):
#         return {
#             'name': self.name,
#             'ext': self.ext,
#             'content_type': self.content_type,
#             'unique_key': str(self.unique_key),
#             'download_url': str(self.download_url),
#             'stable_id': None if self.stable_id is None else str(self.stable_id),
#         }
# 
# Path: mfr/extensions/pdf/settings.py
# EXPORT_TYPE = config.get('EXPORT_TYPE', 'pdf')
# EXPORT_MAXIMUM_SIZE = config.get('EXPORT_MAXIMUM_SIZE', '1200x1200')
# ENABLE_HYPOTHESIS = config.get_bool('ENABLE_HYPOTHESIS', True)
# EXPORT_SUPPORTED = config.get('EXPORT_SUPPORTED', '.tiff .tif').split(' ')
# EXPORT_MAX_PAGES = int(config.get('EXPORT_MAX_PAGES', 40))
# 
# Path: mfr/extensions/utils.py
# def escape_url_for_template(url: str, logs: bool=True) -> str:
#     """Escape (URL Encode) single and double quote(s) for the given URL.
# 
#     Download and export URLs may end up not properly encoded right before they are about to be sent
#     to the mako template due to issues including (but not limited to) (1) ``furl`` dropping encoding
#     for single quote (2) URL (provided by users or constructed by scripts) not having the correct
#     encoding. This helper method must be called for each render request that sends URL to the mako
#     template.
# 
#     :param str url: the URL to be sent to the mako template
#     :param bool logs: whether to enable warnings, default is `True` and is set to `False` for tests
#     :return: the properly encoded URL
#     """
# 
#     safe_url = url.replace('"', '%22').replace("'", '%27')
#     if url != safe_url and logs:
#         logger.warning('Unsafe URL containing unescaped single (double) quote(s) has been replaced')
#     return safe_url


import furl
import pytest
from mfr.core.provider import ProviderMetadata
from mfr.extensions.pdf import settings, PdfRenderer
from mfr.extensions.utils import escape_url_for_template
    return PdfRenderer(tif_metadata, tif_file_path, tif_url, assets_url, export_url)


class TestPdfRenderer:

    def test_render_pdf(self, renderer, metadata, assets_url):
        body = renderer.render()
        assert '<base href="{}/{}/web/" target="_blank">'.format(assets_url, 'pdf') in body
        assert '<div id="viewer" class="pdfViewer"></div>' in body
        assert 'DEFAULT_URL = \'{}\''.format(metadata.download_url) in body

    def test_render_pdf_with_single_quote_in_name(self, assets_url):

        download_url = 'http://wb.osf.io/file/te\'st.pdf?token=1234'
        safe_download_url = 'http://wb.osf.io/file/te%27st.pdf?token=1234'

        metadata = ProviderMetadata('te\'st', '.pdf', 'text/plain', '1234', download_url)
        renderer = PdfRenderer(metadata, '/tmp/te\'st.pdf', 'http://osf.io/file/te\'st.pdf',
                               assets_url,
                               'http://mfr.osf.io/export?url=http://osf.io/file/te\'st.pdf')

        body = renderer.render()

        assert '<base href="{}/{}/web/" target="_blank">'.format(assets_url, 'pdf') in body
        assert '<div id="viewer" class="pdfViewer"></div>' in body
        assert 'DEFAULT_URL = \'{}\''.format(download_url) not in body
        assert 'DEFAULT_URL = \'{}\''.format(safe_download_url) in body

    def test_render_tif(self, tif_renderer, assets_url):
        exported_url = furl.furl(tif_renderer.export_url)

# Path: mfr/core/provider.py
# class ProviderMetadata:
# 
#     def __init__(self, name, ext, content_type, unique_key, download_url, stable_id=None):
#         self.name = name
#         self.ext = ext
#         self.content_type = content_type
#         self.unique_key = unique_key
#         self.download_url = download_url
#         self.stable_id = stable_id
# 
#     def serialize(self):
#         return {
#             'name': self.name,
#             'ext': self.ext,
#             'content_type': self.content_type,
#             'unique_key': str(self.unique_key),
#             'download_url': str(self.download_url),
#             'stable_id': None if self.stable_id is None else str(self.stable_id),
#         }
# 
# Path: mfr/extensions/pdf/settings.py
# EXPORT_TYPE = config.get('EXPORT_TYPE', 'pdf')
# EXPORT_MAXIMUM_SIZE = config.get('EXPORT_MAXIMUM_SIZE', '1200x1200')
# ENABLE_HYPOTHESIS = config.get_bool('ENABLE_HYPOTHESIS', True)
# EXPORT_SUPPORTED = config.get('EXPORT_SUPPORTED', '.tiff .tif').split(' ')
# EXPORT_MAX_PAGES = int(config.get('EXPORT_MAX_PAGES', 40))
# 
# Path: mfr/extensions/utils.py
# def escape_url_for_template(url: str, logs: bool=True) -> str:
#     """Escape (URL Encode) single and double quote(s) for the given URL.
# 
#     Download and export URLs may end up not properly encoded right before they are about to be sent
#     to the mako template due to issues including (but not limited to) (1) ``furl`` dropping encoding
#     for single quote (2) URL (provided by users or constructed by scripts) not having the correct
#     encoding. This helper method must be called for each render request that sends URL to the mako
#     template.
# 
#     :param str url: the URL to be sent to the mako template
#     :param bool logs: whether to enable warnings, default is `True` and is set to `False` for tests
#     :return: the properly encoded URL
#     """
# 
#     safe_url = url.replace('"', '%22').replace("'", '%27')
#     if url != safe_url and logs:
#         logger.warning('Unsafe URL containing unescaped single (double) quote(s) has been replaced')
#     return safe_url


# Path: tests/extensions/pdf/test_renderer.py
import furl
import pytest
from mfr.core.provider import ProviderMetadata
from mfr.extensions.pdf import settings, PdfRenderer
from mfr.extensions.utils import escape_url_for_template

    return PdfRenderer(tif_metadata, tif_file_path, tif_url, assets_url, export_url)


class TestPdfRenderer:

    def test_render_pdf(self, renderer, metadata, assets_url):
        body = renderer.render()
        assert '<base href="{}/{}/web/" target="_blank">'.format(assets_url, 'pdf') in body
        assert '<div id="viewer" class="pdfViewer"></div>' in body
        assert 'DEFAULT_URL = \'{}\''.format(metadata.download_url) in body

    def test_render_pdf_with_single_quote_in_name(self, assets_url):

        download_url = 'http://wb.osf.io/file/te\'st.pdf?token=1234'
        safe_download_url = 'http://wb.osf.io/file/te%27st.pdf?token=1234'

        metadata = ProviderMetadata('te\'st', '.pdf', 'text/plain', '1234', download_url)
        renderer = PdfRenderer(metadata, '/tmp/te\'st.pdf', 'http://osf.io/file/te\'st.pdf',
                               assets_url,
                               'http://mfr.osf.io/export?url=http://osf.io/file/te\'st.pdf')

        body = renderer.render()

        assert '<base href="{}/{}/web/" target="_blank">'.format(assets_url, 'pdf') in body
        assert '<div id="viewer" class="pdfViewer"></div>' in body
        assert 'DEFAULT_URL = \'{}\''.format(download_url) not in body
        assert 'DEFAULT_URL = \'{}\''.format(safe_download_url) in body

    def test_render_tif(self, tif_renderer, assets_url):
        exported_url = furl.furl(tif_renderer.export_url)

        exported_url.args['format'] = '{}.{}'.format(settings.EXPORT_MAXIMUM_SIZE,