2021-02-14 20:03:12 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2021 Chris Caron <lead2gold@gmail.com>
|
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# This code is licensed under the MIT License.
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files(the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions :
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be included in
|
|
|
|
# all copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
# THE SOFTWARE.
|
2022-01-26 00:19:24 +01:00
|
|
|
#
|
|
|
|
# Great Resources:
|
|
|
|
# - Dev/Legacy API:
|
|
|
|
# https://firebase.google.com/docs/cloud-messaging/http-server-ref
|
|
|
|
# - Legacy API (v1) -> OAuth
|
|
|
|
# - https://firebase.google.com/docs/cloud-messaging/migrate-v1
|
|
|
|
|
2021-02-14 20:03:12 +01:00
|
|
|
import os
|
2021-11-25 21:20:22 +01:00
|
|
|
import sys
|
2022-10-08 02:28:36 +02:00
|
|
|
from unittest import mock
|
2022-09-01 02:05:40 +02:00
|
|
|
|
2021-11-25 21:20:22 +01:00
|
|
|
import pytest
|
2021-02-14 20:03:12 +01:00
|
|
|
import requests
|
|
|
|
import json
|
|
|
|
from apprise import Apprise
|
2022-10-09 11:28:18 +02:00
|
|
|
from apprise.plugins.NotifyFCM import NotifyFCM
|
2021-11-25 21:20:22 +01:00
|
|
|
from helpers import AppriseURLTester
|
|
|
|
|
|
|
|
try:
|
|
|
|
from apprise.plugins.NotifyFCM.oauth import GoogleOAuth
|
2022-01-26 00:19:24 +01:00
|
|
|
from apprise.plugins.NotifyFCM.common import FCM_MODES
|
|
|
|
from apprise.plugins.NotifyFCM.priority import (
|
|
|
|
FCMPriorityManager, FCM_PRIORITIES)
|
|
|
|
from apprise.plugins.NotifyFCM.color import FCMColorManager
|
2021-11-25 21:20:22 +01:00
|
|
|
from cryptography.exceptions import UnsupportedAlgorithm
|
|
|
|
|
|
|
|
except ImportError:
|
|
|
|
# No problem; there is no cryptography support
|
|
|
|
pass
|
2021-02-14 20:03:12 +01:00
|
|
|
|
|
|
|
|
2021-11-25 21:20:22 +01:00
|
|
|
# Disable logging for a cleaner testing output
|
|
|
|
import logging
|
|
|
|
logging.disable(logging.CRITICAL)
|
|
|
|
|
2021-02-14 20:03:12 +01:00
|
|
|
# Test files for KeyFile Directory
|
|
|
|
PRIVATE_KEYFILE_DIR = os.path.join(os.path.dirname(__file__), 'var', 'fcm')
|
2022-12-02 01:38:55 +01:00
|
|
|
FCM_KEYFILE = os.path.join(PRIVATE_KEYFILE_DIR, 'service_account.json')
|
|
|
|
|
2021-02-14 20:03:12 +01:00
|
|
|
|
2021-11-25 21:20:22 +01:00
|
|
|
# Our Testing URLs
|
|
|
|
apprise_url_tests = (
|
|
|
|
('fcm://', {
|
|
|
|
# We failed to identify any valid authentication
|
|
|
|
'instance': TypeError,
|
|
|
|
}),
|
|
|
|
('fcm://:@/', {
|
|
|
|
# We failed to identify any valid authentication
|
|
|
|
'instance': TypeError,
|
|
|
|
}),
|
|
|
|
('fcm://project@%20%20/', {
|
|
|
|
# invalid apikey
|
|
|
|
'instance': TypeError,
|
|
|
|
}),
|
|
|
|
('fcm://apikey/', {
|
|
|
|
# no project id specified so we operate in legacy mode
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyFCM,
|
2021-11-25 21:20:22 +01:00
|
|
|
# but there are no targets specified so we return False
|
|
|
|
'notify_response': False,
|
|
|
|
}),
|
|
|
|
('fcm://apikey/device', {
|
|
|
|
# Valid device
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyFCM,
|
2021-11-25 21:20:22 +01:00
|
|
|
'privacy_url': 'fcm://a...y/device',
|
|
|
|
}),
|
|
|
|
('fcm://apikey/#topic', {
|
|
|
|
# Valid topic
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyFCM,
|
2021-11-25 21:20:22 +01:00
|
|
|
'privacy_url': 'fcm://a...y/%23topic',
|
|
|
|
}),
|
|
|
|
('fcm://apikey/device?mode=invalid', {
|
|
|
|
# Valid device, invalid mode
|
|
|
|
'instance': TypeError,
|
|
|
|
}),
|
|
|
|
('fcm://apikey/#topic1/device/%20/', {
|
|
|
|
# Valid topic, valid device, and invalid entry
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyFCM,
|
2021-11-25 21:20:22 +01:00
|
|
|
}),
|
|
|
|
('fcm://apikey?to=#topic1,device', {
|
|
|
|
# Test to=
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyFCM,
|
2021-11-25 21:20:22 +01:00
|
|
|
}),
|
|
|
|
('fcm://?apikey=abc123&to=device', {
|
|
|
|
# Test apikey= to=
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyFCM,
|
2021-11-25 21:20:22 +01:00
|
|
|
}),
|
2022-01-26 00:19:24 +01:00
|
|
|
('fcm://?apikey=abc123&to=device&image=yes', {
|
|
|
|
# Test image boolean
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyFCM,
|
2022-01-26 00:19:24 +01:00
|
|
|
}),
|
|
|
|
('fcm://?apikey=abc123&to=device&color=no', {
|
|
|
|
# Disable colors
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyFCM,
|
2022-01-26 00:19:24 +01:00
|
|
|
}),
|
|
|
|
('fcm://?apikey=abc123&to=device&color=aabbcc', {
|
|
|
|
# custom colors
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyFCM,
|
2022-01-26 00:19:24 +01:00
|
|
|
}),
|
|
|
|
('fcm://?apikey=abc123&to=device'
|
|
|
|
'&image_url=http://example.com/interesting.jpg', {
|
|
|
|
# Test image_url
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyFCM}),
|
2022-01-26 00:19:24 +01:00
|
|
|
('fcm://?apikey=abc123&to=device'
|
|
|
|
'&image_url=http://example.com/interesting.jpg&image=no', {
|
|
|
|
# Test image_url but set to no
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyFCM}),
|
2022-01-26 00:19:24 +01:00
|
|
|
('fcm://?apikey=abc123&to=device&+key=value&+key2=value2', {
|
|
|
|
# Test apikey= to= and data arguments
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyFCM,
|
2022-01-26 00:19:24 +01:00
|
|
|
}),
|
2021-11-25 21:20:22 +01:00
|
|
|
('fcm://%20?to=device&keyfile=/invalid/path', {
|
|
|
|
# invalid Project ID
|
|
|
|
'instance': TypeError,
|
|
|
|
}),
|
|
|
|
('fcm://project_id?to=device&keyfile=/invalid/path', {
|
2022-12-02 01:38:55 +01:00
|
|
|
# Test to= and auto-detection of OAuth mode
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyFCM,
|
2021-11-25 21:20:22 +01:00
|
|
|
# we'll fail to send our notification as a result
|
|
|
|
'response': False,
|
|
|
|
}),
|
|
|
|
('fcm://?to=device&project=project_id&keyfile=/invalid/path', {
|
2022-12-02 01:38:55 +01:00
|
|
|
# Test project= & to= and auto detection of OAuth mode
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyFCM,
|
2021-11-25 21:20:22 +01:00
|
|
|
# we'll fail to send our notification as a result
|
|
|
|
'response': False,
|
|
|
|
}),
|
|
|
|
('fcm://project_id?to=device&mode=oauth2', {
|
|
|
|
# no keyfile was specified
|
|
|
|
'instance': TypeError,
|
|
|
|
}),
|
|
|
|
('fcm://project_id?to=device&mode=oauth2&keyfile=/invalid/path', {
|
|
|
|
# Same test as above except we explicitly set our oauth2 mode
|
2022-12-02 01:38:55 +01:00
|
|
|
# Test to= and auto-detection of OAuth mode
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyFCM,
|
2021-11-25 21:20:22 +01:00
|
|
|
# we'll fail to send our notification as a result
|
|
|
|
'response': False,
|
|
|
|
}),
|
|
|
|
('fcm://apikey/#topic1/device/?mode=legacy', {
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyFCM,
|
2022-12-02 01:38:55 +01:00
|
|
|
# throw a bizarre code forcing us to fail to look it up
|
2021-11-25 21:20:22 +01:00
|
|
|
'response': False,
|
|
|
|
'requests_response_code': 999,
|
|
|
|
}),
|
|
|
|
('fcm://apikey/#topic1/device/?mode=legacy', {
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyFCM,
|
2021-11-25 21:20:22 +01:00
|
|
|
# Throws a series of connection and transfer exceptions when this flag
|
2022-12-02 01:38:55 +01:00
|
|
|
# is set and tests that we gracefully handle them
|
2021-11-25 21:20:22 +01:00
|
|
|
'test_requests_exceptions': True,
|
|
|
|
}),
|
|
|
|
('fcm://project/#topic1/device/?mode=oauth2&keyfile=file://{}'.format(
|
|
|
|
os.path.join(
|
|
|
|
os.path.dirname(__file__), 'var', 'fcm',
|
|
|
|
'service_account.json')), {
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyFCM,
|
2022-12-02 01:38:55 +01:00
|
|
|
# throw a bizarre code forcing us to fail to look it up
|
2021-11-25 21:20:22 +01:00
|
|
|
'response': False,
|
|
|
|
'requests_response_code': 999,
|
|
|
|
}),
|
|
|
|
('fcm://projectid/#topic1/device/?mode=oauth2&keyfile=file://{}'.format(
|
|
|
|
os.path.join(
|
|
|
|
os.path.dirname(__file__), 'var', 'fcm',
|
|
|
|
'service_account.json')), {
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyFCM,
|
2021-11-25 21:20:22 +01:00
|
|
|
# Throws a series of connection and transfer exceptions when
|
2022-12-02 01:38:55 +01:00
|
|
|
# this flag is set and tests that we gracefully handle them
|
2021-11-25 21:20:22 +01:00
|
|
|
'test_requests_exceptions': True,
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
@pytest.fixture
|
|
|
|
def mock_post(mocker):
|
|
|
|
"""
|
|
|
|
Prepare a good OAuth mock response.
|
|
|
|
"""
|
|
|
|
|
|
|
|
mock_thing = mocker.patch("requests.post")
|
|
|
|
|
|
|
|
response = mock.Mock()
|
|
|
|
response.content = json.dumps({
|
|
|
|
"access_token": "ya29.c.abcd",
|
|
|
|
"expires_in": 3599,
|
|
|
|
"token_type": "Bearer",
|
|
|
|
})
|
|
|
|
response.status_code = requests.codes.ok
|
|
|
|
mock_thing.return_value = response
|
|
|
|
|
|
|
|
return mock_thing
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def mock_post_legacy(mocker):
|
|
|
|
"""
|
|
|
|
Prepare a good legacy mock response.
|
|
|
|
"""
|
|
|
|
|
|
|
|
mock_thing = mocker.patch("requests.post")
|
|
|
|
|
|
|
|
response = mock.Mock()
|
|
|
|
response.status_code = requests.codes.ok
|
|
|
|
mock_thing.return_value = response
|
|
|
|
|
|
|
|
return mock_thing
|
|
|
|
|
|
|
|
|
2021-11-25 21:20:22 +01:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
'cryptography' not in sys.modules, reason="Requires cryptography")
|
|
|
|
def test_plugin_fcm_urls():
|
|
|
|
"""
|
|
|
|
NotifyFCM() Apprise URLs
|
2021-02-14 20:03:12 +01:00
|
|
|
|
2021-11-25 21:20:22 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
# Run our general tests
|
|
|
|
AppriseURLTester(tests=apprise_url_tests).run_all()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
'cryptography' not in sys.modules, reason="Requires cryptography")
|
2022-12-02 01:38:55 +01:00
|
|
|
def test_plugin_fcm_legacy_default(mock_post_legacy):
|
2021-02-14 20:03:12 +01:00
|
|
|
"""
|
2022-12-02 01:38:55 +01:00
|
|
|
NotifyFCM() Legacy/APIKey default checks.
|
2022-01-26 00:19:24 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
# A valid Legacy URL
|
|
|
|
obj = Apprise.instantiate(
|
|
|
|
'fcm://abc123/device/'
|
|
|
|
'?+key=value&+key2=value2'
|
|
|
|
'&image_url=https://example.com/interesting.png')
|
|
|
|
|
|
|
|
# Send our notification
|
|
|
|
assert obj.notify("test") is True
|
|
|
|
|
|
|
|
# Test our call count
|
2022-12-02 01:38:55 +01:00
|
|
|
assert mock_post_legacy.call_count == 1
|
|
|
|
assert mock_post_legacy.call_args_list[0][0][0] == \
|
2022-01-26 00:19:24 +01:00
|
|
|
'https://fcm.googleapis.com/fcm/send'
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
payload = mock_post_legacy.mock_calls[0][2]
|
2022-01-26 00:19:24 +01:00
|
|
|
data = json.loads(payload['data'])
|
|
|
|
assert 'data' in data
|
|
|
|
assert isinstance(data, dict)
|
|
|
|
assert 'key' in data['data']
|
|
|
|
assert data['data']['key'] == 'value'
|
|
|
|
assert 'key2' in data['data']
|
|
|
|
assert data['data']['key2'] == 'value2'
|
|
|
|
|
|
|
|
assert 'notification' in data
|
|
|
|
assert isinstance(data['notification'], dict)
|
|
|
|
assert 'notification' in data['notification']
|
|
|
|
assert isinstance(data['notification']['notification'], dict)
|
|
|
|
assert 'image' in data['notification']['notification']
|
|
|
|
assert data['notification']['notification']['image'] == \
|
|
|
|
'https://example.com/interesting.png'
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
'cryptography' not in sys.modules, reason="Requires cryptography")
|
|
|
|
def test_plugin_fcm_legacy_priorities(mock_post_legacy):
|
|
|
|
"""
|
|
|
|
NotifyFCM() Legacy/APIKey priorities checks.
|
|
|
|
"""
|
|
|
|
|
2022-01-26 00:19:24 +01:00
|
|
|
obj = Apprise.instantiate(
|
|
|
|
'fcm://abc123/device/?priority=low')
|
2022-12-02 01:38:55 +01:00
|
|
|
assert mock_post_legacy.call_count == 0
|
2022-01-26 00:19:24 +01:00
|
|
|
|
|
|
|
# Send our notification
|
|
|
|
assert obj.notify(title="title", body="body") is True
|
|
|
|
|
|
|
|
# Test our call count
|
2022-12-02 01:38:55 +01:00
|
|
|
assert mock_post_legacy.call_count == 1
|
|
|
|
assert mock_post_legacy.call_args_list[0][0][0] == \
|
2022-01-26 00:19:24 +01:00
|
|
|
'https://fcm.googleapis.com/fcm/send'
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
payload = mock_post_legacy.mock_calls[0][2]
|
2022-01-26 00:19:24 +01:00
|
|
|
data = json.loads(payload['data'])
|
|
|
|
assert 'data' not in data
|
|
|
|
assert 'notification' in data
|
|
|
|
assert isinstance(data['notification'], dict)
|
|
|
|
assert 'notification' in data['notification']
|
|
|
|
assert isinstance(data['notification']['notification'], dict)
|
|
|
|
assert 'image' not in data['notification']['notification']
|
|
|
|
assert 'priority' in data
|
|
|
|
|
|
|
|
# legacy can only switch between high/low
|
|
|
|
assert data['priority'] == "normal"
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
'cryptography' not in sys.modules, reason="Requires cryptography")
|
|
|
|
def test_plugin_fcm_legacy_no_colors(mock_post_legacy):
|
|
|
|
"""
|
|
|
|
NotifyFCM() Legacy/APIKey `color=no` checks.
|
|
|
|
"""
|
|
|
|
|
2022-01-26 00:19:24 +01:00
|
|
|
obj = Apprise.instantiate(
|
|
|
|
'fcm://abc123/device/?color=no')
|
2022-12-02 01:38:55 +01:00
|
|
|
assert mock_post_legacy.call_count == 0
|
2022-01-26 00:19:24 +01:00
|
|
|
|
|
|
|
# Send our notification
|
|
|
|
assert obj.notify(title="title", body="body") is True
|
|
|
|
|
|
|
|
# Test our call count
|
2022-12-02 01:38:55 +01:00
|
|
|
assert mock_post_legacy.call_count == 1
|
|
|
|
assert mock_post_legacy.call_args_list[0][0][0] == \
|
2022-01-26 00:19:24 +01:00
|
|
|
'https://fcm.googleapis.com/fcm/send'
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
payload = mock_post_legacy.mock_calls[0][2]
|
2022-01-26 00:19:24 +01:00
|
|
|
data = json.loads(payload['data'])
|
|
|
|
assert 'data' not in data
|
|
|
|
assert 'notification' in data
|
|
|
|
assert isinstance(data['notification'], dict)
|
|
|
|
assert 'notification' in data['notification']
|
|
|
|
assert isinstance(data['notification']['notification'], dict)
|
|
|
|
assert 'image' not in data['notification']['notification']
|
|
|
|
assert 'color' not in data['notification']['notification']
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
'cryptography' not in sys.modules, reason="Requires cryptography")
|
|
|
|
def test_plugin_fcm_legacy_colors(mock_post_legacy):
|
|
|
|
"""
|
|
|
|
NotifyFCM() Legacy/APIKey colors checks.
|
|
|
|
"""
|
|
|
|
|
2022-01-26 00:19:24 +01:00
|
|
|
obj = Apprise.instantiate(
|
|
|
|
'fcm://abc123/device/?color=AA001b')
|
2022-12-02 01:38:55 +01:00
|
|
|
assert mock_post_legacy.call_count == 0
|
2022-01-26 00:19:24 +01:00
|
|
|
|
|
|
|
# Send our notification
|
|
|
|
assert obj.notify(title="title", body="body") is True
|
|
|
|
|
|
|
|
# Test our call count
|
2022-12-02 01:38:55 +01:00
|
|
|
assert mock_post_legacy.call_count == 1
|
|
|
|
assert mock_post_legacy.call_args_list[0][0][0] == \
|
2022-01-26 00:19:24 +01:00
|
|
|
'https://fcm.googleapis.com/fcm/send'
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
payload = mock_post_legacy.mock_calls[0][2]
|
2022-01-26 00:19:24 +01:00
|
|
|
data = json.loads(payload['data'])
|
|
|
|
assert 'data' not in data
|
|
|
|
assert 'notification' in data
|
|
|
|
assert isinstance(data['notification'], dict)
|
|
|
|
assert 'notification' in data['notification']
|
|
|
|
assert isinstance(data['notification']['notification'], dict)
|
|
|
|
assert 'image' not in data['notification']['notification']
|
|
|
|
assert 'color' in data['notification']['notification']
|
|
|
|
assert data['notification']['notification']['color'] == '#aa001b'
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
'cryptography' not in sys.modules, reason="Requires cryptography")
|
2022-12-02 01:38:55 +01:00
|
|
|
def test_plugin_fcm_oauth_default(mock_post):
|
2022-01-26 00:19:24 +01:00
|
|
|
"""
|
2022-12-02 01:38:55 +01:00
|
|
|
NotifyFCM() general OAuth checks - success.
|
|
|
|
Test using a valid Project ID and key file.
|
2021-02-14 20:03:12 +01:00
|
|
|
"""
|
2021-11-25 21:20:22 +01:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
obj = Apprise.instantiate(
|
|
|
|
f'fcm://mock-project-id/device/#topic/?keyfile={FCM_KEYFILE}')
|
2021-02-14 20:03:12 +01:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# send our notification
|
|
|
|
assert obj.notify("test") is True
|
2021-02-14 20:03:12 +01:00
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# Test our call count
|
|
|
|
assert mock_post.call_count == 3
|
|
|
|
assert mock_post.call_args_list[0][0][0] == \
|
|
|
|
'https://accounts.google.com/o/oauth2/token'
|
|
|
|
assert mock_post.call_args_list[1][0][0] == \
|
|
|
|
'https://fcm.googleapis.com/v1/projects/mock-project-id/messages:send'
|
|
|
|
assert mock_post.call_args_list[2][0][0] == \
|
|
|
|
'https://fcm.googleapis.com/v1/projects/mock-project-id/messages:send'
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
'cryptography' not in sys.modules, reason="Requires cryptography")
|
|
|
|
def test_plugin_fcm_oauth_invalid_project_id(mock_post):
|
|
|
|
"""
|
|
|
|
NotifyFCM() OAuth checks, with invalid project id.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Test having a valid keyfile, but not a valid project id match.
|
2021-02-14 20:03:12 +01:00
|
|
|
obj = Apprise.instantiate(
|
2022-12-02 01:38:55 +01:00
|
|
|
f'fcm://invalid_project_id/device/?keyfile={FCM_KEYFILE}')
|
|
|
|
|
2021-02-14 20:03:12 +01:00
|
|
|
# we'll fail as a result
|
|
|
|
assert obj.notify("test") is False
|
|
|
|
|
|
|
|
# Test our call count
|
|
|
|
assert mock_post.call_count == 0
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
'cryptography' not in sys.modules, reason="Requires cryptography")
|
|
|
|
def test_plugin_fcm_oauth_keyfile_error(mock_post):
|
|
|
|
"""
|
|
|
|
NotifyFCM() OAuth checks, while unable to read key file.
|
|
|
|
"""
|
|
|
|
|
2021-02-14 20:03:12 +01:00
|
|
|
# Now we test using a valid Project ID but we can't open our file
|
|
|
|
obj = Apprise.instantiate(
|
2022-12-02 01:38:55 +01:00
|
|
|
f'fcm://mock-project-id/device/?keyfile={FCM_KEYFILE}')
|
2021-02-14 20:03:12 +01:00
|
|
|
|
2022-10-08 02:28:36 +02:00
|
|
|
with mock.patch('builtins.open', side_effect=OSError):
|
2021-02-14 20:03:12 +01:00
|
|
|
# we'll fail as a result
|
|
|
|
assert obj.notify("test") is False
|
|
|
|
|
|
|
|
# Test our call count
|
|
|
|
assert mock_post.call_count == 0
|
|
|
|
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
'cryptography' not in sys.modules, reason="Requires cryptography")
|
|
|
|
def test_plugin_fcm_oauth_data_parameters(mock_post):
|
|
|
|
"""
|
|
|
|
NotifyFCM() OAuth checks, success.
|
|
|
|
Test using a valid Project ID and data parameters.
|
|
|
|
"""
|
2021-02-14 20:03:12 +01:00
|
|
|
|
2022-01-26 00:19:24 +01:00
|
|
|
obj = Apprise.instantiate(
|
2022-12-02 01:38:55 +01:00
|
|
|
f'fcm://mock-project-id/device/#topic/?keyfile={FCM_KEYFILE}'
|
2022-01-26 00:19:24 +01:00
|
|
|
'&+key=value&+key2=value2'
|
2022-12-02 01:38:55 +01:00
|
|
|
'&image_url=https://example.com/interesting.png')
|
2022-01-26 00:19:24 +01:00
|
|
|
assert mock_post.call_count == 0
|
|
|
|
|
|
|
|
# send our notification
|
|
|
|
assert obj.notify("test") is True
|
|
|
|
|
|
|
|
# Test our call count
|
|
|
|
assert mock_post.call_count == 3
|
|
|
|
assert mock_post.call_args_list[0][0][0] == \
|
|
|
|
'https://accounts.google.com/o/oauth2/token'
|
|
|
|
|
|
|
|
assert mock_post.call_args_list[1][0][0] == \
|
|
|
|
'https://fcm.googleapis.com/v1/projects/mock-project-id/messages:send'
|
|
|
|
payload = mock_post.mock_calls[1][2]
|
|
|
|
data = json.loads(payload['data'])
|
|
|
|
assert 'message' in data
|
|
|
|
assert isinstance(data['message'], dict)
|
|
|
|
assert 'data' in data['message']
|
|
|
|
assert isinstance(data['message']['data'], dict)
|
|
|
|
assert 'key' in data['message']['data']
|
|
|
|
assert data['message']['data']['key'] == 'value'
|
|
|
|
assert 'key2' in data['message']['data']
|
|
|
|
assert data['message']['data']['key2'] == 'value2'
|
|
|
|
|
|
|
|
assert 'notification' in data['message']
|
|
|
|
assert isinstance(data['message']['notification'], dict)
|
|
|
|
assert 'image' in data['message']['notification']
|
|
|
|
assert data['message']['notification']['image'] == \
|
|
|
|
'https://example.com/interesting.png'
|
|
|
|
|
|
|
|
assert mock_post.call_args_list[2][0][0] == \
|
|
|
|
'https://fcm.googleapis.com/v1/projects/mock-project-id/messages:send'
|
|
|
|
|
|
|
|
payload = mock_post.mock_calls[2][2]
|
|
|
|
data = json.loads(payload['data'])
|
|
|
|
assert 'message' in data
|
|
|
|
assert isinstance(data['message'], dict)
|
|
|
|
assert 'data' in data['message']
|
|
|
|
assert isinstance(data['message']['data'], dict)
|
|
|
|
assert 'key' in data['message']['data']
|
|
|
|
assert data['message']['data']['key'] == 'value'
|
|
|
|
assert 'key2' in data['message']['data']
|
|
|
|
assert data['message']['data']['key2'] == 'value2'
|
|
|
|
|
|
|
|
assert 'notification' in data['message']
|
|
|
|
assert isinstance(data['message']['notification'], dict)
|
|
|
|
assert 'image' in data['message']['notification']
|
|
|
|
assert data['message']['notification']['image'] == \
|
|
|
|
'https://example.com/interesting.png'
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
'cryptography' not in sys.modules, reason="Requires cryptography")
|
|
|
|
def test_plugin_fcm_oauth_priorities(mock_post):
|
|
|
|
"""
|
|
|
|
Verify priorities work as intended.
|
|
|
|
"""
|
|
|
|
|
2022-01-26 00:19:24 +01:00
|
|
|
obj = Apprise.instantiate(
|
2022-12-02 01:38:55 +01:00
|
|
|
f'fcm://mock-project-id/device/?keyfile={FCM_KEYFILE}'
|
|
|
|
'&priority=high')
|
2022-01-26 00:19:24 +01:00
|
|
|
assert mock_post.call_count == 0
|
|
|
|
|
|
|
|
# Send our notification
|
|
|
|
assert obj.notify(title="title", body="body") is True
|
|
|
|
|
|
|
|
# Test our call count
|
|
|
|
assert mock_post.call_count == 2
|
|
|
|
assert mock_post.call_args_list[0][0][0] == \
|
|
|
|
'https://accounts.google.com/o/oauth2/token'
|
|
|
|
|
|
|
|
assert mock_post.call_args_list[1][0][0] == \
|
|
|
|
'https://fcm.googleapis.com/v1/projects/mock-project-id/messages:send'
|
|
|
|
payload = mock_post.mock_calls[1][2]
|
|
|
|
data = json.loads(payload['data'])
|
|
|
|
assert 'message' in data
|
|
|
|
assert isinstance(data['message'], dict)
|
|
|
|
assert 'data' not in data['message']
|
|
|
|
assert 'notification' in data['message']
|
|
|
|
assert isinstance(data['message']['notification'], dict)
|
|
|
|
assert 'image' not in data['message']['notification']
|
|
|
|
assert data['message']['apns']['headers']['apns-priority'] == "10"
|
|
|
|
assert data['message']['webpush']['headers']['Urgency'] == "high"
|
|
|
|
assert data['message']['android']['priority'] == "HIGH"
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
'cryptography' not in sys.modules, reason="Requires cryptography")
|
|
|
|
def test_plugin_fcm_oauth_no_colors(mock_post):
|
|
|
|
"""
|
|
|
|
Verify `color=no` work as intended.
|
|
|
|
"""
|
|
|
|
|
2022-01-26 00:19:24 +01:00
|
|
|
obj = Apprise.instantiate(
|
2022-12-02 01:38:55 +01:00
|
|
|
f'fcm://mock-project-id/device/?keyfile={FCM_KEYFILE}'
|
|
|
|
'&color=no')
|
2022-01-26 00:19:24 +01:00
|
|
|
assert mock_post.call_count == 0
|
|
|
|
|
|
|
|
# Send our notification
|
|
|
|
assert obj.notify(title="title", body="body") is True
|
|
|
|
|
|
|
|
# Test our call count
|
|
|
|
assert mock_post.call_count == 2
|
|
|
|
assert mock_post.call_args_list[0][0][0] == \
|
|
|
|
'https://accounts.google.com/o/oauth2/token'
|
|
|
|
|
|
|
|
assert mock_post.call_args_list[1][0][0] == \
|
|
|
|
'https://fcm.googleapis.com/v1/projects/mock-project-id/messages:send'
|
|
|
|
payload = mock_post.mock_calls[1][2]
|
|
|
|
data = json.loads(payload['data'])
|
|
|
|
assert 'message' in data
|
|
|
|
assert isinstance(data['message'], dict)
|
|
|
|
assert 'data' not in data['message']
|
|
|
|
assert 'notification' in data['message']
|
|
|
|
assert isinstance(data['message']['notification'], dict)
|
|
|
|
assert 'color' not in data['message']['notification']
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
'cryptography' not in sys.modules, reason="Requires cryptography")
|
|
|
|
def test_plugin_fcm_oauth_colors(mock_post):
|
|
|
|
"""
|
|
|
|
Verify colors work as intended.
|
|
|
|
"""
|
|
|
|
|
2022-01-26 00:19:24 +01:00
|
|
|
obj = Apprise.instantiate(
|
2022-12-02 01:38:55 +01:00
|
|
|
f'fcm://mock-project-id/device/?keyfile={FCM_KEYFILE}'
|
|
|
|
'&color=#12AAbb')
|
2022-01-26 00:19:24 +01:00
|
|
|
assert mock_post.call_count == 0
|
|
|
|
|
|
|
|
# Send our notification
|
|
|
|
assert obj.notify(title="title", body="body") is True
|
|
|
|
|
|
|
|
# Test our call count
|
|
|
|
assert mock_post.call_count == 2
|
|
|
|
assert mock_post.call_args_list[0][0][0] == \
|
|
|
|
'https://accounts.google.com/o/oauth2/token'
|
|
|
|
|
|
|
|
assert mock_post.call_args_list[1][0][0] == \
|
|
|
|
'https://fcm.googleapis.com/v1/projects/mock-project-id/messages:send'
|
|
|
|
payload = mock_post.mock_calls[1][2]
|
|
|
|
data = json.loads(payload['data'])
|
|
|
|
assert 'message' in data
|
|
|
|
assert isinstance(data['message'], dict)
|
|
|
|
assert 'data' not in data['message']
|
|
|
|
assert 'notification' in data['message']
|
|
|
|
assert isinstance(data['message']['notification'], dict)
|
|
|
|
assert 'color' in data['message']['android']['notification']
|
|
|
|
assert data['message']['android']['notification']['color'] == '#12aabb'
|
|
|
|
|
2021-02-14 20:03:12 +01:00
|
|
|
|
2021-11-25 21:20:22 +01:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
'cryptography' not in sys.modules, reason="Requires cryptography")
|
2022-12-02 01:38:55 +01:00
|
|
|
def test_plugin_fcm_keyfile_parse_default(mock_post):
|
2021-02-14 20:03:12 +01:00
|
|
|
"""
|
2021-11-25 21:20:22 +01:00
|
|
|
NotifyFCM() KeyFile Tests
|
2021-02-14 20:03:12 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
oauth = GoogleOAuth()
|
|
|
|
# We can not get an Access Token without content loaded
|
|
|
|
assert oauth.access_token is None
|
|
|
|
|
|
|
|
# Load our content
|
2022-12-02 01:38:55 +01:00
|
|
|
assert oauth.load(FCM_KEYFILE) is True
|
2021-02-14 20:03:12 +01:00
|
|
|
assert oauth.access_token is not None
|
|
|
|
|
|
|
|
# Test our call count
|
|
|
|
assert mock_post.call_count == 1
|
|
|
|
assert mock_post.call_args_list[0][0][0] == \
|
|
|
|
'https://accounts.google.com/o/oauth2/token'
|
|
|
|
|
|
|
|
mock_post.reset_mock()
|
2022-12-02 01:38:55 +01:00
|
|
|
|
2021-02-14 20:03:12 +01:00
|
|
|
# a second call uses cache since our token hasn't expired yet
|
|
|
|
assert oauth.access_token is not None
|
|
|
|
assert mock_post.call_count == 0
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
'cryptography' not in sys.modules, reason="Requires cryptography")
|
|
|
|
def test_plugin_fcm_keyfile_parse_no_expiry(mock_post):
|
|
|
|
"""
|
|
|
|
Test case without `expires_in` entry.
|
|
|
|
"""
|
|
|
|
|
|
|
|
mock_post.return_value.content = json.dumps({
|
2021-02-14 20:03:12 +01:00
|
|
|
"access_token": "ya29.c.abcd",
|
|
|
|
"token_type": "Bearer",
|
|
|
|
})
|
|
|
|
|
|
|
|
oauth = GoogleOAuth()
|
2022-12-02 01:38:55 +01:00
|
|
|
assert oauth.load(FCM_KEYFILE) is True
|
2021-02-14 20:03:12 +01:00
|
|
|
assert oauth.access_token is not None
|
|
|
|
|
|
|
|
# Test our call count
|
|
|
|
assert mock_post.call_count == 1
|
|
|
|
assert mock_post.call_args_list[0][0][0] == \
|
|
|
|
'https://accounts.google.com/o/oauth2/token'
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
'cryptography' not in sys.modules, reason="Requires cryptography")
|
|
|
|
def test_plugin_fcm_keyfile_parse_user_agent(mock_post):
|
|
|
|
"""
|
|
|
|
Test case with `user-agent` override.
|
|
|
|
"""
|
|
|
|
|
2021-02-14 20:03:12 +01:00
|
|
|
oauth = GoogleOAuth(user_agent="test-agent-override")
|
2022-12-02 01:38:55 +01:00
|
|
|
assert oauth.load(FCM_KEYFILE) is True
|
2021-02-14 20:03:12 +01:00
|
|
|
assert oauth.access_token is not None
|
|
|
|
assert mock_post.call_count == 1
|
|
|
|
assert mock_post.call_args_list[0][0][0] == \
|
|
|
|
'https://accounts.google.com/o/oauth2/token'
|
|
|
|
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
'cryptography' not in sys.modules, reason="Requires cryptography")
|
|
|
|
def test_plugin_fcm_keyfile_parse_keyfile_failures(mock_post: mock.Mock):
|
|
|
|
"""
|
|
|
|
Test some errors that can get thrown when trying to handle
|
|
|
|
the `service_account.json` file.
|
|
|
|
"""
|
2021-02-14 20:03:12 +01:00
|
|
|
|
|
|
|
# Now we test a case where we can't access the file we've been pointed to:
|
|
|
|
oauth = GoogleOAuth()
|
2022-10-08 02:28:36 +02:00
|
|
|
with mock.patch('builtins.open', side_effect=OSError):
|
2021-02-14 20:03:12 +01:00
|
|
|
# We will fail to retrieve our Access Token
|
2022-12-02 01:38:55 +01:00
|
|
|
assert oauth.load(FCM_KEYFILE) is False
|
2021-02-14 20:03:12 +01:00
|
|
|
assert oauth.access_token is None
|
|
|
|
|
|
|
|
oauth = GoogleOAuth()
|
|
|
|
with mock.patch('json.loads', side_effect=([], )):
|
|
|
|
# We will fail to retrieve our Access Token since we did not parse
|
|
|
|
# a dictionary
|
2022-12-02 01:38:55 +01:00
|
|
|
assert oauth.load(FCM_KEYFILE) is False
|
2021-02-14 20:03:12 +01:00
|
|
|
assert oauth.access_token is None
|
|
|
|
|
|
|
|
# Case where we can't load the PEM key:
|
|
|
|
oauth = GoogleOAuth()
|
|
|
|
with mock.patch(
|
|
|
|
'cryptography.hazmat.primitives.serialization'
|
|
|
|
'.load_pem_private_key',
|
|
|
|
side_effect=ValueError("")):
|
2022-12-02 01:38:55 +01:00
|
|
|
assert oauth.load(FCM_KEYFILE) is False
|
2021-02-14 20:03:12 +01:00
|
|
|
assert oauth.access_token is None
|
|
|
|
|
|
|
|
# Case where we can't load the PEM key:
|
|
|
|
oauth = GoogleOAuth()
|
|
|
|
with mock.patch(
|
|
|
|
'cryptography.hazmat.primitives.serialization'
|
|
|
|
'.load_pem_private_key',
|
|
|
|
side_effect=TypeError("")):
|
2022-12-02 01:38:55 +01:00
|
|
|
assert oauth.load(FCM_KEYFILE) is False
|
2021-02-14 20:03:12 +01:00
|
|
|
assert oauth.access_token is None
|
|
|
|
|
|
|
|
# Case where we can't load the PEM key:
|
|
|
|
oauth = GoogleOAuth()
|
|
|
|
with mock.patch(
|
|
|
|
'cryptography.hazmat.primitives.serialization'
|
|
|
|
'.load_pem_private_key',
|
|
|
|
side_effect=UnsupportedAlgorithm("")):
|
|
|
|
# Note: This test should be te
|
2022-12-02 01:38:55 +01:00
|
|
|
assert oauth.load(FCM_KEYFILE) is False
|
2021-02-14 20:03:12 +01:00
|
|
|
assert oauth.access_token is None
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
# Verify that not a single call to the web escaped the test harness.
|
|
|
|
assert mock_post.mock_calls == []
|
2021-02-14 20:03:12 +01:00
|
|
|
|
|
|
|
|
2022-12-02 01:38:55 +01:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
'cryptography' not in sys.modules, reason="Requires cryptography")
|
|
|
|
def test_plugin_fcm_keyfile_parse_token_failures(mock_post):
|
|
|
|
"""
|
|
|
|
Test some web errors that can occur when speaking upstream
|
|
|
|
with Google to get our token generated.
|
|
|
|
"""
|
|
|
|
|
|
|
|
mock_post.return_value.status_code = requests.codes.internal_server_error
|
|
|
|
|
2021-02-14 20:03:12 +01:00
|
|
|
oauth = GoogleOAuth()
|
2022-12-02 01:38:55 +01:00
|
|
|
assert oauth.load(FCM_KEYFILE) is True
|
2021-02-14 20:03:12 +01:00
|
|
|
|
|
|
|
# We'll fail due to an bad web response
|
|
|
|
assert oauth.access_token is None
|
|
|
|
|
|
|
|
# Return our status code to how it was
|
2022-12-02 01:38:55 +01:00
|
|
|
mock_post.return_value.status_code = requests.codes.ok
|
2021-02-14 20:03:12 +01:00
|
|
|
|
|
|
|
# No access token
|
|
|
|
bad_response_1 = mock.Mock()
|
|
|
|
bad_response_1.content = json.dumps({
|
|
|
|
"expires_in": 3599,
|
|
|
|
"token_type": "Bearer",
|
|
|
|
})
|
|
|
|
|
|
|
|
# Invalid JSON
|
|
|
|
bad_response_2 = mock.Mock()
|
|
|
|
bad_response_2.content = '{'
|
|
|
|
|
|
|
|
mock_post.return_value = None
|
|
|
|
# Throw an exception on the first call to requests.post()
|
|
|
|
for side_effect in (
|
|
|
|
requests.RequestException(), bad_response_1, bad_response_2):
|
|
|
|
mock_post.side_effect = side_effect
|
|
|
|
|
|
|
|
# Test all of our bad side effects
|
|
|
|
oauth = GoogleOAuth()
|
2022-12-02 01:38:55 +01:00
|
|
|
assert oauth.load(FCM_KEYFILE) is True
|
2021-02-14 20:03:12 +01:00
|
|
|
|
|
|
|
# We'll fail due to an bad web response
|
|
|
|
assert oauth.access_token is None
|
|
|
|
|
|
|
|
|
2021-11-25 21:20:22 +01:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
'cryptography' not in sys.modules, reason="Requires cryptography")
|
|
|
|
def test_plugin_fcm_bad_keyfile_parse():
|
2021-02-14 20:03:12 +01:00
|
|
|
"""
|
2021-11-25 21:20:22 +01:00
|
|
|
NotifyFCM() KeyFile Bad Service Account Type Tests
|
2021-02-14 20:03:12 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
path = os.path.join(PRIVATE_KEYFILE_DIR, 'service_account-bad-type.json')
|
|
|
|
oauth = GoogleOAuth()
|
|
|
|
assert oauth.load(path) is False
|
|
|
|
|
|
|
|
|
2021-11-25 21:20:22 +01:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
'cryptography' not in sys.modules, reason="Requires cryptography")
|
|
|
|
def test_plugin_fcm_keyfile_missing_entries_parse(tmpdir):
|
2021-02-14 20:03:12 +01:00
|
|
|
"""
|
2021-11-25 21:20:22 +01:00
|
|
|
NotifyFCM() KeyFile Missing Entries Test
|
2021-02-14 20:03:12 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
# Prepare a base keyfile reference to use
|
|
|
|
path = os.path.join(PRIVATE_KEYFILE_DIR, 'service_account.json')
|
2022-10-08 02:28:36 +02:00
|
|
|
with open(path, mode="r", encoding='utf-8') as fp:
|
2021-02-14 20:03:12 +01:00
|
|
|
content = json.loads(fp.read())
|
|
|
|
|
|
|
|
path = tmpdir.join('fcm_keyfile.json')
|
|
|
|
|
|
|
|
# Test that we fail to load if the following keys are missing:
|
|
|
|
for entry in (
|
|
|
|
'client_email', 'private_key_id', 'private_key', 'type',
|
|
|
|
'project_id'):
|
|
|
|
|
|
|
|
# Ensure the key actually exists in our file
|
|
|
|
assert entry in content
|
|
|
|
|
|
|
|
# Create a copy of our content
|
|
|
|
content_copy = content.copy()
|
|
|
|
|
|
|
|
# Remove our entry we expect to validate against
|
|
|
|
del content_copy[entry]
|
|
|
|
assert entry not in content_copy
|
|
|
|
|
|
|
|
path.write(json.dumps(content_copy))
|
|
|
|
|
|
|
|
oauth = GoogleOAuth()
|
|
|
|
assert oauth.load(str(path)) is False
|
|
|
|
|
|
|
|
# Now write ourselves a bad JSON file
|
|
|
|
path.write('{')
|
|
|
|
oauth = GoogleOAuth()
|
|
|
|
assert oauth.load(str(path)) is False
|
2021-11-25 21:20:22 +01:00
|
|
|
|
|
|
|
|
2022-01-26 00:19:24 +01:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
'cryptography' not in sys.modules, reason="Requires cryptography")
|
2022-12-02 01:38:55 +01:00
|
|
|
def test_plugin_fcm_priority_manager():
|
2022-01-26 00:19:24 +01:00
|
|
|
"""
|
|
|
|
NotifyFCM() FCMPriorityManager() Testing
|
|
|
|
"""
|
|
|
|
|
|
|
|
for mode in FCM_MODES:
|
|
|
|
for priority in FCM_PRIORITIES:
|
|
|
|
instance = FCMPriorityManager(mode, priority)
|
|
|
|
assert isinstance(instance.payload(), dict)
|
|
|
|
# Verify it's not empty
|
|
|
|
assert bool(instance)
|
|
|
|
assert instance.payload()
|
|
|
|
assert str(instance) == priority
|
|
|
|
|
|
|
|
# We do not have to set a priority
|
|
|
|
instance = FCMPriorityManager(mode)
|
|
|
|
assert isinstance(instance.payload(), dict)
|
|
|
|
|
|
|
|
# Dictionary is empty though
|
|
|
|
assert not bool(instance)
|
|
|
|
assert not instance.payload()
|
|
|
|
assert str(instance) == ''
|
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
instance = FCMPriorityManager(mode, 'invalid')
|
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
instance = FCMPriorityManager('invald', 'high')
|
|
|
|
|
|
|
|
# mode validation is done at the higher NotifyFCM() level so
|
|
|
|
# it is not tested here (not required)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
'cryptography' not in sys.modules, reason="Requires cryptography")
|
2022-12-02 01:38:55 +01:00
|
|
|
def test_plugin_fcm_color_manager():
|
2022-01-26 00:19:24 +01:00
|
|
|
"""
|
|
|
|
NotifyFCM() FCMColorManager() Testing
|
|
|
|
"""
|
|
|
|
|
|
|
|
# No colors
|
|
|
|
instance = FCMColorManager('no')
|
|
|
|
assert bool(instance) is False
|
|
|
|
assert instance.get() is None
|
|
|
|
# We'll return that we are not defined
|
|
|
|
assert str(instance) == 'no'
|
|
|
|
|
|
|
|
# Asset colors
|
|
|
|
instance = FCMColorManager('yes')
|
2022-10-08 02:28:36 +02:00
|
|
|
assert isinstance(instance.get(), str)
|
2022-01-26 00:19:24 +01:00
|
|
|
# Output: #rrggbb
|
|
|
|
assert len(instance.get()) == 7
|
|
|
|
# Starts with has symbol
|
|
|
|
assert instance.get()[0] == '#'
|
|
|
|
# We'll return that we are defined but using default configuration
|
|
|
|
assert str(instance) == 'yes'
|
|
|
|
|
|
|
|
# We will be `true` because we can acquire a color based on what was
|
|
|
|
# passed in
|
|
|
|
assert bool(instance) is True
|
|
|
|
|
|
|
|
# Custom color
|
|
|
|
instance = FCMColorManager('#A2B3A4')
|
2022-10-08 02:28:36 +02:00
|
|
|
assert isinstance(instance.get(), str)
|
2022-01-26 00:19:24 +01:00
|
|
|
assert instance.get() == '#a2b3a4'
|
|
|
|
assert bool(instance) is True
|
|
|
|
# str() response does not include hashtag
|
|
|
|
assert str(instance) == 'a2b3a4'
|
|
|
|
|
|
|
|
# Custom color (no hashtag)
|
|
|
|
instance = FCMColorManager('A2B3A4')
|
2022-10-08 02:28:36 +02:00
|
|
|
assert isinstance(instance.get(), str)
|
2022-01-26 00:19:24 +01:00
|
|
|
# Hashtag is always part of output
|
|
|
|
assert instance.get() == '#a2b3a4'
|
|
|
|
assert bool(instance) is True
|
|
|
|
# str() response does not include hashtag
|
|
|
|
assert str(instance) == 'a2b3a4'
|
|
|
|
|
|
|
|
# Custom color (no hashtag) but only using 3 letter rgb values
|
|
|
|
instance = FCMColorManager('AC4')
|
2022-10-08 02:28:36 +02:00
|
|
|
assert isinstance(instance.get(), str)
|
2022-01-26 00:19:24 +01:00
|
|
|
# Hashtag is always part of output
|
|
|
|
assert instance.get() == '#aacc44'
|
|
|
|
assert bool(instance) is True
|
|
|
|
# str() response does not include hashtag
|
|
|
|
assert str(instance) == 'aacc44'
|
|
|
|
|
|
|
|
|
2021-11-25 21:20:22 +01:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
'cryptography' in sys.modules,
|
|
|
|
reason="Requires that cryptography NOT be installed")
|
|
|
|
def test_plugin_fcm_cryptography_import_error():
|
|
|
|
"""
|
|
|
|
NotifyFCM Cryptography loading failure
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Prepare a base keyfile reference to use
|
|
|
|
path = os.path.join(PRIVATE_KEYFILE_DIR, 'service_account.json')
|
|
|
|
|
|
|
|
# Attempt to instantiate our object
|
|
|
|
obj = Apprise.instantiate(
|
|
|
|
'fcm://mock-project-id/device/#topic/?keyfile={}'.format(str(path)))
|
|
|
|
|
|
|
|
# It's not possible because our cryptography depedancy is missing
|
|
|
|
assert obj is None
|
2022-01-26 00:19:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
'cryptography' not in sys.modules, reason="Requires cryptography")
|
|
|
|
@mock.patch('requests.post')
|
2022-10-16 16:48:44 +02:00
|
|
|
def test_plugin_fcm_edge_cases(mock_post):
|
2022-01-26 00:19:24 +01:00
|
|
|
"""
|
|
|
|
NotifyFCM() Edge Cases
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Prepare a good response
|
|
|
|
response = mock.Mock()
|
|
|
|
response.status_code = requests.codes.ok
|
|
|
|
mock_post.return_value = response
|
|
|
|
|
|
|
|
# this tests an edge case where verify if the data_kwargs is a dictionary
|
|
|
|
# or not. Below, we don't even define it, so it will be None (causing
|
|
|
|
# the check to go). We'll still correctly instantiate a plugin:
|
2022-10-09 11:28:18 +02:00
|
|
|
obj = NotifyFCM("project", "api:123", targets='device')
|
2022-01-26 00:19:24 +01:00
|
|
|
assert obj is not None
|