Skip to content

Commit 68830bc

Browse files
committed
Add software tests for mocked "pushover" service plugin
Along the lines, - Improve its parameter juggling re. "PUSHOVER_*" environment variables - Fix reading the fourth target address parameter "devices" - Use "base64.decodebytes" instead of "base64.decodestring"
1 parent 59da288 commit 68830bc

File tree

5 files changed

+546
-14
lines changed

5 files changed

+546
-14
lines changed

HANDBOOK.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2161,7 +2161,6 @@ and one or more _application keys_ which you configure in the targets definition
21612161
```ini
21622162
[config:pushover]
21632163
callback = None
2164-
device = cellphone1,cellphone2
21652164
targets = {
21662165
'nagios' : ['userkey1', 'appkey1', 'sound1'],
21672166
'alerts' : ['userkey2', 'appkey2'],

mqttwarn/services/pushover.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ class PushoverError(Exception): pass
3434
def pushover(image, **kwargs):
3535
assert 'message' in kwargs
3636

37-
if not 'token' in kwargs:
38-
kwargs['token'] = os.environ['PUSHOVER_TOKEN']
39-
if not 'user' in kwargs:
40-
kwargs['user'] = os.environ['PUSHOVER_USER']
41-
4237
url = urljoin(PUSHOVER_API, "messages.json")
4338
headers = { 'User-Agent': 'mqttwarn' }
4439

@@ -70,9 +65,18 @@ def plugin(srv, item):
7065

7166
try:
7267
userkey = addrs[0]
73-
appkey = addrs[1]
68+
token = addrs[1]
7469
except:
75-
srv.logging.warn("No pushover userkey/appkey configured for target `%s'" % (item.target))
70+
srv.logging.warning("Invalid address configuration for target `%s'" % (item.target))
71+
return False
72+
73+
if userkey is None and "PUSHOVER_USER" in os.environ:
74+
userkey = os.environ["PUSHOVER_USER"].strip()
75+
if token is None and "PUSHOVER_TOKEN" in os.environ:
76+
token = os.environ["PUSHOVER_TOKEN"].strip()
77+
78+
if not userkey or not token:
79+
srv.logging.warning("No pushover credentials configured for target `%s'" % (item.target))
7680
return False
7781

7882
params = {
@@ -84,7 +88,7 @@ def plugin(srv, item):
8488
params['sound'] = addrs[2]
8589

8690
if len(addrs) > 3:
87-
params['sound'] = addrs[3]
91+
params['devices'] = addrs[3]
8892

8993
if title is not None:
9094
params['title'] = title
@@ -112,22 +116,22 @@ def plugin(srv, item):
112116
authuser = item.data['user']
113117
authpass = item.data['password']
114118
if authtype == 'digest':
115-
image = requests.get(imageurl, stream=True,auth=HTTPDigestAuth(authuser, authpass)).raw
119+
image = requests.get(imageurl, stream=True, auth=HTTPDigestAuth(authuser, authpass)).raw
116120
else:
117-
image = requests.get(imageurl, stream=True,auth=HTTPBasicAuth(authuser, authpass)).raw
121+
image = requests.get(imageurl, stream=True, auth=HTTPBasicAuth(authuser, authpass)).raw
118122
else:
119123
image = requests.get(imageurl, stream=True).raw
120124
elif 'imagebase64' in item.data:
121125
imagebase64 = item.data['imagebase64']
122126
srv.logging.debug("Image (base64 encoded) detected")
123-
image = base64.decodestring(imagebase64)
127+
image = base64.decodebytes(imagebase64)
124128

125129
try:
126130
srv.logging.debug("Sending pushover notification to %s [%s]...." % (item.target, params))
127-
pushover(image=image, user=userkey, token=appkey, **params)
131+
pushover(image=image, user=userkey, token=token, **params)
128132
srv.logging.debug("Successfully sent pushover notification")
129133
except Exception as e:
130-
srv.logging.warn("Error sending pushover notification: %s" % e)
134+
srv.logging.warning("Error sending pushover notification: %s" % e)
131135
return False
132136

133137
return True

setup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@
149149
'tox>=3.14.2',
150150
'surrogate==0.1',
151151
'dataclasses; python_version<"3.7"',
152+
'requests-toolbelt>=0.9.1,<1',
153+
'responses>=0.13.3,<1',
152154
]
153155

154156

0 commit comments

Comments
 (0)