''' Tests for the account resources ''' import pytest from pyramid.testing import DummyRequest, DummyResource from .. import app_config, dbsession, get_example_user # noqa: F401 def test_registration_token_acl(): ''' test access controll list for RegistrationTokenResource ''' from pyramid.security import Allow, Everyone, DENY_ALL from ordr.resources.account import RegistrationTokenResource parent = DummyResource(request='request') resource = RegistrationTokenResource('name', parent) assert resource.__acl__() == [(Allow, Everyone, 'view'), DENY_ALL] def test_registration_acl(): ''' test access controll list for RegistrationResource ''' from pyramid.security import Allow, Everyone, DENY_ALL from ordr.resources.account import RegistrationResource parent = DummyResource(request='request') resource = RegistrationResource('a name', parent) assert resource.__acl__() == [(Allow, Everyone, 'view'), DENY_ALL] def test_registration_get_registration_form(): ''' test 'get_registration_form()' method of RegistrationResource ''' from pyramid.security import Allow, Everyone, DENY_ALL from ordr.resources.account import RegistrationResource import deform request = DummyRequest() parent = DummyResource(request=request) resource = RegistrationResource('a name', parent) form = resource.get_registration_form() assert isinstance(form, deform.Form) assert len(form.buttons) == 2 assert form.buttons[0].title == 'Create Account' assert form.buttons[1].title == 'Cancel' def test_registration_getitem_found(dbsession): # noqa: F811 ''' test '__getitem__()' method returns child resource ''' from ordr.models.account import Role, TokenSubject from ordr.resources.account import ( RegistrationResource, RegistrationTokenResource ) request = DummyRequest(dbsession=dbsession) user = get_example_user(Role.NEW) token = user.issue_token(request, TokenSubject.REGISTRATION) dbsession.add(user) dbsession.flush() parent = DummyResource(request=request) resource = RegistrationResource('a name', parent) result = resource[token.hash] assert isinstance(result, RegistrationTokenResource) assert result.__name__ == token.hash assert result.__parent__ == resource assert result.model == token def test_registration_getitem_not_found(dbsession): # noqa: F811 ''' test '__getitem__()' method raises KeyError ''' from ordr.models.account import Role, TokenSubject from ordr.resources.account import RegistrationResource request = DummyRequest(dbsession=dbsession) user = get_example_user(Role.NEW) user.issue_token(request, TokenSubject.REGISTRATION) dbsession.add(user) dbsession.flush() parent = DummyResource(request=request) resource = RegistrationResource('a name', parent) with pytest.raises(KeyError): resource['unknown hash']