| 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 |
|
| 29 |
import logging |
| 30 |
from unittest import mock |
| 31 |
|
| 32 |
from helpers import AppriseURLTester |
| 33 |
import pytest |
| 34 |
import requests |
| 35 |
|
| 36 |
from apprise import Apprise, NotifyType |
| 37 |
from apprise.plugins.octopush import NotifyOctopush |
| 38 |
|
| 39 |
logging.disable(logging.CRITICAL) |
| 40 |
|
| 41 |
|
| 42 |
apprise_url_tests = ( |
| 43 |
( |
| 44 |
"octopush://", |
| 45 |
{ |
| 46 |
|
| 47 |
"instance": TypeError, |
| 48 |
}, |
| 49 |
), |
| 50 |
( |
| 51 |
"octopush://:@/", |
| 52 |
{ |
| 53 |
|
| 54 |
"instance": TypeError, |
| 55 |
}, |
| 56 |
), |
| 57 |
( |
| 58 |
"octopush://[email protected]", |
| 59 |
{ |
| 60 |
|
| 61 |
"instance": TypeError, |
| 62 |
}, |
| 63 |
), |
| 64 |
( |
| 65 |
"octopush://_/apikey?login=invalid", |
| 66 |
{ |
| 67 |
|
| 68 |
"instance": TypeError, |
| 69 |
}, |
| 70 |
), |
| 71 |
( |
| 72 |
"octopush://[email protected]/%20", |
| 73 |
{ |
| 74 |
|
| 75 |
"instance": TypeError, |
| 76 |
}, |
| 77 |
), |
| 78 |
( |
| 79 |
"octopush://%20:[email protected]/apikey", |
| 80 |
{ |
| 81 |
|
| 82 |
"instance": TypeError, |
| 83 |
}, |
| 84 |
), |
| 85 |
( |
| 86 |
"octopush://[email protected]/apikey", |
| 87 |
{ |
| 88 |
|
| 89 |
"instance": NotifyOctopush, |
| 90 |
"response": False, |
| 91 |
}, |
| 92 |
), |
| 93 |
( |
| 94 |
"octopush://[email protected]/apikey/+0987654321", |
| 95 |
{ |
| 96 |
|
| 97 |
"instance": NotifyOctopush, |
| 98 |
"requests_response_code": requests.codes.created, |
| 99 |
|
| 100 |
"privacy_url": "octopush://u...m/****/+0987654321", |
| 101 |
}, |
| 102 |
), |
| 103 |
( |
| 104 |
"octopush://sender:[email protected]/apikey/+1111111111", |
| 105 |
{ |
| 106 |
|
| 107 |
"instance": NotifyOctopush, |
| 108 |
"requests_response_code": requests.codes.created, |
| 109 |
|
| 110 |
"privacy_url": "octopush://sender:u...m/****/+1111111111", |
| 111 |
}, |
| 112 |
), |
| 113 |
( |
| 114 |
"octopush://[email protected]&key=key&to=9999999999" |
| 115 |
"&purpose=wholesale", |
| 116 |
{ |
| 117 |
|
| 118 |
"instance": NotifyOctopush, |
| 119 |
"requests_response_code": requests.codes.created, |
| 120 |
}, |
| 121 |
), |
| 122 |
( |
| 123 |
"octopush://[email protected]&key=key&to=9999999999" |
| 124 |
"&purpose=invalid", |
| 125 |
{ |
| 126 |
|
| 127 |
"instance": TypeError, |
| 128 |
}, |
| 129 |
), |
| 130 |
( |
| 131 |
"octopush://[email protected]&key=key&to=9999999999" |
| 132 |
"&type=premium", |
| 133 |
{ |
| 134 |
|
| 135 |
"instance": NotifyOctopush, |
| 136 |
"requests_response_code": requests.codes.created, |
| 137 |
}, |
| 138 |
), |
| 139 |
( |
| 140 |
"octopush://[email protected]&key=key&to=9999999999" |
| 141 |
"&type=invalid", |
| 142 |
{ |
| 143 |
|
| 144 |
"instance": TypeError, |
| 145 |
}, |
| 146 |
), |
| 147 |
( |
| 148 |
"octopush://[email protected]/apikey/+3333333333?replies=yes", |
| 149 |
{ |
| 150 |
|
| 151 |
"instance": NotifyOctopush, |
| 152 |
"requests_response_code": requests.codes.created, |
| 153 |
}, |
| 154 |
), |
| 155 |
( |
| 156 |
"octopush://sender:[email protected]/apikey/{}/{}/{}/?batch=yes".format( |
| 157 |
"1" * 10, "2" * 3, "3" * 11 |
| 158 |
), |
| 159 |
{ |
| 160 |
|
| 161 |
"instance": NotifyOctopush, |
| 162 |
"requests_response_code": requests.codes.created, |
| 163 |
}, |
| 164 |
), |
| 165 |
( |
| 166 |
"octopush://_?key=abc123&[email protected]&sender=abc" |
| 167 |
"&to=2222222222", |
| 168 |
{ |
| 169 |
|
| 170 |
"instance": NotifyOctopush, |
| 171 |
"requests_response_code": requests.codes.created, |
| 172 |
|
| 173 |
"privacy_url": "octopush://abc:u...m/****/+2222222222", |
| 174 |
}, |
| 175 |
), |
| 176 |
( |
| 177 |
"octopush://[email protected]/apikey/1234567890", |
| 178 |
{ |
| 179 |
"instance": NotifyOctopush, |
| 180 |
|
| 181 |
"response": False, |
| 182 |
"requests_response_code": 999, |
| 183 |
}, |
| 184 |
), |
| 185 |
( |
| 186 |
"octopush://[email protected]/apikey/1234567890", |
| 187 |
{ |
| 188 |
"instance": NotifyOctopush, |
| 189 |
|
| 190 |
|
| 191 |
"test_requests_exceptions": True, |
| 192 |
}, |
| 193 |
), |
| 194 |
) |
| 195 |
|
| 196 |
|
| 197 |
def test_plugin_octopush_urls(): |
| 198 |
"""NotifyOctopush() Apprise URLs.""" |
| 199 |
|
| 200 |
assert ( |
| 201 |
NotifyOctopush.setup_url == "https://appriseit.com/services/octopush/" |
| 202 |
) |
| 203 |
|
| 204 |
|
| 205 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 206 |
|
| 207 |
|
| 208 |
def test_plugin_octopush_parse_url_and_validation(): |
| 209 |
"""NotifyOctopush() parse_url() and validation coverage.""" |
| 210 |
|
| 211 |
assert NotifyOctopush.parse_url(None) is None |
| 212 |
|
| 213 |
with pytest.raises(TypeError): |
| 214 |
NotifyOctopush( |
| 215 |
api_login="[email protected]", |
| 216 |
api_key="apikey", |
| 217 |
targets=("1234567890",), |
| 218 |
purpose=" ", |
| 219 |
) |
| 220 |
|
| 221 |
results = NotifyOctopush.parse_url( |
| 222 |
"octopush://sender:[email protected]/apikey/1234567890" |
| 223 |
"?to=2345678901&type=sms_premium&purpose=alert&batch=yes" |
| 224 |
"&replies=no" |
| 225 |
) |
| 226 |
assert isinstance(results, dict) |
| 227 |
assert results["api_key"] == "apikey" |
| 228 |
assert results["api_login"] == "[email protected]" |
| 229 |
assert results["sender"] == "sender" |
| 230 |
assert results["targets"] == ["1234567890", "2345678901"] |
| 231 |
assert results["mtype"] == "sms_premium" |
| 232 |
assert results["purpose"] == "alert" |
| 233 |
assert results["batch"] is True |
| 234 |
assert results["replies"] is False |
| 235 |
|
| 236 |
results = NotifyOctopush.parse_url( |
| 237 |
"octopush://[email protected]&key=apikey&sender=abc" |
| 238 |
"&to=3456789012&type=sms_low_cost&purpose=wholesale" |
| 239 |
"&batch=no&replies=yes" |
| 240 |
) |
| 241 |
assert isinstance(results, dict) |
| 242 |
assert results["api_key"] == "apikey" |
| 243 |
assert results["api_login"] == "[email protected]" |
| 244 |
assert results["sender"] == "abc" |
| 245 |
assert results["targets"] == ["3456789012"] |
| 246 |
assert results["mtype"] == "sms_low_cost" |
| 247 |
assert results["purpose"] == "wholesale" |
| 248 |
assert results["batch"] is False |
| 249 |
assert results["replies"] is True |
| 250 |
|
| 251 |
|
| 252 |
@mock.patch("requests.post") |
| 253 |
def test_plugin_octopush_edge_cases(mock_post): |
| 254 |
"""NotifyOctopush() Edge Cases.""" |
| 255 |
|
| 256 |
response = requests.Request() |
| 257 |
response.status_code = requests.codes.created |
| 258 |
mock_post.return_value = response |
| 259 |
|
| 260 |
obj = Apprise.instantiate( |
| 261 |
"octopush://sender:[email protected]/apikey/1234567890/2345678901" |
| 262 |
"?batch=yes&replies=yes&type=low_cost&purpose=wholesale" |
| 263 |
) |
| 264 |
|
| 265 |
assert isinstance(obj, NotifyOctopush) |
| 266 |
assert len(obj) == 1 |
| 267 |
assert obj.targets == ["+1234567890", "+2345678901"] |
| 268 |
assert obj.mtype == "sms_low_cost" |
| 269 |
assert obj.purpose == "wholesale" |
| 270 |
assert obj.replies is True |
| 271 |
|
| 272 |
assert obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 273 |
assert mock_post.call_count == 1 |
| 274 |
|
| 275 |
call = mock_post.call_args_list[0] |
| 276 |
assert call[0][0] == "https://api.octopush.com/v1/public/sms-campaign/send" |
| 277 |
|
| 278 |
headers = call[1]["headers"] |
| 279 |
assert headers["api-login"] == "[email protected]" |
| 280 |
assert headers["api-key"] == "apikey" |
| 281 |
assert headers["Content-Type"] == "application/json; charset=utf-8" |
| 282 |
|
| 283 |
payload = call[1]["data"] |
| 284 |
assert '"type": "sms_low_cost"' in payload |
| 285 |
assert '"purpose": "wholesale"' in payload |
| 286 |
assert '"with_replies": true' in payload |
| 287 |
assert '"phone_number": "+1234567890"' in payload |
| 288 |
assert '"phone_number": "+2345678901"' in payload |
| 289 |
|
| 290 |
obj = NotifyOctopush( |
| 291 |
api_login="[email protected]", |
| 292 |
api_key="apikey", |
| 293 |
targets=tuple(str(1000000000 + i) for i in range(501)), |
| 294 |
batch=True, |
| 295 |
) |
| 296 |
assert len(obj) == 2 |
| 297 |
|
| 298 |
mock_post.reset_mock() |
| 299 |
assert obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 300 |
assert mock_post.call_count == 2 |
| 301 |
|
| 302 |
first_payload = mock_post.call_args_list[0][1]["data"] |
| 303 |
assert first_payload.count('"phone_number"') == 500 |
| 304 |
|
| 305 |
second_payload = mock_post.call_args_list[1][1]["data"] |
| 306 |
assert second_payload.count('"phone_number"') == 1 |