| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 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 |
|
| 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 |
|
| 104 |
"instance": NotifySMPP, |
| 105 |
|
| 106 |
"notify_response": False, |
| 107 |
}, |
| 108 |
), |
| 109 |
( |
| 110 |
"smpp://user:pass@host:2775/{}".format("1" * 10), |
| 111 |
{ |
| 112 |
|
| 113 |
"instance": NotifySMPP, |
| 114 |
|
| 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 |
|
| 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 |
|
| 143 |
obj = Apprise.instantiate( |
| 144 |
"smpp://user:pass@host/{}/{}".format("1" * 10, "1" * 10) |
| 145 |
) |
| 146 |
|
| 147 |
|
| 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 |
|
| 155 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 174 |
|
| 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 |
|
| 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 |
|
| 187 |
obj = Apprise.instantiate( |
| 188 |
"smpp://user:pass@host/{}/{}".format("1" * 10, "1" * 10) |
| 189 |
) |
| 190 |
|
| 191 |
|
| 192 |
assert ( |
| 193 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 194 |
is False |
| 195 |
) |
| 196 |
|
| 197 |
|
| 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 |
|
| 205 |
assert ( |
| 206 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 207 |
is False |
| 208 |
) |