|
|
@ -1,12 +1,26 @@ |
|
|
|
''' Tests for ordr2.views.account ''' |
|
|
|
''' Tests for ordr2.views.account ''' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import deform |
|
|
|
import pytest |
|
|
|
import pytest |
|
|
|
|
|
|
|
|
|
|
|
from pyramid.httpexceptions import HTTPFound |
|
|
|
from pyramid.httpexceptions import HTTPFound |
|
|
|
from pyramid.testing import DummyRequest, DummyResource |
|
|
|
from pyramid.testing import DummyRequest, DummyResource |
|
|
|
|
|
|
|
from pyramid_mailer import get_mailer |
|
|
|
|
|
|
|
from webob.multidict import MultiDict |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from .. import app_config, dbsession, get_user, create_users, set_deform_data |
|
|
|
|
|
|
|
|
|
|
|
from .. import app_config, dbsession, get_user, create_users |
|
|
|
|
|
|
|
|
|
|
|
REGISTRATION_FORM_DATA = MultiDict([ |
|
|
|
|
|
|
|
('username', 'AmyMcDonald'), |
|
|
|
|
|
|
|
('first_name', 'Amy'), |
|
|
|
|
|
|
|
('last_name', 'McDonald'), |
|
|
|
|
|
|
|
('email', 'mcdonald@example.com'), |
|
|
|
|
|
|
|
('__start__', 'password:mapping'), |
|
|
|
|
|
|
|
('password', 'Amy'), |
|
|
|
|
|
|
|
('password-confirm', 'Amy'), |
|
|
|
|
|
|
|
('__end__', 'password:mapping'), |
|
|
|
|
|
|
|
]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('rolename', ['user', 'purchaser', 'admin']) |
|
|
|
@pytest.mark.parametrize('rolename', ['user', 'purchaser', 'admin']) |
|
|
@ -83,3 +97,94 @@ def test_logout(app_config): |
|
|
|
|
|
|
|
|
|
|
|
assert isinstance(result, HTTPFound) |
|
|
|
assert isinstance(result, HTTPFound) |
|
|
|
assert result.location == 'http://example.com//' |
|
|
|
assert result.location == 'http://example.com//' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_registration_form(app_config): |
|
|
|
|
|
|
|
''' registration form ''' |
|
|
|
|
|
|
|
from ordr2.views.account import registration_form |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
request = DummyRequest() |
|
|
|
|
|
|
|
context = DummyResource() |
|
|
|
|
|
|
|
result = registration_form(context, request) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert isinstance(result['form'], deform.Form) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_registration_form_processing_ok(dbsession): |
|
|
|
|
|
|
|
''' registration form processing with valid data''' |
|
|
|
|
|
|
|
from ordr2.models.account import User, Role, TokenSubject |
|
|
|
|
|
|
|
from ordr2.views.account import registration_form_processing |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
user = get_user('user') # intentionally not added to database |
|
|
|
|
|
|
|
context = DummyResource(model=user) |
|
|
|
|
|
|
|
request = DummyRequest(dbsession=dbsession, context=context) |
|
|
|
|
|
|
|
set_deform_data(request, REGISTRATION_FORM_DATA) |
|
|
|
|
|
|
|
result = registration_form_processing(context, request) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# return value of function call |
|
|
|
|
|
|
|
assert isinstance(result, HTTPFound) |
|
|
|
|
|
|
|
assert result.location == 'http://example.com/registered' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# user should be added to database |
|
|
|
|
|
|
|
user = dbsession.query(User).first() |
|
|
|
|
|
|
|
assert user.username == REGISTRATION_FORM_DATA['username'] |
|
|
|
|
|
|
|
assert user.first_name == REGISTRATION_FORM_DATA['first_name'] |
|
|
|
|
|
|
|
assert user.last_name == REGISTRATION_FORM_DATA['last_name'] |
|
|
|
|
|
|
|
assert user.email == REGISTRATION_FORM_DATA['email'] |
|
|
|
|
|
|
|
assert user.check_password(REGISTRATION_FORM_DATA['password']) |
|
|
|
|
|
|
|
assert user.role == Role.UNVALIDATED |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# a token should be created |
|
|
|
|
|
|
|
token = user.tokens[0] |
|
|
|
|
|
|
|
assert token.subject == TokenSubject.USER_REGISTRATION |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# and a verification email should be sent |
|
|
|
|
|
|
|
# mailer = get_mailer(request.registry) |
|
|
|
|
|
|
|
# last_mail = mailer.outbox[-1] |
|
|
|
|
|
|
|
# assert 'Please verify your email address ' in last_mail.html |
|
|
|
|
|
|
|
# assert 'http://example.com/' + token.hash in last_mail.html |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_registration_form_processing_cancel(app_config): |
|
|
|
|
|
|
|
''' canceling registration form processing ''' |
|
|
|
|
|
|
|
from ordr2.models.account import User, Role, TokenSubject |
|
|
|
|
|
|
|
from ordr2.views.account import registration_form_processing |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
user = get_user('user') # intentionally not added to database |
|
|
|
|
|
|
|
context = DummyResource(model=user) |
|
|
|
|
|
|
|
request = DummyRequest(dbsession=dbsession, context=context) |
|
|
|
|
|
|
|
set_deform_data(request, REGISTRATION_FORM_DATA, {'Cancel': 'Cancel'}) |
|
|
|
|
|
|
|
result = registration_form_processing(context, request) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert isinstance(result, HTTPFound) |
|
|
|
|
|
|
|
assert result.location == 'http://example.com//' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize( |
|
|
|
|
|
|
|
'key,value', [ |
|
|
|
|
|
|
|
('username', ''), |
|
|
|
|
|
|
|
('username', 'TerryGilliam'), |
|
|
|
|
|
|
|
('first_name', ''), |
|
|
|
|
|
|
|
('last_name', ''), |
|
|
|
|
|
|
|
('email', ''), |
|
|
|
|
|
|
|
('email', 'no email'), |
|
|
|
|
|
|
|
('email', 'gilliam@example.com'), |
|
|
|
|
|
|
|
('password', ''), |
|
|
|
|
|
|
|
('password-confirm', ''), |
|
|
|
|
|
|
|
('password-confirm', 'no match') |
|
|
|
|
|
|
|
] |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
def test_registration_form_processing_validation_error(dbsession, key, value): |
|
|
|
|
|
|
|
''' registration form processing with valid data''' |
|
|
|
|
|
|
|
from ordr2.models.account import User, Role, TokenSubject |
|
|
|
|
|
|
|
from ordr2.views.account import registration_form_processing |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
admin = get_user('user') |
|
|
|
|
|
|
|
dbsession.add(admin) |
|
|
|
|
|
|
|
context = DummyResource(model=get_user('admin')) |
|
|
|
|
|
|
|
request = DummyRequest(dbsession=dbsession, context=context) |
|
|
|
|
|
|
|
set_deform_data(request, REGISTRATION_FORM_DATA, {key: value}) |
|
|
|
|
|
|
|
result = registration_form_processing(context, request) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# return value of function call |
|
|
|
|
|
|
|
assert isinstance(result['form'], deform.Form) |
|
|
|