| 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 |
import socket |
| 31 |
from unittest import mock |
| 32 |
|
| 33 |
import apprise |
| 34 |
from apprise.plugins.aprs import NotifyAprs |
| 35 |
|
| 36 |
logging.disable(logging.CRITICAL) |
| 37 |
|
| 38 |
|
| 39 |
@mock.patch("socket.create_connection") |
| 40 |
def test_plugin_aprs_urls(mock_create_connection): |
| 41 |
"""NotifyAprs() Apprise URLs.""" |
| 42 |
|
| 43 |
sobj = mock.Mock() |
| 44 |
sobj.return_value = 1 |
| 45 |
sobj.getpeername.return_value = ("localhost", 1234) |
| 46 |
sobj.socket_close.return_value = None |
| 47 |
sobj.setblocking.return_value = True |
| 48 |
sobj.recv.return_value = "ping\npong pong DF1JSL-15 verified pong".encode( |
| 49 |
"latin-1" |
| 50 |
) |
| 51 |
sobj.sendall.return_value = True |
| 52 |
sobj.settimeout.return_value = True |
| 53 |
|
| 54 |
|
| 55 |
mock_create_connection.return_value = sobj |
| 56 |
|
| 57 |
|
| 58 |
assert apprise.Apprise.instantiate("aprs://") is None |
| 59 |
assert apprise.Apprise.instantiate("aprs://:@/") is None |
| 60 |
|
| 61 |
|
| 62 |
assert apprise.Apprise.instantiate("aprs://DF1JSL-15:12345") is None |
| 63 |
|
| 64 |
|
| 65 |
assert NotifyAprs.parse_url(None) is None |
| 66 |
|
| 67 |
|
| 68 |
assert apprise.Apprise.instantiate("aprs://DF1JSL-15:@DF1ABC") is None |
| 69 |
assert apprise.Apprise.instantiate("aprs://DF1JSL-15@DF1ABC") is None |
| 70 |
|
| 71 |
assert apprise.Apprise.instantiate("aprs://DF1JSL-15:-1@DF1ABC") is None |
| 72 |
|
| 73 |
assert apprise.Apprise.instantiate("aprs://DF1JSL-15:abcd@DF1ABC") is None |
| 74 |
|
| 75 |
|
| 76 |
instance = apprise.Apprise.instantiate("aprs://DF1JSL-15:12345@DF1ABC") |
| 77 |
assert isinstance(instance, NotifyAprs) |
| 78 |
assert instance.url(privacy=True).startswith( |
| 79 |
"aprs://DF1JSL-15:****@D...C?" |
| 80 |
) |
| 81 |
assert instance.notify("test") is True |
| 82 |
|
| 83 |
|
| 84 |
instance = apprise.Apprise.instantiate("aprs://D1JSL-15:12345@D1ABC") |
| 85 |
assert isinstance(instance, NotifyAprs) |
| 86 |
|
| 87 |
instance = apprise.Apprise.instantiate( |
| 88 |
"aprs://DF1JSL-15:12345@DF1ABC?delay=3.0" |
| 89 |
) |
| 90 |
assert isinstance(instance, NotifyAprs) |
| 91 |
instance = apprise.Apprise.instantiate( |
| 92 |
"aprs://DF1JSL-15:12345@DF1ABC?delay=2" |
| 93 |
) |
| 94 |
assert isinstance(instance, NotifyAprs) |
| 95 |
instance = apprise.Apprise.instantiate( |
| 96 |
"aprs://DF1JSL-15:12345@DF1ABC?delay=-3.0" |
| 97 |
) |
| 98 |
assert instance is None |
| 99 |
instance = apprise.Apprise.instantiate( |
| 100 |
"aprs://DF1JSL-15:12345@DF1ABC?delay=40.0" |
| 101 |
) |
| 102 |
assert instance is None |
| 103 |
instance = apprise.Apprise.instantiate( |
| 104 |
"aprs://DF1JSL-15:12345@DF1ABC?delay=invalid" |
| 105 |
) |
| 106 |
assert instance is None |
| 107 |
|
| 108 |
instance = apprise.Apprise.instantiate( |
| 109 |
"aprs://DF1JSL-15:12345@DF1ABC/DF1DEF" |
| 110 |
) |
| 111 |
assert isinstance(instance, NotifyAprs) |
| 112 |
assert instance.url(privacy=True).startswith( |
| 113 |
"aprs://DF1JSL-15:****@D...C/D...F?" |
| 114 |
) |
| 115 |
assert instance.notify("test") is True |
| 116 |
|
| 117 |
instance = apprise.Apprise.instantiate( |
| 118 |
"aprs://DF1JSL-15:12345@DF1ABC-1/DF1ABC/DF1ABC-15" |
| 119 |
) |
| 120 |
assert isinstance(instance, NotifyAprs) |
| 121 |
assert instance.url(privacy=True).startswith( |
| 122 |
"aprs://DF1JSL-15:****@D...1/D...C/D...5?" |
| 123 |
) |
| 124 |
assert instance.notify("test") is True |
| 125 |
|
| 126 |
instance = apprise.Apprise.instantiate( |
| 127 |
"aprs://DF1JSL-15:12345@?to=DF1ABC,DF1DEF" |
| 128 |
) |
| 129 |
assert isinstance(instance, NotifyAprs) |
| 130 |
assert instance.url(privacy=True).startswith( |
| 131 |
"aprs://DF1JSL-15:****@D...C/D...F?" |
| 132 |
) |
| 133 |
assert instance.notify("test") is True |
| 134 |
|
| 135 |
|
| 136 |
instance = apprise.Apprise.instantiate( |
| 137 |
"aprs://DF1JSL-15:12345@DF1ABC?locale=EURO" |
| 138 |
) |
| 139 |
assert isinstance(instance, NotifyAprs) |
| 140 |
assert instance.url(privacy=True).startswith( |
| 141 |
"aprs://DF1JSL-15:****@D...C?" |
| 142 |
) |
| 143 |
|
| 144 |
assert "locale=" not in instance.url(privacy=True) |
| 145 |
assert instance.notify("test") is True |
| 146 |
|
| 147 |
instance = apprise.Apprise.instantiate( |
| 148 |
"aprs://DF1JSL-15:12345@DF1ABC?locale=NOAM" |
| 149 |
) |
| 150 |
assert isinstance(instance, NotifyAprs) |
| 151 |
assert instance.url(privacy=True).startswith( |
| 152 |
"aprs://DF1JSL-15:****@D...C?" |
| 153 |
) |
| 154 |
|
| 155 |
assert "locale=NOAM" in instance.url(privacy=True) |
| 156 |
assert instance.notify("test") is True |
| 157 |
|
| 158 |
|
| 159 |
assert ( |
| 160 |
apprise.Apprise.instantiate( |
| 161 |
"aprs://DF1JSL-15:12345@DF1ABC?locale=invalid" |
| 162 |
) |
| 163 |
is None |
| 164 |
) |
| 165 |
|
| 166 |
|
| 167 |
instance = apprise.Apprise.instantiate( |
| 168 |
"aprs://DF1JSL-15:12345@abcdefghi/a" |
| 169 |
) |
| 170 |
|
| 171 |
|
| 172 |
assert isinstance(instance, NotifyAprs) |
| 173 |
|
| 174 |
|
| 175 |
assert instance.url(privacy=True).startswith( |
| 176 |
"aprs://DF1JSL-15:****@A...I/A...A?" |
| 177 |
) |
| 178 |
|
| 179 |
|
| 180 |
assert instance.notify("test") is False |
| 181 |
|
| 182 |
|
| 183 |
del instance |
| 184 |
|
| 185 |
|
| 186 |
@mock.patch("socket.create_connection") |
| 187 |
def test_plugin_aprs_edge_cases(mock_create_connection): |
| 188 |
"""NotifyAprs() Edge Cases.""" |
| 189 |
|
| 190 |
|
| 191 |
sobj = mock.Mock() |
| 192 |
sobj.return_value = 1 |
| 193 |
sobj.getpeername.return_value = ("localhost", 1234) |
| 194 |
sobj.socket_close.return_value = None |
| 195 |
sobj.setblocking.return_value = True |
| 196 |
sobj.recv.return_value = "ping\npong pong DF1JSL-15 verified pong".encode( |
| 197 |
"latin-1" |
| 198 |
) |
| 199 |
sobj.sendall.return_value = True |
| 200 |
sobj.settimeout.return_value = True |
| 201 |
|
| 202 |
|
| 203 |
mock_create_connection.return_value = sobj |
| 204 |
|
| 205 |
|
| 206 |
instance = apprise.Apprise.instantiate( |
| 207 |
"aprs://DF1JSL-15:12345@DF1ABC/DF1DEF" |
| 208 |
) |
| 209 |
assert isinstance(instance, NotifyAprs) |
| 210 |
|
| 211 |
|
| 212 |
assert isinstance(instance.url_id(), str) |
| 213 |
|
| 214 |
|
| 215 |
assert len(instance) == 2 |
| 216 |
|
| 217 |
|
| 218 |
sobj.recv.return_value = "one line".encode("latin-1") |
| 219 |
assert instance.notify(body="body", title="title") is False |
| 220 |
sobj.recv.return_value = "\n\n\n".encode("latin-1") |
| 221 |
assert instance.notify(body="body", title="title") is False |
| 222 |
sobj.recv.return_value = "".encode("latin-1") |
| 223 |
assert instance.notify(body="body", title="title") is False |
| 224 |
sobj.recv.return_value = "\ndata".encode("latin-1") |
| 225 |
assert instance.notify(body="body", title="title") is False |
| 226 |
|
| 227 |
sobj.recv.return_value = "ping\npong pong DF1JSL-14 verified, pong".encode( |
| 228 |
"latin-1" |
| 229 |
) |
| 230 |
assert instance.notify(body="body", title="title") is False |
| 231 |
|
| 232 |
sobj.recv.return_value = ( |
| 233 |
"ping\npong pong DF1JSL-15 unverified, pong".encode("latin-1") |
| 234 |
) |
| 235 |
assert instance.notify(body="body", title="title") is False |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
sobj.return_value = False |
| 241 |
assert instance.aprsis_login() is False |
| 242 |
sobj.return_value = 1 |
| 243 |
sobj.recv.return_value = "".encode("latin-1") |
| 244 |
assert instance.aprsis_login() is False |
| 245 |
sobj.recv.return_value = "ping\npong pong DF1JSL-15 verified pong".encode( |
| 246 |
"latin-1" |
| 247 |
) |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
sobj.sendall.return_value = None |
| 253 |
sobj.sendall.side_effect = socket.gaierror("gaierror") |
| 254 |
|
| 255 |
assert instance.socket_send("data") is False |
| 256 |
|
| 257 |
assert instance.socket_open() is True |
| 258 |
assert instance.socket_send("data") is False |
| 259 |
sobj.sendall.side_effect = socket.timeout("timeout") |
| 260 |
assert instance.socket_open() is True |
| 261 |
assert instance.socket_send("data") is False |
| 262 |
assert instance.socket_open() is True |
| 263 |
sobj.sendall.side_effect = OSError("error") |
| 264 |
assert instance.socket_send("data") is False |
| 265 |
|
| 266 |
|
| 267 |
sobj.return_value = 1 |
| 268 |
assert instance.socket_open() is True |
| 269 |
assert instance.aprsis_login() is False |
| 270 |
|
| 271 |
|
| 272 |
sobj.sendall.side_effect = None |
| 273 |
sobj.sendall.return_value = True |
| 274 |
|
| 275 |
assert instance.socket_open() is True |
| 276 |
sobj.close.return_value = None |
| 277 |
sobj.close.side_effect = socket.gaierror("gaierror") |
| 278 |
instance.socket_close() |
| 279 |
sobj.close.side_effect = socket.timeout("timeout") |
| 280 |
instance.socket_close() |
| 281 |
sobj.close.side_effect = OSError("error") |
| 282 |
instance.socket_close() |
| 283 |
sobj.return_value = None |
| 284 |
instance.socket_close() |
| 285 |
|
| 286 |
assert instance.socket_receive(100) is False |
| 287 |
sobj.close.side_effect = None |
| 288 |
sobj.close.return_value = None |
| 289 |
|
| 290 |
instance.socket_close() |
| 291 |
|
| 292 |
sobj.return_value = 1 |
| 293 |
mock_create_connection.return_value = None |
| 294 |
mock_create_connection.side_effect = socket.gaierror("gaierror") |
| 295 |
assert instance.socket_open() is False |
| 296 |
assert instance.notify("test") is False |
| 297 |
mock_create_connection.side_effect = socket.timeout("timeout") |
| 298 |
assert instance.socket_open() is False |
| 299 |
assert instance.notify("test") is False |
| 300 |
mock_create_connection.side_effect = OSError("error") |
| 301 |
assert instance.socket_open() is False |
| 302 |
assert instance.notify("test") is False |
| 303 |
mock_create_connection.side_effect = ConnectionError("ConnectionError") |
| 304 |
assert instance.socket_open() is False |
| 305 |
assert instance.notify("test") is False |
| 306 |
|
| 307 |
|
| 308 |
mock_create_connection.return_value = sobj |
| 309 |
mock_create_connection.side_effect = None |
| 310 |
|
| 311 |
|
| 312 |
assert instance.socket_open() is True |
| 313 |
|
| 314 |
|
| 315 |
sobj.getpeername.return_value = None |
| 316 |
sobj.getpeername.side_effect = ValueError("getpeername ValueError") |
| 317 |
assert instance.socket_open() is True |
| 318 |
|
| 319 |
sobj.getpeername.return_value = ("localhost", 1234) |
| 320 |
assert instance.socket_open() is True |
| 321 |
|
| 322 |
assert instance.socket_receive(0) |
| 323 |
assert instance.socket_receive(-1) |
| 324 |
assert instance.socket_receive(100) |
| 325 |
|
| 326 |
sobj.recv.side_effect = socket.gaierror("gaierror") |
| 327 |
assert instance.socket_open() is True |
| 328 |
assert instance.socket_receive(100) is False |
| 329 |
sobj.recv.side_effect = socket.timeout("timeout") |
| 330 |
assert instance.socket_open() is True |
| 331 |
assert instance.socket_receive(100) is False |
| 332 |
sobj.recv.side_effect = OSError("error") |
| 333 |
assert instance.socket_open() is True |
| 334 |
assert instance.socket_receive(100) is False |
| 335 |
|
| 336 |
|
| 337 |
sobj.recv.side_effect = None |
| 338 |
sobj.recv.return_value = "ping\npong pong DF1JSL-15 verified pong".encode( |
| 339 |
"latin-1" |
| 340 |
) |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
sobj.sendall.return_value = True |
| 346 |
assert instance.notify("test") is True |
| 347 |
|
| 348 |
sobj.sendall.return_value = None |
| 349 |
sobj.sendall.side_effect = (True, socket.gaierror("gaierror")) |
| 350 |
assert instance.notify("test") is False |
| 351 |
|
| 352 |
sobj.sendall.return_value = True |
| 353 |
sobj.sendall.side_effect = None |
| 354 |
del sobj |
| 355 |
|
| 356 |
|
| 357 |
def test_plugin_aprs_config_files(): |
| 358 |
"""NotifyAprs() Config File Cases.""" |
| 359 |
content = """ |
| 360 |
urls: |
| 361 |
- aprs://DF1JSL-15:12345@DF1ABC": |
| 362 |
- locale: NOAM |
| 363 |
|
| 364 |
- aprs://DF1JSL-15:12345@DF1ABC: |
| 365 |
- locale: SOAM |
| 366 |
|
| 367 |
- aprs://DF1JSL-15:12345@DF1ABC: |
| 368 |
- locale: EURO |
| 369 |
|
| 370 |
- aprs://DF1JSL-15:12345@DF1ABC: |
| 371 |
- locale: ASIA |
| 372 |
|
| 373 |
- aprs://DF1JSL-15:12345@DF1ABC: |
| 374 |
- locale: AUNZ |
| 375 |
|
| 376 |
- aprs://DF1JSL-15:12345@DF1ABC: |
| 377 |
- locale: ROTA |
| 378 |
|
| 379 |
# This will fail to load because the locale is bad |
| 380 |
- aprs://DF1JSL-15:12345@DF1ABC: |
| 381 |
- locale: aprs_invalid |
| 382 |
""" |
| 383 |
|
| 384 |
|
| 385 |
ac = apprise.AppriseConfig() |
| 386 |
assert ac.add_config(content=content) is True |
| 387 |
|
| 388 |
aobj = apprise.Apprise() |
| 389 |
|
| 390 |
|
| 391 |
aobj.add(ac) |
| 392 |
|
| 393 |
assert len(ac.servers()) == 6 |
| 394 |
assert len(aobj) == 6 |