| 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 requests |
| 34 |
|
| 35 |
from apprise import Apprise, NotifyType |
| 36 |
from apprise.plugins.africas_talking import NotifyAfricasTalking |
| 37 |
|
| 38 |
logging.disable(logging.CRITICAL) |
| 39 |
|
| 40 |
|
| 41 |
apprise_url_tests = ( |
| 42 |
( |
| 43 |
"atalk://", |
| 44 |
{ |
| 45 |
|
| 46 |
"instance": TypeError, |
| 47 |
}, |
| 48 |
), |
| 49 |
( |
| 50 |
"atalk://:@/", |
| 51 |
{ |
| 52 |
|
| 53 |
"instance": TypeError |
| 54 |
}, |
| 55 |
), |
| 56 |
( |
| 57 |
"atalk://user@^/", |
| 58 |
{ |
| 59 |
|
| 60 |
"instance": TypeError |
| 61 |
}, |
| 62 |
), |
| 63 |
( |
| 64 |
"atalk://user@apikey/{}".format("3" * 5), |
| 65 |
{ |
| 66 |
|
| 67 |
"instance": NotifyAfricasTalking, |
| 68 |
|
| 69 |
"notify_response": False, |
| 70 |
}, |
| 71 |
), |
| 72 |
( |
| 73 |
"atalk://user@apikey/123/{}/abcd/+{}".format("3" * 11, "4" * 11), |
| 74 |
{ |
| 75 |
|
| 76 |
"instance": NotifyAfricasTalking, |
| 77 |
"privacy_url": "atalk://[email protected]/33333333333/+44444444444", |
| 78 |
}, |
| 79 |
), |
| 80 |
( |
| 81 |
"atalk://user@apikey/+{}?batch=y".format("4" * 11), |
| 82 |
{ |
| 83 |
"instance": NotifyAfricasTalking, |
| 84 |
|
| 85 |
"privacy_url": "atalk://[email protected]/+44444444444", |
| 86 |
}, |
| 87 |
), |
| 88 |
( |
| 89 |
"atalk://user@apikey/+{}?mode=invalid".format("4" * 11), |
| 90 |
{"instance": TypeError}, |
| 91 |
), |
| 92 |
( |
| 93 |
"atalk://user@apikey/+{}?mode=s".format("4" * 11), |
| 94 |
{ |
| 95 |
|
| 96 |
"instance": NotifyAfricasTalking, |
| 97 |
}, |
| 98 |
), |
| 99 |
( |
| 100 |
"atalk://user@apikey/+{}?mode=PREM".format("4" * 11), |
| 101 |
{ |
| 102 |
|
| 103 |
"instance": NotifyAfricasTalking, |
| 104 |
}, |
| 105 |
), |
| 106 |
( |
| 107 |
"atalk://{}?apikey=key&user=user&from=FROMUSER".format("1" * 11), |
| 108 |
{ |
| 109 |
|
| 110 |
"instance": NotifyAfricasTalking, |
| 111 |
}, |
| 112 |
), |
| 113 |
( |
| 114 |
"atalk://_?user=user&to={},{}&key={}&from={}".format( |
| 115 |
"1" * 11, "2" * 11, "b" * 10, "5" * 13 |
| 116 |
), |
| 117 |
{ |
| 118 |
|
| 119 |
"instance": NotifyAfricasTalking, |
| 120 |
}, |
| 121 |
), |
| 122 |
( |
| 123 |
"atalk://user@apikey/{}/".format("1" * 11), |
| 124 |
{ |
| 125 |
"instance": NotifyAfricasTalking, |
| 126 |
|
| 127 |
"response": False, |
| 128 |
"requests_response_code": 999, |
| 129 |
}, |
| 130 |
), |
| 131 |
( |
| 132 |
"atalk://user@apikey/{}/".format("1" * 11), |
| 133 |
{ |
| 134 |
"instance": NotifyAfricasTalking, |
| 135 |
|
| 136 |
|
| 137 |
"test_requests_exceptions": True, |
| 138 |
}, |
| 139 |
), |
| 140 |
) |
| 141 |
|
| 142 |
|
| 143 |
def test_plugin_atalk_urls(): |
| 144 |
"""NotifyTemplate() Apprise URLs.""" |
| 145 |
|
| 146 |
|
| 147 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 148 |
|
| 149 |
|
| 150 |
@mock.patch("requests.post") |
| 151 |
def test_plugin_atalk_edge_cases(mock_post): |
| 152 |
"""NotifyAfricasTalking() Edge Cases.""" |
| 153 |
|
| 154 |
|
| 155 |
apikey = "my-api-key" |
| 156 |
appuser = "my-app-user" |
| 157 |
targets = [ |
| 158 |
"+1(555) 123-1234", |
| 159 |
"1555 5555555", |
| 160 |
|
| 161 |
"12", |
| 162 |
] |
| 163 |
|
| 164 |
|
| 165 |
response = requests.Request() |
| 166 |
response.status_code = requests.codes.ok |
| 167 |
|
| 168 |
|
| 169 |
mock_post.return_value = response |
| 170 |
|
| 171 |
|
| 172 |
obj = Apprise.instantiate( |
| 173 |
"atalk://{}@{}/{}?batch=n".format(appuser, apikey, "/".join(targets)) |
| 174 |
) |
| 175 |
|
| 176 |
assert ( |
| 177 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 178 |
is True |
| 179 |
) |
| 180 |
|
| 181 |
|
| 182 |
assert len(obj) == 2 |
| 183 |
|
| 184 |
|
| 185 |
assert mock_post.call_count == 2 |
| 186 |
|
| 187 |
|
| 188 |
details = mock_post.call_args_list[0] |
| 189 |
headers = details[1]["headers"] |
| 190 |
assert headers["apiKey"] == apikey |
| 191 |
payload = details[1]["data"] |
| 192 |
assert payload["username"] == appuser |
| 193 |
assert payload["from"] == "AFRICASTKNG" |
| 194 |
assert payload["to"] == "+15551231234" |
| 195 |
assert payload["message"] == "title\r\nbody" |
| 196 |
|
| 197 |
details = mock_post.call_args_list[1] |
| 198 |
headers = details[1]["headers"] |
| 199 |
assert headers["apiKey"] == apikey |
| 200 |
payload = details[1]["data"] |
| 201 |
assert payload["username"] == appuser |
| 202 |
assert payload["from"] == "AFRICASTKNG" |
| 203 |
assert payload["to"] == "15555555555" |
| 204 |
assert payload["message"] == "title\r\nbody" |
| 205 |
|
| 206 |
|
| 207 |
assert obj.url().startswith( |
| 208 |
"atalk://{}@{}/{}".format( |
| 209 |
appuser, apikey, "/".join(["+15551231234", "15555555555"]) |
| 210 |
) |
| 211 |
) |
| 212 |
|
| 213 |
assert "batch=no" in obj.url() |
| 214 |
|
| 215 |
|
| 216 |
mock_post.reset_mock() |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
obj = Apprise.instantiate( |
| 222 |
"atalk://{}?user={}&apikey={}&batch=y&from=TEST".format( |
| 223 |
"/".join(targets), appuser, apikey |
| 224 |
) |
| 225 |
) |
| 226 |
|
| 227 |
|
| 228 |
assert len(obj) == 1 |
| 229 |
|
| 230 |
assert ( |
| 231 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 232 |
is True |
| 233 |
) |
| 234 |
|
| 235 |
|
| 236 |
assert mock_post.call_count == 1 |
| 237 |
|
| 238 |
details = mock_post.call_args_list[0] |
| 239 |
headers = details[1]["headers"] |
| 240 |
assert headers["apiKey"] == apikey |
| 241 |
payload = details[1]["data"] |
| 242 |
assert payload["username"] == appuser |
| 243 |
assert payload["from"] == "TEST" |
| 244 |
assert payload["to"] == "+15551231234,15555555555" |
| 245 |
assert payload["message"] == "title\r\nbody" |