CPI Ordering System (the old version)
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

301 lines
10 KiB

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
)
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
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']
assert isinstance(form, deform.Form)
assert isinstance(form.schema, SettingsSchema)
def test_settings_form_processing_valid_data(dbsession): # noqa: F811
''' tests for processing the settings form
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
data = {
'username': 'TerryG',
'first_name': 'Amy',
'last_name': 'McDonald',
'email': 'gilliam@example.com',
'confirmation': 'Terry',
'change': 'Change Settings'
}
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)
assert isinstance(result, HTTPFound)
assert result.location == 'http://example.com//'
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
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
data = {
'username': 'TerryG',
'first_name': 'Amy',
'last_name': 'McDonald',
'email': 'amy@example.com',
'confirmation': 'Terry',
'change': 'Change Settings'
}
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)
assert isinstance(result, HTTPFound)
assert result.location == 'http://example.com//verify'
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'
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
data = {
'username': 'TerryG',
'first_name': 'Amy',
'last_name': 'McDonald',
'email': 'this is not an email address',
'confirmation': 'Terry',
'change': 'Change Settings'
}
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']
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
data = {
'username': 'TerryG',
'first_name': 'Amy',
'last_name': 'McDonald',
'email': 'this is not an email address',
'confirmation': 'Terry',
'cancel': 'cancel'
}
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)
assert isinstance(result, HTTPFound)
assert result.location == 'http://example.com//'
account = dbsession.query(User).first()
assert account.first_name == 'Terry'
def test_verify_email_change(dbsession): # noqa: F811
''' tests for processing the change password form '''
from ordr.models.account import Role, Token, TokenSubject
from ordr.views.account import verify_email_change
user = get_example_user(Role.USER)
request = DummyRequest(dbsession=dbsession, user=user)
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)
result = verify_email_change(context, request)
assert result == {}
assert user.email == 'amy@example.com'
assert dbsession.query(Token).count() == 0
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
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']
assert isinstance(form, deform.Form)
assert isinstance(form.schema, ChangePasswordSchema)
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
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'
}
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)
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
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'
}
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']
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
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'
}
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)
assert isinstance(result, HTTPFound)
assert result.location == 'http://example.com//'
assert user.check_password('Terry')
def test_password_changed():
''' show password has changed message '''
from ordr.views.account import password_changed
result = password_changed(None, None)
assert result == {}