From 79b7b1dd5c356f8df8165e716100a15da6dd7135 Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Sun, 17 Mar 2019 09:08:52 -0700 Subject: [PATCH] Fix removing profile details (Closes #894) (#899) * Fix removing profile details (Closes #894) * Update tests to properly check setting and removing profile values --- CTFd/schemas/users.py | 20 +++-- CTFd/themes/core/static/js/settings.js | 2 +- CTFd/themes/core/templates/settings.html | 2 +- CTFd/utils/validators/__init__.py | 2 + tests/users/test_settings.py | 104 +++++++++++++++++++++++ tests/users/test_views.py | 28 ------ tests/utils/test_validators.py | 11 +++ 7 files changed, 130 insertions(+), 39 deletions(-) create mode 100644 tests/users/test_settings.py create mode 100644 tests/utils/test_validators.py diff --git a/CTFd/schemas/users.py b/CTFd/schemas/users.py index 9bc82e7f..9b71ef29 100644 --- a/CTFd/schemas/users.py +++ b/CTFd/schemas/users.py @@ -39,10 +39,13 @@ class UserSchema(ma.ModelSchema): website = field_for( Users, 'website', - validate=validate.URL( - error='Websites must be a proper URL starting with http or https', - schemes={'http', 'https'} - ) + validate=[ + # This is a dirty hack to let website accept empty strings so you can remove your website + lambda website: validate.URL( + error='Websites must be a proper URL starting with http or https', + schemes={'http', 'https'} + )(website) if website else True + ] ) country = field_for( Users, @@ -54,9 +57,6 @@ class UserSchema(ma.ModelSchema): password = field_for( Users, 'password', - validate=[ - validate.Length(min=1, error='Passwords must not be empty'), - ] ) @pre_load @@ -123,12 +123,11 @@ class UserSchema(ma.ModelSchema): password = data.get('password') confirm = data.get('confirm') target_user = get_current_user() - user_id = data.get('id') if is_admin(): pass else: - if password and (confirm is None): + if password and (bool(confirm) is False): raise ValidationError('Please confirm your current password', field_names=['confirm']) if password and confirm: @@ -137,6 +136,9 @@ class UserSchema(ma.ModelSchema): return data else: raise ValidationError('Your previous password is incorrect', field_names=['confirm']) + else: + data.pop('password', None) + data.pop('confirm', None) views = { 'user': [ diff --git a/CTFd/themes/core/static/js/settings.js b/CTFd/themes/core/static/js/settings.js index d4d5c8a1..d7d48220 100644 --- a/CTFd/themes/core/static/js/settings.js +++ b/CTFd/themes/core/static/js/settings.js @@ -17,7 +17,7 @@ $(function () { form.submit(function(e){ e.preventDefault(); $('#results').empty(); - var params = $('#user-settings-form').serializeJSON(true); + var params = $('#user-settings-form').serializeJSON(); CTFd.fetch('/api/v1/users/me', { method: 'PATCH', diff --git a/CTFd/themes/core/templates/settings.html b/CTFd/themes/core/templates/settings.html index 909e061e..c39506d6 100644 --- a/CTFd/themes/core/templates/settings.html +++ b/CTFd/themes/core/templates/settings.html @@ -72,7 +72,7 @@ Country