2017-12-25 21:07:41 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2023-10-13 23:08:01 +02:00
|
|
|
# BSD 2-Clause License
|
2017-12-25 21:07:41 +01:00
|
|
|
#
|
2023-02-09 09:54:55 +01:00
|
|
|
# Apprise - Push Notification Library.
|
2024-01-27 21:35:11 +01:00
|
|
|
# Copyright (c) 2024, Chris Caron <lead2gold@gmail.com>
|
2017-12-25 21:07:41 +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:
|
2017-12-25 21:07:41 +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.
|
2017-12-25 21:07:41 +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.
|
2017-12-25 21:07:41 +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.
|
2017-12-25 21:07:41 +01:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
import json
|
|
|
|
import logging
|
2022-03-07 23:52:22 +01:00
|
|
|
import os
|
2022-12-02 01:38:55 +01:00
|
|
|
from datetime import datetime
|
2023-06-14 22:39:34 +02:00
|
|
|
from datetime import timezone
|
2022-12-02 01:38:55 +01:00
|
|
|
from unittest.mock import Mock, patch
|
2022-09-01 02:05:40 +02:00
|
|
|
|
2019-10-09 18:39:31 +02:00
|
|
|
import pytest
|
2019-06-30 00:27:59 +02:00
|
|
|
import requests
|
2022-12-02 01:38:55 +01:00
|
|
|
|
2022-03-07 23:52:22 +01:00
|
|
|
from apprise import Apprise
|
|
|
|
from apprise import NotifyType
|
|
|
|
from apprise import AppriseAttachment
|
2024-04-27 19:02:27 +02:00
|
|
|
from apprise.plugins.twitter import NotifyTwitter
|
2021-11-25 21:20:22 +01:00
|
|
|
from helpers import AppriseURLTester
|
2017-12-25 21:07:41 +01:00
|
|
|
|
2019-02-25 07:02:29 +01:00
|
|
|
# Disable logging for a cleaner testing output
|
|
|
|
logging.disable(logging.CRITICAL)
|
|
|
|
|
2022-03-07 23:52:22 +01:00
|
|
|
# Attachment Directory
|
|
|
|
TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), 'var')
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
TWITTER_SCREEN_NAME = 'apprise'
|
|
|
|
|
|
|
|
|
2021-11-25 21:20:22 +01:00
|
|
|
# Our Testing URLs
|
|
|
|
apprise_url_tests = (
|
|
|
|
##################################
|
|
|
|
# NotifyTwitter
|
|
|
|
##################################
|
|
|
|
('twitter://', {
|
|
|
|
# Missing Consumer API Key
|
|
|
|
'instance': TypeError,
|
|
|
|
}),
|
|
|
|
('twitter://:@/', {
|
|
|
|
'instance': TypeError,
|
|
|
|
}),
|
|
|
|
('twitter://consumer_key', {
|
|
|
|
# Missing Keys
|
|
|
|
'instance': TypeError,
|
|
|
|
}),
|
|
|
|
('twitter://consumer_key/consumer_secret/', {
|
|
|
|
# Missing Keys
|
|
|
|
'instance': TypeError,
|
|
|
|
}),
|
2022-11-11 21:46:06 +01:00
|
|
|
('twitter://consumer_key/consumer_secret/atoken1/', {
|
2021-11-25 21:20:22 +01:00
|
|
|
# Missing Access Secret
|
|
|
|
'instance': TypeError,
|
|
|
|
}),
|
2022-11-11 21:46:06 +01:00
|
|
|
('twitter://consumer_key/consumer_secret/atoken2/access_secret', {
|
2021-11-25 21:20:22 +01:00
|
|
|
# No user mean's we message ourselves
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyTwitter,
|
2021-11-25 21:20:22 +01:00
|
|
|
# Expected notify() response False (because we won't be able
|
|
|
|
# to detect our user)
|
|
|
|
'notify_response': False,
|
|
|
|
|
|
|
|
# Our expected url(privacy=True) startswith() response:
|
2023-10-08 14:56:00 +02:00
|
|
|
'privacy_url': 'x://c...y/****/a...2/****',
|
2021-11-25 21:20:22 +01:00
|
|
|
}),
|
2022-11-11 21:46:06 +01:00
|
|
|
('twitter://consumer_key/consumer_secret/atoken3/access_secret'
|
2021-11-25 21:20:22 +01:00
|
|
|
'?cache=no', {
|
|
|
|
# No user mean's we message ourselves
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyTwitter,
|
2021-11-25 21:20:22 +01:00
|
|
|
# However we'll be okay if we return a proper response
|
|
|
|
'requests_response_text': {
|
|
|
|
'id': 12345,
|
2022-03-07 23:52:22 +01:00
|
|
|
'screen_name': 'test',
|
|
|
|
# For attachment handling
|
|
|
|
'media_id': 123,
|
2021-11-25 21:20:22 +01:00
|
|
|
},
|
|
|
|
}),
|
2022-11-11 21:46:06 +01:00
|
|
|
('twitter://consumer_key/consumer_secret/atoken4/access_secret', {
|
2021-11-25 21:20:22 +01:00
|
|
|
# No user mean's we message ourselves
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyTwitter,
|
2021-11-25 21:20:22 +01:00
|
|
|
# However we'll be okay if we return a proper response
|
|
|
|
'requests_response_text': {
|
|
|
|
'id': 12345,
|
2022-03-07 23:52:22 +01:00
|
|
|
'screen_name': 'test',
|
|
|
|
# For attachment handling
|
|
|
|
'media_id': 123,
|
2021-11-25 21:20:22 +01:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
# A duplicate of the entry above, this will cause cache to be referenced
|
2022-11-11 21:46:06 +01:00
|
|
|
('twitter://consumer_key/consumer_secret/atoken5/access_secret', {
|
2021-11-25 21:20:22 +01:00
|
|
|
# No user mean's we message ourselves
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyTwitter,
|
2021-11-25 21:20:22 +01:00
|
|
|
# However we'll be okay if we return a proper response
|
|
|
|
'requests_response_text': {
|
|
|
|
'id': 12345,
|
2022-03-07 23:52:22 +01:00
|
|
|
'screen_name': 'test',
|
|
|
|
# For attachment handling
|
|
|
|
'media_id': 123,
|
2021-11-25 21:20:22 +01:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
# handle cases where the screen_name is missing from the response causing
|
|
|
|
# an exception during parsing
|
2022-11-11 21:46:06 +01:00
|
|
|
('twitter://consumer_key/consumer_secret2/atoken6/access_secret', {
|
2021-11-25 21:20:22 +01:00
|
|
|
# No user mean's we message ourselves
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyTwitter,
|
2021-11-25 21:20:22 +01:00
|
|
|
# However we'll be okay if we return a proper response
|
|
|
|
'requests_response_text': {
|
|
|
|
'id': 12345,
|
2022-03-07 23:52:22 +01:00
|
|
|
# For attachment handling
|
|
|
|
'media_id': 123,
|
2021-11-25 21:20:22 +01:00
|
|
|
},
|
|
|
|
# due to a mangled response_text we'll fail
|
|
|
|
'notify_response': False,
|
|
|
|
}),
|
2022-11-11 21:46:06 +01:00
|
|
|
('twitter://user@consumer_key/csecret2/atoken7/access_secret/-/%/', {
|
2021-11-25 21:20:22 +01:00
|
|
|
# One Invalid User
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyTwitter,
|
2021-11-25 21:20:22 +01:00
|
|
|
# Expected notify() response False (because we won't be able
|
|
|
|
# to detect our user)
|
|
|
|
'notify_response': False,
|
|
|
|
}),
|
2022-11-11 21:46:06 +01:00
|
|
|
('twitter://user@consumer_key/csecret/atoken8/access_secret'
|
2022-03-07 23:52:22 +01:00
|
|
|
'?cache=No&batch=No', {
|
|
|
|
# No Cache & No Batch
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyTwitter,
|
2021-11-25 21:20:22 +01:00
|
|
|
'requests_response_text': [{
|
|
|
|
'id': 12345,
|
|
|
|
'screen_name': 'user'
|
|
|
|
}],
|
|
|
|
}),
|
2022-11-11 21:46:06 +01:00
|
|
|
('twitter://user@consumer_key/csecret/atoken9/access_secret', {
|
2021-11-25 21:20:22 +01:00
|
|
|
# We're good!
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyTwitter,
|
2021-11-25 21:20:22 +01:00
|
|
|
'requests_response_text': [{
|
|
|
|
'id': 12345,
|
|
|
|
'screen_name': 'user'
|
|
|
|
}],
|
|
|
|
}),
|
2022-11-11 21:46:06 +01:00
|
|
|
('twitter://user@consumer_key/csecret/atoken11/access_secret', {
|
2021-11-25 21:20:22 +01:00
|
|
|
# We're identifying the same user we already sent to
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyTwitter,
|
2022-11-11 21:46:06 +01:00
|
|
|
'notify_response': False,
|
2021-11-25 21:20:22 +01:00
|
|
|
}),
|
2022-11-11 21:46:06 +01:00
|
|
|
('tweet://ckey/csecret/atoken12/access_secret', {
|
2021-11-25 21:20:22 +01:00
|
|
|
# A Public Tweet
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyTwitter,
|
2021-11-25 21:20:22 +01:00
|
|
|
}),
|
2022-11-11 21:46:06 +01:00
|
|
|
('twitter://user@ckey/csecret/atoken13/access_secret?mode=invalid', {
|
2021-11-25 21:20:22 +01:00
|
|
|
# An invalid mode
|
|
|
|
'instance': TypeError,
|
|
|
|
}),
|
2022-11-11 21:46:06 +01:00
|
|
|
('twitter://usera@consumer_key/consumer_secret/atoken14/'
|
2021-11-25 21:20:22 +01:00
|
|
|
'access_secret/user/?to=userb', {
|
|
|
|
# We're good!
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyTwitter,
|
2021-11-25 21:20:22 +01:00
|
|
|
'requests_response_text': [{
|
|
|
|
'id': 12345,
|
|
|
|
'screen_name': 'usera'
|
|
|
|
}, {
|
|
|
|
'id': 12346,
|
|
|
|
'screen_name': 'userb'
|
|
|
|
}, {
|
|
|
|
# A garbage entry we can test exception handling on
|
|
|
|
'id': 123,
|
|
|
|
}],
|
|
|
|
}),
|
2022-11-11 21:46:06 +01:00
|
|
|
('twitter://ckey/csecret/atoken15/access_secret', {
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyTwitter,
|
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,
|
|
|
|
}),
|
2022-11-11 21:46:06 +01:00
|
|
|
('twitter://ckey/csecret/atoken16/access_secret', {
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyTwitter,
|
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,
|
|
|
|
}),
|
2022-11-11 21:46:06 +01:00
|
|
|
('twitter://ckey/csecret/atoken17/access_secret?mode=tweet', {
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyTwitter,
|
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,
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
def good_response(data):
|
|
|
|
"""
|
|
|
|
Prepare a good response.
|
|
|
|
"""
|
|
|
|
response = Mock()
|
|
|
|
response.content = json.dumps(data)
|
|
|
|
response.status_code = requests.codes.ok
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
def bad_response(data):
|
|
|
|
"""
|
|
|
|
Prepare a bad response.
|
|
|
|
"""
|
|
|
|
response = Mock()
|
|
|
|
response.content = json.dumps(data)
|
|
|
|
response.status_code = requests.codes.internal_server_error
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def twitter_url():
|
|
|
|
ckey = 'ckey'
|
|
|
|
csecret = 'csecret'
|
|
|
|
akey = 'akey'
|
|
|
|
asecret = 'asecret'
|
|
|
|
url = 'twitter://{}/{}/{}/{}'.format(ckey, csecret, akey, asecret)
|
|
|
|
return url
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def good_message_response():
|
|
|
|
"""
|
|
|
|
Prepare a good tweet response.
|
|
|
|
"""
|
|
|
|
response = good_response({
|
|
|
|
'screen_name': TWITTER_SCREEN_NAME,
|
|
|
|
'id': 9876,
|
|
|
|
})
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def bad_message_response():
|
|
|
|
"""
|
|
|
|
Prepare a bad message response.
|
|
|
|
"""
|
|
|
|
response = bad_response({
|
|
|
|
"errors": [
|
|
|
|
{
|
|
|
|
"code": 999,
|
|
|
|
"message": "Something failed",
|
|
|
|
}]})
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def good_media_response():
|
|
|
|
"""
|
|
|
|
Prepare a good media response.
|
|
|
|
"""
|
|
|
|
response = Mock()
|
|
|
|
response.content = json.dumps({
|
|
|
|
"media_id": 710511363345354753,
|
|
|
|
"media_id_string": "710511363345354753",
|
|
|
|
"media_key": "3_710511363345354753",
|
|
|
|
"size": 11065,
|
|
|
|
"expires_after_secs": 86400,
|
|
|
|
"image": {
|
|
|
|
"image_type": "image/jpeg",
|
|
|
|
"w": 800,
|
|
|
|
"h": 320
|
|
|
|
}
|
|
|
|
})
|
|
|
|
response.status_code = requests.codes.ok
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def bad_media_response():
|
|
|
|
"""
|
|
|
|
Prepare a bad media response.
|
|
|
|
"""
|
|
|
|
response = bad_response({
|
|
|
|
"errors": [
|
|
|
|
{
|
|
|
|
"code": 93,
|
|
|
|
"message": "This application is not allowed to access or "
|
|
|
|
"delete your direct messages.",
|
|
|
|
}]})
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def ensure_get_verify_credentials_is_mocked(mocker, good_message_response):
|
|
|
|
"""
|
|
|
|
Make sure requests to https://api.twitter.com/1.1/account/verify_credentials.json
|
|
|
|
do not escape the test harness, for all test case functions.
|
|
|
|
""" # noqa:E501
|
|
|
|
mock_get = mocker.patch("requests.get")
|
|
|
|
mock_get.return_value = good_message_response
|
|
|
|
|
|
|
|
|
2021-11-25 21:20:22 +01:00
|
|
|
def test_plugin_twitter_urls():
|
2017-12-25 21:07:41 +01:00
|
|
|
"""
|
2021-11-25 21:20:22 +01:00
|
|
|
NotifyTwitter() Apprise URLs
|
2017-12-25 21:07:41 +01:00
|
|
|
"""
|
|
|
|
|
2021-11-25 21:20:22 +01:00
|
|
|
# Run our general tests
|
|
|
|
AppriseURLTester(tests=apprise_url_tests).run_all()
|
2017-12-25 21:07:41 +01:00
|
|
|
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
def test_plugin_twitter_general(mocker):
|
2019-06-30 00:27:59 +02:00
|
|
|
"""
|
2021-11-25 21:20:22 +01:00
|
|
|
NotifyTwitter() General Tests
|
2019-06-30 00:27:59 +02:00
|
|
|
"""
|
2022-12-02 01:38:55 +01:00
|
|
|
|
|
|
|
mock_get = mocker.patch("requests.get")
|
|
|
|
mock_post = mocker.patch("requests.post")
|
|
|
|
|
2019-06-30 00:27:59 +02:00
|
|
|
ckey = 'ckey'
|
|
|
|
csecret = 'csecret'
|
|
|
|
akey = 'akey'
|
|
|
|
asecret = 'asecret'
|
|
|
|
|
|
|
|
response_obj = [{
|
2022-12-02 01:38:55 +01:00
|
|
|
'screen_name': TWITTER_SCREEN_NAME,
|
2019-06-30 00:27:59 +02:00
|
|
|
'id': 9876,
|
|
|
|
}]
|
|
|
|
|
|
|
|
# Epoch time:
|
2023-06-14 22:39:34 +02:00
|
|
|
epoch = datetime.fromtimestamp(0, timezone.utc)
|
2019-06-30 00:27:59 +02:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
request = Mock()
|
|
|
|
request.content = json.dumps(response_obj)
|
2019-06-30 00:27:59 +02:00
|
|
|
request.status_code = requests.codes.ok
|
|
|
|
request.headers = {
|
2023-06-14 22:39:34 +02:00
|
|
|
'x-rate-limit-reset': (
|
|
|
|
datetime.now(timezone.utc) - epoch).total_seconds(),
|
2019-06-30 00:27:59 +02:00
|
|
|
'x-rate-limit-remaining': 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
# Prepare Mock
|
|
|
|
mock_get.return_value = request
|
|
|
|
mock_post.return_value = request
|
|
|
|
|
|
|
|
# Variation Initializations
|
2022-10-09 11:28:18 +02:00
|
|
|
obj = NotifyTwitter(
|
2019-06-30 00:27:59 +02:00
|
|
|
ckey=ckey,
|
|
|
|
csecret=csecret,
|
|
|
|
akey=akey,
|
|
|
|
asecret=asecret,
|
2022-12-02 01:38:55 +01:00
|
|
|
targets=TWITTER_SCREEN_NAME)
|
2019-06-30 00:27:59 +02:00
|
|
|
|
2022-10-09 11:28:18 +02:00
|
|
|
assert isinstance(obj, NotifyTwitter) is True
|
2022-10-08 02:28:36 +02:00
|
|
|
assert isinstance(obj.url(), str) is True
|
2019-06-30 00:27:59 +02:00
|
|
|
|
|
|
|
# apprise room was found
|
|
|
|
assert obj.send(body="test") is True
|
|
|
|
|
|
|
|
# Change our status code and try again
|
|
|
|
request.status_code = 403
|
|
|
|
assert obj.send(body="test") is False
|
|
|
|
assert obj.ratelimit_remaining == 1
|
|
|
|
|
|
|
|
# Return the status
|
|
|
|
request.status_code = requests.codes.ok
|
|
|
|
# Force a reset
|
|
|
|
request.headers['x-rate-limit-remaining'] = 0
|
|
|
|
# behind the scenes, it should cause us to update our rate limit
|
|
|
|
assert obj.send(body="test") is True
|
|
|
|
assert obj.ratelimit_remaining == 0
|
|
|
|
|
|
|
|
# This should cause us to block
|
|
|
|
request.headers['x-rate-limit-remaining'] = 10
|
|
|
|
assert obj.send(body="test") is True
|
|
|
|
assert obj.ratelimit_remaining == 10
|
|
|
|
|
|
|
|
# Handle cases where we simply couldn't get this field
|
|
|
|
del request.headers['x-rate-limit-remaining']
|
|
|
|
assert obj.send(body="test") is True
|
|
|
|
# It remains set to the last value
|
|
|
|
assert obj.ratelimit_remaining == 10
|
|
|
|
|
|
|
|
# Reset our variable back to 1
|
|
|
|
request.headers['x-rate-limit-remaining'] = 1
|
|
|
|
|
|
|
|
# Handle cases where our epoch time is wrong
|
|
|
|
del request.headers['x-rate-limit-reset']
|
|
|
|
assert obj.send(body="test") is True
|
|
|
|
|
|
|
|
# Return our object, but place it in the future forcing us to block
|
|
|
|
request.headers['x-rate-limit-reset'] = \
|
2023-06-14 22:39:34 +02:00
|
|
|
(datetime.now(timezone.utc) - epoch).total_seconds() + 1
|
2019-06-30 00:27:59 +02:00
|
|
|
request.headers['x-rate-limit-remaining'] = 0
|
|
|
|
obj.ratelimit_remaining = 0
|
|
|
|
assert obj.send(body="test") is True
|
|
|
|
|
|
|
|
# Return our object, but place it in the future forcing us to block
|
|
|
|
request.headers['x-rate-limit-reset'] = \
|
2023-06-14 22:39:34 +02:00
|
|
|
(datetime.now(timezone.utc) - epoch).total_seconds() - 1
|
2019-06-30 00:27:59 +02:00
|
|
|
request.headers['x-rate-limit-remaining'] = 0
|
|
|
|
obj.ratelimit_remaining = 0
|
|
|
|
assert obj.send(body="test") is True
|
|
|
|
|
|
|
|
# Return our limits to always work
|
|
|
|
request.headers['x-rate-limit-reset'] = \
|
2023-06-14 22:39:34 +02:00
|
|
|
(datetime.now(timezone.utc) - epoch).total_seconds()
|
2019-06-30 00:27:59 +02:00
|
|
|
request.headers['x-rate-limit-remaining'] = 1
|
|
|
|
obj.ratelimit_remaining = 1
|
|
|
|
|
|
|
|
# Alter pending targets
|
|
|
|
obj.targets.append('usera')
|
2022-12-02 01:38:55 +01:00
|
|
|
request.content = json.dumps(response_obj)
|
2019-06-30 00:27:59 +02:00
|
|
|
response_obj = [{
|
|
|
|
'screen_name': 'usera',
|
|
|
|
'id': 1234,
|
|
|
|
}]
|
|
|
|
|
|
|
|
assert obj.send(body="test") is True
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# Flush our cache forcing it is re-creating
|
2022-11-11 21:46:06 +01:00
|
|
|
NotifyTwitter._user_cache = {}
|
2019-06-30 00:27:59 +02:00
|
|
|
assert obj.send(body="test") is True
|
|
|
|
|
|
|
|
# Cause content response to be None
|
|
|
|
request.content = None
|
|
|
|
assert obj.send(body="test") is True
|
|
|
|
|
|
|
|
# Invalid JSON
|
|
|
|
request.content = '{'
|
|
|
|
assert obj.send(body="test") is True
|
|
|
|
|
|
|
|
# Return it to a parseable string
|
|
|
|
request.content = '{}'
|
|
|
|
|
2022-10-09 11:28:18 +02:00
|
|
|
results = NotifyTwitter.parse_url(
|
2019-06-30 00:27:59 +02:00
|
|
|
'twitter://{}/{}/{}/{}?to={}'.format(
|
2022-12-02 01:38:55 +01:00
|
|
|
ckey, csecret, akey, asecret, TWITTER_SCREEN_NAME))
|
2019-06-30 00:27:59 +02:00
|
|
|
assert isinstance(results, dict) is True
|
2022-12-02 01:38:55 +01:00
|
|
|
assert TWITTER_SCREEN_NAME in results['targets']
|
2019-06-30 00:27:59 +02:00
|
|
|
|
|
|
|
# cause a json parsing issue now
|
|
|
|
response_obj = None
|
|
|
|
assert obj.send(body="test") is True
|
|
|
|
|
|
|
|
response_obj = '{'
|
|
|
|
assert obj.send(body="test") is True
|
|
|
|
|
|
|
|
# Set ourselves up to handle whoami calls
|
|
|
|
|
|
|
|
# Flush out our cache
|
2022-11-11 21:46:06 +01:00
|
|
|
NotifyTwitter._user_cache = {}
|
2019-06-30 00:27:59 +02:00
|
|
|
|
|
|
|
response_obj = {
|
2022-12-02 01:38:55 +01:00
|
|
|
'screen_name': TWITTER_SCREEN_NAME,
|
2019-06-30 00:27:59 +02:00
|
|
|
'id': 9876,
|
|
|
|
}
|
2022-12-02 01:38:55 +01:00
|
|
|
request.content = json.dumps(response_obj)
|
2019-06-30 00:27:59 +02:00
|
|
|
|
2022-10-09 11:28:18 +02:00
|
|
|
obj = NotifyTwitter(
|
2019-06-30 00:27:59 +02:00
|
|
|
ckey=ckey,
|
|
|
|
csecret=csecret,
|
|
|
|
akey=akey,
|
|
|
|
asecret=asecret)
|
|
|
|
|
|
|
|
assert obj.send(body="test") is True
|
|
|
|
|
|
|
|
# Alter the key forcing us to look up a new value of ourselves again
|
2022-11-11 21:46:06 +01:00
|
|
|
NotifyTwitter._user_cache = {}
|
|
|
|
NotifyTwitter._whoami_cache = None
|
2019-06-30 00:27:59 +02:00
|
|
|
obj.ckey = 'different.then.it.was'
|
|
|
|
assert obj.send(body="test") is True
|
|
|
|
|
2022-11-11 21:46:06 +01:00
|
|
|
NotifyTwitter._whoami_cache = None
|
2019-06-30 00:27:59 +02:00
|
|
|
obj.ckey = 'different.again'
|
|
|
|
assert obj.send(body="test") is True
|
2021-11-25 21:20:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_plugin_twitter_edge_cases():
|
|
|
|
"""
|
|
|
|
NotifyTwitter() Edge Cases
|
|
|
|
"""
|
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
2022-10-09 11:28:18 +02:00
|
|
|
NotifyTwitter(
|
2021-11-25 21:20:22 +01:00
|
|
|
ckey=None, csecret=None, akey=None, asecret=None)
|
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
2022-10-09 11:28:18 +02:00
|
|
|
NotifyTwitter(
|
2021-11-25 21:20:22 +01:00
|
|
|
ckey='value', csecret=None, akey=None, asecret=None)
|
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
2022-10-09 11:28:18 +02:00
|
|
|
NotifyTwitter(
|
2021-11-25 21:20:22 +01:00
|
|
|
ckey='value', csecret='value', akey=None, asecret=None)
|
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
2022-10-09 11:28:18 +02:00
|
|
|
NotifyTwitter(
|
2021-11-25 21:20:22 +01:00
|
|
|
ckey='value', csecret='value', akey='value', asecret=None)
|
|
|
|
|
|
|
|
assert isinstance(
|
2022-10-09 11:28:18 +02:00
|
|
|
NotifyTwitter(
|
2021-11-25 21:20:22 +01:00
|
|
|
ckey='value', csecret='value', akey='value', asecret='value'),
|
2022-10-09 11:28:18 +02:00
|
|
|
NotifyTwitter,
|
2021-11-25 21:20:22 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
assert isinstance(
|
2022-10-09 11:28:18 +02:00
|
|
|
NotifyTwitter(
|
2021-11-25 21:20:22 +01:00
|
|
|
ckey='value', csecret='value', akey='value', asecret='value',
|
|
|
|
user='l2gnux'),
|
2022-10-09 11:28:18 +02:00
|
|
|
NotifyTwitter,
|
2021-11-25 21:20:22 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# Invalid Target User
|
|
|
|
with pytest.raises(TypeError):
|
2022-10-09 11:28:18 +02:00
|
|
|
NotifyTwitter(
|
2021-11-25 21:20:22 +01:00
|
|
|
ckey='value', csecret='value', akey='value', asecret='value',
|
|
|
|
targets='%G@rB@g3')
|
2022-03-07 23:52:22 +01:00
|
|
|
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
def test_plugin_twitter_dm_caching(
|
|
|
|
mocker, twitter_url,
|
|
|
|
good_message_response, good_media_response):
|
2022-03-07 23:52:22 +01:00
|
|
|
"""
|
2022-12-02 01:38:55 +01:00
|
|
|
Verify that the `NotifyTwitter.{_user_cache,_whoami_cache}` caches
|
|
|
|
work as intended.
|
2022-03-07 23:52:22 +01:00
|
|
|
"""
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# This is the request to `account/verify_credentials.json`.
|
|
|
|
# Explicitly mock it here so the calls to it can be evaluated.
|
|
|
|
mock_get = mocker.patch("requests.get")
|
|
|
|
mock_get.return_value = good_message_response
|
2022-03-07 23:52:22 +01:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# This test case submits two notifications, so make sure to provide two
|
|
|
|
# mocked responses.
|
|
|
|
mock_post = mocker.patch("requests.post")
|
|
|
|
mock_post.side_effect = [good_message_response, good_message_response]
|
2022-11-11 21:46:06 +01:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# Make sure to start with empty caches.
|
|
|
|
if hasattr(NotifyTwitter, "_user_cache"):
|
|
|
|
NotifyTwitter._user_cache = {}
|
|
|
|
if hasattr(NotifyTwitter, "_whoami_cache"):
|
|
|
|
NotifyTwitter._whoami_cache = {}
|
2022-03-07 23:52:22 +01:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# Create application objects.
|
|
|
|
obj = Apprise.instantiate(twitter_url)
|
2022-03-07 23:52:22 +01:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# Send the first notification.
|
|
|
|
assert obj.notify(
|
|
|
|
body='body', title='title', notify_type=NotifyType.INFO) is True
|
2022-03-07 23:52:22 +01:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# Test call counts.
|
|
|
|
assert mock_get.call_count == 1
|
|
|
|
assert mock_get.call_args_list[0][0][0] == \
|
|
|
|
'https://api.twitter.com/1.1/account/verify_credentials.json'
|
|
|
|
|
|
|
|
assert mock_post.call_count == 1
|
|
|
|
assert mock_post.call_args_list[0][0][0] == \
|
|
|
|
'https://api.twitter.com/1.1/direct_messages/events/new.json'
|
|
|
|
|
|
|
|
# Reset the mocks to start counting calls from scratch.
|
|
|
|
mock_get.reset_mock()
|
|
|
|
mock_post.reset_mock()
|
2022-03-07 23:52:22 +01:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# Send another notification.
|
|
|
|
assert obj.notify(
|
|
|
|
body='body', title='title', notify_type=NotifyType.INFO) is True
|
2022-03-07 23:52:22 +01:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# Calls to `verify_credentials.json` will get cached by `NotifyTwitter`.
|
|
|
|
# So, the `GET` request to `verify_credentials.json` should only have been
|
|
|
|
# issued once.
|
|
|
|
assert mock_get.call_count == 0
|
|
|
|
assert mock_post.call_count == 1
|
2022-03-07 23:52:22 +01:00
|
|
|
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
def test_plugin_twitter_dm_attachments_basic(
|
|
|
|
mocker, twitter_url,
|
|
|
|
good_message_response, good_media_response):
|
|
|
|
"""
|
|
|
|
NotifyTwitter() DM Attachment Checks - Basic
|
|
|
|
"""
|
|
|
|
|
|
|
|
mock_get = mocker.patch("requests.get")
|
|
|
|
mock_post = mocker.patch("requests.post")
|
|
|
|
|
|
|
|
# Epoch time:
|
2023-06-14 22:39:34 +02:00
|
|
|
epoch = datetime.fromtimestamp(0, timezone.utc)
|
2022-12-02 01:38:55 +01:00
|
|
|
mock_get.return_value = good_message_response
|
|
|
|
mock_post.return_value.headers = {
|
2023-06-14 22:39:34 +02:00
|
|
|
'x-rate-limit-reset': (
|
|
|
|
datetime.now(timezone.utc) - epoch).total_seconds(),
|
2022-12-02 01:38:55 +01:00
|
|
|
'x-rate-limit-remaining': 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
# The first response is for uploading the attachment,
|
|
|
|
# the second one for posting the actual message.
|
|
|
|
mock_post.side_effect = [good_media_response, good_message_response]
|
|
|
|
|
|
|
|
# Create application objects.
|
2022-03-07 23:52:22 +01:00
|
|
|
obj = Apprise.instantiate(twitter_url)
|
2022-12-02 01:38:55 +01:00
|
|
|
attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, 'apprise-test.gif'))
|
2022-03-07 23:52:22 +01:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# Send our notification.
|
2022-03-07 23:52:22 +01:00
|
|
|
assert obj.notify(
|
|
|
|
body='body', title='title', notify_type=NotifyType.INFO,
|
|
|
|
attach=attach) is True
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# Test call counts.
|
2022-03-07 23:52:22 +01:00
|
|
|
assert mock_get.call_count == 1
|
|
|
|
assert mock_get.call_args_list[0][0][0] == \
|
|
|
|
'https://api.twitter.com/1.1/account/verify_credentials.json'
|
2022-12-02 01:38:55 +01:00
|
|
|
|
2022-03-07 23:52:22 +01:00
|
|
|
assert mock_post.call_count == 2
|
|
|
|
assert mock_post.call_args_list[0][0][0] == \
|
|
|
|
'https://upload.twitter.com/1.1/media/upload.json'
|
|
|
|
assert mock_post.call_args_list[1][0][0] == \
|
|
|
|
'https://api.twitter.com/1.1/direct_messages/events/new.json'
|
|
|
|
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
def test_plugin_twitter_dm_attachments_message_fails(
|
|
|
|
mocker, twitter_url,
|
|
|
|
good_media_response, bad_message_response):
|
|
|
|
"""
|
|
|
|
Test case with a bad media response.
|
|
|
|
"""
|
2022-03-07 23:52:22 +01:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
mock_post = mocker.patch("requests.post")
|
|
|
|
mock_post.side_effect = [good_media_response, bad_message_response]
|
|
|
|
|
|
|
|
# Create application objects.
|
2022-03-07 23:52:22 +01:00
|
|
|
obj = Apprise.instantiate(twitter_url)
|
2022-12-02 01:38:55 +01:00
|
|
|
attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, 'apprise-test.gif'))
|
2022-03-07 23:52:22 +01:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# Send our notification; it will fail because of the message response.
|
2022-03-07 23:52:22 +01:00
|
|
|
assert obj.notify(
|
|
|
|
body='body', title='title', notify_type=NotifyType.INFO,
|
|
|
|
attach=attach) is False
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
assert mock_post.call_count == 2
|
2022-03-07 23:52:22 +01:00
|
|
|
assert mock_post.call_args_list[0][0][0] == \
|
|
|
|
'https://upload.twitter.com/1.1/media/upload.json'
|
2022-12-02 01:38:55 +01:00
|
|
|
assert mock_post.call_args_list[1][0][0] == \
|
|
|
|
'https://api.twitter.com/1.1/direct_messages/events/new.json'
|
2022-03-07 23:52:22 +01:00
|
|
|
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
def test_plugin_twitter_dm_attachments_upload_fails(
|
|
|
|
mocker, twitter_url,
|
|
|
|
good_message_response, bad_media_response):
|
|
|
|
"""
|
|
|
|
Test case where upload fails.
|
|
|
|
"""
|
2022-03-07 23:52:22 +01:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
mock_post = mocker.patch("requests.post")
|
|
|
|
mock_post.side_effect = [bad_media_response, good_message_response]
|
|
|
|
|
|
|
|
# Create application objects.
|
2022-03-07 23:52:22 +01:00
|
|
|
obj = Apprise.instantiate(twitter_url)
|
2022-12-02 01:38:55 +01:00
|
|
|
attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, 'apprise-test.gif'))
|
2022-03-07 23:52:22 +01:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# Send our notification; it will fail because of the media response.
|
2022-03-07 23:52:22 +01:00
|
|
|
assert obj.notify(
|
|
|
|
body='body', title='title', notify_type=NotifyType.INFO,
|
|
|
|
attach=attach) is False
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# Test call counts.
|
|
|
|
assert mock_post.call_count == 1
|
2022-03-07 23:52:22 +01:00
|
|
|
assert mock_post.call_args_list[0][0][0] == \
|
|
|
|
'https://upload.twitter.com/1.1/media/upload.json'
|
|
|
|
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
def test_plugin_twitter_dm_attachments_invalid_attachment(
|
|
|
|
mocker, twitter_url, good_message_response):
|
|
|
|
"""
|
|
|
|
Test case with an invalid attachment.
|
|
|
|
"""
|
|
|
|
|
|
|
|
mock_post: Mock = mocker.patch("requests.post")
|
|
|
|
mock_post.side_effect = [good_media_response, good_message_response]
|
|
|
|
|
|
|
|
# Create application objects.
|
|
|
|
# An invalid attachment will cause a failure.
|
|
|
|
obj = Apprise.instantiate(twitter_url)
|
|
|
|
attach = AppriseAttachment(
|
|
|
|
os.path.join(TEST_VAR_DIR, '/invalid/path/to/an/invalid/file.jpg'))
|
2022-03-07 23:52:22 +01:00
|
|
|
|
|
|
|
assert obj.notify(
|
|
|
|
body='body', title='title', notify_type=NotifyType.INFO,
|
2022-12-02 01:38:55 +01:00
|
|
|
attach=attach) is False
|
2022-03-07 23:52:22 +01:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# Verify no post requests have been issued, because attachment is not good.
|
|
|
|
assert mock_post.mock_calls == []
|
2022-03-07 23:52:22 +01:00
|
|
|
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
def test_plugin_twitter_dm_attachments_multiple(
|
|
|
|
mocker, twitter_url,
|
|
|
|
good_message_response, good_media_response):
|
|
|
|
|
|
|
|
mock_post = mocker.patch("requests.post")
|
2022-03-07 23:52:22 +01:00
|
|
|
|
|
|
|
mock_post.side_effect = [
|
|
|
|
good_media_response, good_media_response, good_media_response,
|
2022-12-02 01:38:55 +01:00
|
|
|
good_media_response, good_message_response, good_message_response,
|
|
|
|
good_message_response, good_message_response]
|
2022-03-07 23:52:22 +01:00
|
|
|
|
|
|
|
# 4 images are produced
|
|
|
|
attach = [
|
|
|
|
os.path.join(TEST_VAR_DIR, 'apprise-test.gif'),
|
|
|
|
os.path.join(TEST_VAR_DIR, 'apprise-test.gif'),
|
|
|
|
os.path.join(TEST_VAR_DIR, 'apprise-test.jpeg'),
|
|
|
|
os.path.join(TEST_VAR_DIR, 'apprise-test.png'),
|
|
|
|
# This one is not supported, so it's ignored gracefully
|
|
|
|
os.path.join(TEST_VAR_DIR, 'apprise-test.mp4'),
|
|
|
|
]
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# Create application objects.
|
|
|
|
obj = Apprise.instantiate(twitter_url)
|
|
|
|
|
2022-03-07 23:52:22 +01:00
|
|
|
assert obj.notify(
|
|
|
|
body='body', title='title', notify_type=NotifyType.INFO,
|
|
|
|
attach=attach) is True
|
|
|
|
|
|
|
|
assert mock_post.call_count == 8
|
|
|
|
assert mock_post.call_args_list[0][0][0] == \
|
|
|
|
'https://upload.twitter.com/1.1/media/upload.json'
|
|
|
|
assert mock_post.call_args_list[1][0][0] == \
|
|
|
|
'https://upload.twitter.com/1.1/media/upload.json'
|
|
|
|
assert mock_post.call_args_list[2][0][0] == \
|
|
|
|
'https://upload.twitter.com/1.1/media/upload.json'
|
|
|
|
assert mock_post.call_args_list[3][0][0] == \
|
|
|
|
'https://upload.twitter.com/1.1/media/upload.json'
|
|
|
|
assert mock_post.call_args_list[4][0][0] == \
|
|
|
|
'https://api.twitter.com/1.1/direct_messages/events/new.json'
|
|
|
|
assert mock_post.call_args_list[5][0][0] == \
|
|
|
|
'https://api.twitter.com/1.1/direct_messages/events/new.json'
|
|
|
|
assert mock_post.call_args_list[6][0][0] == \
|
|
|
|
'https://api.twitter.com/1.1/direct_messages/events/new.json'
|
|
|
|
assert mock_post.call_args_list[7][0][0] == \
|
|
|
|
'https://api.twitter.com/1.1/direct_messages/events/new.json'
|
|
|
|
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
def test_plugin_twitter_dm_attachments_multiple_oserror(
|
|
|
|
mocker, twitter_url,
|
|
|
|
good_message_response, good_media_response):
|
|
|
|
|
|
|
|
# Inject an `OSError` into the middle of the operation.
|
|
|
|
mock_post = mocker.patch("requests.post")
|
2022-03-07 23:52:22 +01:00
|
|
|
mock_post.side_effect = [good_media_response, OSError()]
|
|
|
|
|
|
|
|
# 2 images are produced
|
|
|
|
attach = [
|
|
|
|
os.path.join(TEST_VAR_DIR, 'apprise-test.gif'),
|
|
|
|
os.path.join(TEST_VAR_DIR, 'apprise-test.png'),
|
|
|
|
# This one is not supported, so it's ignored gracefully
|
|
|
|
os.path.join(TEST_VAR_DIR, 'apprise-test.mp4'),
|
|
|
|
]
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# Create application objects.
|
|
|
|
obj = Apprise.instantiate(twitter_url)
|
|
|
|
|
2022-03-07 23:52:22 +01:00
|
|
|
# We'll fail to send this time
|
|
|
|
assert obj.notify(
|
|
|
|
body='body', title='title', notify_type=NotifyType.INFO,
|
|
|
|
attach=attach) is False
|
|
|
|
|
|
|
|
assert mock_post.call_count == 2
|
|
|
|
assert mock_post.call_args_list[0][0][0] == \
|
|
|
|
'https://upload.twitter.com/1.1/media/upload.json'
|
|
|
|
assert mock_post.call_args_list[1][0][0] == \
|
|
|
|
'https://upload.twitter.com/1.1/media/upload.json'
|
|
|
|
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
@patch('requests.post')
|
|
|
|
def test_plugin_twitter_tweet_attachments_basic(
|
|
|
|
mock_post, twitter_url, good_message_response, good_media_response):
|
2022-03-07 23:52:22 +01:00
|
|
|
"""
|
2022-12-02 01:38:55 +01:00
|
|
|
NotifyTwitter() Tweet Attachment Checks - Basic
|
2022-03-07 23:52:22 +01:00
|
|
|
"""
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
mock_post.side_effect = [good_media_response, good_message_response]
|
2022-03-07 23:52:22 +01:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# Create application objects.
|
|
|
|
twitter_url += '?mode=tweet'
|
2022-03-07 23:52:22 +01:00
|
|
|
obj = Apprise.instantiate(twitter_url)
|
2022-12-02 01:38:55 +01:00
|
|
|
attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, 'apprise-test.gif'))
|
2022-03-07 23:52:22 +01:00
|
|
|
|
|
|
|
# Send our notification
|
|
|
|
assert obj.notify(
|
|
|
|
body='body', title='title', notify_type=NotifyType.INFO,
|
|
|
|
attach=attach) is True
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# Verify API calls.
|
2022-03-07 23:52:22 +01:00
|
|
|
assert mock_post.call_count == 2
|
|
|
|
assert mock_post.call_args_list[0][0][0] == \
|
|
|
|
'https://upload.twitter.com/1.1/media/upload.json'
|
|
|
|
assert mock_post.call_args_list[1][0][0] == \
|
|
|
|
'https://api.twitter.com/1.1/statuses/update.json'
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
|
|
|
|
@patch('requests.post')
|
|
|
|
def test_plugin_twitter_tweet_attachments_more_logging(
|
|
|
|
mock_post, twitter_url, good_media_response):
|
|
|
|
"""
|
|
|
|
NotifyTwitter() Tweet Attachment Checks - More logging
|
|
|
|
|
|
|
|
TODO: The "more logging" aspect is not verified yet?
|
|
|
|
"""
|
|
|
|
|
|
|
|
good_tweet_response = good_response({
|
|
|
|
'screen_name': TWITTER_SCREEN_NAME,
|
2022-03-11 02:59:40 +01:00
|
|
|
'id': 9876,
|
|
|
|
# needed for additional logging
|
|
|
|
'id_str': '12345',
|
|
|
|
'user': {
|
2022-12-02 01:38:55 +01:00
|
|
|
'screen_name': TWITTER_SCREEN_NAME,
|
2022-03-11 02:59:40 +01:00
|
|
|
}
|
2022-12-02 01:38:55 +01:00
|
|
|
})
|
2022-03-11 02:59:40 +01:00
|
|
|
|
|
|
|
mock_post.side_effect = [good_media_response, good_tweet_response]
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# Create application objects.
|
|
|
|
twitter_url += '?mode=tweet'
|
2022-03-11 02:59:40 +01:00
|
|
|
obj = Apprise.instantiate(twitter_url)
|
2022-12-02 01:38:55 +01:00
|
|
|
attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, 'apprise-test.gif'))
|
2022-03-11 02:59:40 +01:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# Send our notification (again); this time there will be more logging
|
|
|
|
# TODO: The "more logging" aspect is not verified yet?
|
2022-03-11 02:59:40 +01:00
|
|
|
assert obj.notify(
|
|
|
|
body='body', title='title', notify_type=NotifyType.INFO,
|
|
|
|
attach=attach) is True
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# Verify API calls.
|
2022-03-11 02:59:40 +01:00
|
|
|
assert mock_post.call_count == 2
|
|
|
|
assert mock_post.call_args_list[0][0][0] == \
|
|
|
|
'https://upload.twitter.com/1.1/media/upload.json'
|
|
|
|
assert mock_post.call_args_list[1][0][0] == \
|
|
|
|
'https://api.twitter.com/1.1/statuses/update.json'
|
|
|
|
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
@patch('requests.post')
|
|
|
|
def test_plugin_twitter_tweet_attachments_bad_message_response(
|
|
|
|
mock_post, twitter_url, good_media_response, bad_message_response):
|
2022-03-11 02:59:40 +01:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
mock_post.side_effect = [good_media_response, bad_message_response]
|
|
|
|
|
|
|
|
# Create application objects.
|
|
|
|
twitter_url += '?mode=tweet'
|
2022-03-11 02:59:40 +01:00
|
|
|
obj = Apprise.instantiate(twitter_url)
|
2022-12-02 01:38:55 +01:00
|
|
|
attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, 'apprise-test.gif'))
|
2022-03-11 02:59:40 +01:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# Our notification will fail now since our tweet will error out.
|
2022-03-11 02:59:40 +01:00
|
|
|
assert obj.notify(
|
|
|
|
body='body', title='title', notify_type=NotifyType.INFO,
|
|
|
|
attach=attach) is False
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# Verify API calls.
|
2022-03-11 02:59:40 +01:00
|
|
|
assert mock_post.call_count == 2
|
|
|
|
assert mock_post.call_args_list[0][0][0] == \
|
|
|
|
'https://upload.twitter.com/1.1/media/upload.json'
|
|
|
|
assert mock_post.call_args_list[1][0][0] == \
|
|
|
|
'https://api.twitter.com/1.1/statuses/update.json'
|
|
|
|
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
@patch('requests.post')
|
|
|
|
def test_plugin_twitter_tweet_attachments_bad_message_response_unparseable(
|
|
|
|
mock_post, twitter_url, good_media_response):
|
2022-03-11 02:59:40 +01:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
bad_message_response = bad_response("")
|
|
|
|
mock_post.side_effect = [good_media_response, bad_message_response]
|
2022-03-11 02:59:40 +01:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# Create application objects.
|
|
|
|
twitter_url += '?mode=tweet'
|
2022-03-11 02:59:40 +01:00
|
|
|
obj = Apprise.instantiate(twitter_url)
|
2022-12-02 01:38:55 +01:00
|
|
|
attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, 'apprise-test.gif'))
|
2022-03-11 02:59:40 +01:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# The notification will fail now since the tweet will error out.
|
|
|
|
# This is the same test as above, except that the error response is not
|
|
|
|
# parseable.
|
2022-03-11 02:59:40 +01:00
|
|
|
assert obj.notify(
|
|
|
|
body='body', title='title', notify_type=NotifyType.INFO,
|
|
|
|
attach=attach) is False
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# Verify API calls.
|
2022-03-11 02:59:40 +01:00
|
|
|
assert mock_post.call_count == 2
|
|
|
|
assert mock_post.call_args_list[0][0][0] == \
|
|
|
|
'https://upload.twitter.com/1.1/media/upload.json'
|
|
|
|
assert mock_post.call_args_list[1][0][0] == \
|
|
|
|
'https://api.twitter.com/1.1/statuses/update.json'
|
|
|
|
|
2022-03-07 23:52:22 +01:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
@patch('requests.post')
|
|
|
|
def test_plugin_twitter_tweet_attachments_upload_fails(
|
|
|
|
mock_post, twitter_url, good_media_response):
|
2022-03-07 23:52:22 +01:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# Prepare a bad tweet response.
|
|
|
|
bad_tweet_response = bad_response({})
|
|
|
|
|
|
|
|
# Test case where upload fails.
|
|
|
|
mock_post.side_effect = [good_media_response, bad_tweet_response]
|
|
|
|
|
|
|
|
# Create application objects.
|
|
|
|
twitter_url += '?mode=tweet'
|
2022-03-07 23:52:22 +01:00
|
|
|
obj = Apprise.instantiate(twitter_url)
|
2022-12-02 01:38:55 +01:00
|
|
|
attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, 'apprise-test.gif'))
|
2022-03-07 23:52:22 +01:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# Send our notification; it will fail because of the message response.
|
2022-03-07 23:52:22 +01:00
|
|
|
assert obj.notify(
|
|
|
|
body='body', title='title', notify_type=NotifyType.INFO,
|
|
|
|
attach=attach) is False
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# Verify API calls.
|
2022-03-07 23:52:22 +01:00
|
|
|
assert mock_post.call_count == 2
|
|
|
|
assert mock_post.call_args_list[0][0][0] == \
|
|
|
|
'https://upload.twitter.com/1.1/media/upload.json'
|
|
|
|
assert mock_post.call_args_list[1][0][0] == \
|
|
|
|
'https://api.twitter.com/1.1/statuses/update.json'
|
|
|
|
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
@patch('requests.post')
|
|
|
|
def test_plugin_twitter_tweet_attachments_invalid_attachment(
|
|
|
|
mock_post, twitter_url, good_message_response, good_media_response):
|
|
|
|
|
|
|
|
mock_post.side_effect = [good_media_response, good_message_response]
|
|
|
|
|
|
|
|
# Create application objects.
|
|
|
|
twitter_url += '?mode=tweet'
|
|
|
|
obj = Apprise.instantiate(twitter_url)
|
|
|
|
attach = AppriseAttachment(
|
|
|
|
os.path.join(TEST_VAR_DIR, '/invalid/path/to/an/invalid/file.jpg'))
|
2022-03-07 23:52:22 +01:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# An invalid attachment will cause a failure.
|
2022-03-07 23:52:22 +01:00
|
|
|
assert obj.notify(
|
|
|
|
body='body', title='title', notify_type=NotifyType.INFO,
|
2022-12-02 01:38:55 +01:00
|
|
|
attach=attach) is False
|
2022-03-07 23:52:22 +01:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# No post request as attachment is not good.
|
2022-03-07 23:52:22 +01:00
|
|
|
assert mock_post.call_count == 0
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
|
|
|
|
@patch('requests.post')
|
|
|
|
def test_plugin_twitter_tweet_attachments_multiple_batch(
|
|
|
|
mock_post, twitter_url, good_message_response, good_media_response):
|
2022-03-07 23:52:22 +01:00
|
|
|
|
|
|
|
mock_post.side_effect = [
|
|
|
|
good_media_response, good_media_response, good_media_response,
|
2022-12-02 01:38:55 +01:00
|
|
|
good_media_response, good_message_response, good_message_response,
|
|
|
|
good_message_response, good_message_response]
|
2022-03-07 23:52:22 +01:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# instantiate our object
|
|
|
|
obj = Apprise.instantiate(twitter_url + "?mode=tweet")
|
2022-03-07 23:52:22 +01:00
|
|
|
|
|
|
|
# 4 images are produced
|
|
|
|
attach = [
|
|
|
|
os.path.join(TEST_VAR_DIR, 'apprise-test.gif'),
|
|
|
|
os.path.join(TEST_VAR_DIR, 'apprise-test.gif'),
|
|
|
|
os.path.join(TEST_VAR_DIR, 'apprise-test.jpeg'),
|
|
|
|
os.path.join(TEST_VAR_DIR, 'apprise-test.png'),
|
|
|
|
# This one is not supported, so it's ignored gracefully
|
|
|
|
os.path.join(TEST_VAR_DIR, 'apprise-test.mp4'),
|
|
|
|
]
|
|
|
|
|
|
|
|
assert obj.notify(
|
|
|
|
body='body', title='title', notify_type=NotifyType.INFO,
|
|
|
|
attach=attach) is True
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
assert mock_post.call_count == 7
|
2022-03-07 23:52:22 +01:00
|
|
|
assert mock_post.call_args_list[0][0][0] == \
|
|
|
|
'https://upload.twitter.com/1.1/media/upload.json'
|
|
|
|
assert mock_post.call_args_list[1][0][0] == \
|
|
|
|
'https://upload.twitter.com/1.1/media/upload.json'
|
|
|
|
assert mock_post.call_args_list[2][0][0] == \
|
|
|
|
'https://upload.twitter.com/1.1/media/upload.json'
|
|
|
|
assert mock_post.call_args_list[3][0][0] == \
|
|
|
|
'https://upload.twitter.com/1.1/media/upload.json'
|
|
|
|
assert mock_post.call_args_list[4][0][0] == \
|
|
|
|
'https://api.twitter.com/1.1/statuses/update.json'
|
|
|
|
assert mock_post.call_args_list[5][0][0] == \
|
|
|
|
'https://api.twitter.com/1.1/statuses/update.json'
|
2022-12-02 01:38:55 +01:00
|
|
|
# The 2 images are grouped together (batch mode)
|
2022-03-07 23:52:22 +01:00
|
|
|
assert mock_post.call_args_list[6][0][0] == \
|
|
|
|
'https://api.twitter.com/1.1/statuses/update.json'
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
|
|
|
|
@patch('requests.post')
|
|
|
|
def test_plugin_twitter_tweet_attachments_multiple_nobatch(
|
|
|
|
mock_post, twitter_url, good_message_response, good_media_response):
|
2022-03-07 23:52:22 +01:00
|
|
|
|
|
|
|
mock_post.side_effect = [
|
|
|
|
good_media_response, good_media_response, good_media_response,
|
2022-12-02 01:38:55 +01:00
|
|
|
good_media_response, good_message_response, good_message_response,
|
|
|
|
good_message_response, good_message_response]
|
2022-03-07 23:52:22 +01:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# instantiate our object (without a batch mode)
|
|
|
|
obj = Apprise.instantiate(twitter_url + "?mode=tweet&batch=no")
|
2022-03-07 23:52:22 +01:00
|
|
|
|
|
|
|
# 4 images are produced
|
|
|
|
attach = [
|
|
|
|
os.path.join(TEST_VAR_DIR, 'apprise-test.gif'),
|
|
|
|
os.path.join(TEST_VAR_DIR, 'apprise-test.gif'),
|
|
|
|
os.path.join(TEST_VAR_DIR, 'apprise-test.jpeg'),
|
|
|
|
os.path.join(TEST_VAR_DIR, 'apprise-test.png'),
|
|
|
|
# This one is not supported, so it's ignored gracefully
|
|
|
|
os.path.join(TEST_VAR_DIR, 'apprise-test.mp4'),
|
|
|
|
]
|
|
|
|
|
|
|
|
assert obj.notify(
|
|
|
|
body='body', title='title', notify_type=NotifyType.INFO,
|
|
|
|
attach=attach) is True
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
assert mock_post.call_count == 8
|
2022-03-07 23:52:22 +01:00
|
|
|
assert mock_post.call_args_list[0][0][0] == \
|
|
|
|
'https://upload.twitter.com/1.1/media/upload.json'
|
|
|
|
assert mock_post.call_args_list[1][0][0] == \
|
|
|
|
'https://upload.twitter.com/1.1/media/upload.json'
|
|
|
|
assert mock_post.call_args_list[2][0][0] == \
|
|
|
|
'https://upload.twitter.com/1.1/media/upload.json'
|
|
|
|
assert mock_post.call_args_list[3][0][0] == \
|
|
|
|
'https://upload.twitter.com/1.1/media/upload.json'
|
|
|
|
assert mock_post.call_args_list[4][0][0] == \
|
|
|
|
'https://api.twitter.com/1.1/statuses/update.json'
|
|
|
|
assert mock_post.call_args_list[5][0][0] == \
|
|
|
|
'https://api.twitter.com/1.1/statuses/update.json'
|
|
|
|
assert mock_post.call_args_list[6][0][0] == \
|
|
|
|
'https://api.twitter.com/1.1/statuses/update.json'
|
2022-12-02 01:38:55 +01:00
|
|
|
assert mock_post.call_args_list[7][0][0] == \
|
|
|
|
'https://api.twitter.com/1.1/statuses/update.json'
|
2022-03-07 23:52:22 +01:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
|
|
|
|
@patch('requests.post')
|
|
|
|
def test_plugin_twitter_tweet_attachments_multiple_oserror(
|
|
|
|
mock_post, twitter_url, good_media_response):
|
2022-03-07 23:52:22 +01:00
|
|
|
|
|
|
|
# We have an OSError thrown in the middle of our preparation
|
|
|
|
mock_post.side_effect = [good_media_response, OSError()]
|
|
|
|
|
|
|
|
# 2 images are produced
|
|
|
|
attach = [
|
|
|
|
os.path.join(TEST_VAR_DIR, 'apprise-test.gif'),
|
|
|
|
os.path.join(TEST_VAR_DIR, 'apprise-test.png'),
|
|
|
|
# This one is not supported, so it's ignored gracefully
|
|
|
|
os.path.join(TEST_VAR_DIR, 'apprise-test.mp4'),
|
|
|
|
]
|
|
|
|
|
|
|
|
# We'll fail to send this time
|
2022-12-02 01:38:55 +01:00
|
|
|
obj = Apprise.instantiate(twitter_url + "?mode=tweet")
|
2022-03-07 23:52:22 +01:00
|
|
|
assert obj.notify(
|
|
|
|
body='body', title='title', notify_type=NotifyType.INFO,
|
|
|
|
attach=attach) is False
|
|
|
|
|
|
|
|
assert mock_post.call_count == 2
|
|
|
|
assert mock_post.call_args_list[0][0][0] == \
|
|
|
|
'https://upload.twitter.com/1.1/media/upload.json'
|
|
|
|
assert mock_post.call_args_list[1][0][0] == \
|
|
|
|
'https://upload.twitter.com/1.1/media/upload.json'
|