Skip to content
Snippets Groups Projects
Commit 15911f8a authored by Jan Mach's avatar Jan Mach
Browse files

Implemented unit tests for 'hawat.blueprints.users' module.

(Redmine issue: #4410,#1017,#3443)
parent 4cbff93f
No related branches found
No related tags found
No related merge requests found
...@@ -58,8 +58,10 @@ ...@@ -58,8 +58,10 @@
{{ macros_form.render_form_errors(form.next.errors) }} {{ macros_form.render_form_errors(form.next.errors) }}
{{ form.next }} {{ form.next }}
{%- if 'csrf_token' in form %}
{{ macros_form.render_form_errors(form.csrf_token.errors) }} {{ macros_form.render_form_errors(form.csrf_token.errors) }}
{{ form.csrf_token }} {{ form.csrf_token }}
{%- endif %}
<div class="btn-toolbar" role="toolbar" aria-label="{{ _('Form submission buttons') }}"> <div class="btn-toolbar" role="toolbar" aria-label="{{ _('Form submission buttons') }}">
<div class="btn-group" role="group"> <div class="btn-group" role="group">
......
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#-------------------------------------------------------------------------------
# This file is part of Mentat system (https://mentat.cesnet.cz/).
#
# Copyright (C) since 2011 CESNET, z.s.p.o (http://www.ces.net/)
# Use of this source is governed by the MIT license, see LICENSE file.
#-------------------------------------------------------------------------------
"""
Unit tests for :py:mod:`hawat.blueprints.users`.
"""
import unittest
import vial.const
import vial.test
import vial.db
from hawat.test import BaseAppTestCase, ItemCreateTestCase
class UsersListTestCase(BaseAppTestCase):
"""Class for testing ``users.list`` endpoint."""
def _attempt_fail(self):
self.assertGetURL(
'/users/list',
403
)
def _attempt_succeed(self):
self.assertGetURL(
'/users/list',
200,
[
b'Show details of user account &quot;user&quot;',
b'Show details of user account &quot;developer&quot;',
b'Show details of user account &quot;maintainer&quot;',
b'Show details of user account &quot;admin&quot;'
]
)
@vial.test.do_as_user_decorator(vial.const.ROLE_USER)
def test_01_as_user(self):
"""Test access as user ``user``."""
self._attempt_fail()
@vial.test.do_as_user_decorator(vial.const.ROLE_DEVELOPER)
def test_02_as_developer(self):
"""Test access as user ``developer``."""
self._attempt_fail()
@vial.test.do_as_user_decorator(vial.const.ROLE_MAINTAINER)
def test_03_as_maintainer(self):
"""Test access as user ``admin``."""
self._attempt_succeed()
@vial.test.do_as_user_decorator(vial.const.ROLE_ADMIN)
def test_04_as_admin(self):
"""Test access as user ``admin``."""
self._attempt_succeed()
class BaseUsersShowTestCase(BaseAppTestCase):
"""Base class for testing ``users.show`` endpoint."""
def _attempt_fail(self, uname):
uid = self.user_id(uname, True)
self.assertGetURL(
'/users/{}/show'.format(uid),
403
)
def _attempt_succeed(self, uname):
with self.app.app_context():
uobj = self.user_get(uname)
uid = uobj.id
ufname = uobj.fullname
self.assertGetURL(
'/users/{}/show'.format(uid),
200,
[
'<h3>{} ({})</h3>'.format(ufname, uname).encode('utf8'),
b'<strong>Account created:</strong>'
]
)
class UsersShowOwnTestCase(BaseUsersShowTestCase):
"""Class for testing ``users.show`` endpoint: access to user`s own accounts."""
@vial.test.do_as_user_decorator(vial.const.ROLE_USER)
def test_01_as_user(self):
"""Test access as user 'user'."""
self._attempt_succeed(vial.const.ROLE_USER)
@vial.test.do_as_user_decorator(vial.const.ROLE_DEVELOPER)
def test_02_as_developer(self):
"""Test access as user 'developer'."""
self._attempt_succeed(vial.const.ROLE_DEVELOPER)
@vial.test.do_as_user_decorator(vial.const.ROLE_MAINTAINER)
def test_03_as_maintainer(self):
"""Test access as user 'maintainer'."""
self._attempt_succeed(vial.const.ROLE_MAINTAINER)
@vial.test.do_as_user_decorator(vial.const.ROLE_ADMIN)
def test_04_as_admin(self):
"""Test access as user 'admin'."""
self._attempt_succeed(vial.const.ROLE_ADMIN)
class UsersShowOtherTestCase(BaseUsersShowTestCase):
"""Class for testing ``users.show`` endpoint: access to other user`s accounts."""
@vial.test.do_as_user_decorator(vial.const.ROLE_USER)
def test_01_as_user_developer(self):
"""Test access to 'developer' account as user 'user'."""
self._attempt_fail(vial.const.ROLE_DEVELOPER)
@vial.test.do_as_user_decorator(vial.const.ROLE_USER)
def test_02_as_user_maintainer(self):
"""Test access to 'maintainer' account as user 'user'."""
self._attempt_fail(vial.const.ROLE_MAINTAINER)
@vial.test.do_as_user_decorator(vial.const.ROLE_USER)
def test_03_as_user_admin(self):
"""Test access to 'admin' account as user 'user'."""
self._attempt_fail(vial.const.ROLE_ADMIN)
#--------------------------------------------------------------------------
@vial.test.do_as_user_decorator(vial.const.ROLE_DEVELOPER)
def test_04_as_developer_user(self):
"""Test access to 'user' account as user 'developer'."""
self._attempt_fail(vial.const.ROLE_USER)
@vial.test.do_as_user_decorator(vial.const.ROLE_DEVELOPER)
def test_05_as_developer_maintainer(self):
"""Test access to 'maintainer' account as user 'developer'."""
self._attempt_fail(vial.const.ROLE_MAINTAINER)
@vial.test.do_as_user_decorator(vial.const.ROLE_DEVELOPER)
def test_06_as_developer_admin(self):
"""Test access to 'admin' account as user 'developer'."""
self._attempt_fail(vial.const.ROLE_ADMIN)
#--------------------------------------------------------------------------
@vial.test.do_as_user_decorator(vial.const.ROLE_MAINTAINER)
def test_07_as_maintainer_user(self):
"""Test access to 'user' account as user 'maintainer'."""
self._attempt_succeed(vial.const.ROLE_USER)
@vial.test.do_as_user_decorator(vial.const.ROLE_MAINTAINER)
def test_08_as_maintainer_developer(self):
"""Test access to 'developer' account as user 'maintainer'."""
self._attempt_succeed(vial.const.ROLE_DEVELOPER)
@vial.test.do_as_user_decorator(vial.const.ROLE_MAINTAINER)
def test_09_as_maintainer_admin(self):
"""Test access to 'maintainer' account as user 'maintainer'."""
self._attempt_succeed(vial.const.ROLE_MAINTAINER)
#--------------------------------------------------------------------------
@vial.test.do_as_user_decorator(vial.const.ROLE_ADMIN)
def test_10_as_admin_user(self):
"""Test access to 'user' account as user 'admin'."""
self._attempt_succeed(vial.const.ROLE_USER)
@vial.test.do_as_user_decorator(vial.const.ROLE_ADMIN)
def test_11_as_admin_developer(self):
"""Test access to 'developer' account as user 'admin'."""
self._attempt_succeed(vial.const.ROLE_DEVELOPER)
@vial.test.do_as_user_decorator(vial.const.ROLE_ADMIN)
def test_12_as_admin_maintainer(self):
"""Test access to 'maintainer' account as user 'admin'."""
self._attempt_succeed(vial.const.ROLE_MAINTAINER)
class UsersCreateTestCase(ItemCreateTestCase):
"""Class for testing ``users.create`` endpoint."""
user_data_fixture = [
('submit', 'Create'),
('login', 'test'),
('fullname', 'Test User'),
('email', 'test.user@domain.org'),
('organization', 'TEST, org.'),
('enabled', True)
]
def _attempt_fail(self):
self.assertGetURL(
'/users/create',
403
)
def _attempt_succeed(self):
self.assertCreate(
'/users/create',
self.user_data_fixture,
[
b'User account <strong>test</strong> was successfully created.'
]
)
@vial.test.do_as_user_decorator(vial.const.ROLE_USER)
def test_01_as_user(self):
"""Test access as user 'user'."""
self._attempt_fail()
@vial.test.do_as_user_decorator(vial.const.ROLE_DEVELOPER)
def test_02_as_developer(self):
"""Test access as user 'developer'."""
self._attempt_fail()
@vial.test.do_as_user_decorator(vial.const.ROLE_MAINTAINER)
def test_03_as_maintainer(self):
"""Test access as user 'maintainer'."""
self._attempt_succeed()
@vial.test.do_as_user_decorator(vial.const.ROLE_ADMIN)
def test_04_as_admin(self):
"""Test access as user 'admin'."""
self._attempt_succeed()
class UsersUpdateOwnTestCase(BaseAppTestCase):
"""Class for testing ``users.update`` endpoint: access to user`s own accounts."""
def _attempt_succeed(self, uname):
uid = self.user_id(uname, True)
self.assertGetURL(
'/users/{}/update'.format(uid),
200,
[
b'Update user account details'
]
)
@vial.test.do_as_user_decorator(vial.const.ROLE_USER)
def test_01_as_user(self):
"""Test access as user 'user'."""
self._attempt_succeed(vial.const.ROLE_USER)
@vial.test.do_as_user_decorator(vial.const.ROLE_DEVELOPER)
def test_02_as_developer(self):
"""Test access as user 'developer'."""
self._attempt_succeed(vial.const.ROLE_DEVELOPER)
@vial.test.do_as_user_decorator(vial.const.ROLE_MAINTAINER)
def test_03_as_maintainer(self):
"""Test access as user 'maintainer'."""
self._attempt_succeed(vial.const.ROLE_MAINTAINER)
@vial.test.do_as_user_decorator(vial.const.ROLE_ADMIN)
def test_04_as_admin(self):
"""Test access as user 'admin'."""
self._attempt_succeed(vial.const.ROLE_ADMIN)
class UsersUpdateOtherTestCase(BaseAppTestCase):
"""Class for testing ``users.update`` endpoint: access to other user`s accounts."""
def _attempt_fail(self, uname):
uid = self.user_id(uname, True)
self.assertGetURL(
'/users/{}/update'.format(uid),
403
)
def _attempt_succeed(self, uname):
uid = self.user_id(uname, True)
self.assertGetURL(
'/users/{}/update'.format(uid),
200,
[
b'Update user account details'
]
)
@vial.test.do_as_user_decorator(vial.const.ROLE_USER)
def test_01_as_user_developer(self):
"""Test access to 'developer' account as user 'user'."""
self._attempt_fail(vial.const.ROLE_DEVELOPER)
@vial.test.do_as_user_decorator(vial.const.ROLE_USER)
def test_02_as_user_maintainer(self):
"""Test access to 'maintainer' account as user 'user'."""
self._attempt_fail(vial.const.ROLE_MAINTAINER)
@vial.test.do_as_user_decorator(vial.const.ROLE_USER)
def test_03_as_user_admin(self):
"""Test access to 'admin' account as user 'user'."""
self._attempt_fail(vial.const.ROLE_ADMIN)
#--------------------------------------------------------------------------
@vial.test.do_as_user_decorator(vial.const.ROLE_DEVELOPER)
def test_04_as_developer_user(self):
"""Test access to 'user' account as user 'developer'."""
self._attempt_fail(vial.const.ROLE_USER)
@vial.test.do_as_user_decorator(vial.const.ROLE_DEVELOPER)
def test_05_as_developer_maintainer(self):
"""Test access to 'maintainer' account as user 'developer'."""
self._attempt_fail(vial.const.ROLE_MAINTAINER)
@vial.test.do_as_user_decorator(vial.const.ROLE_DEVELOPER)
def test_06_as_developer_admin(self):
"""Test access to 'admin' account as user 'developer'."""
self._attempt_fail(vial.const.ROLE_ADMIN)
#--------------------------------------------------------------------------
@vial.test.do_as_user_decorator(vial.const.ROLE_MAINTAINER)
def test_07_as_maintainer_user(self):
"""Test access to 'user' account as user 'maintainer'."""
self._attempt_fail(vial.const.ROLE_USER)
@vial.test.do_as_user_decorator(vial.const.ROLE_MAINTAINER)
def test_08_as_maintainer_developer(self):
"""Test access to 'developer' account as user 'maintainer'."""
self._attempt_fail(vial.const.ROLE_DEVELOPER)
@vial.test.do_as_user_decorator(vial.const.ROLE_MAINTAINER)
def test_09_as_maintainer_admin(self):
"""Test access to 'admin' account as user 'maintainer'."""
self._attempt_fail(vial.const.ROLE_ADMIN)
#--------------------------------------------------------------------------
@vial.test.do_as_user_decorator(vial.const.ROLE_ADMIN)
def test_10_as_admin_user(self):
"""Test access to 'user' account as user 'admin'."""
self._attempt_succeed(vial.const.ROLE_USER)
@vial.test.do_as_user_decorator(vial.const.ROLE_ADMIN)
def test_11_as_admin_developer(self):
"""Test access to 'developer' account as user 'admin'."""
self._attempt_succeed(vial.const.ROLE_DEVELOPER)
@vial.test.do_as_user_decorator(vial.const.ROLE_ADMIN)
def test_12_as_admin_maintainer(self):
"""Test access to 'maintainer' account as user 'admin'."""
self._attempt_succeed(vial.const.ROLE_MAINTAINER)
class UsersEnableDisableTestCase(BaseAppTestCase):
"""Class for testing ``users.enable`` and ``users.disable`` endpoint."""
def _attempt_fail(self, uname):
uid = self.user_id(uname, True)
self.assertGetURL(
'/users/{}/disable'.format(uid),
403
)
self.assertGetURL(
'/users/{}/enable'.format(uid),
403
)
def _attempt_succeed(self, uname):
uid = self.user_id(uname, True)
self.mailbox_monitoring('on')
self.assertGetURL(
'/users/{}/disable'.format(uid),
200,
[
b'Are you really sure you want to disable following item:'
]
)
self.assertPostURL(
'/users/{}/disable'.format(uid),
{
'submit': 'Confirm'
},
200,
[
b'was successfully disabled.'
]
)
self.assertGetURL(
'/users/{}/enable'.format(uid),
200,
[
b'Are you really sure you want to enable following item:'
]
)
self.assertPostURL(
'/users/{}/enable'.format(uid),
{
'submit': 'Confirm'
},
200,
[
b'was successfully enabled.'
]
)
self.assertMailbox(
{
'subject': [
'[{}] Account activation - {}'.format(self.app.config['APPLICATION_NAME'], uname)
],
'sender': [
'root@unittest'
],
'recipients': [
['{}@bogus-domain.org'.format(uname)]
],
'cc': [[]],
'bcc': [['admin@unittest']]
}
)
self.mailbox_monitoring('off')
self.mailbox_clear()
@vial.test.do_as_user_decorator(vial.const.ROLE_USER)
def test_01_as_user(self):
"""Test access as user 'user'."""
for uname in (
vial.const.ROLE_USER,
vial.const.ROLE_DEVELOPER,
vial.const.ROLE_MAINTAINER,
vial.const.ROLE_ADMIN
):
self._attempt_fail(uname)
@vial.test.do_as_user_decorator(vial.const.ROLE_DEVELOPER)
def test_02_as_developer(self):
"""Test access as user 'developer'."""
for uname in (
vial.const.ROLE_USER,
vial.const.ROLE_DEVELOPER,
vial.const.ROLE_MAINTAINER,
vial.const.ROLE_ADMIN
):
self._attempt_fail(uname)
@vial.test.do_as_user_decorator(vial.const.ROLE_MAINTAINER)
def test_03_as_maintainer(self):
"""Test access as user 'maintainer'."""
for uname in (
vial.const.ROLE_USER,
vial.const.ROLE_DEVELOPER,
vial.const.ROLE_ADMIN
):
self._attempt_succeed(uname)
@vial.test.do_as_user_decorator(vial.const.ROLE_ADMIN)
def test_04_as_admin(self):
"""Test access as user 'admin'."""
for uname in (
vial.const.ROLE_USER,
vial.const.ROLE_DEVELOPER,
vial.const.ROLE_MAINTAINER
):
self._attempt_succeed(uname)
class UsersDeleteTestCase(BaseAppTestCase):
"""Class for testing ``users.update`` endpoint."""
def _attempt_fail(self, uname):
uid = self.user_id(uname, True)
self.assertGetURL(
'/users/{}/delete'.format(uid),
403
)
def _attempt_succeed(self, uname):
uid = self.user_id(uname, True)
self.assertGetURL(
'/users/{}/delete'.format(uid),
200,
[
b'Are you really sure you want to permanently remove following item:'
]
)
self.assertPostURL(
'/users/{}/delete'.format(uid),
{
'submit': 'Confirm'
},
200,
[
b'was successfully and permanently deleted.'
]
)
@vial.test.do_as_user_decorator(vial.const.ROLE_USER)
def test_01_as_user(self):
"""Test access as user 'user'."""
for uname in (
vial.const.ROLE_USER,
vial.const.ROLE_DEVELOPER,
vial.const.ROLE_MAINTAINER,
vial.const.ROLE_ADMIN
):
self._attempt_fail(uname)
@vial.test.do_as_user_decorator(vial.const.ROLE_DEVELOPER)
def test_02_as_developer(self):
"""Test access as user 'developer'."""
for uname in (
vial.const.ROLE_USER,
vial.const.ROLE_DEVELOPER,
vial.const.ROLE_MAINTAINER,
vial.const.ROLE_ADMIN
):
self._attempt_fail(uname)
@vial.test.do_as_user_decorator(vial.const.ROLE_MAINTAINER)
def test_03_as_maintainer(self):
"""Test access as user 'maintainer'."""
for uname in (
vial.const.ROLE_USER,
vial.const.ROLE_DEVELOPER,
vial.const.ROLE_MAINTAINER,
vial.const.ROLE_ADMIN
):
self._attempt_fail(uname)
@vial.test.do_as_user_decorator(vial.const.ROLE_ADMIN)
def test_04_as_admin(self):
"""Test access as user 'admin'."""
for uname in (
vial.const.ROLE_USER,
vial.const.ROLE_DEVELOPER,
vial.const.ROLE_MAINTAINER
):
self._attempt_succeed(uname)
#-------------------------------------------------------------------------------
if __name__ == "__main__":
unittest.main()
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
Base library for Hawat unit tests. Base library for Hawat unit tests.
""" """
from vial.test import _config_testapp_vial, VialTestCase, RegistrationVialTestCase from vial.test import _config_testapp_vial, VialTestCase, ItemCreateVialTestCase, RegistrationVialTestCase
from mentat.const import CKEY_CORE_DATABASE, CKEY_CORE_DATABASE_EVENTSTORAGE from mentat.const import CKEY_CORE_DATABASE, CKEY_CORE_DATABASE_EVENTSTORAGE
...@@ -48,6 +48,16 @@ class BaseAppTestCase(VialTestCase): ...@@ -48,6 +48,16 @@ class BaseAppTestCase(VialTestCase):
config_func = _config_testapp_hawat config_func = _config_testapp_hawat
) )
class ItemCreateTestCase(ItemCreateVialTestCase, BaseAppTestCase):
"""
Class for testing :py:class:`hawat.base.HawatApp` application.
"""
def setup_app(self):
return create_app_full(
config_object = 'hawat.config.TestingConfig',
config_func = _config_testapp_hawat
)
class RegistrationTestCase(RegistrationVialTestCase, BaseAppTestCase): class RegistrationTestCase(RegistrationVialTestCase, BaseAppTestCase):
""" """
Class for testing :py:class:`hawat.base.HawatApp` application. Class for testing :py:class:`hawat.base.HawatApp` application.
......
...@@ -115,6 +115,7 @@ class VialTestCase(unittest.TestCase): ...@@ -115,6 +115,7 @@ class VialTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
self.setup_logging() self.setup_logging()
self.mailbox = [] self.mailbox = []
self.fixtures_db = {}
self.app = self.setup_app() self.app = self.setup_app()
self.client = self.app.test_client() self.client = self.app.test_client()
...@@ -276,34 +277,126 @@ class VialTestCase(unittest.TestCase): ...@@ -276,34 +277,126 @@ class VialTestCase(unittest.TestCase):
) )
) )
def user_get(self, user_type): def user_get(self, user_type, with_app_context = False):
""" """
Get given user. Get given user.
""" """
user_model = self.app.get_model(vial.const.MODEL_USER) if not with_app_context:
return vial.db.db_session().query(user_model).filter(user_model.login == user_type).one_or_none() user_model = self.app.get_model(vial.const.MODEL_USER)
return vial.db.db_session().query(user_model).filter(user_model.login == user_type).one_or_none()
with self.app.app_context():
user_model = self.app.get_model(vial.const.MODEL_USER)
return vial.db.db_session().query(user_model).filter(user_model.login == user_type).one_or_none()
def user_save(self, user_object): def user_save(self, user_object, with_app_context = False):
""" """
Update given user. Update given user.
""" """
vial.db.db_session().add(user_object) if not with_app_context:
vial.db.db_session().commit() vial.db.db_session().add(user_object)
vial.db.db_session().commit()
with self.app.app_context():
vial.db.db_session().add(user_object)
vial.db.db_session().commit()
def user_id(self, user_type, with_app_context = False):
"""
Get ID of given user.
"""
if not with_app_context:
uobj = self.user_get(user_type)
return uobj.id
with self.app.app_context():
uobj = self.user_get(user_type)
return uobj.id
def assertGetURL(self, url, status_code = 200, content_checks = None):
"""
Perform GET request and check some default assertions against the response.
"""
response = self.client.get(
url,
follow_redirects = True
)
self.assertEqual(response.status_code, status_code)
if content_checks:
for cch in content_checks:
self.assertTrue(cch in response.data)
return response
def assertPostURL(self, url, data, status_code = 200, content_checks = None):
"""
Perform POST request and check some default assertions against the response.
"""
response = self.client.post(
url,
data = data,
follow_redirects = True
)
self.assertEqual(response.status_code, status_code)
if content_checks:
for cch in content_checks:
self.assertTrue(cch in response.data)
return response
def assertMailbox(self, checklist): # pylint: disable=locally-disabled,invalid-name def assertMailbox(self, checklist): # pylint: disable=locally-disabled,invalid-name
""" """
Check internal mailbox. Check internal mailbox.
""" """
for attr_name in ('subject', 'sender', 'recipients', 'cc', 'bcc', 'body', 'html'): for attr_name in ('subject', 'sender', 'recipients', 'cc', 'bcc', 'body', 'html'):
self.assertEqual( if attr_name in checklist:
list( self.assertEqual(
map( list(
lambda x: getattr(x, attr_name), map(
self.mailbox, lambda x: getattr(x, attr_name),
) self.mailbox,
), )
checklist[attr_name] ),
checklist[attr_name]
)
class ItemCreateVialTestCase(VialTestCase):
"""
Class for testing :py:class:`vial.app.Vial` application item creation views.
"""
maxDiff = None
def assertCreate(self, url, data, content_checks = None): # pylint: disable=locally-disabled,invalid-name
response = response = self.client.get(
url,
follow_redirects = True
)
self.assertEqual(response.status_code, 200)
self.assertTrue(b'<div class="btn-toolbar" role="toolbar" aria-label="Form submission buttons">' in response.data)
for idx, param in enumerate(data):
if idx == len(data) - 1:
break
response = response = self.client.post(
url,
follow_redirects = True,
data = {
i[0]: i[1] for i in data[0:idx+1]
}
) )
self.assertEqual(response.status_code, 200)
self.assertTrue(b'This field is required.' in response.data)
self.assertTrue(b'help-block form-error' in response.data)
response = response = self.client.post(
url,
follow_redirects = True,
data = {
i[0]: i[1] for i in data
}
)
self.assertEqual(response.status_code, 200)
self.assertTrue(b'<div class="alert alert-success alert-dismissible">' in response.data)
if content_checks:
for cch in content_checks:
self.assertTrue(cch in response.data)
return response
class RegistrationVialTestCase(VialTestCase): class RegistrationVialTestCase(VialTestCase):
""" """
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment