Coverage for intelligence_toolkit/tests/unit/helpers/test_wkhtmltopdf.py: 100%
48 statements
« prev ^ index » next coverage.py v7.10.7, created at 2025-10-16 13:41 -0300
« prev ^ index » next coverage.py v7.10.7, created at 2025-10-16 13:41 -0300
1# Copyright (c) 2024 Microsoft Corporation. All rights reserved.
2# Licensed under the MIT license. See LICENSE file in the project.
3#
4import os
5from unittest.mock import MagicMock, patch
7import pytest
9from intelligence_toolkit.helpers.wkhtmltopdf import config_pdfkit, is_in_path
12def test_is_in_path_executable_exists(monkeypatch):
13 # Mock PATH with a directory containing the executable
14 test_path = "/usr/bin:/usr/local/bin"
15 monkeypatch.setenv("PATH", test_path)
17 with patch("os.path.exists") as mock_exists:
18 mock_exists.return_value = True
20 result = is_in_path("wkhtmltopdf")
22 assert result is True
25def test_is_in_path_executable_not_exists(monkeypatch):
26 test_path = "/usr/bin:/usr/local/bin"
27 monkeypatch.setenv("PATH", test_path)
29 with patch("os.path.exists") as mock_exists:
30 mock_exists.return_value = False
32 result = is_in_path("wkhtmltopdf")
34 assert result is False
37def test_is_in_path_empty_path(monkeypatch):
38 monkeypatch.setenv("PATH", "")
40 result = is_in_path("wkhtmltopdf")
42 assert result is False
45def test_is_in_path_with_quotes(monkeypatch):
46 test_path = '"/usr/bin":/usr/local/bin'
47 monkeypatch.setenv("PATH", test_path)
49 with patch("os.path.exists") as mock_exists:
50 mock_exists.return_value = True
52 result = is_in_path("wkhtmltopdf")
54 assert result is True
55 # Verify that quotes are stripped
56 mock_exists.assert_called()
59def test_config_pdfkit_executable_in_path():
60 with patch("intelligence_toolkit.helpers.wkhtmltopdf.is_in_path") as mock_is_in_path, \
61 patch("intelligence_toolkit.helpers.wkhtmltopdf.pdfkit.configuration") as mock_config:
63 mock_is_in_path.return_value = True
65 config_pdfkit()
67 # When executable is in PATH, empty string should be passed
68 mock_config.assert_called_once_with(wkhtmltopdf="")
71def test_config_pdfkit_executable_not_in_path():
72 with patch("intelligence_toolkit.helpers.wkhtmltopdf.is_in_path") as mock_is_in_path, \
73 patch("intelligence_toolkit.helpers.wkhtmltopdf.pdfkit.configuration") as mock_config, \
74 patch("intelligence_toolkit.helpers.wkhtmltopdf.constants") as mock_constants:
76 mock_is_in_path.return_value = False
77 mock_constants.PDF_WKHTMLTOPDF_PATH = "C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe"
79 config_pdfkit()
81 # When executable is not in PATH, configured path should be used
82 mock_config.assert_called_once_with(
83 wkhtmltopdf="C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe"
84 )
87def test_pdfkit_options_structure():
88 # Test that pdfkit_options can be imported and has expected structure
89 from intelligence_toolkit.helpers import wkhtmltopdf
91 # This will fail if PDF_MARGIN_INCHES doesn't exist, but we can test the structure
92 # that should exist
93 assert hasattr(wkhtmltopdf, 'pdfkit_options')
95 # Check that encoding is set correctly
96 if 'encoding' in wkhtmltopdf.pdfkit_options:
97 assert wkhtmltopdf.pdfkit_options['encoding'] == "UTF-8"
99 # Check that enable-local-file-access is set
100 if 'enable-local-file-access' in wkhtmltopdf.pdfkit_options:
101 assert wkhtmltopdf.pdfkit_options['enable-local-file-access'] is True