Browse Source

changed set_deform_data() helper function

The MultiDict.update() method will remove and then append changes instead of manipulating the data in place. The set_deform_data() function was updated to make changes in place
master
Holger Frey 7 years ago
parent
commit
c361f0f71d
  1. 3
      ordr2/views/account.py
  2. 23
      tests/__init__.py
  3. 22
      tests/events.py
  4. 97
      tests/views/account.py

3
ordr2/views/account.py

@ -259,11 +259,12 @@ def reset_password_form_processing(context, request):
'warning' 'warning'
) )
request.session.flash('Your password was changed', 'ok') request.session.flash('Your password was changed', 'success')
return HTTPFound(request.resource_url(context.__parent__, 'login')) return HTTPFound(request.resource_url(context.__parent__, 'login'))
# account settings # account settings
@view_config( @view_config(
context='ordr2:resources.account.AccountResource', context='ordr2:resources.account.AccountResource',
request_method='GET', request_method='GET',

23
tests/__init__.py

@ -101,15 +101,22 @@ def create_users(db):
db.add(user) db.add(user)
def set_deform_data(request, form_data, modifyer=None): def set_deform_data(request, form_data, modifier=None):
''' augments the request to include post data as provided by deform ''' ''' augments the request to include post data as provided by deform '''
post_dict = MultiDict() post_dict = MultiDict([
post_dict['__formid__'] = 'deform' ('__formid__', 'deform'),
post_dict['_charset_'] = 'UTF-8' ('_charset_', 'UTF-8'),
post_dict['csrf_token'] = get_csrf_token(request) ('csrf_token', get_csrf_token(request))
post_dict.update(form_data) ])
if modifyer: for key, value in form_data.items():
post_dict.update(modifyer) post_dict.add(key, value)
if modifier:
# the update() or __setitem__ function of multidict will append
# items with the same key instead of replacing them
items = list(post_dict.items())
modified = [(key, modifier.pop(key, value)) for key, value in items]
post_dict = MultiDict(modified)
post_dict.update(modifier)
request.POST = post_dict request.POST = post_dict

22
tests/events.py

@ -9,15 +9,27 @@ from pyramid_mailer import get_mailer
from . import app_config, get_user from . import app_config, get_user
def test_user_notification_init(): def test_user_notification_init(app_config):
''' test creation of user notification events ''' ''' test creation of user notification events '''
from ordr2.events import UserNotification from ordr2.events import UserNotification
notification = UserNotification('a', 'b', 'c') user = get_user('user')
notification = UserNotification('request', user, 'data')
assert notification.request == 'request'
assert notification.account == user
assert notification.data == 'data'
assert notification.send_to == user.email
def test_user_notification_init_different_send_to(app_config):
''' test creation of user notification events '''
from ordr2.events import UserNotification
user = get_user('user')
notification = UserNotification(None, None, None, 'eric@example.com')
assert notification.request == 'a' assert notification.send_to == 'eric@example.com'
assert notification.account == 'b'
assert notification.data == 'c'
def test_notify_user(app_config): def test_notify_user(app_config):

97
tests/views/account.py

@ -29,6 +29,26 @@ PASSWORD_RESET_FORM_DATA = MultiDict([
('__end__', 'password:mapping'), ('__end__', 'password:mapping'),
]) ])
SETTINGS_FORM_DATA = MultiDict([
('__start__', 'general:mapping'),
('username', 'TerryGilliam'),
('first_name', 'Terry'),
('last_name', 'Gilliam'),
('email', 'gilliam@example.com'),
('role', 'USER'),
('__end__', 'general:mapping'),
('__start__', 'change_password:mapping'),
('__start__', 'new_password:mapping'),
('new_password', 'Amy'),
('new_password-confirm', 'Amy'),
('__end__', 'new_password:mapping'),
('__end__', 'change_password:mapping'),
('__start__', 'confirm_changes:mapping'),
('password', 'Terry'),
('__end__', 'confirm_changes:mapping')
])
@pytest.mark.parametrize('rolename', ['user', 'purchaser', 'admin']) @pytest.mark.parametrize('rolename', ['user', 'purchaser', 'admin'])
def test_account_login_active_users(dbsession, rolename): def test_account_login_active_users(dbsession, rolename):
@ -152,7 +172,7 @@ def test_registration_form_processing_ok(dbsession):
# doesn't know about event subscribers in the unittest # doesn't know about event subscribers in the unittest
def test_registration_form_processing_cancel(app_config): def test_registration_form_processing_cancel(dbsession):
''' canceling registration form processing ''' ''' canceling registration form processing '''
from ordr2.models.account import User, Role, TokenSubject from ordr2.models.account import User, Role, TokenSubject
from ordr2.views.account import registration_form_processing from ordr2.views.account import registration_form_processing
@ -325,7 +345,7 @@ def test_reset_password_form_processing_ok(dbsession):
assert dbsession.query(User).count() == 1 assert dbsession.query(User).count() == 1
def test_reset_password_form_processing_cancel(): def test_reset_password_form_processing_cancel(dbsession):
''' reset password form processing is canceled ''' ''' reset password form processing is canceled '''
from ordr2.views.account import reset_password_form_processing from ordr2.views.account import reset_password_form_processing
@ -354,3 +374,76 @@ def test_reset_password_form_processing_invalid(pw, confirm):
result = reset_password_form_processing(None, request) result = reset_password_form_processing(None, request)
assert isinstance(result['form'], deform.Form) assert isinstance(result['form'], deform.Form)
# settings
def test_settings_form(dbsession):
''' settings form display '''
from ordr2.views.account import settings_form
user = get_user('user')
context = DummyResource(model=user)
request = DummyRequest()
result = settings_form(context, request)
assert isinstance(result['form'], deform.Form)
def test_settings_form_processing_cancel(dbsession):
''' settings form processing is canceled '''
from ordr2.views.account import settings_form_processing
user = get_user('user')
context = DummyResource(model=user)
request = DummyRequest(dbsession=dbsession, POST={'Cancel': 'cancel'})
result = settings_form_processing(None, request)
assert isinstance(result, HTTPFound)
assert result.location == 'http://example.com//'
def test_settings_form_processing_change_name(dbsession):
''' settings form processing change name only '''
from ordr2.views.account import settings_form_processing
user = get_user('user')
context = DummyResource(model=user)
request = DummyRequest(dbsession=dbsession, user=user, context=context)
changes = {
'username': 'AmyMcDonald',
'first_name': 'Amy',
'last_name': 'McDonald',
'role': 'ADMIN',
'new_password': '',
'new_password-confirm': '',
}
set_deform_data(request, SETTINGS_FORM_DATA, changes)
result = settings_form_processing(context, request)
assert isinstance(result, HTTPFound)
assert result.location == 'http://example.com//'
assert user.username == 'TerryGilliam'
assert user.first_name == 'Amy'
assert user.last_name == 'McDonald'
assert user.email == 'gilliam@example.com'
assert user.check_password('Terry')
def test_settings_form_processing_change_password(dbsession):
''' settings form processing change name only '''
from ordr2.views.account import settings_form_processing
user = get_user('user')
context = DummyResource(model=user)
request = DummyRequest(dbsession=dbsession, user=user, context=context)
changes = {
'new_password': 'Amy',
'new_password-confirm': 'Amy',
}
set_deform_data(request, SETTINGS_FORM_DATA, changes)
result = settings_form_processing(context, request)
assert isinstance(result, HTTPFound)
assert result.location == 'http://example.com//'
assert user.check_password('Amy')