Browse Source

updated tests for resources

master
Holger Frey 7 years ago
parent
commit
900f5a3b30
  1. 2
      ordr2/resources/__init__.py
  2. 48
      tests/resources/__init__.py
  3. 39
      tests/resources/account.py
  4. 47
      tests/resources/base.py

2
ordr2/resources/__init__.py

@ -31,7 +31,7 @@ class RootResource(BaseResource): @@ -31,7 +31,7 @@ class RootResource(BaseResource):
def __acl__(self):
''' access controll list for the resource '''
return [(Allow, Everyone, 'view')]
return [(Allow, Everyone, 'view'), DENY_ALL]
def includeme(config):

48
tests/resources/__init__.py

@ -1,49 +1,7 @@ @@ -1,49 +1,7 @@
''' Test package for ordr2.resources '''
import pytest
def test_base_resource_init():
''' test __init__ function of base resource '''
from ordr2.resources import BaseResource, RootResource
root = RootResource('request object')
resource = BaseResource('resource name', root, 'model instance')
assert resource.__name__ == 'resource name'
assert resource.__parent__ == root
assert resource.request == 'request object'
assert resource.model == 'model instance'
def test_base_resource_acl():
''' test __acl__ function of base resource '''
from ordr2.resources import BaseResource, RootResource
from pyramid.security import DENY_ALL
root = RootResource('request object')
resource = BaseResource('resource name', root, 'model instance')
assert resource.__acl__() == [ DENY_ALL ]
@pytest.mark.parametrize(
'segment', [
'known',
pytest.mark.xfail('unknown', raises=KeyError)
]
)
def test_base_resource_getitem(segment):
''' test the __getitem__ function of base resource '''
from ordr2.resources import BaseResource, RootResource
root = RootResource('request object')
root.nodes = {'known': BaseResource}
resource = root[segment]
assert resource.__name__ == 'known'
assert resource.__parent__ == root
assert resource.request == 'request object'
import pytest
def test_root_resource_init():
@ -60,8 +18,8 @@ def test_root_resource_init(): @@ -60,8 +18,8 @@ def test_root_resource_init():
def test_root_resource_acl():
''' test __acl__ function of root resource '''
from ordr2.resources import RootResource
from pyramid.security import Allow, Everyone
from pyramid.security import Allow, Everyone, DENY_ALL
root = RootResource('request object')
assert root.__acl__() == [ (Allow, Everyone, 'view') ]
assert root.__acl__() == [ (Allow, Everyone, 'view'), DENY_ALL ]

39
tests/resources/account.py

@ -0,0 +1,39 @@ @@ -0,0 +1,39 @@
''' Tests for ordr2.resources.account '''
import pytest
def test_account_resource_init():
''' test __init__ function of base resource '''
from pyramid.testing import DummyRequest
from ordr2.resources import AccountResource, RootResource
request = DummyRequest(user='Eric Idle')
root = RootResource(request)
resource = AccountResource('resource name', root)
assert resource.__name__ == 'resource name'
assert resource.__parent__ == root
assert resource.request == request
assert resource.model == request.user
def test_account_resource_acl():
''' test __acl__ function of base resource '''
from pyramid.security import Allow, Authenticated, Deny, Everyone, DENY_ALL
from pyramid.testing import DummyRequest
from ordr2.resources import AccountResource, RootResource
request = DummyRequest(user=None)
root = RootResource(request)
resource = AccountResource('resource name', root)
assert resource.__acl__() == [
(Allow, Everyone, 'login'),
(Allow, Everyone, 'logout'),
(Deny, Authenticated, 'register'),
(Allow, Everyone, 'register'),
(Allow, Authenticated, 'settings'),
DENY_ALL
]

47
tests/resources/base.py

@ -0,0 +1,47 @@ @@ -0,0 +1,47 @@
''' Tests for ordr2.resources.base '''
import pytest
def test_base_resource_init():
''' test __init__ function of base resource '''
from ordr2.resources import BaseResource, RootResource
root = RootResource('request object')
resource = BaseResource('resource name', root, 'model instance')
assert resource.__name__ == 'resource name'
assert resource.__parent__ == root
assert resource.request == 'request object'
assert resource.model == 'model instance'
def test_base_resource_acl():
''' test __acl__ function of base resource '''
from ordr2.resources import BaseResource, RootResource
from pyramid.security import DENY_ALL
root = RootResource('request object')
resource = BaseResource('resource name', root, 'model instance')
with pytest.raises(NotImplementedError):
assert resource.__acl__()
@pytest.mark.parametrize(
'segment', [
'known',
pytest.mark.xfail('unknown', raises=KeyError)
]
)
def test_base_resource_getitem(segment):
''' test the __getitem__ function of base resource '''
from ordr2.resources import BaseResource, RootResource
root = RootResource('request object')
root.nodes = {'known': BaseResource}
resource = root[segment]
assert resource.__name__ == 'known'
assert resource.__parent__ == root
assert resource.request == 'request object'