@ -3,7 +3,7 @@ import deform
from pyramid . httpexceptions import HTTPFound
from pyramid . httpexceptions import HTTPFound
from pyramid . testing import DummyRequest , DummyResource
from pyramid . testing import DummyRequest , DummyResource
from . . import ( # noqa: F401
from . . . import ( # noqa: F401
app_config ,
app_config ,
dbsession ,
dbsession ,
get_example_user ,
get_example_user ,
@ -11,44 +11,33 @@ from .. import ( # noqa: F401
)
)
def test_account_redirect ( ) :
''' redirect on root of account resource '''
from ordr . views . account import account
request = DummyRequest ( )
result = account ( None , request )
assert isinstance ( result , HTTPFound )
assert result . location == ' http://example.com// '
def test_settings_form ( ) :
def test_settings_form ( ) :
''' tests for displaying the settings form '''
''' tests for displaying the settings form '''
from ordr . models . account import Role
from ordr . models . account import Role
from ordr . resources . account import AccountResource
from ordr . resources . account import AccountResource
from ordr . schemas . account import SettingsSchema
from ordr . schemas . account import SettingsSchema
from ordr . views . account import settings_form
from ordr . views . account import settings_form
user = get_example_user ( Role . USER )
user = get_example_user ( Role . USER )
request = DummyRequest ( user = user )
request = DummyRequest ( user = user )
parent = DummyResource ( request = request )
parent = DummyResource ( request = request )
context = AccountResource ( None , parent )
context = AccountResource ( None , parent )
result = settings_form ( context , request )
result = settings_form ( context , request )
form = result [ ' form ' ]
form = result [ ' form ' ]
assert isinstance ( form , deform . Form )
assert isinstance ( form , deform . Form )
assert isinstance ( form . schema , SettingsSchema )
assert isinstance ( form . schema , SettingsSchema )
def test_settings_form_processing_valid_data ( dbsession ) : # noqa: F811
def test_settings_form_processing_valid_data ( dbsession ) : # noqa: F811
''' tests for processing the settings form
''' tests for processing the settings form
The data is valid , but no email change requested
The data is valid , but no email change requested
'''
'''
from ordr . models . account import Role , Token , User
from ordr . models . account import Role , Token , User
from ordr . resources . account import AccountResource
from ordr . resources . account import AccountResource
from ordr . views . account import settings_form_processing
from ordr . views . account import settings_form_processing
data = {
data = {
' username ' : ' TerryG ' ,
' username ' : ' TerryG ' ,
' first_name ' : ' Amy ' ,
' first_name ' : ' Amy ' ,
@ -57,7 +46,7 @@ def test_settings_form_processing_valid_data(dbsession): # noqa: F811
' confirmation ' : ' Terry ' ,
' confirmation ' : ' Terry ' ,
' change ' : ' Change Settings '
' change ' : ' Change Settings '
}
}
user = get_example_user ( Role . USER )
user = get_example_user ( Role . USER )
dbsession . add ( user )
dbsession . add ( user )
dbsession . flush ( )
dbsession . flush ( )
@ -66,10 +55,10 @@ def test_settings_form_processing_valid_data(dbsession): # noqa: F811
context = AccountResource ( None , parent )
context = AccountResource ( None , parent )
request . context = context
request . context = context
result = settings_form_processing ( context , request )
result = settings_form_processing ( context , request )
assert isinstance ( result , HTTPFound )
assert isinstance ( result , HTTPFound )
assert result . location == ' http://example.com// '
assert result . location == ' http://example.com// '
account = dbsession . query ( User ) . first ( )
account = dbsession . query ( User ) . first ( )
assert account . username == ' TerryGilliam '
assert account . username == ' TerryGilliam '
assert account . first_name == ' Amy '
assert account . first_name == ' Amy '
@ -80,13 +69,13 @@ def test_settings_form_processing_valid_data(dbsession): # noqa: F811
def test_settings_form_processing_mail_change ( dbsession ) : # noqa: F811
def test_settings_form_processing_mail_change ( dbsession ) : # noqa: F811
''' tests for processing the settings form
''' tests for processing the settings form
The data is valid and an email change is requested
The data is valid and an email change is requested
'''
'''
from ordr . models . account import Role , Token , TokenSubject , User
from ordr . models . account import Role , Token , TokenSubject , User
from ordr . resources . account import AccountResource
from ordr . resources . account import AccountResource
from ordr . views . account import settings_form_processing
from ordr . views . account import settings_form_processing
data = {
data = {
' username ' : ' TerryG ' ,
' username ' : ' TerryG ' ,
' first_name ' : ' Amy ' ,
' first_name ' : ' Amy ' ,
@ -95,7 +84,7 @@ def test_settings_form_processing_mail_change(dbsession): # noqa: F811
' confirmation ' : ' Terry ' ,
' confirmation ' : ' Terry ' ,
' change ' : ' Change Settings '
' change ' : ' Change Settings '
}
}
user = get_example_user ( Role . USER )
user = get_example_user ( Role . USER )
dbsession . add ( user )
dbsession . add ( user )
request = get_post_request ( data = data , dbsession = dbsession , user = user )
request = get_post_request ( data = data , dbsession = dbsession , user = user )
@ -103,16 +92,16 @@ def test_settings_form_processing_mail_change(dbsession): # noqa: F811
context = AccountResource ( None , parent )
context = AccountResource ( None , parent )
request . context = context
request . context = context
result = settings_form_processing ( context , request )
result = settings_form_processing ( context , request )
assert isinstance ( result , HTTPFound )
assert isinstance ( result , HTTPFound )
assert result . location == ' http://example.com//verify '
assert result . location == ' http://example.com//verify '
account = dbsession . query ( User ) . first ( )
account = dbsession . query ( User ) . first ( )
assert account . username == ' TerryGilliam '
assert account . username == ' TerryGilliam '
assert account . first_name == ' Amy '
assert account . first_name == ' Amy '
assert account . last_name == ' McDonald '
assert account . last_name == ' McDonald '
assert account . email == ' gilliam@example.com '
assert account . email == ' gilliam@example.com '
token = dbsession . query ( Token ) . first ( )
token = dbsession . query ( Token ) . first ( )
assert token . subject == TokenSubject . CHANGE_EMAIL
assert token . subject == TokenSubject . CHANGE_EMAIL
assert token . payload == { ' email ' : ' amy@example.com ' }
assert token . payload == { ' email ' : ' amy@example.com ' }
@ -128,7 +117,7 @@ def test_settings_form_processing_invalid_data(dbsession): # noqa: F811
from ordr . resources . account import AccountResource
from ordr . resources . account import AccountResource
from ordr . schemas . account import SettingsSchema
from ordr . schemas . account import SettingsSchema
from ordr . views . account import settings_form_processing
from ordr . views . account import settings_form_processing
data = {
data = {
' username ' : ' TerryG ' ,
' username ' : ' TerryG ' ,
' first_name ' : ' Amy ' ,
' first_name ' : ' Amy ' ,
@ -137,7 +126,7 @@ def test_settings_form_processing_invalid_data(dbsession): # noqa: F811
' confirmation ' : ' Terry ' ,
' confirmation ' : ' Terry ' ,
' change ' : ' Change Settings '
' change ' : ' Change Settings '
}
}
user = get_example_user ( Role . USER )
user = get_example_user ( Role . USER )
dbsession . add ( user )
dbsession . add ( user )
request = get_post_request ( data = data , dbsession = dbsession , user = user )
request = get_post_request ( data = data , dbsession = dbsession , user = user )
@ -146,7 +135,7 @@ def test_settings_form_processing_invalid_data(dbsession): # noqa: F811
request . context = context
request . context = context
result = settings_form_processing ( context , request )
result = settings_form_processing ( context , request )
form = result [ ' form ' ]
form = result [ ' form ' ]
assert isinstance ( form , deform . Form )
assert isinstance ( form , deform . Form )
assert isinstance ( form . schema , SettingsSchema )
assert isinstance ( form . schema , SettingsSchema )
@ -156,7 +145,7 @@ def test_settings_form_processing_cancel(dbsession): # noqa: F811
from ordr . models . account import Role , User
from ordr . models . account import Role , User
from ordr . resources . account import AccountResource
from ordr . resources . account import AccountResource
from ordr . views . account import settings_form_processing
from ordr . views . account import settings_form_processing
data = {
data = {
' username ' : ' TerryG ' ,
' username ' : ' TerryG ' ,
' first_name ' : ' Amy ' ,
' first_name ' : ' Amy ' ,
@ -165,7 +154,7 @@ def test_settings_form_processing_cancel(dbsession): # noqa: F811
' confirmation ' : ' Terry ' ,
' confirmation ' : ' Terry ' ,
' cancel ' : ' cancel '
' cancel ' : ' cancel '
}
}
user = get_example_user ( Role . USER )
user = get_example_user ( Role . USER )
dbsession . add ( user )
dbsession . add ( user )
request = get_post_request ( data = data , dbsession = dbsession , user = user )
request = get_post_request ( data = data , dbsession = dbsession , user = user )
@ -173,22 +162,22 @@ def test_settings_form_processing_cancel(dbsession): # noqa: F811
context = AccountResource ( None , parent )
context = AccountResource ( None , parent )
request . context = context
request . context = context
result = settings_form_processing ( context , request )
result = settings_form_processing ( context , request )
assert isinstance ( result , HTTPFound )
assert isinstance ( result , HTTPFound )
assert result . location == ' http://example.com// '
assert result . location == ' http://example.com// '
account = dbsession . query ( User ) . first ( )
account = dbsession . query ( User ) . first ( )
assert account . first_name == ' Terry '
assert account . first_name == ' Terry '
def test_verify_email ( dbsession ) : # noqa: F811
def test_verify_email_change ( dbsession ) : # noqa: F811
''' tests for processing the change password form '''
''' tests for processing the change password form '''
from ordr . models . account import Role , Token , TokenSubject
from ordr . models . account import Role , Token , TokenSubject
from ordr . views . account import verify_email
from ordr . views . account import verify_email_change
user = get_example_user ( Role . USER )
user = get_example_user ( Role . USER )
request = DummyRequest ( dbsession = dbsession , user = user )
request = DummyRequest ( dbsession = dbsession , user = user )
user . issue_token (
user . issue_token (
request ,
request ,
TokenSubject . CHANGE_EMAIL ,
TokenSubject . CHANGE_EMAIL ,
@ -198,12 +187,12 @@ def test_verify_email(dbsession): # noqa: F811
dbsession . flush ( )
dbsession . flush ( )
token = dbsession . query ( Token ) . first ( )
token = dbsession . query ( Token ) . first ( )
context = DummyResource ( model = token )
context = DummyResource ( model = token )
result = verify_email ( context , request )
result = verify_email_change ( context , request )
assert result == { }
assert result == { }
assert user . email == ' amy@example.com '
assert user . email == ' amy@example.com '
assert dbsession . query ( Token ) . count ( ) == 0
assert dbsession . query ( Token ) . count ( ) == 0
def test_password_form ( ) :
def test_password_form ( ) :
''' tests for displaying the change password form '''
''' tests for displaying the change password form '''
@ -211,24 +200,24 @@ def test_password_form():
from ordr . resources . account import AccountResource
from ordr . resources . account import AccountResource
from ordr . schemas . account import ChangePasswordSchema
from ordr . schemas . account import ChangePasswordSchema
from ordr . views . account import password_form
from ordr . views . account import password_form
user = get_example_user ( Role . USER )
user = get_example_user ( Role . USER )
request = DummyRequest ( user = user )
request = DummyRequest ( user = user )
parent = DummyResource ( request = request )
parent = DummyResource ( request = request )
context = AccountResource ( None , parent )
context = AccountResource ( None , parent )
result = password_form ( context , request )
result = password_form ( context , request )
form = result [ ' form ' ]
form = result [ ' form ' ]
assert isinstance ( form , deform . Form )
assert isinstance ( form , deform . Form )
assert isinstance ( form . schema , ChangePasswordSchema )
assert isinstance ( form . schema , ChangePasswordSchema )
def test_password_form_processing_valid ( dbsession ) : # noqa: F811
def test_password_form_processing_valid ( dbsession ) : # noqa: F811
''' tests for processing the change password form '''
''' tests for processing the change password form '''
from ordr . models . account import Role
from ordr . models . account import Role
from ordr . resources . account import AccountResource
from ordr . resources . account import AccountResource
from ordr . views . account import password_form_processing
from ordr . views . account import password_form_processing
data = {
data = {
' __start__ ' : ' password:mapping ' ,
' __start__ ' : ' password:mapping ' ,
' password ' : ' Make Amy McDonald A Rich Girl Fund ' ,
' password ' : ' Make Amy McDonald A Rich Girl Fund ' ,
@ -237,13 +226,13 @@ def test_password_form_processing_valid(dbsession): # noqa: F811
' confirmation ' : ' Terry ' ,
' confirmation ' : ' Terry ' ,
' change ' : ' Change Password '
' change ' : ' Change Password '
}
}
user = get_example_user ( Role . USER )
user = get_example_user ( Role . USER )
request = get_post_request ( data = data , user = user )
request = get_post_request ( data = data , user = user )
parent = DummyResource ( request = request )
parent = DummyResource ( request = request )
context = AccountResource ( None , parent )
context = AccountResource ( None , parent )
result = password_form_processing ( context , request )
result = password_form_processing ( context , request )
assert isinstance ( result , HTTPFound )
assert isinstance ( result , HTTPFound )
assert result . location == ' http://example.com//changed '
assert result . location == ' http://example.com//changed '
assert not user . check_password ( ' Terry ' )
assert not user . check_password ( ' Terry ' )
@ -256,7 +245,7 @@ def test_password_form_processing_invalid(dbsession): # noqa: F811
from ordr . resources . account import AccountResource
from ordr . resources . account import AccountResource
from ordr . schemas . account import ChangePasswordSchema
from ordr . schemas . account import ChangePasswordSchema
from ordr . views . account import password_form_processing
from ordr . views . account import password_form_processing
data = {
data = {
' __start__ ' : ' password:mapping ' ,
' __start__ ' : ' password:mapping ' ,
' password ' : ' Make Amy McDonald A Rich Girl Fund ' ,
' password ' : ' Make Amy McDonald A Rich Girl Fund ' ,
@ -265,14 +254,14 @@ def test_password_form_processing_invalid(dbsession): # noqa: F811
' confirmation ' : ' not the right password for confirmation ' ,
' confirmation ' : ' not the right password for confirmation ' ,
' change ' : ' Change Password '
' change ' : ' Change Password '
}
}
user = get_example_user ( Role . USER )
user = get_example_user ( Role . USER )
request = get_post_request ( data = data , user = user )
request = get_post_request ( data = data , user = user )
parent = DummyResource ( request = request )
parent = DummyResource ( request = request )
context = AccountResource ( None , parent )
context = AccountResource ( None , parent )
result = password_form_processing ( context , request )
result = password_form_processing ( context , request )
form = result [ ' form ' ]
form = result [ ' form ' ]
assert isinstance ( form , deform . Form )
assert isinstance ( form , deform . Form )
assert isinstance ( form . schema , ChangePasswordSchema )
assert isinstance ( form . schema , ChangePasswordSchema )
assert user . check_password ( ' Terry ' )
assert user . check_password ( ' Terry ' )
@ -283,7 +272,7 @@ def test_password_form_processing_cancel(dbsession): # noqa: F811
from ordr . models . account import Role
from ordr . models . account import Role
from ordr . resources . account import AccountResource
from ordr . resources . account import AccountResource
from ordr . views . account import password_form_processing
from ordr . views . account import password_form_processing
data = {
data = {
' __start__ ' : ' password:mapping ' ,
' __start__ ' : ' password:mapping ' ,
' password ' : ' Make Amy McDonald A Rich Girl Fund ' ,
' password ' : ' Make Amy McDonald A Rich Girl Fund ' ,
@ -292,17 +281,17 @@ def test_password_form_processing_cancel(dbsession): # noqa: F811
' confirmation ' : ' Terry ' ,
' confirmation ' : ' Terry ' ,
' cancel ' : ' cancel '
' cancel ' : ' cancel '
}
}
user = get_example_user ( Role . USER )
user = get_example_user ( Role . USER )
request = get_post_request ( data = data , user = user )
request = get_post_request ( data = data , user = user )
parent = DummyResource ( request = request )
parent = DummyResource ( request = request )
context = AccountResource ( None , parent )
context = AccountResource ( None , parent )
result = password_form_processing ( context , request )
result = password_form_processing ( context , request )
assert isinstance ( result , HTTPFound )
assert isinstance ( result , HTTPFound )
assert result . location == ' http://example.com// '
assert result . location == ' http://example.com// '
assert user . check_password ( ' Terry ' )
assert user . check_password ( ' Terry ' )
def test_password_changed ( ) :
def test_password_changed ( ) :
''' show password has changed message '''
''' show password has changed message '''