Browse Source

added registration resource

rework
Holger Frey 7 years ago
parent
commit
bbf89ad3a5
  1. 15
      ordr/resources/__init__.py
  2. 29
      ordr/resources/account.py
  3. 20
      tests/resources/account.py
  4. 36
      tests/resources/root.py
  5. 14
      tests/resources/root_resource.py

15
ordr/resources/__init__.py

@ -2,6 +2,8 @@
from pyramid.security import Allow, Everyone, DENY_ALL from pyramid.security import Allow, Everyone, DENY_ALL
from .account import RegistrationResource
class RootResource: class RootResource:
''' The root resource for the application ''' The root resource for the application
@ -24,6 +26,19 @@ class RootResource:
''' access controll list for the resource ''' ''' access controll list for the resource '''
return [(Allow, Everyone, 'view'), DENY_ALL] return [(Allow, Everyone, 'view'), DENY_ALL]
def __getitem__(self, key):
''' retruns a child resource
:param str key: name of the child resource
:returns: child resource
:raises: KeyError if child resource is not found
'''
map = {
'registration': RegistrationResource
}
child_class = map[key]
return child_class(request=self.request, name=key, parent=self)
def includeme(config): def includeme(config):
''' '''

29
ordr/resources/account.py

@ -0,0 +1,29 @@
''' Resources (sub) package, used to connect URLs to views '''
from pyramid.security import Allow, Everyone, DENY_ALL
class RegistrationResource:
''' The resource for new user registration
:param pyramid.request.Request request: the current request object
:param str name: the name of the resource
:param parent: the parent resouce
'''
nav_active = 'welcome'
def __init__(self, request, name, parent):
''' Create registration resource
:param pyramid.request.Request request: the current request object
:param str name: the name of the resource
:param parent: the parent resouce
'''
self.request = request
self.__name__ = name
self.__parent__ = parent
def __acl__(self):
''' access controll list for the resource '''
return [(Allow, Everyone, 'view'), DENY_ALL]

20
tests/resources/account.py

@ -0,0 +1,20 @@
''' Tests for the account resources '''
def test_registration_init():
from ordr.resources.account import RegistrationResource
resource = RegistrationResource(
request='some request',
name='a name',
parent='the parent'
)
assert resource.__name__ == 'a name'
assert resource.__parent__ == 'the parent'
assert resource.request == 'some request'
def test_registration_acl():
from pyramid.security import Allow, Everyone, DENY_ALL
from ordr.resources.account import RegistrationResource
resource = RegistrationResource('some request', 'a name', 'the parent')
assert resource.__acl__() == [(Allow, Everyone, 'view'), DENY_ALL]

36
tests/resources/root.py

@ -0,0 +1,36 @@
''' Tests for the root resource '''
import pytest
def test_root_init():
from ordr.resources import RootResource
root = RootResource('request')
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 ordr.resources import RootResource
root = RootResource(None)
assert root.__acl__() == [(Allow, Everyone, 'view'), DENY_ALL]
def test_root_getitem():
from ordr.resources import RootResource
from ordr.resources.account import RegistrationResource
root = RootResource(None)
child = root['registration']
assert isinstance(child, RegistrationResource)
assert child.__name__ == 'registration'
assert child.__parent__ == root
assert child.request == root.request
def test_root_getitem_raises_error():
from ordr.resources import RootResource
root = RootResource(None)
with pytest.raises(KeyError):
root['unknown child name']

14
tests/resources/root_resource.py

@ -1,14 +0,0 @@
def test_root_init():
from ordr.resources import RootResource
root = RootResource('request')
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 ordr.resources import RootResource
root = RootResource(None)
assert root.__acl__() == [(Allow, Everyone, 'view'), DENY_ALL]