From e0cf956fe2b63fff19710bc0c12a47e795598b2d Mon Sep 17 00:00:00 2001 From: Arnav Choudhury Date: Fri, 29 Jan 2021 17:55:10 +0530 Subject: [PATCH 1/5] Refactored users.forms to make the code more readeable --- .../users/forms.py | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/forms.py b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/forms.py index 7d3a296b..80cd97ae 100644 --- a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/forms.py +++ b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/forms.py @@ -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.")} + } From 0a241a8f75dfee56c0af5b7d8c7eea2f071b8ff3 Mon Sep 17 00:00:00 2001 From: Arnav Choudhury Date: Fri, 29 Jan 2021 22:16:11 +0530 Subject: [PATCH 2/5] Updated test_forms.py to not check whether UserCreationForm has a clean_username() method --- .../{{cookiecutter.project_slug}}/users/tests/test_forms.py | 1 - 1 file changed, 1 deletion(-) diff --git a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_forms.py b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_forms.py index dfa5da52..3b4a1210 100644 --- a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_forms.py +++ b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_forms.py @@ -20,7 +20,6 @@ class TestUserCreationForm: ) assert form.is_valid() - assert form.clean_username() == proto_user.username # Creating a user. form.save() From e8996ef281ada2dab4188f2eff1435d2a694f50c Mon Sep 17 00:00:00 2001 From: Arnav Choudhury Date: Sat, 30 Jan 2021 11:41:16 +0530 Subject: [PATCH 3/5] Updated the test_clean_username test to also test for the custom validation message raised in case username already exists in the db --- .../{{cookiecutter.project_slug}}/users/tests/test_forms.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_forms.py b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_forms.py index 3b4a1210..437829b2 100644 --- a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_forms.py +++ b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_forms.py @@ -1,4 +1,5 @@ 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 @@ -37,3 +38,4 @@ class TestUserCreationForm: 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.") From 47768d495f928196da015cd22c4c9c0cdfd1c429 Mon Sep 17 00:00:00 2001 From: Arnav Choudhury Date: Fri, 5 Feb 2021 09:47:06 +0530 Subject: [PATCH 4/5] Refactored test_views to test only the required characteristics of the UserCreationForm. --- .../users/tests/test_forms.py | 38 +++++++++---------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_forms.py b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_forms.py index 437829b2..617f2cbe 100644 --- a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_forms.py +++ b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_forms.py @@ -1,37 +1,33 @@ +""" +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 + """ + def test_username_validation_error_msg(self, user: User): + """ + Tests UserCreation Form's unique validator functions correctly by testing: + 1) Only 1 error is raised by the UserCreation Form + 2) The desired error message is raised + """ - form = UserCreationForm( - { - "username": proto_user.username, - "password1": proto_user._password, - "password2": proto_user._password, - } - ) - - assert form.is_valid() - - # 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, } ) From 352232f61706c22a1184094fe79798eef174baa3 Mon Sep 17 00:00:00 2001 From: Arnav Choudhury Date: Fri, 5 Feb 2021 09:55:30 +0530 Subject: [PATCH 5/5] Fixed Formatting Issues. --- .../{{cookiecutter.project_slug}}/users/tests/test_forms.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_forms.py b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_forms.py index 617f2cbe..66118f49 100644 --- a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_forms.py +++ b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_forms.py @@ -14,11 +14,13 @@ class TestUserCreationForm: """ Test class for all tests related to the UserCreationForm """ + def test_username_validation_error_msg(self, user: User): """ Tests UserCreation Form's unique validator functions correctly by testing: - 1) Only 1 error is raised by the UserCreation Form - 2) The desired error message is raised + 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 """ # The user already exists,