Debian Codegraph @

tests/test_plugin_smpp.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 from contextlib import suppress
29 import logging
30 import sys
31 from unittest import mock
32
33 from helpers import AppriseURLTester
34 import pytest
35
36 from apprise import Apprise, NotifyType
37 from apprise.exception import AppriseImproperlyConfigured
38 from apprise.plugins.smpp import NotifySMPP
39
40 with suppress(ImportError):
41 import smpplib
42
43 logging.disable(logging.CRITICAL)
44
45 # Our Testing URLs
46 apprise_url_tests = (
47 (
48 "smpp://",
49 {
50 "instance": AppriseImproperlyConfigured,
51 },
52 ),
53 (
54 "smpp:///",
55 {
56 "instance": AppriseImproperlyConfigured,
57 },
58 ),
59 (
60 "smpp://@/",
61 {
62 "instance": AppriseImproperlyConfigured,
63 },
64 ),
65 (
66 "smpp://user@/",
67 {
68 "instance": AppriseImproperlyConfigured,
69 },
70 ),
71 (
72 "smpp://user:pass/",
73 {
74 "instance": AppriseImproperlyConfigured,
75 },
76 ),
77 (
78 "smpp://user:pass@/",
79 {
80 "instance": AppriseImproperlyConfigured,
81 },
82 ),
83 (
84 "smpp://user@hostname",
85 {
86 "instance": AppriseImproperlyConfigured,
87 },
88 ),
89 (
90 "smpp://user:pass@host:/",
91 {
92 "instance": AppriseImproperlyConfigured,
93 },
94 ),
95 (
96 "smpp://user:pass@host:2775/",
97 {
98 "instance": AppriseImproperlyConfigured,
99 },
100 ),
101 (
102 "smpp://user:pass@host:2775/{}/{}".format("1" * 10, "a" * 32),
103 {
104 # valid everything but target numbers
105 "instance": NotifySMPP,
106 # We have no one to notify
107 "notify_response": False,
108 },
109 ),
110 (
111 "smpp://user:pass@host:2775/{}".format("1" * 10),
112 {
113 # everything valid
114 "instance": NotifySMPP,
115 # We have no one to notify
116 "notify_response": False,
117 },
118 ),
119 (
120 "smpp://user:pass@host/{}/{}".format("1" * 10, "1" * 10),
121 {
122 "instance": NotifySMPP,
123 },
124 ),
125 (
126 "smpps://_?&from={}&to={},{}&user=user&password=pw".format(
127 "1" * 10, "1" * 10, "1" * 10
128 ),
129 {
130 # use get args to accomplish the same thing
131 "instance": NotifySMPP,
132 },
133 ),
134 )
135
136
137 @pytest.mark.skipif(
138 "smpplib" in sys.modules, reason="Requires that smpplib NOT be installed"
139 )
140 def test_plugin_smpplib_import_error():
141 """NotifySMPP() smpplib loading failure."""
142
143 # Attempt to instantiate our object
144 obj = Apprise.instantiate(
145 "smpp://user:pass@host/{}/{}".format("1" * 10, "1" * 10)
146 )
147
148 # It's not possible because our cryptography depedancy is missing
149 assert obj is None
150
151
152 @pytest.mark.skipif("smpplib" not in sys.modules, reason="Requires smpplib")
153 def test_plugin_smpp_urls():
154 """NotifySMPP() Apprise URLs."""
155 # mock nested inside of outside function to avoid failing
156 # when smpplib is unavailable
157 with mock.patch("smpplib.client.Client") as mock_client_class:
158 mock_client_instance = mock.Mock()
159 mock_client_class.return_value = mock_client_instance
160
161 # Raise exception on connect
162 mock_client_instance.connect.return_value = True
163 mock_client_instance.bind_transmitter.return_value = True
164 mock_client_instance.send_message.return_value = True
165
166 # Run our general tests
167 AppriseURLTester(tests=apprise_url_tests).run_all()
168
169
170 @pytest.mark.skipif("smpplib" not in sys.modules, reason="Requires smpplib")
171 def test_plugin_smpp_edge_case():
172 """NotifySMPP() Apprise Edge Case."""
173
174 # mock nested inside of outside function to avoid failing
175 # when smpplib is unavailable
176 with mock.patch("smpplib.client.Client") as mock_client_class:
177 mock_client_instance = mock.Mock()
178 mock_client_class.return_value = mock_client_instance
179
180 # Raise exception on connect
181 mock_client_instance.connect.side_effect = (
182 smpplib.exceptions.ConnectionError
183 )
184 mock_client_instance.bind_transmitter.return_value = True
185 mock_client_instance.send_message.return_value = True
186
187 # Instantiate our object
188 obj = Apprise.instantiate(
189 "smpp://user:pass@host/{}/{}".format("1" * 10, "1" * 10)
190 )
191
192 # Well fail to establish a connection
193 assert (
194 bool(
195 obj.notify(
196 body="body", title="title", notify_type=NotifyType.INFO
197 )
198 )
199 is False
200 )
201
202 # Raise exception on connect
203 mock_client_instance.connect.side_effect = None
204 mock_client_instance.bind_transmitter.return_value = True
205 mock_client_instance.send_message.side_effect = (
206 smpplib.exceptions.ConnectionError
207 )
208
209 # Well fail to deliver our message
210 assert (
211 bool(
212 obj.notify(
213 body="body", title="title", notify_type=NotifyType.INFO
214 )
215 )
216 is False
217 )