1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
|
# -*- coding: utf-8 -*-
# BSD 2-Clause License
#
# Apprise - Push Notification Library.
# Copyright (c) 2025, Chris Caron <lead2gold@gmail.com>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 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.
#
# 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.
import requests
from apprise.plugins.reddit import NotifyReddit
from helpers import AppriseURLTester
from unittest import mock
from json import dumps
from datetime import datetime
from datetime import timedelta
from datetime import timezone
# Disable logging for a cleaner testing output
import logging
logging.disable(logging.CRITICAL)
# Our Testing URLs
apprise_url_tests = (
('reddit://', {
# Missing all credentials
'instance': TypeError,
}),
('reddit://:@/', {
'instance': TypeError,
}),
('reddit://user@app_id/app_secret/', {
# No password
'instance': TypeError,
}),
('reddit://user:password@app_id/', {
# No app secret
'instance': TypeError,
}),
('reddit://user:password@app%id/appsecret/apprise', {
# No invalid app_id (has percent)
'instance': TypeError,
}),
('reddit://user:password@app%id/app_secret/apprise', {
# No invalid app_secret (has percent)
'instance': TypeError,
}),
('reddit://user:password@app-id/app-secret/apprise?kind=invalid', {
# An Invalid Kind
'instance': TypeError,
}),
('reddit://user:password@app-id/app-secret/apprise', {
# Login failed
'instance': NotifyReddit,
# Expected notify() response is False because internally we would
# have failed to login
'notify_response': False,
}),
('reddit://user:password@app-id/app-secret', {
# Login successful, but there was no subreddit to notify
'instance': NotifyReddit,
'requests_response_text': {
"access_token": 'abc123',
"token_type": "bearer",
"expires_in": 100000,
"scope": '*',
"refresh_token": 'def456',
# The below is used in the response:
"json": {
# No errors during post
"errors": [],
},
},
# Expected notify() response is False
'notify_response': False,
}),
('reddit://user:password@app-id/app-secret/apprise', {
'instance': NotifyReddit,
'requests_response_text': {
"access_token": 'abc123',
"token_type": "bearer",
"expires_in": 100000,
"scope": '*',
"refresh_token": 'def456',
# The below is used in the response:
"json": {
# Identify an error
"errors": [('KEY', 'DESC', 'INFO'), ],
},
},
# Expected notify() response is False because the
# reddit server provided us errors
'notify_response': False,
}),
('reddit://user:password@app-id/app-secret/apprise', {
'instance': NotifyReddit,
'requests_response_text': {
"access_token": 'abc123',
"token_type": "bearer",
# Test case where 'expires_in' entry is missing
"scope": '*',
"refresh_token": 'def456',
# The below is used in the response:
"json": {
# No errors during post
"errors": [],
},
},
# Our expected url(privacy=True) startswith() response:
'privacy_url': 'reddit://user:****@****/****/apprise',
}),
('reddit://user:password@app-id/app-secret/apprise/subreddit2', {
# password:login acceptable
'instance': NotifyReddit,
'requests_response_text': {
"access_token": 'abc123',
"token_type": "bearer",
"expires_in": 100000,
"scope": '*',
"refresh_token": 'def456',
# The below is used in the response:
"json": {
# No errors during post
"errors": [],
},
},
# Our expected url(privacy=True) startswith() response:
'privacy_url':
'reddit://user:****@****/****/apprise/subreddit2',
}),
# Pass in some arguments to over-ride defaults
('reddit://user:pass@id/secret/sub/'
'?ad=yes&nsfw=yes&replies=no&resubmit=yes&spoiler=yes&kind=self', {
'instance': NotifyReddit,
'requests_response_text': {
"access_token": 'abc123',
"token_type": "bearer",
"expires_in": 100000,
"scope": '*',
"refresh_token": 'def456',
# The below is used in the response:
"json": {
# No errors during post
"errors": [],
},
},
# Our expected url(privacy=True) startswith() response:
'privacy_url': 'reddit://user:****@****/****/sub'}),
# Pass in more arguments
('reddit://'
'?user=l2g&pass=pass&app_secret=abc123&app_id=54321&to=sub1,sub2', {
'instance': NotifyReddit,
'requests_response_text': {
"access_token": 'abc123',
"token_type": "bearer",
"expires_in": 100000,
"scope": '*',
"refresh_token": 'def456',
# The below is used in the response:
"json": {
# No errors during post
"errors": [],
},
},
# Our expected url(privacy=True) startswith() response:
'privacy_url': 'reddit://l2g:****@****/****/sub1/sub2'}),
# More arguments ...
('reddit://user:pass@id/secret/sub7/sub6/sub5/'
'?flair_id=wonder&flair_text=not%20for%20you', {
'instance': NotifyReddit,
'requests_response_text': {
"access_token": 'abc123',
"token_type": "bearer",
"expires_in": 100000,
"scope": '*',
"refresh_token": 'def456',
# The below is used in the response:
"json": {
# No errors during post
"errors": [],
},
},
# Our expected url(privacy=True) startswith() response:
'privacy_url': 'reddit://user:****@****/****/sub'}),
('reddit://user:password@app-id/app-secret/apprise', {
'instance': NotifyReddit,
# throw a bizzare code forcing us to fail to look it up
'response': False,
'requests_response_code': 999,
}),
('reddit://user:password@app-id/app-secret/apprise', {
'instance': NotifyReddit,
# 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_reddit_urls():
"""
NotifyReddit() Apprise URLs
"""
# Run our general tests
AppriseURLTester(tests=apprise_url_tests).run_all()
@mock.patch('requests.post')
def test_plugin_reddit_general(mock_post):
"""
NotifyReddit() General Tests
"""
NotifyReddit.clock_skew = timedelta(seconds=0)
# Generate a valid credentials:
kwargs = {
'app_id': 'a' * 10,
'app_secret': 'b' * 20,
'user': 'user',
'password': 'pasword',
'targets': 'apprise',
}
# Epoch time:
epoch = datetime.fromtimestamp(0, timezone.utc)
good_response = mock.Mock()
good_response.content = dumps({
"access_token": 'abc123',
"token_type": "bearer",
"expires_in": 100000,
"scope": '*',
"refresh_token": 'def456',
# The below is used in the response:
"json": {
# No errors during post
"errors": [],
},
})
good_response.status_code = requests.codes.ok
good_response.headers = {
'X-RateLimit-Reset': (
datetime.now(timezone.utc) - epoch).total_seconds(),
'X-RateLimit-Remaining': 1,
}
# Prepare Mock
mock_post.return_value = good_response
# Variation Initializations
obj = NotifyReddit(**kwargs)
assert isinstance(obj, NotifyReddit)
assert isinstance(obj.url(), str)
# Dynamically pick up on a link
assert obj.send(body="http://hostname") is True
bad_response = mock.Mock()
bad_response.content = ''
bad_response.status_code = 401
# Change our status code and try again
mock_post.return_value = bad_response
assert obj.send(body="test") is False
assert obj.ratelimit_remaining == 1
# Return the status
mock_post.return_value = good_response
# Force a case where there are no more remaining posts allowed
good_response.headers = {
'X-RateLimit-Reset': (
datetime.now(timezone.utc) - epoch).total_seconds(),
'X-RateLimit-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
good_response.headers = {
'X-RateLimit-Reset': (
datetime.now(timezone.utc) - epoch).total_seconds(),
'X-RateLimit-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 good_response.headers['X-RateLimit-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
good_response.headers = {
'X-RateLimit-Reset': (
datetime.now(timezone.utc) - epoch).total_seconds(),
'X-RateLimit-Remaining': 1,
}
# Handle cases where our epoch time is wrong
del good_response.headers['X-RateLimit-Reset']
assert obj.send(body="test") is True
# Return our object, but place it in the future forcing us to block
good_response.headers = {
'X-RateLimit-Reset': (
datetime.now(timezone.utc) - epoch).total_seconds() + 1,
'X-RateLimit-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
good_response.headers = {
'X-RateLimit-Reset': (
datetime.now(timezone.utc) - epoch).total_seconds() - 1,
'X-RateLimit-Remaining': 0,
}
assert obj.send(body="test") is True
# Return our limits to always work
obj.ratelimit_remaining = 1
# Invalid JSON
response = mock.Mock()
response.headers = {
'X-RateLimit-Reset': (
datetime.now(timezone.utc) - epoch).total_seconds(),
'X-RateLimit-Remaining': 1,
}
response.content = '{'
response.status_code = requests.codes.ok
mock_post.return_value = response
obj = NotifyReddit(**kwargs)
assert obj.send(body="test") is False
# Return it to a parseable string but missing the entries we expect
response.content = '{}'
obj = NotifyReddit(**kwargs)
assert obj.send(body="test") is False
# No access token provided
response.content = dumps({
"access_token": '',
"json": {
# No errors during post
"errors": [],
},
})
obj = NotifyReddit(**kwargs)
assert obj.send(body="test") is False
# cause a json parsing issue now
response.content = None
obj = NotifyReddit(**kwargs)
assert obj.send(body="test") is False
# Reset to what we consider a good response
good_response.content = dumps({
"access_token": 'abc123',
"token_type": "bearer",
"expires_in": 100000,
"scope": '*',
"refresh_token": 'def456',
# The below is used in the response:
"json": {
# No errors during post
"errors": [],
},
})
good_response.status_code = requests.codes.ok
good_response.headers = {
'X-RateLimit-Reset': (
datetime.now(timezone.utc) - epoch).total_seconds(),
'X-RateLimit-Remaining': 1,
}
# Reset our mock object
mock_post.reset_mock()
# Test sucessful re-authentication after failed post
mock_post.side_effect = [
good_response, bad_response, good_response, good_response]
obj = NotifyReddit(**kwargs)
assert obj.send(body="test") is True
assert mock_post.call_count == 4
assert mock_post.call_args_list[0][0][0] == \
'https://www.reddit.com/api/v1/access_token'
assert mock_post.call_args_list[1][0][0] == \
'https://oauth.reddit.com/api/submit'
assert mock_post.call_args_list[2][0][0] == \
'https://www.reddit.com/api/v1/access_token'
assert mock_post.call_args_list[3][0][0] == \
'https://oauth.reddit.com/api/submit'
# Test failed re-authentication
mock_post.side_effect = [
good_response, bad_response, bad_response]
obj = NotifyReddit(**kwargs)
assert obj.send(body="test") is False
# Test exception handing on re-auth attempt
response.content = '{'
response.status_code = requests.codes.ok
mock_post.side_effect = [
good_response, bad_response, good_response, response]
obj = NotifyReddit(**kwargs)
assert obj.send(body="test") is False
|