-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added simplepush provider * Added tests * Updated readme * Added negative test * Revert "Added negative test" This reverts commit 811d96f
- Loading branch information
Showing
5 changed files
with
90 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from . import pushover | ||
from . import pushover, simplepush | ||
|
||
_all_providers = { | ||
'pushover': pushover.Pushover | ||
'pushover': pushover.Pushover, | ||
'simplepush': simplepush.SimplePush | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import requests | ||
|
||
from ..core import Provider, Response | ||
|
||
|
||
class SimplePush(Provider): | ||
base_url = 'https://api.simplepush.io/send' | ||
site_url = 'https://simplepush.io/' | ||
provider_name = 'simplepush' | ||
|
||
@property | ||
def schema(self): | ||
return { | ||
'type': 'object', | ||
'properties': { | ||
'key': {'type': 'string', | ||
'title': 'your user key'}, | ||
'message': {'type': 'string', | ||
'title': 'your message'}, | ||
}, | ||
'required': ['key', 'message'], | ||
'additionalProperties': False | ||
} | ||
|
||
def _prepare_data(self, data: dict): | ||
data['msg'] = data.pop('message') | ||
return data | ||
|
||
def _send_notification(self, data): | ||
status = 'Success' | ||
errors = [] | ||
response = None | ||
try: | ||
response = requests.post(self.base_url, data=data) | ||
response.raise_for_status() | ||
except requests.RequestException as e: | ||
status = 'Failure' | ||
if e.response is not None: | ||
response = e.response | ||
errors = [response.json()['message']] | ||
else: | ||
errors.append(str(e)) | ||
finally: | ||
return Response(status=status, | ||
provider=self.provider_name, | ||
data=data, | ||
response=response, | ||
errors=errors) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import pytest | ||
|
||
from notifiers import get_notifier | ||
from notifiers.exceptions import BadArguments, NotificationError | ||
|
||
|
||
class TestSimplePush(object): | ||
"""SimplePush notifier tests | ||
Note: These tests assume correct environs set for NOTIFIERS_SIMPLEPUSH_KEY | ||
""" | ||
|
||
def test_simplepush_metadata(self): | ||
p = get_notifier('simplepush') | ||
assert p.metadata == {'base_url': 'https://api.simplepush.io/send', | ||
'site_url': 'https://simplepush.io/', | ||
'provider_name': 'simplepush'} | ||
|
||
@pytest.mark.parametrize('data, message', [ | ||
({}, 'key'), | ||
({'key': 'foo'}, 'message'), | ||
]) | ||
def test_simplepush_missing_required(self, data, message): | ||
p = get_notifier('simplepush') | ||
data['env_prefix'] = 'test' | ||
with pytest.raises(BadArguments) as e: | ||
p.notify(**data) | ||
assert f'\'{message}\' is a required property' in e.value.message | ||
|
||
@pytest.mark.online | ||
def test_simplepush_sanity(self): | ||
"""Successful simplepush notification""" | ||
p = get_notifier('simplepush') | ||
data = {'message': 'foo'} | ||
rsp = p.notify(**data) | ||
rsp.raise_on_errors() |