Debian Codegraph @

tests/test_attach_memory.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 import re
31 import urllib
32
33 import pytest
34
35 from apprise import AppriseAttachment, exception
36 from apprise.attachment.base import AttachBase
37 from apprise.attachment.memory import AttachMemory
38 from apprise.common import ContentLocation
39
40 logging.disable(logging.CRITICAL)
41
42
43 def test_attach_memory_parse_url():
44 """
45 API: AttachMemory().parse_url()
46
47 """
48
49 # Bad Entry
50 assert AttachMemory.parse_url(object) is None
51
52 # Our filename is detected automatically
53 assert AttachMemory.parse_url("memory://")
54
55 # pass our content in as a string
56 mem = AttachMemory(content="string")
57 # it loads a string type by default
58 assert mem.mimetype == "text/plain"
59 # Our filename is automatically generated (with .txt)
60 assert re.match(r"^[a-z0-9-]+\.txt$", mem.name, re.I)
61
62 # open our file
63 with mem as fp:
64 assert fp.getbuffer().nbytes == len(mem)
65
66 # pass our content in as a string
67 mem = AttachMemory(
68 content="<html/>", name="test.html", mimetype="text/html"
69 )
70 # it loads a string type by default
71 assert mem.mimetype == "text/html"
72 assert mem.name == "test.html"
73
74 # Stub function
75 assert mem.download()
76
77 with pytest.raises(TypeError):
78 # garbage in, garbage out
79 AttachMemory(content=3)
80
81 # pointer to our data
82 pointer = mem.open()
83 assert pointer.read() == b"<html/>"
84
85 # pass our content in as a string
86 mem = AttachMemory(content=b"binary-data", name="raw.dat")
87 # it loads a string type by default
88 assert mem.mimetype == "application/octet-stream"
89 assert mem.name == "raw.dat"
90
91 # pass our content in as a string
92 mem = AttachMemory(content=b"binary-data")
93 # it loads a string type by default
94 assert mem.mimetype == "application/octet-stream"
95 # Our filename is automatically generated (with .dat)
96 assert re.match(r"^[a-z0-9-]+\.dat$", mem.name, re.I)
97
98
99 def test_attach_memory():
100 """
101 API: AttachMemory()
102
103 """
104 # A url we can test with
105 fname = "testfile"
106 url = f"memory:///ignored/path/{fname}"
107
108 # Simple gif test
109 response = AppriseAttachment.instantiate(url)
110 assert isinstance(response, AttachMemory)
111
112 # There is no path yet as we haven't written anything to our memory object
113 # yet
114 assert response.path is None
115 assert bool(response) is False
116
117 with response as memobj:
118 memobj.write(b"content")
119
120 # Memory object defaults
121 assert response.name == fname
122 assert response.path == response.name
123 assert response.mimetype == "application/octet-stream"
124 assert bool(response) is True
125
126 #
127 fname_in_url = urllib.parse.quote(response.name)
128 assert response.url().startswith(f"memory://{fname_in_url}")
129
130 # Mime is always part of url
131 assert re.search(r"[?&]mime=", response.url()) is not None
132
133 # Test case where location is simply set to INACCESSIBLE
134 # Below is a bad example, but it proves the section of code properly works.
135 # Ideally a server admin may wish to just disable all File based
136 # attachments entirely. In this case, they simply just need to change the
137 # global singleton at the start of their program like:
138 #
139 # import apprise
140 # apprise.attachment.AttachMemory.location = \
141 # apprise.ContentLocation.INACCESSIBLE
142 #
143 response = AppriseAttachment.instantiate(url)
144 assert isinstance(response, AttachMemory)
145 with response as memobj:
146 memobj.write(b"content")
147
148 response.location = ContentLocation.INACCESSIBLE
149 assert response.path is None
150 # Downloads just don't work period
151 assert response.download() is False
152
153 # File handling (even if image is set to maxium allowable)
154 response = AppriseAttachment.instantiate(url)
155 assert isinstance(response, AttachMemory)
156 with response as memobj:
157 memobj.write(b"content")
158
159 # Memory handling when size is to large
160 response = AppriseAttachment.instantiate(url)
161 assert isinstance(response, AttachMemory)
162 with response as memobj:
163 memobj.write(b"content")
164
165 # Test case where we exceed our defined max_file_size in memory
166 prev_value = AttachBase.max_file_size
167 AttachBase.max_file_size = len(response) - 1
168 # We can't work in this case
169 assert response.path is None
170 assert response.download() is False
171
172 # Restore our file_size
173 AttachBase.max_file_size = prev_value
174
175 response = AppriseAttachment.instantiate(
176 "memory://apprise-file.gif?mime=image/gif"
177 )
178 assert isinstance(response, AttachMemory)
179 with response as memobj:
180 memobj.write(b"content")
181
182 assert response.name == "apprise-file.gif"
183 assert response.path == response.name
184 assert response.mimetype == "image/gif"
185 # No mime-type and/or filename over-ride was specified, so therefore it
186 # won't show up in the generated URL
187 assert re.search(r"[?&]mime=", response.url()) is not None
188 assert "image/gif" in response.url()
189
190 # Force a mime-type and new name
191 response = AppriseAttachment.instantiate(
192 "memory://{}?mime={}&name={}".format(
193 "ignored.gif", "image/jpeg", "test.jpeg"
194 )
195 )
196 assert isinstance(response, AttachMemory)
197 with response as memobj:
198 memobj.write(b"content")
199
200 assert response.name == "test.jpeg"
201 assert response.path == response.name
202 assert response.mimetype == "image/jpeg"
203 # We will match on mime type now (%2F = /)
204 assert re.search(r"[?&]mime=image/jpeg", response.url(), re.I)
205 assert response.url().startswith("memory://test.jpeg")
206
207 # Test hosted configuration and that we can't add a valid memory file
208 aa = AppriseAttachment(location=ContentLocation.HOSTED)
209 assert aa.add(response) is False
210
211 # now test our base64 output
212 assert isinstance(response.base64(), str)
213 # No encoding if we choose
214 assert isinstance(response.base64(encoding=None), bytes)
215
216 response.invalidate()
217 with pytest.raises(exception.AppriseFileNotFound):
218 response.base64()