"""Test for the htmltopdf module."""
import os
import stat
from unittest import mock
import pytest
from salessheets import config
from salessheets.utils import htmltopdf
@pytest.mark.parametrize('html, output_pdf', [
('filedoesntexist', 'pdfs/result.pdf'),
('tests/utils/valid.html', 'nonexistentdirectory/result.pdf')
])
def test_converting_invalid_html_and_pdf(html, output_pdf):
"""Test converting with non-existent html file and wrong pdf path."""
result = htmltopdf.convert_html_to_pdf(html, output_pdf)
assert not result
@pytest.mark.skip(reason="Passes locally but fails inside docker.")
@pytest.mark.parametrize('html', [
'tests/utils/valid.html'
])
def test_converting_no_write_access_to_pdf_directory(html, create_test_dir):
"""Test convert_html_to_pdf with output_pdf directory without write rights.
Function should raise NoWriteAccessException.
"""
test_dir = create_test_dir
output_pdf = '{path}/output.pdf'.format(path=test_dir)
os.chmod(str(test_dir), stat.S_IREAD)
result = htmltopdf.convert_html_to_pdf(html, output_pdf)
os.chmod(str(test_dir), stat.S_IRWXU | stat.S_IRWXG | stat.S_IROTH)
assert not result
@mock.patch('subprocess.check_call', new=mock.MagicMock())
@pytest.mark.parametrize('html', [
os.path.join(config.ROOT_PATH, 'tests', 'utils', 'valid.html')
])
def test_converting_success(html, create_test_dir):
"""Test convert_html_to_pdf with successful result."""
test_dir = create_test_dir
output_pdf = '{path}/output.pdf'.format(path=test_dir)
result = htmltopdf.convert_html_to_pdf(html, output_pdf)
assert result