From 2be869262547162b21d528fa87023d13135ef72e Mon Sep 17 00:00:00 2001 From: Holger Frey Date: Mon, 26 Mar 2018 11:15:21 +0200 Subject: [PATCH] added model for account roles --- ordr/models/account.py | 23 +++++++++++++++++++++++ ordr/tests/account.py | 27 +++++++++++++++++++++++++++ ordr/tests/models/meta.py | 6 +++--- ordr/tests/resources/root_resource.py | 8 ++++---- ordr/tests/views/__init__.py | 2 +- 5 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 ordr/models/account.py create mode 100644 ordr/tests/account.py diff --git a/ordr/models/account.py b/ordr/models/account.py new file mode 100644 index 0000000..ad1eb18 --- /dev/null +++ b/ordr/models/account.py @@ -0,0 +1,23 @@ +''' Models for User Accounts and Roles ''' + +import enum + + +class Role(enum.Enum): + ''' roles of user accounts ''' + + UNVALIDATED = 'unvalidated' #: new user, email not validated + NEW = 'new' #: new user, email validated, not active + USER = 'user' #: standard user, may place and view orders + PURCHASER = 'purchaser' #: privileged user, may edit orders + ADMIN = 'admin' #: fully privileged user + INACTIVE = 'inactive' #: user that is no longer active ("deleted") + + @property + def principal(self): + ''' returns the principal identifier of the role ''' + return 'role:' + self.name.lower() + + def __str__(self): + ''' string representation ''' + return self.name.capitalize() diff --git a/ordr/tests/account.py b/ordr/tests/account.py new file mode 100644 index 0000000..068b9cb --- /dev/null +++ b/ordr/tests/account.py @@ -0,0 +1,27 @@ +'UNVALIDATED' +'NEW' +'USER' +'PURCHASER' +'ADMIN' +'INACTIVE' + +import pytest + + +@pytest.mark.parametrize( + 'key,result', [('NEW', 'role:new'), ('USER', 'role:user')] + ) +def test_role_principal(key, result): + from ordr.models.account import Role + subject = Role[key] + assert subject.principal == result + + +@pytest.mark.parametrize( + 'key,result', [('NEW', 'New'), ('USER', 'User')] + ) +def test_role__str__(key, result): + from ordr.models.account import Role + subject = Role[key] + assert str(subject) == result + diff --git a/ordr/tests/models/meta.py b/ordr/tests/models/meta.py index f6fdb58..dbbcf38 100644 --- a/ordr/tests/models/meta.py +++ b/ordr/tests/models/meta.py @@ -5,7 +5,7 @@ import pytest 'value,expected', [ (None, None), ([1, 2, 3], '[1, 2, 3]'), - ({'a':1, 'b':2}, '{"a": 1, "b": 2}'), + ({'a': 1, 'b': 2}, '{"a": 1, "b": 2}'), ] ) def test_json_encoder_bind(value, expected): @@ -18,7 +18,7 @@ def test_json_encoder_bind(value, expected): 'value,expected', [ (None, None), ('[1, 2, 3]', [1, 2, 3]), - ('{"a": 1, "b":2}', {'a':1, 'b':2}), + ('{"a": 1, "b":2}', {'a': 1, 'b': 2}), ] ) def test_json_encoder_result(value, expected): @@ -27,7 +27,7 @@ def test_json_encoder_result(value, expected): assert encoder.process_result_value(value, None) == expected -@pytest.mark.parametrize('value', [None, [1, 2, 3], {'a':1, 'b':2}]) +@pytest.mark.parametrize('value', [None, [1, 2, 3], {'a': 1, 'b': 2}]) def test_json_encoder_bind_and_result(value): from ordr.models.meta import JsonEncoder encoder = JsonEncoder() diff --git a/ordr/tests/resources/root_resource.py b/ordr/tests/resources/root_resource.py index fae6938..7499fd0 100644 --- a/ordr/tests/resources/root_resource.py +++ b/ordr/tests/resources/root_resource.py @@ -2,13 +2,13 @@ def test_root_init(): from ordr.resources import RootResource root = RootResource('request') - assert root.__name__ == None - assert root.__parent__ == None + assert root.__name__ is None + assert root.__parent__ is None assert root.request == 'request' def test_root_acl(): - from pyramid.security import Allow, Everyone, DENY_ALL + from pyramid.security import Allow, Everyone, DENY_ALL from ordr.resources import RootResource root = RootResource(None) - assert root.__acl__() == [(Allow, Everyone, 'view'), DENY_ALL] + assert root.__acl__() == [(Allow, Everyone, 'view'), DENY_ALL] diff --git a/ordr/tests/views/__init__.py b/ordr/tests/views/__init__.py index 8b13789..5bb534f 100644 --- a/ordr/tests/views/__init__.py +++ b/ordr/tests/views/__init__.py @@ -1 +1 @@ - +# package