''' Tests for the account resources ''' import pytest from datetime import datetime, timedelta from pyramid.testing import DummyRequest, DummyResource from .. import app_config, dbsession, get_example_user # noqa: F401 def test_registration_token_acl(): 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(): 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(): 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 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 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'] def test_registration_getitem_expired(dbsession): # noqa: F811 from ordr.models.account import Role, Token, TokenSubject from ordr.resources.account import RegistrationResource request = DummyRequest(dbsession=dbsession) user = get_example_user(Role.NEW) token = user.issue_token(request, TokenSubject.REGISTRATION) token.expires = datetime.utcnow() - timedelta(weeks=1) dbsession.add(user) dbsession.flush() parent = DummyResource(request=request) resource = RegistrationResource('a name', parent) with pytest.raises(KeyError): resource[token.hash] dbsession.flush() assert dbsession.query(Token).count() == 0