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

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 

6 

7import pytest 

8 

9from intelligence_toolkit.helpers.wkhtmltopdf import config_pdfkit, is_in_path 

10 

11 

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) 

16 

17 with patch("os.path.exists") as mock_exists: 

18 mock_exists.return_value = True 

19 

20 result = is_in_path("wkhtmltopdf") 

21 

22 assert result is True 

23 

24 

25def test_is_in_path_executable_not_exists(monkeypatch): 

26 test_path = "/usr/bin:/usr/local/bin" 

27 monkeypatch.setenv("PATH", test_path) 

28 

29 with patch("os.path.exists") as mock_exists: 

30 mock_exists.return_value = False 

31 

32 result = is_in_path("wkhtmltopdf") 

33 

34 assert result is False 

35 

36 

37def test_is_in_path_empty_path(monkeypatch): 

38 monkeypatch.setenv("PATH", "") 

39 

40 result = is_in_path("wkhtmltopdf") 

41 

42 assert result is False 

43 

44 

45def test_is_in_path_with_quotes(monkeypatch): 

46 test_path = '"/usr/bin":/usr/local/bin' 

47 monkeypatch.setenv("PATH", test_path) 

48 

49 with patch("os.path.exists") as mock_exists: 

50 mock_exists.return_value = True 

51 

52 result = is_in_path("wkhtmltopdf") 

53 

54 assert result is True 

55 # Verify that quotes are stripped 

56 mock_exists.assert_called() 

57 

58 

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: 

62 

63 mock_is_in_path.return_value = True 

64 

65 config_pdfkit() 

66 

67 # When executable is in PATH, empty string should be passed 

68 mock_config.assert_called_once_with(wkhtmltopdf="") 

69 

70 

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: 

75 

76 mock_is_in_path.return_value = False 

77 mock_constants.PDF_WKHTMLTOPDF_PATH = "C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe" 

78 

79 config_pdfkit() 

80 

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 ) 

85 

86 

87def test_pdfkit_options_structure(): 

88 # Test that pdfkit_options can be imported and has expected structure 

89 from intelligence_toolkit.helpers import wkhtmltopdf 

90 

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') 

94 

95 # Check that encoding is set correctly 

96 if 'encoding' in wkhtmltopdf.pdfkit_options: 

97 assert wkhtmltopdf.pdfkit_options['encoding'] == "UTF-8" 

98 

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