You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
132 lines
4.6 KiB
132 lines
4.6 KiB
7 years ago
|
import deform
|
||
7 years ago
|
|
||
|
from pyramid.httpexceptions import HTTPFound
|
||
7 years ago
|
from pyramid.testing import DummyRequest, DummyResource
|
||
7 years ago
|
|
||
7 years ago
|
from ... import ( # noqa: F401
|
||
7 years ago
|
app_config,
|
||
|
dbsession,
|
||
|
get_example_user,
|
||
|
get_post_request
|
||
|
)
|
||
7 years ago
|
|
||
|
|
||
7 years ago
|
REGISTRATION_FORM_DATA = {
|
||
|
'username': 'AmyMcDonald',
|
||
|
'first_name': 'Amy',
|
||
7 years ago
|
'last_name': 'McDonald',
|
||
7 years ago
|
'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():
|
||
7 years ago
|
''' test the view for the registration form '''
|
||
7 years ago
|
from ordr.resources.account import RegistrationResource
|
||
|
from ordr.schemas.account import RegistrationSchema
|
||
7 years ago
|
from ordr.views.account import registration_form
|
||
7 years ago
|
|
||
|
request = DummyRequest()
|
||
7 years ago
|
parent = DummyResource(request=request)
|
||
|
context = RegistrationResource(name=None, parent=parent)
|
||
7 years ago
|
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
|
||
7 years ago
|
''' test processing the registration form with valid data '''
|
||
7 years ago
|
from ordr.models.account import User, Role, TokenSubject
|
||
|
from ordr.resources.account import RegistrationResource
|
||
7 years ago
|
from ordr.views.account import registration_form_processing
|
||
7 years ago
|
|
||
|
data = REGISTRATION_FORM_DATA.copy()
|
||
7 years ago
|
request = get_post_request(data, dbsession=dbsession)
|
||
7 years ago
|
parent = DummyResource(request=request)
|
||
|
context = RegistrationResource(name=None, parent=parent)
|
||
7 years ago
|
result = registration_form_processing(context, request)
|
||
|
|
||
|
# return value of function call
|
||
|
assert isinstance(result, HTTPFound)
|
||
7 years ago
|
assert result.location == 'http://example.com//verify'
|
||
7 years ago
|
|
||
|
# 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
|
||
7 years ago
|
''' test processing registration form with invalid data '''
|
||
7 years ago
|
from ordr.views.account import registration_form_processing
|
||
7 years ago
|
from ordr.resources.account import RegistrationResource
|
||
7 years ago
|
|
||
7 years ago
|
data = REGISTRATION_FORM_DATA.copy()
|
||
|
data['email'] = 'not an email address'
|
||
7 years ago
|
request = get_post_request(data, dbsession=dbsession)
|
||
7 years ago
|
parent = DummyResource(request=request)
|
||
|
context = RegistrationResource(name=None, parent=parent)
|
||
7 years ago
|
result = registration_form_processing(context, request)
|
||
7 years ago
|
|
||
7 years ago
|
assert result['form'].error is not None
|
||
|
|
||
|
|
||
|
def test_registration_form_no_create_button(dbsession): # noqa: F811
|
||
7 years ago
|
''' test processing registration form, create button not clicked '''
|
||
7 years ago
|
from ordr.views.account import registration_form_processing
|
||
7 years ago
|
from ordr.resources.account import RegistrationResource
|
||
7 years ago
|
|
||
7 years ago
|
data = REGISTRATION_FORM_DATA.copy()
|
||
|
data.pop('create')
|
||
7 years ago
|
request = get_post_request(data, dbsession=dbsession)
|
||
7 years ago
|
parent = DummyResource(request=request)
|
||
|
context = RegistrationResource(name=None, parent=parent)
|
||
7 years ago
|
result = registration_form_processing(context, request)
|
||
7 years ago
|
|
||
7 years ago
|
assert result.location == 'http://example.com//'
|
||
7 years ago
|
|
||
|
|
||
7 years ago
|
def test_registration_verify_email():
|
||
7 years ago
|
''' test the view displaying that a verifcation email has been sent '''
|
||
7 years ago
|
from ordr.views.account import registration_verify_email
|
||
|
result = registration_verify_email(None, None)
|
||
7 years ago
|
assert result == {}
|
||
|
|
||
|
|
||
|
def test_registration_completed(dbsession): # noqa: F811
|
||
7 years ago
|
''' test the view for the completed registration process '''
|
||
7 years ago
|
from ordr.models.account import User, Role, Token, TokenSubject
|
||
7 years ago
|
from ordr.views.account import registration_completed
|
||
7 years ago
|
|
||
|
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)
|
||
7 years ago
|
result = registration_completed(context, request)
|
||
7 years ago
|
|
||
|
assert result == {}
|
||
|
assert user.role == Role.NEW
|
||
|
assert dbsession.query(Token).count() == 0
|
||
|
assert dbsession.query(User).count() == 1
|