mirror of
https://github.com/caronc/apprise.git
synced 2025-08-17 12:01:33 +02:00
OneSignal to support custom data in payload (#1163)
Co-authored-by: phantom943 <105282577+phantom943@users.noreply.github.com>
This commit is contained in:
@ -26,6 +26,10 @@
|
||||
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
# POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
from json import loads
|
||||
from unittest import mock
|
||||
import pytest
|
||||
import requests
|
||||
from apprise.plugins.one_signal import NotifyOneSignal
|
||||
from helpers import AppriseURLTester
|
||||
from apprise import Apprise
|
||||
@ -105,6 +109,16 @@ apprise_url_tests = (
|
||||
# Test Kwargs
|
||||
'instance': NotifyOneSignal,
|
||||
}),
|
||||
('onesignal://?apikey=abc&template=tp&app=123&to=playerid&body=no'
|
||||
'&:key1=val1&:key2=val2', {
|
||||
# Test Kwargs
|
||||
'instance': NotifyOneSignal,
|
||||
}),
|
||||
('onesignal://?apikey=abc&template=tp&app=123&to=playerid&body=no'
|
||||
'&+key1=val1&+key2=val2', {
|
||||
# Test Kwargs
|
||||
'instance': NotifyOneSignal,
|
||||
}),
|
||||
('onesignal://appid@apikey/#segment/playerid/', {
|
||||
'instance': NotifyOneSignal,
|
||||
# throw a bizzare code forcing us to fail to look it up
|
||||
@ -244,3 +258,106 @@ def test_plugin_onesignal_edge_cases():
|
||||
|
||||
# Individual queries
|
||||
assert len(obj) == 16
|
||||
|
||||
# custom must be a dictionary
|
||||
with pytest.raises(TypeError):
|
||||
NotifyOneSignal(
|
||||
app='appid', apikey='key', targets=['@user'], custom='not-a-dict')
|
||||
|
||||
# postback must be a dictionary
|
||||
with pytest.raises(TypeError):
|
||||
NotifyOneSignal(
|
||||
app='appid', apikey='key', targets=['@user'],
|
||||
custom=[], postback='not-a-dict')
|
||||
|
||||
|
||||
@mock.patch('requests.post')
|
||||
def test_plugin_onesignal_notifications(mock_post):
|
||||
"""
|
||||
OneSignal() Notifications Support
|
||||
|
||||
"""
|
||||
# Prepare Mock
|
||||
mock_post.return_value = requests.Request()
|
||||
mock_post.return_value.status_code = requests.codes.ok
|
||||
|
||||
# Load URL with Template
|
||||
instance = Apprise.instantiate(
|
||||
'onesignal://templateid:appid@apikey/@user/?:key1=value1&+key3=value3')
|
||||
|
||||
# Validate that it loaded okay
|
||||
assert isinstance(instance, NotifyOneSignal)
|
||||
|
||||
response = instance.notify("hello world")
|
||||
assert response is True
|
||||
assert mock_post.call_count == 1
|
||||
assert mock_post.call_args_list[0][0][0] == \
|
||||
'https://api.onesignal.com/notifications'
|
||||
|
||||
details = mock_post.call_args_list[0]
|
||||
payload = loads(details[1]['data'])
|
||||
|
||||
assert payload == {
|
||||
'app_id': 'appid',
|
||||
'contents': {'en': 'hello world'},
|
||||
'content_available': True,
|
||||
'template_id': 'templateid',
|
||||
'custom_data': {'key1': 'value1'},
|
||||
'data': {'key3': 'value3'},
|
||||
'large_icon': 'https://github.com/caronc/apprise'
|
||||
'/raw/master/apprise/assets/themes/default/apprise-info-72x72.png',
|
||||
'small_icon': 'https://github.com/caronc/apprise'
|
||||
'/raw/master/apprise/assets/themes/default/apprise-info-32x32.png',
|
||||
'include_external_user_ids': ['@user']}
|
||||
|
||||
mock_post.reset_mock()
|
||||
|
||||
# Load URL with Template and disable body
|
||||
instance = Apprise.instantiate(
|
||||
'onesignal://templateid:appid@apikey/@user/?contents=no')
|
||||
|
||||
# Validate that it loaded okay
|
||||
assert isinstance(instance, NotifyOneSignal)
|
||||
|
||||
response = instance.notify("hello world")
|
||||
assert response is True
|
||||
assert mock_post.call_count == 1
|
||||
assert mock_post.call_args_list[0][0][0] == \
|
||||
'https://api.onesignal.com/notifications'
|
||||
|
||||
details = mock_post.call_args_list[0]
|
||||
payload = loads(details[1]['data'])
|
||||
|
||||
assert payload == {
|
||||
'app_id': 'appid',
|
||||
'content_available': True,
|
||||
'template_id': 'templateid',
|
||||
'large_icon': 'https://github.com/caronc/apprise'
|
||||
'/raw/master/apprise/assets/themes/default/apprise-info-72x72.png',
|
||||
'small_icon': 'https://github.com/caronc/apprise'
|
||||
'/raw/master/apprise/assets/themes/default/apprise-info-32x32.png',
|
||||
'include_external_user_ids': ['@user']}
|
||||
|
||||
# Now set a title
|
||||
mock_post.reset_mock()
|
||||
|
||||
response = instance.notify("hello world", title="mytitle")
|
||||
|
||||
assert response is True
|
||||
assert mock_post.call_count == 1
|
||||
assert mock_post.call_args_list[0][0][0] == \
|
||||
'https://api.onesignal.com/notifications'
|
||||
|
||||
details = mock_post.call_args_list[0]
|
||||
payload = loads(details[1]['data'])
|
||||
|
||||
assert payload == {
|
||||
'app_id': 'appid',
|
||||
'headings': {'en': 'mytitle'},
|
||||
'content_available': True,
|
||||
'template_id': 'templateid',
|
||||
'large_icon': 'https://github.com/caronc/apprise'
|
||||
'/raw/master/apprise/assets/themes/default/apprise-info-72x72.png',
|
||||
'small_icon': 'https://github.com/caronc/apprise'
|
||||
'/raw/master/apprise/assets/themes/default/apprise-info-32x32.png',
|
||||
'include_external_user_ids': ['@user']}
|
||||
|
Reference in New Issue
Block a user