Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
reload(sys.modules['apprise'])
# Create our instance (identify all supported types)
obj = apprise.Apprise.instantiate('dbus://', suppress_exceptions=False)
assert isinstance(obj, apprise.plugins.NotifyDBus) is True
assert isinstance(obj.url(), six.string_types) is True
assert obj.url().startswith('dbus://_/')
obj = apprise.Apprise.instantiate('kde://', suppress_exceptions=False)
assert isinstance(obj, apprise.plugins.NotifyDBus) is True
assert isinstance(obj.url(), six.string_types) is True
assert obj.url().startswith('kde://_/')
obj = apprise.Apprise.instantiate('qt://', suppress_exceptions=False)
assert isinstance(obj, apprise.plugins.NotifyDBus) is True
assert isinstance(obj.url(), six.string_types) is True
assert obj.url().startswith('qt://_/')
obj = apprise.Apprise.instantiate('glib://', suppress_exceptions=False)
assert isinstance(obj, apprise.plugins.NotifyDBus) is True
assert isinstance(obj.url(), six.string_types) is True
assert obj.url().startswith('glib://_/')
obj.duration = 0
# Check that it found our mocked environments
assert obj._enabled is True
# Test our class loading using a series of arguments
with pytest.raises(TypeError):
apprise.plugins.NotifyDBus(**{'schema': 'invalid'})
# Invalid URLs
assert apprise.plugins.NotifyDBus.parse_url('') is None
# Set our X and Y coordinate and try the notification
obj = apprise.Apprise.instantiate(
'windows://_/?duration=1', suppress_exceptions=False)
assert isinstance(obj.url(), six.string_types) is True
assert obj.notify(
title='title', body='body',
notify_type=apprise.NotifyType.INFO) is True
# loads okay
assert obj.duration == 1
obj = apprise.Apprise.instantiate(
'windows://_/?duration=invalid', suppress_exceptions=False)
# Falls back to default
assert obj.duration == obj.default_popup_duration_sec
obj = apprise.Apprise.instantiate(
'windows://_/?duration=-1', suppress_exceptions=False)
# Falls back to default
assert obj.duration == obj.default_popup_duration_sec
obj = apprise.Apprise.instantiate(
'windows://_/?duration=0', suppress_exceptions=False)
# Falls back to default
assert obj.duration == obj.default_popup_duration_sec
# Test our loading of our icon exception; it will still allow the
# notification to be sent
win32gui.LoadImage.side_effect = AttributeError
assert obj.notify(
title='title', body='body',
notify_type=apprise.NotifyType.INFO) is True
# Undo our change
if instance is None:
# Expected None but didn't get it
print('%s instantiated %s (but expected None)' % (
url, str(obj)))
assert(False)
assert(isinstance(obj, instance))
if isinstance(obj, plugins.NotifyBase):
# We loaded okay; now lets make sure we can reverse this url
assert(isinstance(obj.url(), six.string_types) is True)
# Instantiate the exact same object again using the URL from
# the one that was already created properly
obj_cmp = Apprise.instantiate(obj.url())
# Our object should be the same instance as what we had
# originally expected above.
if not isinstance(obj_cmp, plugins.NotifyBase):
# Assert messages are hard to trace back with the way
# these tests work. Just printing before throwing our
# assertion failure makes things easier to debug later on
print('TEST FAIL: {} regenerated as {}'.format(
url, obj.url()))
assert(False)
if self:
# Iterate over our expected entries inside of our object
for key, val in self.items():
# Test that our object has the desired key
assert(hasattr(key, obj))
assert isinstance(obj, apprise.plugins.NotifyDBus) is True
assert isinstance(obj.url(), six.string_types) is True
assert obj.notify(
title='title', body='body',
notify_type=apprise.NotifyType.INFO) is True
# Test urgency handling
obj = apprise.Apprise.instantiate(
'dbus://_/?urgency=invalid', suppress_exceptions=False)
assert isinstance(obj, apprise.plugins.NotifyDBus) is True
assert isinstance(obj.url(), six.string_types) is True
assert obj.notify(
title='title', body='body',
notify_type=apprise.NotifyType.INFO) is True
obj = apprise.Apprise.instantiate(
'dbus://_/?urgency=high', suppress_exceptions=False)
assert isinstance(obj, apprise.plugins.NotifyDBus) is True
assert isinstance(obj.url(), six.string_types) is True
assert obj.notify(
title='title', body='body',
notify_type=apprise.NotifyType.INFO) is True
obj = apprise.Apprise.instantiate(
'dbus://_/?urgency=2', suppress_exceptions=False)
assert isinstance(obj, apprise.plugins.NotifyDBus) is True
assert isinstance(obj.url(), six.string_types) is True
assert obj.notify(
title='title', body='body',
notify_type=apprise.NotifyType.INFO) is True
obj = apprise.Apprise.instantiate(
# Basically we want to check that ' ' equates to %20 and % equates to %25
# So the above translates to ' %20' (a space in front of %20). We want
# to verify the handling of the password escaping and when it happens.
# a very bad response would be ' ' (double space)
obj = plugins.NotifyEmail.parse_url(
'mailto://user:{}@gmail.com?format=text'.format(passwd))
assert isinstance(obj, dict) is True
assert 'password' in obj
# Escaping doesn't happen at this stage because we want to leave this to
# the plugins discretion
assert obj.get('password') == '%20%2520'
obj = Apprise.instantiate(
'mailto://user:{}@gmail.com?format=text'.format(passwd),
suppress_exceptions=False)
assert isinstance(obj, plugins.NotifyEmail) is True
# The password is escapped 'once' at this point
assert obj.password == ' %20'
# The login will fail because the 'User' or 'Id' field wasn't parsed
assert obj.login() is False
# Our text content (we intentionally reverse the 2 locations
# that store the same thing; we do this so we can test which
# one it defaults to if both are present
mock_post.return_value.content = dumps({
u'User': {
u'Id': u'abcd123',
},
u'Id': u'123abc',
u'AccessToken': u'0000-0000-0000-0000',
})
mock_get.return_value.content = mock_post.return_value.content
obj = Apprise.instantiate('emby://l2g:l2gpass@localhost')
assert isinstance(obj, plugins.NotifyEmby)
# Login
assert obj.login() is True
assert obj.user_id == '123abc'
assert obj.access_token == '0000-0000-0000-0000'
# We're going to log in a second time which checks that we logout
# first before logging in again. But this time we'll scrap the
# 'Id' area and use the one found in the User area if detected
mock_post.return_value.content = dumps({
u'User': {
u'Id': u'abcd123',
},
u'AccessToken': u'0000-0000-0000-0000',
})
# This is the response we expect
assert True
except Exception:
# We can't handle this exception type
assert False
# We're done this part of the test
continue
else:
# Store our response
mock_notifier.notify.return_value = growl_response
try:
obj = Apprise.instantiate(url, suppress_exceptions=False)
assert exception is None
if obj is None:
# We're done
continue
if instance is None:
# Expected None but didn't get it
assert False
assert isinstance(obj, instance) is True
if isinstance(obj, plugins.NotifyBase):
# We loaded okay; now lets make sure we can reverse this url
assert isinstance(obj.url(), six.string_types) is True
# Insert a test email at the head of our table
NotifyEmailBase.EMAIL_TEMPLATES = (
(
# Testing URL
'Testing Lookup',
re.compile(r'^(?P[^@]+)@(?Pl2g\.com)$', re.I),
{
'port': 123,
'smtp_host': 'smtp.l2g.com',
'secure': True,
'login_type': (NotifyEmailBase.WebBaseLogin.USERID, )
},
),
) + NotifyEmailBase.EMAIL_TEMPLATES
obj = Apprise.instantiate(
'mailto://user:pass@l2g.com', suppress_exceptions=True)
assert isinstance(obj, plugins.NotifyEmail)
assert len(obj.targets) == 1
assert 'user@l2g.com' in obj.targets
assert obj.from_addr == 'user@l2g.com'
assert obj.password == 'pass'
assert obj.user == 'user'
assert obj.secure is True
assert obj.port == 123
assert obj.smtp_host == 'smtp.l2g.com'
# We get the same results if an email is identified as the username
# because the USERID variable forces that we can't use an email
obj = Apprise.instantiate(
'mailto://_:pass@l2g.com?user=user@test.com', suppress_exceptions=True)
mock_get.return_value.content = mock_post.return_value.content
# KeyError handling
mock_post.return_value.status_code = 999
mock_get.return_value.status_code = 999
assert obj.login() is False
# General Internal Server Error
mock_post.return_value.status_code = requests.codes.internal_server_error
mock_get.return_value.status_code = requests.codes.internal_server_error
assert obj.login() is False
mock_post.return_value.status_code = requests.codes.ok
mock_get.return_value.status_code = requests.codes.ok
obj = Apprise.instantiate('emby://l2g:l2gpass@localhost:%d' % (
# Increment our port so it will always be something different than
# the default
plugins.NotifyEmby.emby_default_port + 1))
assert isinstance(obj, plugins.NotifyEmby)
assert obj.port == (plugins.NotifyEmby.emby_default_port + 1)
# The login will fail because '' is not a parseable JSON response
assert obj.login() is False
# Disable the port completely
obj.port = None
assert obj.login() is False
# Default port assigments
obj = Apprise.instantiate('emby://l2g:l2gpass@localhost')
assert isinstance(obj, plugins.NotifyEmby)