Merge pull request #3029 from arnav13081994/update_forms

This commit is contained in:
Bruno Alla 2021-02-06 17:00:46 +00:00 committed by GitHub
commit 131b3ca75b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 36 deletions

View File

@ -1,6 +1,5 @@
from django.contrib.auth import forms as admin_forms from django.contrib.auth import forms as admin_forms
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
User = get_user_model() User = get_user_model()
@ -12,20 +11,9 @@ class UserChangeForm(admin_forms.UserChangeForm):
class UserCreationForm(admin_forms.UserCreationForm): class UserCreationForm(admin_forms.UserCreationForm):
error_message = admin_forms.UserCreationForm.error_messages.update(
{"duplicate_username": _("This username has already been taken.")}
)
class Meta(admin_forms.UserCreationForm.Meta): class Meta(admin_forms.UserCreationForm.Meta):
model = User model = User
def clean_username(self): error_messages = {
username = self.cleaned_data["username"] "username": {"unique": _("This username has already been taken.")}
}
try:
User.objects.get(username=username)
except User.DoesNotExist:
return username
raise ValidationError(self.error_messages["duplicate_username"])

View File

@ -1,40 +1,39 @@
"""
Module for all Form Tests.
"""
import pytest import pytest
from django.utils.translation import ugettext_lazy as _
from {{ cookiecutter.project_slug }}.users.forms import UserCreationForm from {{ cookiecutter.project_slug }}.users.forms import UserCreationForm
from {{ cookiecutter.project_slug }}.users.tests.factories import UserFactory from {{ cookiecutter.project_slug }}.users.models import User
pytestmark = pytest.mark.django_db pytestmark = pytest.mark.django_db
class TestUserCreationForm: class TestUserCreationForm:
def test_clean_username(self): """
# A user with proto_user params does not exist yet. Test class for all tests related to the UserCreationForm
proto_user = UserFactory.build() """
form = UserCreationForm( def test_username_validation_error_msg(self, user: User):
{ """
"username": proto_user.username, Tests UserCreation Form's unique validator functions correctly by testing:
"password1": proto_user._password, 1) A new user with an existing username cannot be added.
"password2": proto_user._password, 2) Only 1 error is raised by the UserCreation Form
} 3) The desired error message is raised
) """
assert form.is_valid() # The user already exists,
assert form.clean_username() == proto_user.username
# Creating a user.
form.save()
# The user with proto_user params already exists,
# hence cannot be created. # hence cannot be created.
form = UserCreationForm( form = UserCreationForm(
{ {
"username": proto_user.username, "username": user.username,
"password1": proto_user._password, "password1": user.password,
"password2": proto_user._password, "password2": user.password,
} }
) )
assert not form.is_valid() assert not form.is_valid()
assert len(form.errors) == 1 assert len(form.errors) == 1
assert "username" in form.errors assert "username" in form.errors
assert form.errors["username"][0] == _("This username has already been taken.")