Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate profile form fields #4910

Merged
merged 6 commits into from
Dec 3, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions readthedocs/core/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@


class UserProfileForm(forms.ModelForm):
first_name = CharField(label=_('First name'), required=False)
last_name = CharField(label=_('Last name'), required=False)
first_name = CharField(label=_('First name'), required=False, max_length=30)
last_name = CharField(label=_('Last name'), required=False, max_length=30)

class Meta(object):
model = UserProfile
Expand All @@ -34,18 +34,6 @@ def __init__(self, *args, **kwargs):
except AttributeError:
pass

def clean_first_name(self):
first_name = self.cleaned_data.get('first_name')
if not len(first_name) <= 30:
raise forms.ValidationError('Ensure that first name has at most 30 characters.')
return first_name

def clean_last_name(self):
last_name = self.cleaned_data.get('last_name')
if not len(last_name) <= 30:
raise forms.ValidationError('Ensure that last name has at most 30 characters.')
return last_name

def save(self, commit=True):
first_name = self.cleaned_data.pop('first_name', None)
last_name = self.cleaned_data.pop('last_name', None)
Expand Down
17 changes: 0 additions & 17 deletions readthedocs/rtd_tests/tests/test_profile_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,6 @@ def test_edit_profile(self):
self.assertEqual(self.user.last_name, 'Docs')
self.assertEqual(self.user.profile.homepage, 'readthedocs.org')

def test_edit_profile_with_invalid_values(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should keep the tests

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added the tests.
Just one doubt, max_length is already well-tested feature of django, then what was the need for the these tests ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be sure we don't break this in the future

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay...
Thanks..!!

resp = self.client.get(
reverse('profiles_profile_edit'),
)
self.assertTrue(resp.status_code, 200)
resp = self.client.post(
reverse('profiles_profile_edit'),
data={
'first_name': 'this-is-first_name-whose-length-is-greater-than-30',
'last_name': 'this-is-last_name-whose-length-is-greater-than-30',
'homepage': 'readthedocs.org',
}
)

self.assertFormError(resp, form='form', field='first_name', errors='Ensure that first name has at most 30 characters.')
self.assertFormError(resp, form='form', field='last_name', errors='Ensure that last name has at most 30 characters.')

def test_delete_account(self):
resp = self.client.get(
reverse('delete_account')
Expand Down