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.
301 lines
10 KiB
301 lines
10 KiB
7 years ago
|
import deform
|
||
|
|
||
|
from pyramid.httpexceptions import HTTPFound
|
||
|
from pyramid.testing import DummyRequest, DummyResource
|
||
|
|
||
7 years ago
|
from ... import ( # noqa: F401
|
||
7 years ago
|
app_config,
|
||
|
dbsession,
|
||
|
get_example_user,
|
||
|
get_post_request
|
||
|
)
|
||
|
|
||
|
|
||
|
def test_settings_form():
|
||
|
''' tests for displaying the settings form '''
|
||
|
from ordr.models.account import Role
|
||
|
from ordr.resources.account import AccountResource
|
||
|
from ordr.schemas.account import SettingsSchema
|
||
|
from ordr.views.account import settings_form
|
||
7 years ago
|
|
||
7 years ago
|
user = get_example_user(Role.USER)
|
||
|
request = DummyRequest(user=user)
|
||
|
parent = DummyResource(request=request)
|
||
|
context = AccountResource(None, parent)
|
||
|
result = settings_form(context, request)
|
||
|
form = result['form']
|
||
7 years ago
|
|
||
7 years ago
|
assert isinstance(form, deform.Form)
|
||
|
assert isinstance(form.schema, SettingsSchema)
|
||
7 years ago
|
|
||
7 years ago
|
|
||
|
def test_settings_form_processing_valid_data(dbsession): # noqa: F811
|
||
|
''' tests for processing the settings form
|
||
7 years ago
|
|
||
7 years ago
|
The data is valid, but no email change requested
|
||
|
'''
|
||
|
from ordr.models.account import Role, Token, User
|
||
|
from ordr.resources.account import AccountResource
|
||
|
from ordr.views.account import settings_form_processing
|
||
7 years ago
|
|
||
7 years ago
|
data = {
|
||
|
'username': 'TerryG',
|
||
|
'first_name': 'Amy',
|
||
|
'last_name': 'McDonald',
|
||
|
'email': 'gilliam@example.com',
|
||
|
'confirmation': 'Terry',
|
||
|
'change': 'Change Settings'
|
||
|
}
|
||
7 years ago
|
|
||
7 years ago
|
user = get_example_user(Role.USER)
|
||
|
dbsession.add(user)
|
||
|
dbsession.flush()
|
||
|
request = get_post_request(data=data, dbsession=dbsession, user=user)
|
||
|
parent = DummyResource(request=request)
|
||
|
context = AccountResource(None, parent)
|
||
|
request.context = context
|
||
|
result = settings_form_processing(context, request)
|
||
7 years ago
|
|
||
7 years ago
|
assert isinstance(result, HTTPFound)
|
||
|
assert result.location == 'http://example.com//'
|
||
7 years ago
|
|
||
7 years ago
|
account = dbsession.query(User).first()
|
||
|
assert account.username == 'TerryGilliam'
|
||
|
assert account.first_name == 'Amy'
|
||
|
assert account.last_name == 'McDonald'
|
||
|
assert account.email == 'gilliam@example.com'
|
||
|
assert dbsession.query(Token).count() == 0
|
||
|
|
||
|
|
||
|
def test_settings_form_processing_mail_change(dbsession): # noqa: F811
|
||
|
''' tests for processing the settings form
|
||
7 years ago
|
|
||
7 years ago
|
The data is valid and an email change is requested
|
||
|
'''
|
||
|
from ordr.models.account import Role, Token, TokenSubject, User
|
||
|
from ordr.resources.account import AccountResource
|
||
|
from ordr.views.account import settings_form_processing
|
||
7 years ago
|
|
||
7 years ago
|
data = {
|
||
|
'username': 'TerryG',
|
||
|
'first_name': 'Amy',
|
||
|
'last_name': 'McDonald',
|
||
|
'email': 'amy@example.com',
|
||
|
'confirmation': 'Terry',
|
||
|
'change': 'Change Settings'
|
||
|
}
|
||
7 years ago
|
|
||
7 years ago
|
user = get_example_user(Role.USER)
|
||
|
dbsession.add(user)
|
||
|
request = get_post_request(data=data, dbsession=dbsession, user=user)
|
||
|
parent = DummyResource(request=request)
|
||
|
context = AccountResource(None, parent)
|
||
|
request.context = context
|
||
|
result = settings_form_processing(context, request)
|
||
7 years ago
|
|
||
7 years ago
|
assert isinstance(result, HTTPFound)
|
||
|
assert result.location == 'http://example.com//verify'
|
||
7 years ago
|
|
||
7 years ago
|
account = dbsession.query(User).first()
|
||
|
assert account.username == 'TerryGilliam'
|
||
|
assert account.first_name == 'Amy'
|
||
|
assert account.last_name == 'McDonald'
|
||
|
assert account.email == 'gilliam@example.com'
|
||
7 years ago
|
|
||
7 years ago
|
token = dbsession.query(Token).first()
|
||
|
assert token.subject == TokenSubject.CHANGE_EMAIL
|
||
|
assert token.payload == {'email': 'amy@example.com'}
|
||
|
|
||
|
# 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_settings_form_processing_invalid_data(dbsession): # noqa: F811
|
||
|
''' tests for processing the settings form with invalid data '''
|
||
|
from ordr.models.account import Role
|
||
|
from ordr.resources.account import AccountResource
|
||
|
from ordr.schemas.account import SettingsSchema
|
||
|
from ordr.views.account import settings_form_processing
|
||
7 years ago
|
|
||
7 years ago
|
data = {
|
||
|
'username': 'TerryG',
|
||
|
'first_name': 'Amy',
|
||
|
'last_name': 'McDonald',
|
||
|
'email': 'this is not an email address',
|
||
|
'confirmation': 'Terry',
|
||
|
'change': 'Change Settings'
|
||
|
}
|
||
7 years ago
|
|
||
7 years ago
|
user = get_example_user(Role.USER)
|
||
|
dbsession.add(user)
|
||
|
request = get_post_request(data=data, dbsession=dbsession, user=user)
|
||
|
parent = DummyResource(request=request)
|
||
|
context = AccountResource(None, parent)
|
||
|
request.context = context
|
||
|
result = settings_form_processing(context, request)
|
||
|
form = result['form']
|
||
7 years ago
|
|
||
7 years ago
|
assert isinstance(form, deform.Form)
|
||
|
assert isinstance(form.schema, SettingsSchema)
|
||
|
|
||
|
|
||
|
def test_settings_form_processing_cancel(dbsession): # noqa: F811
|
||
|
''' tests for processing the settings form with invalid data '''
|
||
|
from ordr.models.account import Role, User
|
||
|
from ordr.resources.account import AccountResource
|
||
|
from ordr.views.account import settings_form_processing
|
||
7 years ago
|
|
||
7 years ago
|
data = {
|
||
|
'username': 'TerryG',
|
||
|
'first_name': 'Amy',
|
||
|
'last_name': 'McDonald',
|
||
|
'email': 'this is not an email address',
|
||
|
'confirmation': 'Terry',
|
||
|
'cancel': 'cancel'
|
||
|
}
|
||
7 years ago
|
|
||
7 years ago
|
user = get_example_user(Role.USER)
|
||
|
dbsession.add(user)
|
||
|
request = get_post_request(data=data, dbsession=dbsession, user=user)
|
||
|
parent = DummyResource(request=request)
|
||
|
context = AccountResource(None, parent)
|
||
|
request.context = context
|
||
|
result = settings_form_processing(context, request)
|
||
7 years ago
|
|
||
7 years ago
|
assert isinstance(result, HTTPFound)
|
||
|
assert result.location == 'http://example.com//'
|
||
7 years ago
|
|
||
7 years ago
|
account = dbsession.query(User).first()
|
||
|
assert account.first_name == 'Terry'
|
||
|
|
||
|
|
||
7 years ago
|
def test_verify_email_change(dbsession): # noqa: F811
|
||
7 years ago
|
''' tests for processing the change password form '''
|
||
|
from ordr.models.account import Role, Token, TokenSubject
|
||
7 years ago
|
from ordr.views.account import verify_email_change
|
||
7 years ago
|
|
||
|
user = get_example_user(Role.USER)
|
||
|
request = DummyRequest(dbsession=dbsession, user=user)
|
||
7 years ago
|
|
||
7 years ago
|
user.issue_token(
|
||
|
request,
|
||
|
TokenSubject.CHANGE_EMAIL,
|
||
|
{'email': 'amy@example.com'}
|
||
|
)
|
||
|
dbsession.add(user)
|
||
|
dbsession.flush()
|
||
|
token = dbsession.query(Token).first()
|
||
|
context = DummyResource(model=token)
|
||
7 years ago
|
|
||
|
result = verify_email_change(context, request)
|
||
7 years ago
|
assert result == {}
|
||
|
assert user.email == 'amy@example.com'
|
||
|
assert dbsession.query(Token).count() == 0
|
||
7 years ago
|
|
||
7 years ago
|
|
||
|
def test_password_form():
|
||
|
''' tests for displaying the change password form '''
|
||
|
from ordr.models.account import Role
|
||
|
from ordr.resources.account import AccountResource
|
||
|
from ordr.schemas.account import ChangePasswordSchema
|
||
|
from ordr.views.account import password_form
|
||
7 years ago
|
|
||
7 years ago
|
user = get_example_user(Role.USER)
|
||
|
request = DummyRequest(user=user)
|
||
|
parent = DummyResource(request=request)
|
||
|
context = AccountResource(None, parent)
|
||
|
result = password_form(context, request)
|
||
|
form = result['form']
|
||
7 years ago
|
|
||
7 years ago
|
assert isinstance(form, deform.Form)
|
||
|
assert isinstance(form.schema, ChangePasswordSchema)
|
||
7 years ago
|
|
||
7 years ago
|
|
||
|
def test_password_form_processing_valid(dbsession): # noqa: F811
|
||
|
''' tests for processing the change password form '''
|
||
|
from ordr.models.account import Role
|
||
|
from ordr.resources.account import AccountResource
|
||
|
from ordr.views.account import password_form_processing
|
||
7 years ago
|
|
||
7 years ago
|
data = {
|
||
|
'__start__': 'password:mapping',
|
||
|
'password': 'Make Amy McDonald A Rich Girl Fund',
|
||
|
'password-confirm': 'Make Amy McDonald A Rich Girl Fund',
|
||
|
'__end__': 'password:mapping',
|
||
|
'confirmation': 'Terry',
|
||
|
'change': 'Change Password'
|
||
|
}
|
||
7 years ago
|
|
||
7 years ago
|
user = get_example_user(Role.USER)
|
||
|
request = get_post_request(data=data, user=user)
|
||
|
parent = DummyResource(request=request)
|
||
|
context = AccountResource(None, parent)
|
||
|
result = password_form_processing(context, request)
|
||
7 years ago
|
|
||
7 years ago
|
assert isinstance(result, HTTPFound)
|
||
|
assert result.location == 'http://example.com//changed'
|
||
|
assert not user.check_password('Terry')
|
||
|
assert user.check_password('Make Amy McDonald A Rich Girl Fund')
|
||
|
|
||
|
|
||
|
def test_password_form_processing_invalid(dbsession): # noqa: F811
|
||
|
''' tests for processing the change password form '''
|
||
|
from ordr.models.account import Role
|
||
|
from ordr.resources.account import AccountResource
|
||
|
from ordr.schemas.account import ChangePasswordSchema
|
||
|
from ordr.views.account import password_form_processing
|
||
7 years ago
|
|
||
7 years ago
|
data = {
|
||
|
'__start__': 'password:mapping',
|
||
|
'password': 'Make Amy McDonald A Rich Girl Fund',
|
||
|
'password-confirm': 'Make Amy McDonald A Rich Girl Fund',
|
||
|
'__end__': 'password:mapping',
|
||
|
'confirmation': 'not the right password for confirmation',
|
||
|
'change': 'Change Password'
|
||
|
}
|
||
7 years ago
|
|
||
7 years ago
|
user = get_example_user(Role.USER)
|
||
|
request = get_post_request(data=data, user=user)
|
||
|
parent = DummyResource(request=request)
|
||
|
context = AccountResource(None, parent)
|
||
|
result = password_form_processing(context, request)
|
||
|
form = result['form']
|
||
7 years ago
|
|
||
7 years ago
|
assert isinstance(form, deform.Form)
|
||
|
assert isinstance(form.schema, ChangePasswordSchema)
|
||
|
assert user.check_password('Terry')
|
||
|
|
||
|
|
||
|
def test_password_form_processing_cancel(dbsession): # noqa: F811
|
||
|
''' tests canceling the change password form '''
|
||
|
from ordr.models.account import Role
|
||
|
from ordr.resources.account import AccountResource
|
||
|
from ordr.views.account import password_form_processing
|
||
7 years ago
|
|
||
7 years ago
|
data = {
|
||
|
'__start__': 'password:mapping',
|
||
|
'password': 'Make Amy McDonald A Rich Girl Fund',
|
||
|
'password-confirm': 'Make Amy McDonald A Rich Girl Fund',
|
||
|
'__end__': 'password:mapping',
|
||
|
'confirmation': 'Terry',
|
||
|
'cancel': 'cancel'
|
||
|
}
|
||
7 years ago
|
|
||
7 years ago
|
user = get_example_user(Role.USER)
|
||
|
request = get_post_request(data=data, user=user)
|
||
|
parent = DummyResource(request=request)
|
||
|
context = AccountResource(None, parent)
|
||
|
result = password_form_processing(context, request)
|
||
7 years ago
|
|
||
7 years ago
|
assert isinstance(result, HTTPFound)
|
||
|
assert result.location == 'http://example.com//'
|
||
|
assert user.check_password('Terry')
|
||
7 years ago
|
|
||
7 years ago
|
|
||
|
def test_password_changed():
|
||
|
''' show password has changed message '''
|
||
|
from ordr.views.account import password_changed
|
||
|
result = password_changed(None, None)
|
||
|
assert result == {}
|