import pytest import deform from pyramid.httpexceptions import HTTPFound from pyramid.testing import DummyRequest from .. import app_config, dbsession, get_post_request # noqa: F401 REGISTRATION_FORM_DATA = { 'username': 'AmyMcDonald', 'first_name': 'Amy', 'last_name': 'Mc Donald', '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(): from ordr.resources.account import RegistrationResource from ordr.schemas.account import RegistrationSchema from ordr.views.registration import registration_form request = DummyRequest() context = RegistrationResource(request=request, name=None, parent=None) 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 from ordr.models.account import User, Role, TokenSubject from ordr.resources.account import RegistrationResource from ordr.views.registration import registration_form_processing data = REGISTRATION_FORM_DATA.copy() request = get_post_request(dbsession, data) context = RegistrationResource(request=request, name=None, parent=None) 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 from ordr.views.registration 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(dbsession, data) context = RegistrationResource(request=request, name=None, parent=None) result = registration_form_processing(context, request) assert result['form'].error is not None def test_registration_form_no_create_button(dbsession): # noqa: F811 from ordr.views.registration import registration_form_processing from ordr.resources.account import RegistrationResource data = REGISTRATION_FORM_DATA.copy() data.pop('create') request = get_post_request(dbsession, data) context = RegistrationResource(request=request, name=None, parent=None) result = registration_form_processing(context, request) assert result.location == 'http://example.com//'