import deform from pyramid.httpexceptions import HTTPFound from pyramid.testing import DummyRequest, DummyResource from ... import ( # noqa: F401 app_config, dbsession, get_example_user, get_post_request ) REGISTRATION_FORM_DATA = { 'username': 'AmyMcDonald', 'first_name': 'Amy', 'last_name': 'McDonald', 'email': 'amy.mcdonald@example.com', '__start__': 'password:mapping', 'password': 'Make Amy McDonald A Rich Girl Fund', 'password-confirm': 'Make Amy McDonald A Rich Girl Fund', '__end__': 'password:mapping', 'create': 'create account' } def test_registration_form(): ''' test the view for the registration form ''' from ordr.resources.account import RegistrationResource from ordr.schemas.account import RegistrationSchema from ordr.views.account import registration_form request = DummyRequest() parent = DummyResource(request=request) context = RegistrationResource(name=None, parent=parent) result = registration_form(context, None) form = result['form'] assert isinstance(form, deform.Form) assert isinstance(form.schema, RegistrationSchema) def test_registration_form_valid(dbsession): # noqa: F811 ''' test processing the registration form with valid data ''' from ordr.models.account import User, Role, TokenSubject from ordr.resources.account import RegistrationResource from ordr.views.account import registration_form_processing data = REGISTRATION_FORM_DATA.copy() request = get_post_request(data, dbsession=dbsession) parent = DummyResource(request=request) context = RegistrationResource(name=None, parent=parent) result = registration_form_processing(context, request) # return value of function call assert isinstance(result, HTTPFound) assert result.location == 'http://example.com//verify' # user should be added to database user = dbsession.query(User).first() assert user.username == data['username'] assert user.first_name == data['first_name'] assert user.last_name == data['last_name'] assert user.email == data['email'] assert user.check_password(data['password']) assert user.role == Role.UNVALIDATED # a token should be created token = user.tokens[0] assert token.subject == TokenSubject.REGISTRATION # a verification email should be sent # this is tested in the functional test since request.registry.notify # doesn't know about event subscribers in the unittest def test_registration_form_invalid(dbsession): # noqa: F811 ''' test processing registration form with invalid data ''' from ordr.views.account import registration_form_processing from ordr.resources.account import RegistrationResource data = REGISTRATION_FORM_DATA.copy() data['email'] = 'not an email address' request = get_post_request(data, dbsession=dbsession) parent = DummyResource(request=request) context = RegistrationResource(name=None, parent=parent) result = registration_form_processing(context, request) assert result['form'].error is not None def test_registration_form_no_create_button(dbsession): # noqa: F811 ''' test processing registration form, create button not clicked ''' from ordr.views.account import registration_form_processing from ordr.resources.account import RegistrationResource data = REGISTRATION_FORM_DATA.copy() data.pop('create') request = get_post_request(data, dbsession=dbsession) parent = DummyResource(request=request) context = RegistrationResource(name=None, parent=parent) result = registration_form_processing(context, request) assert result.location == 'http://example.com//' def test_registration_verify_email(): ''' test the view displaying that a verifcation email has been sent ''' from ordr.views.account import registration_verify_email result = registration_verify_email(None, None) assert result == {} def test_registration_completed(dbsession): # noqa: F811 ''' test the view for the completed registration process ''' from ordr.models.account import User, Role, Token, TokenSubject from ordr.views.account import registration_completed request = DummyRequest(dbsession=dbsession) user = get_example_user(Role.UNVALIDATED) user.issue_token(request, TokenSubject.REGISTRATION) dbsession.add(user) dbsession.flush() token = user.tokens[0] context = DummyResource(model=token) result = registration_completed(context, request) assert result == {} assert user.role == Role.NEW assert dbsession.query(Token).count() == 0 assert dbsession.query(User).count() == 1