Debian Codegraph @

tests/test_attach_base.py

raw symbols SCIP
1 # BSD 2-Clause License
2 #
3 # Apprise - Push Notification Library.
4 # Copyright (c) 2026, Chris Caron <[email protected]>
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright notice,
10 # this list of conditions and the following disclaimer.
11 #
12 # 2. Redistributions in binary form must reproduce the above copyright notice,
13 # this list of conditions and the following disclaimer in the documentation
14 # and/or other materials provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 # POSSIBILITY OF SUCH DAMAGE.
27
28 # Disable logging for a cleaner testing output
29 import logging
30 from unittest import mock
31
32 import pytest
33
34 from apprise.attachment.base import AttachBase
35
36 logging.disable(logging.CRITICAL)
37
38
39 def test_mimetype_initialization():
40 """
41 API: AttachBase() mimetype initialization
42
43 """
44 with (
45 mock.patch("mimetypes.init") as mock_init,
46 mock.patch("mimetypes.inited", True),
47 ):
48 AttachBase()
49 assert mock_init.call_count == 0
50
51 with (
52 mock.patch("mimetypes.init") as mock_init,
53 mock.patch("mimetypes.inited", False),
54 ):
55 AttachBase()
56 assert mock_init.call_count == 1
57
58
59 def test_attach_base():
60 """
61 API: AttachBase()
62
63 """
64 # an invalid mime-type
65 with pytest.raises(TypeError):
66 AttachBase(**{"mimetype": "invalid"})
67
68 # a valid mime-type does not cause an exception to throw
69 AttachBase(**{"mimetype": "image/png"})
70
71 # Create an object with no mimetype over-ride
72 obj = AttachBase()
73
74 # Get our url object
75 str(obj)
76
77 # We can not process name/path/mimetype at a Base level
78 with pytest.raises(NotImplementedError):
79 obj.download()
80
81 # Unsupported URLs are not parsed
82 assert AttachBase.parse_url(url="invalid://") is None
83
84 # Valid URL & Valid Format
85 results = AttachBase.parse_url(url="file://relative/path")
86 assert isinstance(results, dict)
87 # No mime is defined
88 assert results.get("mimetype") is None
89
90 # Valid URL & Valid Format with mime type set
91 results = AttachBase.parse_url(url="file://relative/path?mime=image/jpeg")
92 assert isinstance(results, dict)
93 # mime defined
94 assert results.get("mimetype") == "image/jpeg"
95 # We can retrieve our url
96 assert str(results)
97
98
99 def test_attach_base_parse_url_name_sanitization():
100 """
101 API: AttachBase.parse_url() ?name= sanitization
102
103 """
104 # Normal name -- returned as-is (lowercased)
105 results = AttachBase.parse_url(
106 url="file://path/to/file?name=thumbnail.jpg"
107 )
108 assert isinstance(results, dict)
109 assert results.get("name") == "thumbnail.jpg"
110
111 # Path traversal stripped; only the basename is kept
112 results = AttachBase.parse_url(url="file://path/to/file?name=/etc/passwd")
113 assert isinstance(results, dict)
114 assert results.get("name") == "passwd"
115
116 # Deep traversal -- same rule applies
117 results = AttachBase.parse_url(
118 url="file://path/to/file?name=../../secret.jpg"
119 )
120 assert isinstance(results, dict)
121 assert results.get("name") == "secret.jpg"
122
123 # Empty ?name= -- treated as not specified; key must be absent
124 results = AttachBase.parse_url(url="file://path/to/file?name=")
125 assert isinstance(results, dict)
126 assert "name" not in results
127
128 # Whitespace-only ?name= (URL-encoded spaces) -- treated as not specified
129 results = AttachBase.parse_url(url="file://path/to/file?name=%20%20%20")
130 assert isinstance(results, dict)
131 assert "name" not in results
132
133 # Directory-only path (no filename component) -- treated as not specified
134 results = AttachBase.parse_url(url="file://path/to/file?name=/")
135 assert isinstance(results, dict)
136 assert "name" not in results