2019-02-02 19:11:51 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2023-10-13 23:08:01 +02:00
|
|
|
# BSD 2-Clause License
|
2019-02-02 19:11:51 +01:00
|
|
|
#
|
2023-02-09 09:54:55 +01:00
|
|
|
# Apprise - Push Notification Library.
|
|
|
|
# Copyright (c) 2023, Chris Caron <lead2gold@gmail.com>
|
2019-02-02 19:11:51 +01:00
|
|
|
#
|
2023-02-09 09:54:55 +01:00
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
2019-02-02 19:11:51 +01:00
|
|
|
#
|
2023-02-09 09:54:55 +01:00
|
|
|
# 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions and the following disclaimer.
|
2019-02-02 19:11:51 +01:00
|
|
|
#
|
2023-02-09 09:54:55 +01:00
|
|
|
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions and the following disclaimer in the documentation
|
|
|
|
# and/or other materials provided with the distribution.
|
2019-02-02 19:11:51 +01:00
|
|
|
#
|
2023-02-09 09:54:55 +01:00
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
2019-02-02 19:11:51 +01:00
|
|
|
|
2022-10-08 02:28:36 +02:00
|
|
|
from unittest import mock
|
2022-09-01 02:05:40 +02:00
|
|
|
|
2019-10-09 18:39:31 +02:00
|
|
|
import pytest
|
2019-02-10 05:36:27 +01:00
|
|
|
import requests
|
2019-02-02 19:11:51 +01:00
|
|
|
from apprise import Apprise
|
2022-10-09 11:28:18 +02:00
|
|
|
from apprise.plugins.NotifySNS import NotifySNS
|
2021-11-25 21:20:22 +01:00
|
|
|
from helpers import AppriseURLTester
|
2019-02-02 19:11:51 +01:00
|
|
|
|
2019-02-25 07:02:29 +01:00
|
|
|
# Disable logging for a cleaner testing output
|
|
|
|
import logging
|
|
|
|
logging.disable(logging.CRITICAL)
|
|
|
|
|
|
|
|
|
2019-02-02 19:11:51 +01:00
|
|
|
TEST_ACCESS_KEY_ID = 'AHIAJGNT76XIMXDBIJYA'
|
|
|
|
TEST_ACCESS_KEY_SECRET = 'bu1dHSdO22pfaaVy/wmNsdljF4C07D3bndi9PQJ9'
|
|
|
|
TEST_REGION = 'us-east-2'
|
|
|
|
|
2021-11-25 21:20:22 +01:00
|
|
|
# Our Testing URLs
|
|
|
|
apprise_url_tests = (
|
|
|
|
('sns://', {
|
|
|
|
'instance': TypeError,
|
|
|
|
}),
|
|
|
|
('sns://:@/', {
|
|
|
|
'instance': TypeError,
|
|
|
|
}),
|
|
|
|
('sns://T1JJ3T3L2', {
|
|
|
|
# Just Token 1 provided
|
|
|
|
'instance': TypeError,
|
|
|
|
}),
|
|
|
|
('sns://T1JJ3TD4JD/TIiajkdnlazk7FQ/', {
|
|
|
|
# Missing a region
|
|
|
|
'instance': TypeError,
|
|
|
|
}),
|
|
|
|
('sns://T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcevi7FQ/us-west-2/12223334444', {
|
|
|
|
# we have a valid URL and one number to text
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifySNS,
|
2021-11-25 21:20:22 +01:00
|
|
|
}),
|
2021-12-06 23:03:30 +01:00
|
|
|
('sns://?access=T1JJ3T3L2&secret=A1BRTD4JD/TIiajkdnlazkcevi7FQ'
|
|
|
|
'®ion=us-west-2&to=12223334444', {
|
|
|
|
# Initialize using get parameters instead
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifySNS,
|
2021-12-06 23:03:30 +01:00
|
|
|
}),
|
2021-11-25 21:20:22 +01:00
|
|
|
('sns://T1JJ3TD4JD/TIiajkdnlazk7FQ/us-west-2/12223334444/12223334445', {
|
|
|
|
# Multi SNS Suppport
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifySNS,
|
2021-11-25 21:20:22 +01:00
|
|
|
|
|
|
|
# Our expected url(privacy=True) startswith() response:
|
|
|
|
'privacy_url': 'sns://T...D/****/us-west-2',
|
|
|
|
}),
|
|
|
|
('sns://T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/us-east-1'
|
|
|
|
'?to=12223334444', {
|
|
|
|
# Missing a topic and/or phone No
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifySNS,
|
2021-11-25 21:20:22 +01:00
|
|
|
}),
|
|
|
|
('sns://T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcevi7FQ/us-west-2/12223334444', {
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifySNS,
|
2021-11-25 21:20:22 +01:00
|
|
|
# throw a bizzare code forcing us to fail to look it up
|
|
|
|
'response': False,
|
|
|
|
'requests_response_code': 999,
|
|
|
|
}),
|
|
|
|
('sns://T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcevi7FQ/us-west-2/15556667777', {
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifySNS,
|
2021-11-25 21:20:22 +01:00
|
|
|
# Throws a series of connection and transfer exceptions when this flag
|
|
|
|
# is set and tests that we gracfully handle them
|
|
|
|
'test_requests_exceptions': True,
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_plugin_sns_urls():
|
|
|
|
"""
|
|
|
|
NotifySNS() Apprise URLs
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Run our general tests
|
|
|
|
AppriseURLTester(tests=apprise_url_tests).run_all()
|
|
|
|
|
2019-02-02 19:11:51 +01:00
|
|
|
|
2021-07-28 16:32:10 +02:00
|
|
|
# We initialize a post object just incase a test fails below
|
|
|
|
# we don't want it sending any notifications upstream
|
|
|
|
@mock.patch('requests.post')
|
2021-11-25 21:20:22 +01:00
|
|
|
def test_plugin_sns_edge_cases(mock_post):
|
2019-02-02 19:11:51 +01:00
|
|
|
"""
|
2021-11-25 21:20:22 +01:00
|
|
|
NotifySNS() Edge Cases
|
2019-02-02 19:11:51 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Initializes the plugin with a valid access, but invalid access key
|
2019-10-09 18:39:31 +02:00
|
|
|
with pytest.raises(TypeError):
|
2019-02-02 19:11:51 +01:00
|
|
|
# No access_key_id specified
|
2022-10-09 11:28:18 +02:00
|
|
|
NotifySNS(
|
2019-02-02 19:11:51 +01:00
|
|
|
access_key_id=None,
|
|
|
|
secret_access_key=TEST_ACCESS_KEY_SECRET,
|
|
|
|
region_name=TEST_REGION,
|
2019-03-27 04:06:20 +01:00
|
|
|
targets='+1800555999',
|
2019-02-02 19:11:51 +01:00
|
|
|
)
|
|
|
|
|
2019-10-09 18:39:31 +02:00
|
|
|
with pytest.raises(TypeError):
|
2019-02-02 19:11:51 +01:00
|
|
|
# No secret_access_key specified
|
2022-10-09 11:28:18 +02:00
|
|
|
NotifySNS(
|
2019-02-02 19:11:51 +01:00
|
|
|
access_key_id=TEST_ACCESS_KEY_ID,
|
|
|
|
secret_access_key=None,
|
|
|
|
region_name=TEST_REGION,
|
2019-03-27 04:06:20 +01:00
|
|
|
targets='+1800555999',
|
2019-02-02 19:11:51 +01:00
|
|
|
)
|
|
|
|
|
2019-10-09 18:39:31 +02:00
|
|
|
with pytest.raises(TypeError):
|
2019-02-02 19:11:51 +01:00
|
|
|
# No region_name specified
|
2022-10-09 11:28:18 +02:00
|
|
|
NotifySNS(
|
2019-02-02 19:11:51 +01:00
|
|
|
access_key_id=TEST_ACCESS_KEY_ID,
|
|
|
|
secret_access_key=TEST_ACCESS_KEY_SECRET,
|
|
|
|
region_name=None,
|
2019-03-27 04:06:20 +01:00
|
|
|
targets='+1800555999',
|
2019-02-02 19:11:51 +01:00
|
|
|
)
|
|
|
|
|
2021-07-28 16:32:10 +02:00
|
|
|
# No recipients
|
2022-10-09 11:28:18 +02:00
|
|
|
obj = NotifySNS(
|
2021-07-28 16:32:10 +02:00
|
|
|
access_key_id=TEST_ACCESS_KEY_ID,
|
|
|
|
secret_access_key=TEST_ACCESS_KEY_SECRET,
|
|
|
|
region_name=TEST_REGION,
|
|
|
|
targets=None,
|
|
|
|
)
|
2019-02-02 19:11:51 +01:00
|
|
|
|
2021-07-28 16:32:10 +02:00
|
|
|
# The object initializes properly but would not be able to send anything
|
|
|
|
assert obj.notify(body='test', title='test') is False
|
2019-02-02 19:11:51 +01:00
|
|
|
|
2021-07-28 16:32:10 +02:00
|
|
|
# The phone number is invalid, and without it, there is nothing
|
|
|
|
# to notify
|
2022-10-09 11:28:18 +02:00
|
|
|
obj = NotifySNS(
|
2021-07-28 16:32:10 +02:00
|
|
|
access_key_id=TEST_ACCESS_KEY_ID,
|
|
|
|
secret_access_key=TEST_ACCESS_KEY_SECRET,
|
|
|
|
region_name=TEST_REGION,
|
|
|
|
targets='+1809',
|
|
|
|
)
|
2019-02-02 19:11:51 +01:00
|
|
|
|
2021-07-28 16:32:10 +02:00
|
|
|
# The object initializes properly but would not be able to send anything
|
|
|
|
assert obj.notify(body='test', title='test') is False
|
|
|
|
|
|
|
|
# The phone number is invalid, and without it, there is nothing
|
|
|
|
# to notify; we
|
2022-10-09 11:28:18 +02:00
|
|
|
obj = NotifySNS(
|
2021-07-28 16:32:10 +02:00
|
|
|
access_key_id=TEST_ACCESS_KEY_ID,
|
|
|
|
secret_access_key=TEST_ACCESS_KEY_SECRET,
|
|
|
|
region_name=TEST_REGION,
|
|
|
|
targets='#(invalid-topic-because-of-the-brackets)',
|
|
|
|
)
|
|
|
|
|
|
|
|
# The object initializes properly but would not be able to send anything
|
|
|
|
assert obj.notify(body='test', title='test') is False
|
2019-02-02 19:11:51 +01:00
|
|
|
|
|
|
|
|
2021-11-25 21:20:22 +01:00
|
|
|
def test_plugin_sns_url_parsing():
|
2019-02-02 19:11:51 +01:00
|
|
|
"""
|
2021-11-25 21:20:22 +01:00
|
|
|
NotifySNS() URL Parsing
|
2019-02-02 19:11:51 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
# No recipients
|
2022-10-09 11:28:18 +02:00
|
|
|
results = NotifySNS.parse_url('sns://%s/%s/%s/' % (
|
2019-02-02 19:11:51 +01:00
|
|
|
TEST_ACCESS_KEY_ID,
|
|
|
|
TEST_ACCESS_KEY_SECRET,
|
|
|
|
TEST_REGION)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Confirm that there were no recipients found
|
2019-10-09 18:39:31 +02:00
|
|
|
assert len(results['targets']) == 0
|
|
|
|
assert 'region_name' in results
|
|
|
|
assert TEST_REGION == results['region_name']
|
|
|
|
assert 'access_key_id' in results
|
|
|
|
assert TEST_ACCESS_KEY_ID == results['access_key_id']
|
|
|
|
assert 'secret_access_key' in results
|
|
|
|
assert TEST_ACCESS_KEY_SECRET == results['secret_access_key']
|
2019-02-02 19:11:51 +01:00
|
|
|
|
|
|
|
# Detect recipients
|
2022-10-09 11:28:18 +02:00
|
|
|
results = NotifySNS.parse_url('sns://%s/%s/%s/%s/%s/' % (
|
2019-02-02 19:11:51 +01:00
|
|
|
TEST_ACCESS_KEY_ID,
|
|
|
|
TEST_ACCESS_KEY_SECRET,
|
|
|
|
# Uppercase Region won't break anything
|
|
|
|
TEST_REGION.upper(),
|
|
|
|
'+18001234567',
|
|
|
|
'MyTopic')
|
|
|
|
)
|
|
|
|
|
|
|
|
# Confirm that our recipients were found
|
2019-10-09 18:39:31 +02:00
|
|
|
assert len(results['targets']) == 2
|
|
|
|
assert '+18001234567' in results['targets']
|
|
|
|
assert 'MyTopic' in results['targets']
|
|
|
|
assert 'region_name' in results
|
|
|
|
assert TEST_REGION == results['region_name']
|
|
|
|
assert 'access_key_id' in results
|
|
|
|
assert TEST_ACCESS_KEY_ID == results['access_key_id']
|
|
|
|
assert 'secret_access_key' in results
|
|
|
|
assert TEST_ACCESS_KEY_SECRET == results['secret_access_key']
|
2019-02-02 19:11:51 +01:00
|
|
|
|
|
|
|
|
2021-11-25 21:20:22 +01:00
|
|
|
def test_plugin_sns_object_parsing():
|
2019-02-02 19:11:51 +01:00
|
|
|
"""
|
2021-11-25 21:20:22 +01:00
|
|
|
NotifySNS() Object Parsing
|
2019-02-02 19:11:51 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Create our object
|
|
|
|
a = Apprise()
|
|
|
|
|
|
|
|
# Now test failing variations of our URL
|
2019-10-09 18:39:31 +02:00
|
|
|
assert a.add('sns://') is False
|
|
|
|
assert a.add('sns://nosecret') is False
|
|
|
|
assert a.add('sns://nosecret/noregion/') is False
|
2019-02-02 19:11:51 +01:00
|
|
|
|
2021-07-28 16:32:10 +02:00
|
|
|
# This is valid but without valid recipients; while it's still a valid URL
|
|
|
|
# it won't do much when the user goes to send a notification
|
|
|
|
assert a.add('sns://norecipient/norecipient/us-west-2') is True
|
|
|
|
assert len(a) == 1
|
2019-02-02 19:11:51 +01:00
|
|
|
|
|
|
|
# Parse a good one
|
2019-10-09 18:39:31 +02:00
|
|
|
assert a.add('sns://oh/yeah/us-west-2/abcdtopic/+12223334444') is True
|
2021-07-28 16:32:10 +02:00
|
|
|
assert len(a) == 2
|
2019-02-02 19:11:51 +01:00
|
|
|
|
2019-10-09 18:39:31 +02:00
|
|
|
assert a.add('sns://oh/yeah/us-west-2/12223334444') is True
|
2021-07-28 16:32:10 +02:00
|
|
|
assert len(a) == 3
|
2019-02-10 05:36:27 +01:00
|
|
|
|
|
|
|
|
2021-11-25 21:20:22 +01:00
|
|
|
def test_plugin_sns_aws_response_handling():
|
2019-02-10 05:36:27 +01:00
|
|
|
"""
|
2021-11-25 21:20:22 +01:00
|
|
|
NotifySNS() AWS Response Handling
|
2019-02-10 05:36:27 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
# Not a string
|
2022-10-09 11:28:18 +02:00
|
|
|
response = NotifySNS.aws_response_to_dict(None)
|
2019-10-09 18:39:31 +02:00
|
|
|
assert response['type'] is None
|
|
|
|
assert response['request_id'] is None
|
2019-02-10 05:36:27 +01:00
|
|
|
|
|
|
|
# Invalid XML
|
2022-10-09 11:28:18 +02:00
|
|
|
response = NotifySNS.aws_response_to_dict(
|
2019-02-10 05:36:27 +01:00
|
|
|
'<Bad Response xmlns="http://sns.amazonaws.com/doc/2010-03-31/">')
|
2019-10-09 18:39:31 +02:00
|
|
|
assert response['type'] is None
|
|
|
|
assert response['request_id'] is None
|
2019-02-10 05:36:27 +01:00
|
|
|
|
|
|
|
# Single Element in XML
|
2022-10-09 11:28:18 +02:00
|
|
|
response = NotifySNS.aws_response_to_dict(
|
2019-02-10 05:36:27 +01:00
|
|
|
'<SingleElement></SingleElement>')
|
2019-10-09 18:39:31 +02:00
|
|
|
assert response['type'] == 'SingleElement'
|
|
|
|
assert response['request_id'] is None
|
2019-02-10 05:36:27 +01:00
|
|
|
|
|
|
|
# Empty String
|
2022-10-09 11:28:18 +02:00
|
|
|
response = NotifySNS.aws_response_to_dict('')
|
2019-10-09 18:39:31 +02:00
|
|
|
assert response['type'] is None
|
|
|
|
assert response['request_id'] is None
|
2019-02-10 05:36:27 +01:00
|
|
|
|
2022-10-09 11:28:18 +02:00
|
|
|
response = NotifySNS.aws_response_to_dict(
|
2019-02-10 05:36:27 +01:00
|
|
|
"""
|
|
|
|
<PublishResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/">
|
|
|
|
<PublishResult>
|
|
|
|
<MessageId>5e16935a-d1fb-5a31-a716-c7805e5c1d2e</MessageId>
|
|
|
|
</PublishResult>
|
|
|
|
<ResponseMetadata>
|
|
|
|
<RequestId>dc258024-d0e6-56bb-af1b-d4fe5f4181a4</RequestId>
|
|
|
|
</ResponseMetadata>
|
|
|
|
</PublishResponse>
|
|
|
|
""")
|
2019-10-09 18:39:31 +02:00
|
|
|
assert response['type'] == 'PublishResponse'
|
|
|
|
assert response['request_id'] == 'dc258024-d0e6-56bb-af1b-d4fe5f4181a4'
|
|
|
|
assert response['message_id'] == '5e16935a-d1fb-5a31-a716-c7805e5c1d2e'
|
2019-02-10 05:36:27 +01:00
|
|
|
|
2022-10-09 11:28:18 +02:00
|
|
|
response = NotifySNS.aws_response_to_dict(
|
2019-02-10 05:36:27 +01:00
|
|
|
"""
|
|
|
|
<CreateTopicResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/">
|
|
|
|
<CreateTopicResult>
|
|
|
|
<TopicArn>arn:aws:sns:us-east-1:000000000000:abcd</TopicArn>
|
|
|
|
</CreateTopicResult>
|
|
|
|
<ResponseMetadata>
|
|
|
|
<RequestId>604bef0f-369c-50c5-a7a4-bbd474c83d6a</RequestId>
|
|
|
|
</ResponseMetadata>
|
|
|
|
</CreateTopicResponse>
|
|
|
|
""")
|
2019-10-09 18:39:31 +02:00
|
|
|
assert response['type'] == 'CreateTopicResponse'
|
|
|
|
assert response['request_id'] == '604bef0f-369c-50c5-a7a4-bbd474c83d6a'
|
|
|
|
assert response['topic_arn'] == 'arn:aws:sns:us-east-1:000000000000:abcd'
|
2019-02-10 05:36:27 +01:00
|
|
|
|
2022-10-09 11:28:18 +02:00
|
|
|
response = NotifySNS.aws_response_to_dict(
|
2019-02-10 05:36:27 +01:00
|
|
|
"""
|
|
|
|
<ErrorResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/">
|
|
|
|
<Error>
|
|
|
|
<Type>Sender</Type>
|
|
|
|
<Code>InvalidParameter</Code>
|
|
|
|
<Message>Invalid parameter: TopicArn or TargetArn Reason:
|
|
|
|
no value for required parameter</Message>
|
|
|
|
</Error>
|
|
|
|
<RequestId>b5614883-babe-56ca-93b2-1c592ba6191e</RequestId>
|
|
|
|
</ErrorResponse>
|
|
|
|
""")
|
2019-10-09 18:39:31 +02:00
|
|
|
assert response['type'] == 'ErrorResponse'
|
|
|
|
assert response['request_id'] == 'b5614883-babe-56ca-93b2-1c592ba6191e'
|
|
|
|
assert response['error_type'] == 'Sender'
|
|
|
|
assert response['error_code'] == 'InvalidParameter'
|
|
|
|
assert response['error_message'].startswith('Invalid parameter:')
|
|
|
|
assert response['error_message'].endswith('required parameter')
|
2019-02-10 05:36:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
@mock.patch('requests.post')
|
2021-11-25 21:20:22 +01:00
|
|
|
def test_plugin_sns_aws_topic_handling(mock_post):
|
2019-02-10 05:36:27 +01:00
|
|
|
"""
|
2021-11-25 21:20:22 +01:00
|
|
|
NotifySNS() AWS Topic Handling
|
2019-02-10 05:36:27 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
arn_response = \
|
|
|
|
"""
|
|
|
|
<CreateTopicResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/">
|
|
|
|
<CreateTopicResult>
|
|
|
|
<TopicArn>arn:aws:sns:us-east-1:000000000000:abcd</TopicArn>
|
|
|
|
</CreateTopicResult>
|
|
|
|
<ResponseMetadata>
|
|
|
|
<RequestId>604bef0f-369c-50c5-a7a4-bbd474c83d6a</RequestId>
|
|
|
|
</ResponseMetadata>
|
|
|
|
</CreateTopicResponse>
|
|
|
|
"""
|
|
|
|
|
|
|
|
def post(url, data, **kwargs):
|
|
|
|
"""
|
|
|
|
Since Publishing a token requires 2 posts, we need to return our
|
|
|
|
response depending on what step we're on
|
|
|
|
"""
|
|
|
|
|
|
|
|
# A request
|
|
|
|
robj = mock.Mock()
|
2020-11-14 19:52:00 +01:00
|
|
|
robj.text = ''
|
2019-02-10 05:36:27 +01:00
|
|
|
robj.status_code = requests.codes.ok
|
|
|
|
|
|
|
|
if data.find('=CreateTopic') >= 0:
|
|
|
|
# Topic Post Failure
|
|
|
|
robj.status_code = requests.codes.bad_request
|
|
|
|
|
|
|
|
return robj
|
|
|
|
|
|
|
|
# Assign ourselves a new function
|
|
|
|
mock_post.side_effect = post
|
|
|
|
|
|
|
|
# Create our object
|
|
|
|
a = Apprise()
|
|
|
|
|
|
|
|
a.add([
|
|
|
|
# Single Topic
|
|
|
|
'sns://T1JJ3T3L2/A1BRTD4JD/TIiajkdnl/us-west-2/TopicA',
|
|
|
|
# Multi-Topic
|
|
|
|
'sns://T1JJ3T3L2/A1BRTD4JD/TIiajkdnl/us-east-1/TopicA/TopicB/'
|
|
|
|
# Topic-Mix
|
|
|
|
'sns://T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkce/us-west-2/' \
|
|
|
|
'12223334444/TopicA'])
|
|
|
|
|
|
|
|
# CreateTopic fails
|
2019-10-09 18:39:31 +02:00
|
|
|
assert a.notify(title='', body='test') is False
|
2019-02-10 05:36:27 +01:00
|
|
|
|
|
|
|
def post(url, data, **kwargs):
|
|
|
|
"""
|
|
|
|
Since Publishing a token requires 2 posts, we need to return our
|
|
|
|
response depending on what step we're on
|
|
|
|
"""
|
|
|
|
|
|
|
|
# A request
|
|
|
|
robj = mock.Mock()
|
2020-11-14 19:52:00 +01:00
|
|
|
robj.text = ''
|
2019-02-10 05:36:27 +01:00
|
|
|
robj.status_code = requests.codes.ok
|
|
|
|
|
|
|
|
if data.find('=CreateTopic') >= 0:
|
2020-11-14 19:52:00 +01:00
|
|
|
robj.text = arn_response
|
2019-02-10 05:36:27 +01:00
|
|
|
|
|
|
|
# Manipulate Topic Publishing only (not phone)
|
|
|
|
elif data.find('=Publish') >= 0 and data.find('TopicArn=') >= 0:
|
|
|
|
# Topic Post Failure
|
|
|
|
robj.status_code = requests.codes.bad_request
|
|
|
|
|
|
|
|
return robj
|
|
|
|
|
|
|
|
# Assign ourselves a new function
|
|
|
|
mock_post.side_effect = post
|
|
|
|
|
|
|
|
# Publish fails
|
2019-10-09 18:39:31 +02:00
|
|
|
assert a.notify(title='', body='test') is False
|
2019-02-10 05:36:27 +01:00
|
|
|
|
|
|
|
# Disable our side effect
|
|
|
|
mock_post.side_effect = None
|
|
|
|
|
|
|
|
# Handle case where TopicArn is missing:
|
|
|
|
robj = mock.Mock()
|
2020-11-14 19:52:00 +01:00
|
|
|
robj.text = "<CreateTopicResponse></CreateTopicResponse>"
|
2019-02-10 05:36:27 +01:00
|
|
|
robj.status_code = requests.codes.ok
|
|
|
|
|
|
|
|
# Assign ourselves a new function
|
|
|
|
mock_post.return_value = robj
|
2019-10-09 18:39:31 +02:00
|
|
|
assert a.notify(title='', body='test') is False
|
2019-02-10 05:36:27 +01:00
|
|
|
|
|
|
|
# Handle case where we fails get a bad response
|
|
|
|
robj = mock.Mock()
|
2020-11-14 19:52:00 +01:00
|
|
|
robj.text = ''
|
2019-02-10 05:36:27 +01:00
|
|
|
robj.status_code = requests.codes.bad_request
|
|
|
|
mock_post.return_value = robj
|
2019-10-09 18:39:31 +02:00
|
|
|
assert a.notify(title='', body='test') is False
|
2019-02-10 05:36:27 +01:00
|
|
|
|
|
|
|
# Handle case where we get a valid response and TopicARN
|
|
|
|
robj = mock.Mock()
|
2020-11-14 19:52:00 +01:00
|
|
|
robj.text = arn_response
|
2019-02-10 05:36:27 +01:00
|
|
|
robj.status_code = requests.codes.ok
|
|
|
|
mock_post.return_value = robj
|
|
|
|
# We would have failed to make Post
|
2019-10-09 18:39:31 +02:00
|
|
|
assert a.notify(title='', body='test') is True
|