| 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 dumps |
| 29 |
import logging |
| 30 |
|
| 31 |
from helpers import AppriseURLTester |
| 32 |
import requests |
| 33 |
|
| 34 |
from apprise import Apprise |
| 35 |
from apprise.plugins.viber import NotifyViber |
| 36 |
|
| 37 |
logging.disable(logging.CRITICAL) |
| 38 |
|
| 39 |
VIBER_GOOD_RESPONSE = dumps({"status": 0, "status_message": "ok"}) |
| 40 |
VIBER_BAD_RESPONSE = dumps( |
| 41 |
{"status": 12, "status_message": "Too many requests"} |
| 42 |
) |
| 43 |
|
| 44 |
|
| 45 |
apprise_url_tests = ( |
| 46 |
("viber://", False), |
| 47 |
("viber:///", False), |
| 48 |
("viber://tokena", {"instance": NotifyViber, "notify_response": False}), |
| 49 |
( |
| 50 |
"viber://?token=tokenb", |
| 51 |
{"instance": NotifyViber, "notify_response": False}, |
| 52 |
), |
| 53 |
( |
| 54 |
"viber://token/targetx", |
| 55 |
{ |
| 56 |
"instance": NotifyViber, |
| 57 |
|
| 58 |
"requests_response_text": VIBER_GOOD_RESPONSE, |
| 59 |
}, |
| 60 |
), |
| 61 |
( |
| 62 |
"viber://token/t1/t2?from=Viber%20Bot", |
| 63 |
{ |
| 64 |
"instance": NotifyViber, |
| 65 |
"requests_response_text": VIBER_GOOD_RESPONSE, |
| 66 |
}, |
| 67 |
), |
| 68 |
( |
| 69 |
"viber://t1/t2?token=token", |
| 70 |
{ |
| 71 |
"instance": NotifyViber, |
| 72 |
"requests_response_text": VIBER_GOOD_RESPONSE, |
| 73 |
}, |
| 74 |
), |
| 75 |
( |
| 76 |
"viber://?token=token&to=t5", |
| 77 |
{ |
| 78 |
"instance": NotifyViber, |
| 79 |
"requests_response_text": VIBER_GOOD_RESPONSE, |
| 80 |
}, |
| 81 |
), |
| 82 |
( |
| 83 |
"viber://token/t3?avatar=value", |
| 84 |
{ |
| 85 |
"instance": NotifyViber, |
| 86 |
"requests_response_text": VIBER_GOOD_RESPONSE, |
| 87 |
}, |
| 88 |
), |
| 89 |
( |
| 90 |
"viber://token/?to=abc,def", |
| 91 |
{ |
| 92 |
"instance": NotifyViber, |
| 93 |
"requests_response_text": VIBER_GOOD_RESPONSE, |
| 94 |
}, |
| 95 |
), |
| 96 |
( |
| 97 |
"viber://token/m12/?from={}".format( |
| 98 |
"a" * (NotifyViber.viber_sender_name_limit + 1) |
| 99 |
), |
| 100 |
{ |
| 101 |
"instance": NotifyViber, |
| 102 |
"requests_response_text": VIBER_GOOD_RESPONSE, |
| 103 |
}, |
| 104 |
), |
| 105 |
( |
| 106 |
"viber://token/m12/?from={}".format( |
| 107 |
"b" * (NotifyViber.viber_sender_name_limit) |
| 108 |
), |
| 109 |
{ |
| 110 |
"instance": NotifyViber, |
| 111 |
"requests_response_text": VIBER_GOOD_RESPONSE, |
| 112 |
}, |
| 113 |
), |
| 114 |
( |
| 115 |
"viber://?token=token&to=hij,klm", |
| 116 |
{ |
| 117 |
"instance": NotifyViber, |
| 118 |
"requests_response_text": VIBER_GOOD_RESPONSE, |
| 119 |
}, |
| 120 |
), |
| 121 |
( |
| 122 |
"viber://?token=token&to=nop,qrs", |
| 123 |
{ |
| 124 |
"instance": NotifyViber, |
| 125 |
"requests_response_text": VIBER_BAD_RESPONSE, |
| 126 |
"notify_response": False, |
| 127 |
}, |
| 128 |
), |
| 129 |
( |
| 130 |
"viber://?token=token&to=tuv,wxy", |
| 131 |
{ |
| 132 |
"instance": NotifyViber, |
| 133 |
|
| 134 |
"requests_response_text": "{", |
| 135 |
"notify_response": False, |
| 136 |
}, |
| 137 |
), |
| 138 |
|
| 139 |
( |
| 140 |
"viber://token/t10", |
| 141 |
{ |
| 142 |
"instance": NotifyViber, |
| 143 |
"requests_response_text": VIBER_GOOD_RESPONSE, |
| 144 |
"privacy_url": "viber://****/t10", |
| 145 |
}, |
| 146 |
), |
| 147 |
( |
| 148 |
"viber://token/targetZ", |
| 149 |
{ |
| 150 |
"instance": NotifyViber, |
| 151 |
|
| 152 |
"requests_response_text": VIBER_GOOD_RESPONSE, |
| 153 |
"response": False, |
| 154 |
"requests_response_code": 999, |
| 155 |
}, |
| 156 |
), |
| 157 |
( |
| 158 |
"viber://token/targetZ", |
| 159 |
{ |
| 160 |
"instance": NotifyViber, |
| 161 |
|
| 162 |
"response": False, |
| 163 |
"requests_response_text": VIBER_BAD_RESPONSE, |
| 164 |
"requests_response_code": requests.codes.internal_server_error, |
| 165 |
}, |
| 166 |
), |
| 167 |
( |
| 168 |
"viber://token/targetY", |
| 169 |
{ |
| 170 |
"instance": NotifyViber, |
| 171 |
|
| 172 |
|
| 173 |
"requests_response_text": VIBER_GOOD_RESPONSE, |
| 174 |
"test_requests_exceptions": True, |
| 175 |
}, |
| 176 |
), |
| 177 |
) |
| 178 |
|
| 179 |
|
| 180 |
def test_plugin_viber_urls(): |
| 181 |
"""Verify URL parsing, privacy, and basic validation.""" |
| 182 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 183 |
|
| 184 |
|
| 185 |
def test_plugin_viber_http_error_and_exception(mocker): |
| 186 |
"""Verify HTTP error and requests exception paths.""" |
| 187 |
post = mocker.patch("requests.post") |
| 188 |
|
| 189 |
post.return_value.status_code = 400 |
| 190 |
a = Apprise() |
| 191 |
assert a.add("viber://token/target2") is True |
| 192 |
assert a.notify("test") is False |
| 193 |
|
| 194 |
post.side_effect = requests.RequestException("boom") |
| 195 |
a = Apprise() |
| 196 |
assert a.add("viber://token/target2") is True |
| 197 |
assert a.notify("test") is False |