| 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 import NotifyType |
| 37 |
from apprise.plugins.ifttt import NotifyIFTTT |
| 38 |
|
| 39 |
logging.disable(logging.CRITICAL) |
| 40 |
|
| 41 |
|
| 42 |
apprise_url_tests = ( |
| 43 |
( |
| 44 |
"ifttt://", |
| 45 |
{ |
| 46 |
"instance": TypeError, |
| 47 |
}, |
| 48 |
), |
| 49 |
( |
| 50 |
"ifttt://:@/", |
| 51 |
{ |
| 52 |
"instance": TypeError, |
| 53 |
}, |
| 54 |
), |
| 55 |
|
| 56 |
( |
| 57 |
"ifttt://EventID/", |
| 58 |
{ |
| 59 |
"instance": TypeError, |
| 60 |
}, |
| 61 |
), |
| 62 |
|
| 63 |
( |
| 64 |
"ifttt://WebHookID@EventID/?+TemplateKey=TemplateVal", |
| 65 |
{ |
| 66 |
"instance": NotifyIFTTT, |
| 67 |
|
| 68 |
"privacy_url": "ifttt://W...D", |
| 69 |
}, |
| 70 |
), |
| 71 |
|
| 72 |
( |
| 73 |
"ifttt://WebHookID?to=EventID,EventID2", |
| 74 |
{ |
| 75 |
"instance": NotifyIFTTT, |
| 76 |
}, |
| 77 |
), |
| 78 |
|
| 79 |
( |
| 80 |
"ifttt://WebHookID@EventID/?-Value1=&-Value2", |
| 81 |
{ |
| 82 |
"instance": NotifyIFTTT, |
| 83 |
}, |
| 84 |
), |
| 85 |
|
| 86 |
( |
| 87 |
"ifttt://WebHookID@EventID/EventID2/", |
| 88 |
{ |
| 89 |
"instance": NotifyIFTTT, |
| 90 |
}, |
| 91 |
), |
| 92 |
|
| 93 |
( |
| 94 |
"https://maker.ifttt.com/use/WebHookID/", |
| 95 |
{ |
| 96 |
|
| 97 |
"instance": TypeError, |
| 98 |
}, |
| 99 |
), |
| 100 |
( |
| 101 |
"https://maker.ifttt.com/use/WebHookID/EventID/", |
| 102 |
{ |
| 103 |
"instance": NotifyIFTTT, |
| 104 |
}, |
| 105 |
), |
| 106 |
|
| 107 |
( |
| 108 |
"https://maker.ifttt.com/use/WebHookID/EventID/?-Value1=", |
| 109 |
{ |
| 110 |
"instance": NotifyIFTTT, |
| 111 |
}, |
| 112 |
), |
| 113 |
|
| 114 |
( |
| 115 |
"ifttt://WebHookID@EventID", |
| 116 |
{ |
| 117 |
"instance": NotifyIFTTT, |
| 118 |
|
| 119 |
"response": False, |
| 120 |
"requests_response_code": requests.codes.internal_server_error, |
| 121 |
}, |
| 122 |
), |
| 123 |
( |
| 124 |
"ifttt://WebHookID@EventID", |
| 125 |
{ |
| 126 |
"instance": NotifyIFTTT, |
| 127 |
|
| 128 |
"response": False, |
| 129 |
"requests_response_code": 999, |
| 130 |
}, |
| 131 |
), |
| 132 |
( |
| 133 |
"ifttt://WebHookID@EventID", |
| 134 |
{ |
| 135 |
"instance": NotifyIFTTT, |
| 136 |
|
| 137 |
|
| 138 |
"test_requests_exceptions": True, |
| 139 |
}, |
| 140 |
), |
| 141 |
) |
| 142 |
|
| 143 |
|
| 144 |
def test_plugin_ifttt_urls(): |
| 145 |
"""NotifyIFTTT() Apprise URLs.""" |
| 146 |
|
| 147 |
|
| 148 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 149 |
|
| 150 |
|
| 151 |
@mock.patch("requests.get") |
| 152 |
@mock.patch("requests.post") |
| 153 |
def test_plugin_ifttt_edge_cases(mock_post, mock_get): |
| 154 |
"""NotifyIFTTT() Edge Cases.""" |
| 155 |
|
| 156 |
|
| 157 |
webhook_id = "webhook_id" |
| 158 |
events = ["event1", "event2"] |
| 159 |
|
| 160 |
|
| 161 |
mock_get.return_value = requests.Request() |
| 162 |
mock_post.return_value = requests.Request() |
| 163 |
mock_post.return_value.status_code = requests.codes.ok |
| 164 |
mock_get.return_value.status_code = requests.codes.ok |
| 165 |
mock_get.return_value.content = "{}" |
| 166 |
mock_post.return_value.content = "{}" |
| 167 |
|
| 168 |
|
| 169 |
with pytest.raises(TypeError): |
| 170 |
NotifyIFTTT(webhook_id=None, events=None) |
| 171 |
|
| 172 |
|
| 173 |
with pytest.raises(TypeError): |
| 174 |
NotifyIFTTT(webhook_id=None, events=events) |
| 175 |
|
| 176 |
|
| 177 |
with pytest.raises(TypeError): |
| 178 |
NotifyIFTTT(webhook_id=" ", events=events) |
| 179 |
|
| 180 |
|
| 181 |
with pytest.raises(TypeError): |
| 182 |
NotifyIFTTT(webhook_id=webhook_id, events=None) |
| 183 |
|
| 184 |
obj = NotifyIFTTT(webhook_id=webhook_id, events=events) |
| 185 |
assert isinstance(obj, NotifyIFTTT) |
| 186 |
|
| 187 |
assert ( |
| 188 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 189 |
is True |
| 190 |
) |
| 191 |
|
| 192 |
|
| 193 |
assert mock_post.call_count >= 1 |
| 194 |
call = mock_post.call_args_list[-1] |
| 195 |
payload = call[1].get("json") or call[1].get("data") or "" |
| 196 |
payload_text = payload if isinstance(payload, str) else str(payload) |
| 197 |
|
| 198 |
assert "NotifyType." not in payload_text |
| 199 |
|
| 200 |
if isinstance(payload, dict): |
| 201 |
assert NotifyType.INFO.value in payload_text |
| 202 |
|
| 203 |
|
| 204 |
obj = NotifyIFTTT( |
| 205 |
webhook_id=webhook_id, |
| 206 |
events=events, |
| 207 |
add_tokens={"Test": "ValueA", "Test2": "ValueB"}, |
| 208 |
) |
| 209 |
|
| 210 |
assert isinstance(obj, NotifyIFTTT) |
| 211 |
|
| 212 |
assert ( |
| 213 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 214 |
is True |
| 215 |
) |
| 216 |
|
| 217 |
|
| 218 |
with pytest.raises(TypeError): |
| 219 |
NotifyIFTTT( |
| 220 |
webhook_id=webhook_id, |
| 221 |
events=events, |
| 222 |
del_tokens=NotifyIFTTT.ifttt_default_title_key, |
| 223 |
) |
| 224 |
|
| 225 |
assert isinstance(obj, NotifyIFTTT) |
| 226 |
|
| 227 |
assert ( |
| 228 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 229 |
is True |
| 230 |
) |
| 231 |
|
| 232 |
|
| 233 |
obj = NotifyIFTTT( |
| 234 |
webhook_id=webhook_id, |
| 235 |
events=events, |
| 236 |
add_tokens={"MyKey": "MyValue"}, |
| 237 |
del_tokens=( |
| 238 |
NotifyIFTTT.ifttt_default_title_key, |
| 239 |
NotifyIFTTT.ifttt_default_body_key, |
| 240 |
NotifyIFTTT.ifttt_default_type_key, |
| 241 |
), |
| 242 |
) |
| 243 |
|
| 244 |
assert isinstance(obj, NotifyIFTTT) |
| 245 |
|
| 246 |
assert ( |
| 247 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 248 |
is True |
| 249 |
) |
| 250 |
|
| 251 |
|
| 252 |
obj = NotifyIFTTT( |
| 253 |
webhook_id=webhook_id, |
| 254 |
events=events, |
| 255 |
add_tokens={"MyKey": "MyValue"}, |
| 256 |
del_tokens={ |
| 257 |
NotifyIFTTT.ifttt_default_title_key: None, |
| 258 |
NotifyIFTTT.ifttt_default_body_key: None, |
| 259 |
NotifyIFTTT.ifttt_default_type_key: None, |
| 260 |
}, |
| 261 |
) |
| 262 |
|
| 263 |
assert isinstance(obj, NotifyIFTTT) |