| 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.plugins.threema import NotifyThreema |
| 37 |
|
| 38 |
logging.disable(logging.CRITICAL) |
| 39 |
|
| 40 |
|
| 41 |
apprise_url_tests = ( |
| 42 |
( |
| 43 |
"threema://", |
| 44 |
{ |
| 45 |
|
| 46 |
"instance": TypeError, |
| 47 |
}, |
| 48 |
), |
| 49 |
( |
| 50 |
"threema://@:", |
| 51 |
{ |
| 52 |
|
| 53 |
"instance": TypeError, |
| 54 |
}, |
| 55 |
), |
| 56 |
( |
| 57 |
"threema://user@secret", |
| 58 |
{ |
| 59 |
|
| 60 |
"instance": TypeError, |
| 61 |
}, |
| 62 |
), |
| 63 |
( |
| 64 |
"threema://*THEGWID@secret/{targets}/".format( |
| 65 |
targets="/".join(["2222"]) |
| 66 |
), |
| 67 |
{ |
| 68 |
|
| 69 |
"instance": NotifyThreema, |
| 70 |
"notify_response": False, |
| 71 |
"privacy_url": "threema://%2ATHEGWID@****/2222", |
| 72 |
}, |
| 73 |
), |
| 74 |
( |
| 75 |
"threema://*THEGWID@secret/{targets}/".format( |
| 76 |
targets="/".join(["16134442222"]) |
| 77 |
), |
| 78 |
{ |
| 79 |
|
| 80 |
"instance": NotifyThreema, |
| 81 |
"privacy_url": "threema://%2ATHEGWID@****/16134442222", |
| 82 |
}, |
| 83 |
), |
| 84 |
( |
| 85 |
"threema://*THEGWID@secret/{targets}/".format( |
| 86 |
targets="/".join(["16134442222", "16134443333"]) |
| 87 |
), |
| 88 |
{ |
| 89 |
|
| 90 |
"instance": NotifyThreema, |
| 91 |
"privacy_url": "threema://%2ATHEGWID@****/16134442222/16134443333", |
| 92 |
}, |
| 93 |
), |
| 94 |
( |
| 95 |
"threema:///?secret=secret&from=*THEGWID&to={targets}".format( |
| 96 |
targets=",".join(["16134448888", "[email protected]", "abcd1234"]) |
| 97 |
), |
| 98 |
{ |
| 99 |
|
| 100 |
"instance": NotifyThreema, |
| 101 |
}, |
| 102 |
), |
| 103 |
( |
| 104 |
"threema:///?secret=secret&gwid=*THEGWID&to={targets}".format( |
| 105 |
targets=",".join(["16134448888", "[email protected]", "abcd1234"]) |
| 106 |
), |
| 107 |
{ |
| 108 |
|
| 109 |
"instance": NotifyThreema, |
| 110 |
}, |
| 111 |
), |
| 112 |
( |
| 113 |
"threema://*THEGWID@secret", |
| 114 |
{ |
| 115 |
"instance": NotifyThreema, |
| 116 |
|
| 117 |
"notify_response": False, |
| 118 |
}, |
| 119 |
), |
| 120 |
( |
| 121 |
"threema://*THEGWID@secret/16134443333", |
| 122 |
{ |
| 123 |
"instance": NotifyThreema, |
| 124 |
|
| 125 |
"response": False, |
| 126 |
"requests_response_code": 999, |
| 127 |
}, |
| 128 |
), |
| 129 |
( |
| 130 |
"threema://*THEGWID@secret/16134443333", |
| 131 |
{ |
| 132 |
"instance": NotifyThreema, |
| 133 |
|
| 134 |
"test_requests_exceptions": True, |
| 135 |
}, |
| 136 |
), |
| 137 |
) |
| 138 |
|
| 139 |
|
| 140 |
def test_plugin_threema(): |
| 141 |
"""NotifyThreema() Apprise URLs.""" |
| 142 |
|
| 143 |
|
| 144 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 145 |
|
| 146 |
|
| 147 |
@mock.patch("requests.post") |
| 148 |
def test_plugin_threema_edge_cases(mock_post): |
| 149 |
"""NotifyThreema() Edge Cases.""" |
| 150 |
|
| 151 |
|
| 152 |
response = requests.Request() |
| 153 |
response.status_code = requests.codes.ok |
| 154 |
|
| 155 |
|
| 156 |
mock_post.return_value = response |
| 157 |
|
| 158 |
|
| 159 |
gwid = "*THEGWID" |
| 160 |
secret = "mysecret" |
| 161 |
targets = "+1 (555) 123-9876" |
| 162 |
|
| 163 |
|
| 164 |
with pytest.raises(TypeError): |
| 165 |
NotifyThreema(user=gwid, secret=None, targets=targets) |
| 166 |
|
| 167 |
results = NotifyThreema.parse_url( |
| 168 |
f"threema://?gwid={gwid}&secret={secret}&to={targets}" |
| 169 |
) |
| 170 |
|
| 171 |
assert isinstance(results, dict) |
| 172 |
assert results["user"] == gwid |
| 173 |
assert results["secret"] == secret |
| 174 |
assert results["password"] is None |
| 175 |
assert results["port"] is None |
| 176 |
assert results["host"] == "" |
| 177 |
assert results["fullpath"] == "/" |
| 178 |
assert results["path"] == "/" |
| 179 |
assert results["query"] is None |
| 180 |
assert results["schema"] == "threema" |
| 181 |
assert results["url"] == "threema:///" |
| 182 |
assert isinstance(results["targets"], list) |
| 183 |
assert len(results["targets"]) == 1 |
| 184 |
assert results["targets"][0] == targets |
| 185 |
|
| 186 |
instance = NotifyThreema(**results) |
| 187 |
assert len(instance.targets) == 1 |
| 188 |
assert instance.targets[0] == ("phone", "15551239876") |
| 189 |
assert isinstance(instance, NotifyThreema) |
| 190 |
|
| 191 |
response = instance.send(title="title", body="body 😊") |
| 192 |
assert response is True |
| 193 |
assert mock_post.call_count == 1 |
| 194 |
|
| 195 |
details = mock_post.call_args_list[0] |
| 196 |
assert details[0][0] == "https://msgapi.threema.ch/send_simple" |
| 197 |
assert details[1]["headers"]["User-Agent"] == "Apprise" |
| 198 |
assert details[1]["headers"]["Accept"] == "*/*" |
| 199 |
assert ( |
| 200 |
details[1]["headers"]["Content-Type"] |
| 201 |
== "application/x-www-form-urlencoded; charset=utf-8" |
| 202 |
) |
| 203 |
assert details[1]["params"]["secret"] == secret |
| 204 |
assert details[1]["params"]["from"] == gwid |
| 205 |
assert details[1]["params"]["phone"] == "15551239876" |
| 206 |
assert details[1]["params"]["text"] == "body 😊".encode() |