From c361f0f71d9bbf73377c0a0b40e33ff91fdfe4c0 Mon Sep 17 00:00:00 2001 From: Holger Frey Date: Wed, 25 Oct 2017 09:47:22 +0200 Subject: [PATCH] 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 --- ordr2/views/account.py | 3 +- tests/__init__.py | 23 ++++++---- tests/events.py | 22 +++++++--- tests/views/account.py | 97 +++++++++++++++++++++++++++++++++++++++++- 4 files changed, 129 insertions(+), 16 deletions(-) diff --git a/ordr2/views/account.py b/ordr2/views/account.py index 9c20114..2fd2ea7 100644 --- a/ordr2/views/account.py +++ b/ordr2/views/account.py @@ -259,11 +259,12 @@ def reset_password_form_processing(context, request): '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')) # account settings + @view_config( context='ordr2:resources.account.AccountResource', request_method='GET', diff --git a/tests/__init__.py b/tests/__init__.py index d2ea71c..9dde1d6 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -101,15 +101,22 @@ def create_users(db): 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 ''' - post_dict = MultiDict() - post_dict['__formid__'] = 'deform' - post_dict['_charset_'] = 'UTF-8' - post_dict['csrf_token'] = get_csrf_token(request) - post_dict.update(form_data) - if modifyer: - post_dict.update(modifyer) + post_dict = MultiDict([ + ('__formid__', 'deform'), + ('_charset_', 'UTF-8'), + ('csrf_token', get_csrf_token(request)) + ]) + for key, value in form_data.items(): + 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 diff --git a/tests/events.py b/tests/events.py index 3a03987..7771527 100644 --- a/tests/events.py +++ b/tests/events.py @@ -9,15 +9,27 @@ from pyramid_mailer import get_mailer from . import app_config, get_user -def test_user_notification_init(): +def test_user_notification_init(app_config): ''' test creation of user notification events ''' 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.account == 'b' - assert notification.data == 'c' + assert notification.send_to == 'eric@example.com' def test_notify_user(app_config): diff --git a/tests/views/account.py b/tests/views/account.py index 472b60d..c642e8b 100644 --- a/tests/views/account.py +++ b/tests/views/account.py @@ -29,6 +29,26 @@ PASSWORD_RESET_FORM_DATA = MultiDict([ ('__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']) 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 -def test_registration_form_processing_cancel(app_config): +def test_registration_form_processing_cancel(dbsession): ''' canceling registration form processing ''' from ordr2.models.account import User, Role, TokenSubject 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 -def test_reset_password_form_processing_cancel(): +def test_reset_password_form_processing_cancel(dbsession): ''' reset password form processing is canceled ''' 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) 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')