|
|
@ -24,9 +24,9 @@ |
|
|
|
# THE SOFTWARE. |
|
|
|
# THE SOFTWARE. |
|
|
|
|
|
|
|
|
|
|
|
import six |
|
|
|
import six |
|
|
|
import mock |
|
|
|
|
|
|
|
import sys |
|
|
|
import sys |
|
|
|
import ssl |
|
|
|
import ssl |
|
|
|
|
|
|
|
import mock |
|
|
|
|
|
|
|
|
|
|
|
import apprise |
|
|
|
import apprise |
|
|
|
|
|
|
|
|
|
|
@ -49,47 +49,10 @@ logging.disable(logging.CRITICAL) |
|
|
|
def test_xmpp_plugin(tmpdir): |
|
|
|
def test_xmpp_plugin(tmpdir): |
|
|
|
""" |
|
|
|
""" |
|
|
|
API: NotifyXMPP Plugin() |
|
|
|
API: NotifyXMPP Plugin() |
|
|
|
|
|
|
|
|
|
|
|
""" |
|
|
|
""" |
|
|
|
|
|
|
|
|
|
|
|
# Our module base |
|
|
|
# Mock the sleekxmpp module completely. |
|
|
|
sleekxmpp_name = 'sleekxmpp' |
|
|
|
sys.modules['sleekxmpp'] = mock.MagicMock() |
|
|
|
|
|
|
|
|
|
|
|
# First we do an import without the sleekxmpp library available to ensure |
|
|
|
|
|
|
|
# we can handle cases when the library simply isn't available |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if sleekxmpp_name in sys.modules: |
|
|
|
|
|
|
|
# Test cases where the sleekxmpp library exists; we want to remove it |
|
|
|
|
|
|
|
# for the purpose of testing and capture the handling of the |
|
|
|
|
|
|
|
# library when it is missing |
|
|
|
|
|
|
|
del sys.modules[sleekxmpp_name] |
|
|
|
|
|
|
|
reload(sys.modules['apprise.plugins.NotifyXMPP']) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# We need to fake our gnome environment for testing purposes since |
|
|
|
|
|
|
|
# the sleekxmpp library isn't available in Travis CI |
|
|
|
|
|
|
|
sys.modules[sleekxmpp_name] = mock.MagicMock() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
xmpp = mock.Mock() |
|
|
|
|
|
|
|
xmpp.register_plugin.return_value = True |
|
|
|
|
|
|
|
xmpp.send_message.return_value = True |
|
|
|
|
|
|
|
xmpp.connect.return_value = True |
|
|
|
|
|
|
|
xmpp.disconnect.return_value = True |
|
|
|
|
|
|
|
xmpp.send_presence.return_value = True |
|
|
|
|
|
|
|
xmpp.get_roster.return_value = True |
|
|
|
|
|
|
|
xmpp.ssl_version = None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class IqError(Exception): |
|
|
|
|
|
|
|
iq = {'error': {'condition': 'test'}} |
|
|
|
|
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class IqTimeout(Exception): |
|
|
|
|
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Setup our Exceptions |
|
|
|
|
|
|
|
sys.modules[sleekxmpp_name].exceptions.IqError = IqError |
|
|
|
|
|
|
|
sys.modules[sleekxmpp_name].exceptions.IqTimeout = IqTimeout |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sys.modules[sleekxmpp_name].ClientXMPP.return_value = xmpp |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# The following libraries need to be reloaded to prevent |
|
|
|
# The following libraries need to be reloaded to prevent |
|
|
|
# TypeError: super(type, obj): obj must be an instance or subtype of type |
|
|
|
# TypeError: super(type, obj): obj must be an instance or subtype of type |
|
|
@ -103,9 +66,17 @@ def test_xmpp_plugin(tmpdir): |
|
|
|
reload(sys.modules['apprise.Apprise']) |
|
|
|
reload(sys.modules['apprise.Apprise']) |
|
|
|
reload(sys.modules['apprise']) |
|
|
|
reload(sys.modules['apprise']) |
|
|
|
|
|
|
|
|
|
|
|
# An empty CA list |
|
|
|
# Mock the XMPP adapter to override "self.success". |
|
|
|
sys.modules['apprise.plugins.NotifyXMPP']\ |
|
|
|
# This will signal a successful message delivery. |
|
|
|
.CA_CERTIFICATE_FILE_LOCATIONS = [] |
|
|
|
from apprise.plugins.NotifyXMPP import SleekXmppAdapter |
|
|
|
|
|
|
|
class MockedSleekXmppAdapter(SleekXmppAdapter): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs): |
|
|
|
|
|
|
|
super(MockedSleekXmppAdapter, self).__init__(*args, **kwargs) |
|
|
|
|
|
|
|
self.success = True |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NotifyXMPP = sys.modules['apprise.plugins.NotifyXMPP'] |
|
|
|
|
|
|
|
NotifyXMPP.SleekXmppAdapter = MockedSleekXmppAdapter |
|
|
|
|
|
|
|
|
|
|
|
# Disable Throttling to speed testing |
|
|
|
# Disable Throttling to speed testing |
|
|
|
apprise.plugins.NotifyBase.request_rate_per_sec = 0 |
|
|
|
apprise.plugins.NotifyBase.request_rate_per_sec = 0 |
|
|
@ -136,7 +107,7 @@ def test_xmpp_plugin(tmpdir): |
|
|
|
del ssl.PROTOCOL_TLS |
|
|
|
del ssl.PROTOCOL_TLS |
|
|
|
|
|
|
|
|
|
|
|
# Test our URL |
|
|
|
# Test our URL |
|
|
|
url = 'xmpps://user:pass@example.com' |
|
|
|
url = 'xmpps://user:pass@localhost' |
|
|
|
obj = apprise.Apprise.instantiate(url, suppress_exceptions=False) |
|
|
|
obj = apprise.Apprise.instantiate(url, suppress_exceptions=False) |
|
|
|
# Test we loaded |
|
|
|
# Test we loaded |
|
|
|
assert isinstance(obj, apprise.plugins.NotifyXMPP) is True |
|
|
|
assert isinstance(obj, apprise.plugins.NotifyXMPP) is True |
|
|
@ -151,7 +122,7 @@ def test_xmpp_plugin(tmpdir): |
|
|
|
# Handle case where it is not missing |
|
|
|
# Handle case where it is not missing |
|
|
|
setattr(ssl, 'PROTOCOL_TLS', ssl.PROTOCOL_TLSv1) |
|
|
|
setattr(ssl, 'PROTOCOL_TLS', ssl.PROTOCOL_TLSv1) |
|
|
|
# Test our URL |
|
|
|
# Test our URL |
|
|
|
url = 'xmpps://user:pass@example.com' |
|
|
|
url = 'xmpps://user:pass@localhost' |
|
|
|
obj = apprise.Apprise.instantiate(url, suppress_exceptions=False) |
|
|
|
obj = apprise.Apprise.instantiate(url, suppress_exceptions=False) |
|
|
|
# Test we loaded |
|
|
|
# Test we loaded |
|
|
|
assert isinstance(obj, apprise.plugins.NotifyXMPP) is True |
|
|
|
assert isinstance(obj, apprise.plugins.NotifyXMPP) is True |
|
|
@ -164,31 +135,31 @@ def test_xmpp_plugin(tmpdir): |
|
|
|
|
|
|
|
|
|
|
|
urls = ( |
|
|
|
urls = ( |
|
|
|
{ |
|
|
|
{ |
|
|
|
'u': 'xmpps://user:pass@example.com', |
|
|
|
'u': 'xmpps://user:pass@localhost', |
|
|
|
'p': 'xmpps://user:****@example.com', |
|
|
|
'p': 'xmpps://user:****@localhost', |
|
|
|
}, { |
|
|
|
}, { |
|
|
|
'u': 'xmpps://user:pass@example.com?' |
|
|
|
'u': 'xmpps://user:pass@localhost?' |
|
|
|
'xep=30,199,garbage,xep_99999999', |
|
|
|
'xep=30,199,garbage,xep_99999999', |
|
|
|
'p': 'xmpps://user:****@example.com', |
|
|
|
'p': 'xmpps://user:****@localhost', |
|
|
|
}, { |
|
|
|
}, { |
|
|
|
'u': 'xmpps://user:pass@example.com?xep=ignored', |
|
|
|
'u': 'xmpps://user:pass@localhost?xep=ignored', |
|
|
|
'p': 'xmpps://user:****@example.com', |
|
|
|
'p': 'xmpps://user:****@localhost', |
|
|
|
}, { |
|
|
|
}, { |
|
|
|
'u': 'xmpps://pass@example.com/' |
|
|
|
'u': 'xmpps://pass@localhost/' |
|
|
|
'user@test.com, user2@test.com/resource', |
|
|
|
'user@test.com, user2@test.com/resource', |
|
|
|
'p': 'xmpps://****@example.com', |
|
|
|
'p': 'xmpps://****@localhost', |
|
|
|
}, { |
|
|
|
}, { |
|
|
|
'u': 'xmpps://pass@example.com:5226?jid=user@test.com', |
|
|
|
'u': 'xmpps://pass@localhost:5226?jid=user@test.com', |
|
|
|
'p': 'xmpps://****@example.com:5226', |
|
|
|
'p': 'xmpps://****@localhost:5226', |
|
|
|
}, { |
|
|
|
}, { |
|
|
|
'u': 'xmpps://pass@example.com?jid=user@test.com&verify=False', |
|
|
|
'u': 'xmpps://pass@localhost?jid=user@test.com&verify=False', |
|
|
|
'p': 'xmpps://****@example.com', |
|
|
|
'p': 'xmpps://****@localhost', |
|
|
|
}, { |
|
|
|
}, { |
|
|
|
'u': 'xmpps://user:pass@example.com?verify=False', |
|
|
|
'u': 'xmpps://user:pass@localhost?verify=False', |
|
|
|
'p': 'xmpps://user:****@example.com', |
|
|
|
'p': 'xmpps://user:****@localhost', |
|
|
|
}, { |
|
|
|
}, { |
|
|
|
'u': 'xmpp://user:pass@example.com?to=user@test.com', |
|
|
|
'u': 'xmpp://user:pass@localhost?to=user@test.com', |
|
|
|
'p': 'xmpp://user:****@example.com', |
|
|
|
'p': 'xmpp://user:****@localhost', |
|
|
|
} |
|
|
|
} |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
@ -241,31 +212,9 @@ def test_xmpp_plugin(tmpdir): |
|
|
|
.CA_CERTIFICATE_FILE_LOCATIONS = [str(ca_cert), ] |
|
|
|
.CA_CERTIFICATE_FILE_LOCATIONS = [str(ca_cert), ] |
|
|
|
|
|
|
|
|
|
|
|
obj = apprise.Apprise.instantiate( |
|
|
|
obj = apprise.Apprise.instantiate( |
|
|
|
'xmpps://pass@example.com/user@test.com', |
|
|
|
'xmpps://pass@localhost/user@test.com', |
|
|
|
suppress_exceptions=False) |
|
|
|
suppress_exceptions=False) |
|
|
|
|
|
|
|
|
|
|
|
# Our notification now should be able to get a ca_cert to reference |
|
|
|
# Our notification now should be able to get a ca_cert to reference |
|
|
|
assert obj.notify( |
|
|
|
assert obj.notify( |
|
|
|
title='', body='body', notify_type=apprise.NotifyType.INFO) is True |
|
|
|
title='', body='body', notify_type=apprise.NotifyType.INFO) is True |
|
|
|
|
|
|
|
|
|
|
|
# Test Connect Failures |
|
|
|
|
|
|
|
xmpp.connect.return_value = False |
|
|
|
|
|
|
|
assert obj.notify( |
|
|
|
|
|
|
|
title='', body='body', notify_type=apprise.NotifyType.INFO) is False |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Return our object value so we don't obstruct other tests |
|
|
|
|
|
|
|
xmpp.connect.return_value = True |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Test Exceptions |
|
|
|
|
|
|
|
xmpp.get_roster.side_effect = \ |
|
|
|
|
|
|
|
sys.modules[sleekxmpp_name].exceptions.IqTimeout() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert obj.notify( |
|
|
|
|
|
|
|
title='', body='body', notify_type=apprise.NotifyType.INFO) is False |
|
|
|
|
|
|
|
xmpp.get_roster.side_effect = None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
xmpp.get_roster.side_effect = \ |
|
|
|
|
|
|
|
sys.modules[sleekxmpp_name].exceptions.IqError() |
|
|
|
|
|
|
|
assert obj.notify( |
|
|
|
|
|
|
|
title='', body='body', notify_type=apprise.NotifyType.INFO) is False |
|
|
|
|
|
|
|
xmpp.get_roster.side_effect = None |
|
|
|
|
|
|
|