...
 
Commits (2)
import pytest
@pytest.fixture
def volatile_dir(tmpdir):
return tmpdir.mkdir('volatile')
@pytest.fixture
def persistent_dir(tmpdir):
return tmpdir.mkdir('persistent')
@pytest.fixture
def plugin_dir(tmpdir):
return tmpdir.mkdir('plugins')
@pytest.fixture
def simple_plugin_dir(plugin_dir):
return plugin_dir.mkdir('simple')
@pytest.fixture
def simple_plugin():
return """actions:
- name: reject
title: "{% trans %}Reject current update{% endtrans %}"
command: updater --reject-update
- name: dummy
title: "{% trans %}Dummy action{% endtrans %}"
command: /bin/true
templates:
- type: simple_message
supported_media:
- plain
- html
src: simple.j2
- type: complex_message
supported_media:
- plain
- html
- email
src: complex.j2
notifications:
- name: simple
template: simple_message
actions:
- dummy
version: 1
- name: complex
template: complex_message
severity: error
persistent: True
explicit_dismiss: False
actions:
- reject
version: 1
"""
@pytest.fixture
def template_simple():
return """{% if media == 'html' %}
<h3>{{ _("Simple message") }}</h3>
<p>
{{ _("HTML rendered message") }}: {{ message }}
</p>
{% else %}
= {{ _("Simple message") }} =
{{ _("Message") }}: {{message}}
{% endif %}"""
@pytest.fixture
def template_complex():
return """{% if media == 'html' %}
<h1>{{ _("Complex message") }}</h1>
<p>Lorem ipsum dolor sit amet</p>
<p>
{{ _("First message") }}: {{ message }}
<br>
{{ _("Second message") }}: {{ message2 }}
</p>
{% else %}
[{{ _("Complex message") }}]
{{ _("First message") }}: {{message}}
{{ _("Second message") }}: {{ message2 }}
{% endif %}"""
@pytest.fixture
def config(volatile_dir, persistent_dir, plugin_dir):
return {
'settings': {
'volatile_dir': volatile_dir,
'persistent_dir': persistent_dir,
'plugin_dir': plugin_dir,
}
}
@pytest.fixture
def persistent_notification():
return """{
"notif_id": "8c3ad32ca1774eed985604cc1a328773",
"api_version": 1,
"timestamp": 1549381864,
"skeleton": {
"name": "simple",
"plugin_name": "simple",
"version": 1,
"template": {
"type": "simple_message",
"supported_media": [
"plain",
"html"
],
"src": "complex.j2"
},
"actions": {
"dummy": {
"name": "dummy",
"title": "{% trans %}Dummy action{% endtrans %}",
"command": "/bin/true"
}
},
"timeout": null,
"severity": "info",
"persistent": false,
"explicit_dismiss": true
},
"persistent": false,
"timeout": null,
"severity": "info",
"data": {
"message": "egg egg spam"
},
"fallback": {
"plain": "\\n= Simple message =\\nMessage: egg egg spam\\n",
"html": "\\n<h3>Simple message</h3>\\n<p>\\n HTML rendered message: egg egg spam\\n</p>\\n"
},
"valid": true,
"explicit_dismiss": false,
"default_action": "dismiss"
}"""
@pytest.fixture
def dummy_notification():
return """{
"notif_id": "8c3ad32ca1774eed985604cc1a328773",
"api_version": 1,
"timestamp": 1549381864,
"skeleton": {
"name": "simple",
"plugin_name": "simple",
"version": 1,
"template": {
"type": "simple_message",
"supported_media": [
"plain",
"html"
],
"src": "simple.j2"
},
"actions": {
"dummy": {
"name": "dummy",
"title": "{% trans %}Dummy action{% endtrans %}",
"command": "/bin/true"
}
},
"timeout": null,
"severity": "info",
"persistent": false,
"explicit_dismiss": true
},
"persistent": false,
"timeout": null,
"severity": "info",
"data": {
"message": "egg egg spam"
},
"fallback": {
"plain": "\\n= Simple message =\\nMessage: egg egg spam\\n",
"html": "\\n<h3>Simple message</h3>\\n<p>\\n HTML rendered message: egg egg spam\\n</p>\\n"
},
"valid": true,
"explicit_dismiss": true,
"default_action": "dismiss"
}"""
@pytest.fixture
def dummy_notification2():
return """{
"notif_id": "7df5ac1731c649d7a2ae368e5f0af22b",
"api_version": 1,
"timestamp": 1549381864,
"skeleton": {
"name": "simple",
"plugin_name": "simple",
"version": 1,
"template": {
"type": "simple_message",
"supported_media": [
"plain",
"html"
],
"src": "simple.j2"
},
"actions": {
"dummy": {
"name": "dummy",
"title": "{% trans %}Dummy action{% endtrans %}",
"command": "/bin/true"
}
},
"timeout": null,
"severity": "info",
"persistent": false,
"explicit_dismiss": true
},
"persistent": false,
"timeout": null,
"severity": "info",
"data": {
"message": "egg egg spam"
},
"fallback": {
"plain": "\\n= Simple message =\\nMessage: egg egg spam\\n",
"html": "\\n<h3>Simple message</h3>\\n<p>\\n HTML rendered message: egg egg spam\\n</p>\\n"
},
"valid": true,
"explicit_dismiss": true,
"default_action": "dismiss"
}"""
#!/usr/bin/env python3
"""Simple Create-Read-Delete test program"""
import json
import os
import pprint
from notifylib.api import Api
conffile = os.path.abspath('config.conf')
expected_template = 'simple.simple'
message_data = '{"message": "egg spam spam"}'
api = Api(conffile)
# Create
templates = api.get_templates()
print(templates)
opts = {
'skel_id': expected_template,
'data': json.loads(message_data),
}
nid = api.create(**opts)
print(nid)
# Read
n = api.get_rendered_notification(nid, 'simple', 'en')
pprint.pprint(n)
# Delete
api.call_action(nid, 'dismiss')
......@@ -8,199 +8,6 @@ from notifylib.exceptions import (
NotificationNotDismissibleException,
)
# TODO: split fixtures and tests
@pytest.fixture
def volatile_dir(tmpdir):
return tmpdir.mkdir('volatile')
@pytest.fixture
def persistent_dir(tmpdir):
return tmpdir.mkdir('persistent')
@pytest.fixture
def plugin_dir(tmpdir):
return tmpdir.mkdir('plugins')
@pytest.fixture
def simple_plugin_dir(plugin_dir):
return plugin_dir.mkdir('simple')
@pytest.fixture
def simple_plugin():
return """actions:
- name: reject
title: "{% trans %}Reject current update{% endtrans %}"
command: updater --reject-update
- name: dummy
title: "{% trans %}Dummy action{% endtrans %}"
command: /bin/true
templates:
- type: simple_message
supported_media:
- plain
- html
src: simple.j2
- type: complex_message
supported_media:
- plain
- html
- email
src: complex.j2
notifications:
- name: simple
template: simple_message
actions:
- dummy
version: 1
- name: complex
template: complex_message
severity: error
persistent: True
explicit_dismiss: False
actions:
- reject
version: 1
"""
@pytest.fixture
def template_simple():
return """{% if media == 'html' %}
<h3>{{ _("Simple message") }}</h3>
<p>
{{ _("HTML rendered message") }}: {{ message }}
</p>
{% else %}
= {{ _("Simple message") }} =
{{ _("Message") }}: {{message}}
{% endif %}"""
@pytest.fixture
def template_complex():
return """{% if media == 'html' %}
<h1>{{ _("Complex message") }}</h1>
<p>Lorem ipsum dolor sit amet</p>
<p>
{{ _("First message") }}: {{ message }}
<br>
{{ _("Second message") }}: {{ message2 }}
</p>
{% else %}
[{{ _("Complex message") }}]
{{ _("First message") }}: {{message}}
{{ _("Second message") }}: {{ message2 }}
{% endif %}"""
@pytest.fixture
def config(volatile_dir, persistent_dir, plugin_dir):
return {
'settings': {
'volatile_dir': volatile_dir,
'persistent_dir': persistent_dir,
'plugin_dir': plugin_dir,
}
}
@pytest.fixture
def persistent_notification():
return """{
"notif_id": "8c3ad32ca1774eed985604cc1a328773",
"api_version": 1,
"timestamp": 1549381864,
"skeleton": {
"name": "simple",
"plugin_name": "simple",
"version": 1,
"template": {
"type": "simple_message",
"supported_media": [
"plain",
"html"
],
"src": "simple.j2"
},
"actions": {
"dummy": {
"name": "dummy",
"title": "{% trans %}Dummy action{% endtrans %}",
"command": "/bin/true"
}
},
"timeout": null,
"severity": "info",
"persistent": false,
"explicit_dismiss": true
},
"persistent": false,
"timeout": null,
"severity": "info",
"data": {
"message": "egg egg spam"
},
"fallback": {
"plain": "\\n= Simple message =\\nMessage: egg egg spam\\n",
"html": "\\n<h3>Simple message</h3>\\n<p>\\n HTML rendered message: egg egg spam\\n</p>\\n"
},
"valid": true,
"explicit_dismiss": false,
"default_action": "dismiss"
}"""
@pytest.fixture
def dummy_notification():
return """{
"notif_id": "8c3ad32ca1774eed985604cc1a328773",
"api_version": 1,
"timestamp": 1549381864,
"skeleton": {
"name": "simple",
"plugin_name": "simple",
"version": 1,
"template": {
"type": "simple_message",
"supported_media": [
"plain",
"html"
],
"src": "simple.j2"
},
"actions": {
"dummy": {
"name": "dummy",
"title": "{% trans %}Dummy action{% endtrans %}",
"command": "/bin/true"
}
},
"timeout": null,
"severity": "info",
"persistent": false,
"explicit_dismiss": true
},
"persistent": false,
"timeout": null,
"severity": "info",
"data": {
"message": "egg egg spam"
},
"fallback": {
"plain": "\\n= Simple message =\\nMessage: egg egg spam\\n",
"html": "\\n<h3>Simple message</h3>\\n<p>\\n HTML rendered message: egg egg spam\\n</p>\\n"
},
"valid": true,
"explicit_dismiss": true,
"default_action": "dismiss"
}"""
def test_list_templates(config, simple_plugin_dir, simple_plugin, template_simple, template_complex):
plug = simple_plugin_dir.join('plugin.yml')
......@@ -219,16 +26,7 @@ def test_list_templates(config, simple_plugin_dir, simple_plugin, template_simpl
assert templates == ['simple.simple', 'simple.complex']
@pytest.mark.xfail
def test_create_notification(
volatile_dir,
simple_plugin_dir,
config,
dummy_notification,
simple_plugin,
template_simple,
template_complex):
# TODO: figure out what to do with timestamp and notification id
def test_create_notification(config, simple_plugin_dir, simple_plugin, template_simple):
plug = simple_plugin_dir.join('plugin.yml')
plug.write(simple_plugin)
......@@ -236,27 +34,19 @@ def test_create_notification(
tpl_simple = tpl_dir.join('simple.j2')
tpl_simple.write(template_simple)
tpl_complex = tpl_dir.join('complex.j2')
tpl_complex.write(template_complex)
opts = {'data': json.loads('{"message": "egg spam spam"}')}
api = Api(confdict=config)
opts = {
'skel_id': 'simple.simple',
'data': json.loads('{"message": "egg spam spam"}'),
}
nid = api.create(**opts)
assert nid.isalnum() and len(nid) == 32
api.create('simple.simple', **opts)
f = volatile_dir.join("{}.json".format(nid))
result = f.read()
notifications = api.get_notifications()
assert result == dummy_notification
assert isinstance(notifications, dict)
assert len(notifications) == 1
def test_get_notification(config, volatile_dir, simple_plugin_dir, simple_plugin, template_simple, dummy_notification):
"""Test notification lookup with full lenght id"""
"""Test notification lookup with full length id"""
f = volatile_dir.join('8c3ad32ca1774eed985604cc1a328773.json')
f.write(dummy_notification)
......@@ -268,142 +58,113 @@ def test_get_notification(config, volatile_dir, simple_plugin_dir, simple_plugin
tpl_simple.write(template_simple)
api = Api(confdict=config)
n = api.get_rendered_notification('8c3ad32ca1774eed985604cc1a328773')
assert isinstance(n, dict)
# TODO: check json schema?
assert 'message' in n
assert 'actions' in n
assert 'metadata' in n
notifications = api.get_notifications()
assert n['message'] == '\n= Simple message =\nMessage: egg egg spam\n'
assert set(('dummy', 'dismiss')).issubset(n['actions'])
assert set(('persistent', 'timestamp', 'severity', 'default_action')).issubset(n['metadata'])
assert isinstance(notifications, dict)
assert len(notifications) == 1
def test_get_notification_shortid(volatile_dir, config, dummy_notification):
"""Test notification lookup with short id"""
f = volatile_dir.join('8c3ad32ca1774eed985604cc1a328773.json')
f.write(dummy_notification)
api = Api(confdict=config)
n = api.get_rendered_notification('8c3ad32c')
n = api.get_rendered_notification('8c3ad32ca1774eed985604cc1a328773')
assert isinstance(n, dict)
# TODO: check json schema?
assert 'message' in n
assert 'actions' in n
assert 'metadata' in n
assert n['message'] == '\n= Simple message =\nMessage: egg egg spam\n'
assert set(('dummy', 'dismiss')).issubset(n['actions'])
assert set(('persistent', 'timestamp', 'severity', 'default_action')).issubset(n['metadata'])
def test_get_notification_try_media_type(volatile_dir, config, dummy_notification):
"""Try existing media type and it should return it"""
def test_get_multiple_notifications(config, volatile_dir, simple_plugin_dir, simple_plugin, template_simple, dummy_notification, dummy_notification2):
f = volatile_dir.join('8c3ad32ca1774eed985604cc1a328773.json')
f2 = volatile_dir.join('7df5ac1731c649d7a2ae368e5f0af22b.json')
f.write(dummy_notification)
f2.write(dummy_notification2)
api = Api(confdict=config)
n = api.get_rendered_notification('8c3ad32ca1774eed985604cc1a328773', 'html')
plug = simple_plugin_dir.join('plugin.yml')
plug.write(simple_plugin)
assert isinstance(n, dict)
tpl_dir = simple_plugin_dir.mkdir('templates')
tpl_simple = tpl_dir.join('simple.j2')
tpl_simple.write(template_simple)
# TODO: check json schema?
api = Api(confdict=config)
assert 'message' in n
assert 'actions' in n
assert 'metadata' in n
notifications = api.get_notifications()
assert n['message'] == '\n<h3>Simple message</h3>\n<p>\n HTML rendered message: egg egg spam\n</p>\n'
assert set(('dummy', 'dismiss')).issubset(n['actions'])
assert set(('persistent', 'timestamp', 'severity', 'default_action')).issubset(n['metadata'])
assert isinstance(notifications, dict)
assert len(notifications) == 2
def test_get_notification_try_media_type_failure(volatile_dir, config, dummy_notification):
"""Try non-existing media type and it should return default (plain)"""
def test_get_notification_shortid(config, volatile_dir, simple_plugin_dir, simple_plugin, template_simple, dummy_notification):
"""Test notification lookup with full length id"""
f = volatile_dir.join('8c3ad32ca1774eed985604cc1a328773.json')
f.write(dummy_notification)
api = Api(confdict=config)
n = api.get_rendered_notification('8c3ad32ca1774eed985604cc1a328773', 'dummy')
assert isinstance(n, dict)
# TODO: check json schema?
assert 'message' in n
assert 'actions' in n
assert 'metadata' in n
assert n['message'] == '\n= Simple message =\nMessage: egg egg spam\n'
assert set(('dummy', 'dismiss')).issubset(n['actions'])
assert set(('persistent', 'timestamp', 'severity', 'default_action')).issubset(n['metadata'])
plug = simple_plugin_dir.join('plugin.yml')
plug.write(simple_plugin)
def test_get_notification_force_media_type(volatile_dir, config, dummy_notification):
f = volatile_dir.join('8c3ad32ca1774eed985604cc1a328773.json')
f.write(dummy_notification)
tpl_dir = simple_plugin_dir.mkdir('templates')
tpl_simple = tpl_dir.join('simple.j2')
tpl_simple.write(template_simple)
api = Api(confdict=config)
n = api.get_rendered_notification('8c3ad32ca1774eed985604cc1a328773', 'html', 'en', force_media_type=True)
assert isinstance(n, dict)
notifications = api.get_notifications()
# TODO: check json schema?
assert isinstance(notifications, dict)
assert len(notifications) == 1
assert 'message' in n
assert 'actions' in n
assert 'metadata' in n
n = api.get_rendered_notification('8c3ad32c')
assert n['message'] == '\n<h3>Simple message</h3>\n<p>\n HTML rendered message: egg egg spam\n</p>\n'
assert set(('dummy', 'dismiss')).issubset(n['actions'])
assert set(('persistent', 'timestamp', 'severity', 'default_action')).issubset(n['metadata'])
assert isinstance(n, dict)
def test_get_notification_force_media_type_failure(volatile_dir, config, dummy_notification):
"""Try to force non-existing media type"""
def test_dismiss_notification(config, volatile_dir, simple_plugin_dir, simple_plugin, template_simple, dummy_notification):
f = volatile_dir.join('8c3ad32ca1774eed985604cc1a328773.json')
f.write(dummy_notification)
api = Api(confdict=config)
with pytest.raises(MediaTypeNotAvailableException):
api.get_rendered_notification('8c3ad32ca1774eed985604cc1a328773', 'dummy', 'en', force_media_type=True)
plug = simple_plugin_dir.join('plugin.yml')
plug.write(simple_plugin)
def test_dismiss_notification(volatile_dir, config, dummy_notification):
f = volatile_dir.join('8c3ad32ca1774eed985604cc1a328773.json')
f.write(dummy_notification)
tpl_dir = simple_plugin_dir.mkdir('templates')
tpl_simple = tpl_dir.join('simple.j2')
tpl_simple.write(template_simple)
api = Api(confdict=config)
api.call_action('8c3ad32ca1774eed985604cc1a328773', 'dismiss')
notifications = api.get_notifications()
with pytest.raises(NoSuchNotificationException):
api.get_rendered_notification('8c3ad32ca1774eed985604cc1a328773')
assert isinstance(notifications, dict)
assert len(notifications) == 0
def test_dismiss_nondismisable_notification(persistent_dir, config, persistent_notification):
def test_dismiss_nondismisable_notification(config, persistent_dir, simple_plugin_dir, simple_plugin, template_complex, persistent_notification):
f = persistent_dir.join('8c3ad32ca1774eed985604cc1a328773.json')
f.write(persistent_notification)
plug = simple_plugin_dir.join('plugin.yml')
plug.write(simple_plugin)
tpl_dir = simple_plugin_dir.mkdir('templates')
tpl_complex = tpl_dir.join('complex.j2')
tpl_complex.write(template_complex)
api = Api(confdict=config)
with pytest.raises(NotificationNotDismissibleException):
api.call_action('8c3ad32ca1774eed985604cc1a328773', 'dismiss')
def test_call_action_shortid(volatile_dir, config, dummy_notification):
def test_call_action_shortid(config, volatile_dir, simple_plugin_dir, simple_plugin, template_simple, dummy_notification):
f = volatile_dir.join('8c3ad32ca1774eed985604cc1a328773.json')
f.write(dummy_notification)
plug = simple_plugin_dir.join('plugin.yml')
plug.write(simple_plugin)
tpl_dir = simple_plugin_dir.mkdir('templates')
tpl_simple = tpl_dir.join('simple.j2')
tpl_simple.write(template_simple)
api = Api(confdict=config)
api.call_action('8c3ad32ca1774eed985604cc1a328773', 'dummy')
api.call_action('8c3ad32c', 'dummy')
notifications = api.get_notifications()
with pytest.raises(NoSuchNotificationException):
api.get_rendered_notification('8c3ad32ca1774eed985604cc1a328773')
assert isinstance(notifications, dict)
assert len(notifications) == 0