| 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 json import loads |
| 29 |
|
| 30 |
|
| 31 |
import logging |
| 32 |
from unittest import mock |
| 33 |
|
| 34 |
from helpers import AppriseURLTester |
| 35 |
import requests |
| 36 |
|
| 37 |
from apprise import Apprise, NotifyType |
| 38 |
from apprise.plugins.d7networks import NotifyD7Networks |
| 39 |
|
| 40 |
logging.disable(logging.CRITICAL) |
| 41 |
|
| 42 |
|
| 43 |
apprise_url_tests = ( |
| 44 |
( |
| 45 |
"d7sms://", |
| 46 |
{ |
| 47 |
|
| 48 |
"instance": TypeError, |
| 49 |
}, |
| 50 |
), |
| 51 |
( |
| 52 |
"d7sms://:@/", |
| 53 |
{ |
| 54 |
|
| 55 |
"instance": TypeError, |
| 56 |
}, |
| 57 |
), |
| 58 |
( |
| 59 |
"d7sms://token@{}/{}/{}".format("1" * 9, "2" * 15, "a" * 13), |
| 60 |
{ |
| 61 |
|
| 62 |
"instance": NotifyD7Networks, |
| 63 |
|
| 64 |
|
| 65 |
"notify_response": False, |
| 66 |
}, |
| 67 |
), |
| 68 |
( |
| 69 |
"d7sms://token1@{}?batch=yes".format("3" * 14), |
| 70 |
{ |
| 71 |
|
| 72 |
"instance": NotifyD7Networks, |
| 73 |
|
| 74 |
"privacy_url": "d7sms://t...1@", |
| 75 |
}, |
| 76 |
), |
| 77 |
( |
| 78 |
"d7sms://token:colon2@{}?batch=yes".format("3" * 14), |
| 79 |
{ |
| 80 |
|
| 81 |
"instance": NotifyD7Networks, |
| 82 |
|
| 83 |
"privacy_url": "d7sms://t...2@", |
| 84 |
}, |
| 85 |
), |
| 86 |
( |
| 87 |
"d7sms://:token3@{}?batch=yes".format("3" * 14), |
| 88 |
{ |
| 89 |
|
| 90 |
"instance": NotifyD7Networks, |
| 91 |
|
| 92 |
"privacy_url": "d7sms://:...3@", |
| 93 |
}, |
| 94 |
), |
| 95 |
( |
| 96 |
"d7sms://{}?token=token6".format("3" * 14), |
| 97 |
{ |
| 98 |
|
| 99 |
"instance": NotifyD7Networks, |
| 100 |
|
| 101 |
"privacy_url": "d7sms://t...6@", |
| 102 |
}, |
| 103 |
), |
| 104 |
( |
| 105 |
"d7sms://token4@{}?unicode=no".format("3" * 14), |
| 106 |
{ |
| 107 |
|
| 108 |
"instance": NotifyD7Networks, |
| 109 |
|
| 110 |
"privacy_url": "d7sms://t...4@", |
| 111 |
}, |
| 112 |
), |
| 113 |
( |
| 114 |
"d7sms://token8@{}/{}/?unicode=yes".format("3" * 14, "4" * 14), |
| 115 |
{ |
| 116 |
|
| 117 |
"instance": NotifyD7Networks, |
| 118 |
|
| 119 |
"privacy_url": "d7sms://t...8@", |
| 120 |
}, |
| 121 |
), |
| 122 |
( |
| 123 |
"d7sms://token@{}?batch=yes&to={}".format("3" * 14, "6" * 14), |
| 124 |
{ |
| 125 |
|
| 126 |
"instance": NotifyD7Networks, |
| 127 |
}, |
| 128 |
), |
| 129 |
( |
| 130 |
"d7sms://token@{}?batch=yes&from=apprise".format("3" * 14), |
| 131 |
{ |
| 132 |
|
| 133 |
"instance": NotifyD7Networks, |
| 134 |
}, |
| 135 |
), |
| 136 |
( |
| 137 |
"d7sms://token@{}?batch=yes&source=apprise".format("3" * 14), |
| 138 |
{ |
| 139 |
|
| 140 |
|
| 141 |
"instance": NotifyD7Networks, |
| 142 |
}, |
| 143 |
), |
| 144 |
( |
| 145 |
"d7sms://token@{}?batch=no".format("3" * 14), |
| 146 |
{ |
| 147 |
|
| 148 |
"instance": NotifyD7Networks, |
| 149 |
}, |
| 150 |
), |
| 151 |
( |
| 152 |
"d7sms://token@{}".format("3" * 14), |
| 153 |
{ |
| 154 |
"instance": NotifyD7Networks, |
| 155 |
|
| 156 |
"response": False, |
| 157 |
"requests_response_code": 999, |
| 158 |
}, |
| 159 |
), |
| 160 |
( |
| 161 |
"d7sms://token@{}".format("3" * 14), |
| 162 |
{ |
| 163 |
"instance": NotifyD7Networks, |
| 164 |
|
| 165 |
|
| 166 |
"test_requests_exceptions": True, |
| 167 |
}, |
| 168 |
), |
| 169 |
) |
| 170 |
|
| 171 |
|
| 172 |
def test_plugin_d7networks_urls(): |
| 173 |
"""NotifyD7Networks() Apprise URLs.""" |
| 174 |
|
| 175 |
|
| 176 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 177 |
|
| 178 |
|
| 179 |
@mock.patch("requests.post") |
| 180 |
def test_plugin_d7networks_edge_cases(mock_post): |
| 181 |
"""NotifyD7Networks() Edge Cases tests.""" |
| 182 |
|
| 183 |
|
| 184 |
request = mock.Mock() |
| 185 |
request.content = "{}" |
| 186 |
request.status_code = requests.codes.ok |
| 187 |
mock_post.return_value = request |
| 188 |
|
| 189 |
|
| 190 |
aobj = Apprise() |
| 191 |
assert aobj.add("d7sms://Token@15551231234/15551231236") |
| 192 |
|
| 193 |
body = "test message" |
| 194 |
|
| 195 |
|
| 196 |
assert aobj.notify(body=body, title="title", notify_type=NotifyType.INFO) |
| 197 |
|
| 198 |
|
| 199 |
assert mock_post.call_count == 2 |
| 200 |
assert ( |
| 201 |
mock_post.call_args_list[0][0][0] |
| 202 |
== "https://api.d7networks.com/messages/v1/send" |
| 203 |
) |
| 204 |
assert ( |
| 205 |
mock_post.call_args_list[1][0][0] |
| 206 |
== "https://api.d7networks.com/messages/v1/send" |
| 207 |
) |
| 208 |
|
| 209 |
|
| 210 |
data = loads(mock_post.call_args_list[0][1]["data"]) |
| 211 |
assert len(data["messages"]) == 1 |
| 212 |
message = data["messages"][0] |
| 213 |
assert len(message["recipients"]) == 1 |
| 214 |
assert message["content"] == "title\r\ntest message" |
| 215 |
assert message["data_coding"] == "auto" |
| 216 |
|
| 217 |
|
| 218 |
data = loads(mock_post.call_args_list[1][1]["data"]) |
| 219 |
assert len(data["messages"]) == 1 |
| 220 |
message = data["messages"][0] |
| 221 |
assert len(message["recipients"]) == 1 |
| 222 |
assert message["content"] == "title\r\ntest message" |
| 223 |
assert message["data_coding"] == "auto" |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
mock_post.reset_mock() |
| 230 |
|
| 231 |
|
| 232 |
aobj = Apprise() |
| 233 |
assert aobj.add("d7sms://Token@15551231234/15551231236?batch=yes") |
| 234 |
|
| 235 |
body = "test message" |
| 236 |
|
| 237 |
|
| 238 |
assert aobj.notify(body=body, title="title", notify_type=NotifyType.INFO) |
| 239 |
|
| 240 |
|
| 241 |
assert mock_post.call_count == 1 |
| 242 |
assert ( |
| 243 |
mock_post.call_args_list[0][0][0] |
| 244 |
== "https://api.d7networks.com/messages/v1/send" |
| 245 |
) |
| 246 |
|
| 247 |
data = loads(mock_post.call_args_list[0][1]["data"]) |
| 248 |
assert len(data["messages"]) == 1 |
| 249 |
message = data["messages"][0] |
| 250 |
|
| 251 |
assert len(message["recipients"]) == 2 |
| 252 |
assert "15551231234" in message["recipients"] |
| 253 |
assert "15551231236" in message["recipients"] |
| 254 |
assert message["content"] == "title\r\ntest message" |
| 255 |
assert message["data_coding"] == "auto" |