mirror of
https://github.com/cookiecutter/cookiecutter-django.git
synced 2024-11-11 12:17:37 +03:00
Merge pull request #3029 from arnav13081994/update_forms
This commit is contained in:
commit
131b3ca75b
|
@ -1,6 +1,5 @@
|
|||
from django.contrib.auth import forms as admin_forms
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
User = get_user_model()
|
||||
|
@ -12,20 +11,9 @@ class UserChangeForm(admin_forms.UserChangeForm):
|
|||
|
||||
|
||||
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):
|
||||
model = User
|
||||
|
||||
def clean_username(self):
|
||||
username = self.cleaned_data["username"]
|
||||
|
||||
try:
|
||||
User.objects.get(username=username)
|
||||
except User.DoesNotExist:
|
||||
return username
|
||||
|
||||
raise ValidationError(self.error_messages["duplicate_username"])
|
||||
error_messages = {
|
||||
"username": {"unique": _("This username has already been taken.")}
|
||||
}
|
||||
|
|
|
@ -1,40 +1,39 @@
|
|||
"""
|
||||
Module for all Form Tests.
|
||||
"""
|
||||
import pytest
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
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
|
||||
|
||||
|
||||
class TestUserCreationForm:
|
||||
def test_clean_username(self):
|
||||
# A user with proto_user params does not exist yet.
|
||||
proto_user = UserFactory.build()
|
||||
"""
|
||||
Test class for all tests related to the UserCreationForm
|
||||
"""
|
||||
|
||||
form = UserCreationForm(
|
||||
{
|
||||
"username": proto_user.username,
|
||||
"password1": proto_user._password,
|
||||
"password2": proto_user._password,
|
||||
}
|
||||
)
|
||||
def test_username_validation_error_msg(self, user: User):
|
||||
"""
|
||||
Tests UserCreation Form's unique validator functions correctly by testing:
|
||||
1) A new user with an existing username cannot be added.
|
||||
2) Only 1 error is raised by the UserCreation Form
|
||||
3) The desired error message is raised
|
||||
"""
|
||||
|
||||
assert form.is_valid()
|
||||
assert form.clean_username() == proto_user.username
|
||||
|
||||
# Creating a user.
|
||||
form.save()
|
||||
|
||||
# The user with proto_user params already exists,
|
||||
# The user already exists,
|
||||
# hence cannot be created.
|
||||
form = UserCreationForm(
|
||||
{
|
||||
"username": proto_user.username,
|
||||
"password1": proto_user._password,
|
||||
"password2": proto_user._password,
|
||||
"username": user.username,
|
||||
"password1": user.password,
|
||||
"password2": user.password,
|
||||
}
|
||||
)
|
||||
|
||||
assert not form.is_valid()
|
||||
assert len(form.errors) == 1
|
||||
assert "username" in form.errors
|
||||
assert form.errors["username"][0] == _("This username has already been taken.")
|
||||
|
|
Loading…
Reference in New Issue
Block a user