Debian Codegraph @

tests/test_plugin_msg91.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 from json import loads
29
30 # Disable logging for a cleaner testing output
31 import logging
32 from unittest import mock
33
34 from helpers import AppriseURLTester
35 import pytest
36 import requests
37
38 from apprise import Apprise, NotifyType
39 from apprise.plugins.msg91 import NotifyMSG91
40
41 logging.disable(logging.CRITICAL)
42
43 # Our Testing URLs
44 apprise_url_tests = (
45 (
46 "msg91://",
47 {
48 # No hostname/authkey specified
49 "instance": TypeError,
50 },
51 ),
52 (
53 "msg91://-",
54 {
55 # Invalid AuthKey
56 "instance": TypeError,
57 },
58 ),
59 (
60 "msg91://{}".format("a" * 23),
61 {
62 # valid AuthKey but no Template ID
63 "instance": TypeError,
64 },
65 ),
66 (
67 "msg91://{}@{}".format("t" * 20, "a" * 23),
68 {
69 # Valid entry but no targets
70 "instance": NotifyMSG91,
71 # Since there are no targets specified we expect a False return on
72 # send()
73 "notify_response": False,
74 },
75 ),
76 (
77 "msg91://{}@{}/abcd".format("t" * 20, "a" * 23),
78 {
79 # No number to notify
80 "instance": NotifyMSG91,
81 # Since there are no targets specified we expect a False return on
82 # send()
83 "notify_response": False,
84 },
85 ),
86 (
87 "msg91://{}@{}/15551232000".format("t" * 20, "a" * 23),
88 {
89 # a valid message
90 "instance": NotifyMSG91,
91 # Our expected url(privacy=True) startswith() response:
92 "privacy_url": "msg91://[email protected]/15551232000",
93 },
94 ),
95 (
96 "msg91://{}@{}/?to=15551232000&short_url=no".format(
97 "t" * 20, "a" * 23
98 ),
99 {
100 # a valid message
101 "instance": NotifyMSG91,
102 },
103 ),
104 (
105 "msg91://{}@{}/15551232000?short_url=yes".format("t" * 20, "a" * 23),
106 {
107 # testing short_url
108 "instance": NotifyMSG91,
109 },
110 ),
111 (
112 "msg91://{}@{}/15551232000".format("t" * 20, "a" * 23),
113 {
114 # use get args to acomplish the same thing
115 "instance": NotifyMSG91,
116 },
117 ),
118 (
119 "msg91://{}@{}/15551232000".format("t" * 20, "a" * 23),
120 {
121 "instance": NotifyMSG91,
122 # throw a bizarre code forcing us to fail to look it up
123 "response": False,
124 "requests_response_code": 999,
125 },
126 ),
127 (
128 "msg91://{}@{}/15551232000".format("t" * 20, "a" * 23),
129 {
130 "instance": NotifyMSG91,
131 # Throws a series of i/o exceptions with this flag
132 # is set and tests that we gracefully handle them
133 "test_requests_exceptions": True,
134 },
135 ),
136 )
137
138
139 def test_plugin_msg91_urls():
140 """NotifyMSG91() Apprise URLs."""
141
142 # Run our general tests
143 AppriseURLTester(tests=apprise_url_tests).run_all()
144
145
146 @mock.patch("requests.post")
147 def test_plugin_msg91_edge_cases(mock_post):
148 """NotifyMSG91() Edge Cases."""
149
150 # Prepare our response
151 response = requests.Request()
152 response.status_code = requests.codes.ok
153
154 # Prepare Mock
155 mock_post.return_value = response
156
157 # Initialize some generic (but valid) tokens
158 target = "+1 (555) 123-3456"
159
160 # No authkey specified
161 with pytest.raises(TypeError):
162 NotifyMSG91(template="1234", authkey=None, targets=target)
163 with pytest.raises(TypeError):
164 NotifyMSG91(template="1234", authkey=" ", targets=target)
165 with pytest.raises(TypeError):
166 NotifyMSG91(template=" ", authkey="a" * 23, targets=target)
167 with pytest.raises(TypeError):
168 NotifyMSG91(template=None, authkey="a" * 23, targets=target)
169
170
171 @mock.patch("requests.post")
172 def test_plugin_msg91_keywords(mock_post):
173 """NotifyMSG91() Templating."""
174
175 response = mock.Mock()
176 response.content = ""
177 response.status_code = requests.codes.ok
178
179 # Prepare Mock
180 mock_post.return_value = response
181
182 target = "+1 (555) 123-3456"
183 template = "12345"
184 authkey = "{}".format("b" * 32)
185
186 message_contents = "test"
187
188 # Variation of initialization without API key
189 obj = Apprise.instantiate(
190 f"msg91://{template}@{authkey}/{target}?:key=value&:mobiles=ignored"
191 )
192 assert isinstance(obj, NotifyMSG91)
193 assert isinstance(obj.url(), str)
194
195 # Send Notification
196 assert obj.send(body=message_contents) is True
197
198 # Validate expected call parameters
199 assert mock_post.call_count == 1
200 first_call = mock_post.call_args_list[0]
201
202 # URL and message parameters are the same for both calls
203 assert first_call[0][0] == "https://control.msg91.com/api/v5/flow/"
204 response = loads(first_call[1]["data"])
205 assert response["template_id"] == template
206 assert response["short_url"] == 0
207 assert len(response["recipients"]) == 1
208 # mobiles is not over-ridden as it is a special reserved token
209 assert response["recipients"][0]["mobiles"] == "15551233456"
210
211 # Our base tokens
212 assert response["recipients"][0]["body"] == message_contents
213 assert response["recipients"][0]["type"] == NotifyType.INFO.value
214 assert response["recipients"][0]["key"] == "value"
215
216 mock_post.reset_mock()
217
218 # Play with mapping
219 obj = Apprise.instantiate(
220 f"msg91://{template}@{authkey}/{target}?:body&:type=cat"
221 )
222 assert isinstance(obj, NotifyMSG91)
223 assert isinstance(obj.url(), str)
224
225 # Send Notification
226 assert obj.send(body=message_contents) is True
227
228 # Validate expected call parameters
229 assert mock_post.call_count == 1
230 first_call = mock_post.call_args_list[0]
231 assert "NotifyType." not in first_call[1]["data"]
232
233 # URL and message parameters are the same for both calls
234 assert first_call[0][0] == "https://control.msg91.com/api/v5/flow/"
235 response = loads(first_call[1]["data"])
236 assert response["template_id"] == template
237 assert response["short_url"] == 0
238 assert len(response["recipients"]) == 1
239 assert response["recipients"][0]["mobiles"] == "15551233456"
240 assert "body" not in response["recipients"][0]
241 assert "type" not in response["recipients"][0]
242 assert response["recipients"][0]["cat"] == "info"