Refactored test_views to test only the required characteristics of the UserCreationForm.

This commit is contained in:
Arnav Choudhury 2021-02-05 09:47:06 +05:30
parent 4a48b1725e
commit 47768d495f

View File

@ -1,37 +1,33 @@
"""
Module for all Form Tests.
"""
import pytest import pytest
from django.utils.translation import ugettext_lazy as _ 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() """
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( # The user already exists,
{
"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,
# 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,
} }
) )