Debian Codegraph @

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