Debian Codegraph @

tests/test_attach_base.py

raw
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 from apprise.exception import AppriseImproperlyConfigured
36
37 logging.disable(logging.CRITICAL)
38
39
40 def test_mimetype_initialization():
41 """
42 API: AttachBase() mimetype initialization
43
44 """
45 with (
46 mock.patch("mimetypes.init") as mock_init,
47 mock.patch("mimetypes.inited", True),
48 ):
49 AttachBase()
50 assert mock_init.call_count == 0
51
52 with (
53 mock.patch("mimetypes.init") as mock_init,
54 mock.patch("mimetypes.inited", False),
55 ):
56 AttachBase()
57 assert mock_init.call_count == 1
58
59
60 def test_attach_base():
61 """
62 API: AttachBase()
63
64 """
65 # an invalid mime-type
66 with pytest.raises(AppriseImproperlyConfigured):
67 AttachBase(**{"mimetype": "invalid"})
68
69 # a valid mime-type does not cause an exception to throw
70 AttachBase(**{"mimetype": "image/png"})
71
72 # Create an object with no mimetype over-ride
73 obj = AttachBase()
74
75 # Get our url object
76 str(obj)
77
78 # We can not process name/path/mimetype at a Base level
79 with pytest.raises(NotImplementedError):
80 obj.download()
81
82 # Unsupported URLs are not parsed
83 assert AttachBase.parse_url(url="invalid://") is None
84
85 # Valid URL & Valid Format
86 results = AttachBase.parse_url(url="file://relative/path")
87 assert isinstance(results, dict)
88 # No mime is defined
89 assert results.get("mimetype") is None
90
91 # Valid URL & Valid Format with mime type set
92 results = AttachBase.parse_url(url="file://relative/path?mime=image/jpeg")
93 assert isinstance(results, dict)
94 # mime defined
95 assert results.get("mimetype") == "image/jpeg"
96 # We can retrieve our url
97 assert str(results)
98
99
100 def test_attach_base_parse_url_name_sanitization():
101 """
102 API: AttachBase.parse_url() ?name= sanitization
103
104 """
105 # Normal name -- returned as-is (lowercased)
106 results = AttachBase.parse_url(
107 url="file://path/to/file?name=thumbnail.jpg"
108 )
109 assert isinstance(results, dict)
110 assert results.get("name") == "thumbnail.jpg"
111
112 # Path traversal stripped; only the basename is kept
113 results = AttachBase.parse_url(url="file://path/to/file?name=/etc/passwd")
114 assert isinstance(results, dict)
115 assert results.get("name") == "passwd"
116
117 # Deep traversal -- same rule applies
118 results = AttachBase.parse_url(
119 url="file://path/to/file?name=../../secret.jpg"
120 )
121 assert isinstance(results, dict)
122 assert results.get("name") == "secret.jpg"
123
124 # Empty ?name= -- treated as not specified; key must be absent
125 results = AttachBase.parse_url(url="file://path/to/file?name=")
126 assert isinstance(results, dict)
127 assert "name" not in results
128
129 # Whitespace-only ?name= (URL-encoded spaces) -- treated as not specified
130 results = AttachBase.parse_url(url="file://path/to/file?name=%20%20%20")
131 assert isinstance(results, dict)
132 assert "name" not in results
133
134 # Directory-only path (no filename component) -- treated as not specified
135 results = AttachBase.parse_url(url="file://path/to/file?name=/")
136 assert isinstance(results, dict)
137 assert "name" not in results