Skip to content

Commit

Permalink
Add software tests for mocked "pushover" service plugin
Browse files Browse the repository at this point in the history
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"
  • Loading branch information
amotl committed Jun 18, 2021
1 parent 59da288 commit 68830bc
Show file tree
Hide file tree
Showing 5 changed files with 546 additions and 14 deletions.
1 change: 0 additions & 1 deletion HANDBOOK.md
Original file line number Diff line number Diff line change
Expand Up @@ -2161,7 +2161,6 @@ and one or more _application keys_ which you configure in the targets definition
```ini
[config:pushover]
callback = None
device = cellphone1,cellphone2
targets = {
'nagios' : ['userkey1', 'appkey1', 'sound1'],
'alerts' : ['userkey2', 'appkey2'],
Expand Down
30 changes: 17 additions & 13 deletions mqttwarn/services/pushover.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ class PushoverError(Exception): pass
def pushover(image, **kwargs):
assert 'message' in kwargs

if not 'token' in kwargs:
kwargs['token'] = os.environ['PUSHOVER_TOKEN']
if not 'user' in kwargs:
kwargs['user'] = os.environ['PUSHOVER_USER']

url = urljoin(PUSHOVER_API, "messages.json")
headers = { 'User-Agent': 'mqttwarn' }

Expand Down Expand Up @@ -70,9 +65,18 @@ def plugin(srv, item):

try:
userkey = addrs[0]
appkey = addrs[1]
token = addrs[1]
except:
srv.logging.warn("No pushover userkey/appkey configured for target `%s'" % (item.target))
srv.logging.warning("Invalid address configuration for target `%s'" % (item.target))
return False

if userkey is None and "PUSHOVER_USER" in os.environ:
userkey = os.environ["PUSHOVER_USER"].strip()
if token is None and "PUSHOVER_TOKEN" in os.environ:
token = os.environ["PUSHOVER_TOKEN"].strip()

if not userkey or not token:
srv.logging.warning("No pushover credentials configured for target `%s'" % (item.target))
return False

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

if len(addrs) > 3:
params['sound'] = addrs[3]
params['devices'] = addrs[3]

if title is not None:
params['title'] = title
Expand Down Expand Up @@ -112,22 +116,22 @@ def plugin(srv, item):
authuser = item.data['user']
authpass = item.data['password']
if authtype == 'digest':
image = requests.get(imageurl, stream=True,auth=HTTPDigestAuth(authuser, authpass)).raw
image = requests.get(imageurl, stream=True, auth=HTTPDigestAuth(authuser, authpass)).raw
else:
image = requests.get(imageurl, stream=True,auth=HTTPBasicAuth(authuser, authpass)).raw
image = requests.get(imageurl, stream=True, auth=HTTPBasicAuth(authuser, authpass)).raw
else:
image = requests.get(imageurl, stream=True).raw
elif 'imagebase64' in item.data:
imagebase64 = item.data['imagebase64']
srv.logging.debug("Image (base64 encoded) detected")
image = base64.decodestring(imagebase64)
image = base64.decodebytes(imagebase64)

try:
srv.logging.debug("Sending pushover notification to %s [%s]...." % (item.target, params))
pushover(image=image, user=userkey, token=appkey, **params)
pushover(image=image, user=userkey, token=token, **params)
srv.logging.debug("Successfully sent pushover notification")
except Exception as e:
srv.logging.warn("Error sending pushover notification: %s" % e)
srv.logging.warning("Error sending pushover notification: %s" % e)
return False

return True
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@
'tox>=3.14.2',
'surrogate==0.1',
'dataclasses; python_version<"3.7"',
'requests-toolbelt>=0.9.1,<1',
'responses>=0.13.3,<1',
]


Expand Down
Loading

0 comments on commit 68830bc

Please sign in to comment.