From ddabf340390c29bff3dbb39b628acbe53e4270eb Mon Sep 17 00:00:00 2001 From: Daniel Gray Date: Wed, 3 Jul 2024 13:42:01 +0200 Subject: [PATCH] added form tests --- app/accounts/tests.py | 3 - app/accounts/tests/__init__.py | 0 app/accounts/tests/test_signup_form.py | 88 ++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 3 deletions(-) delete mode 100644 app/accounts/tests.py create mode 100644 app/accounts/tests/__init__.py create mode 100644 app/accounts/tests/test_signup_form.py diff --git a/app/accounts/tests.py b/app/accounts/tests.py deleted file mode 100644 index 7ce503c2..00000000 --- a/app/accounts/tests.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.test import TestCase - -# Create your tests here. diff --git a/app/accounts/tests/__init__.py b/app/accounts/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/accounts/tests/test_signup_form.py b/app/accounts/tests/test_signup_form.py new file mode 100644 index 00000000..250c6dcd --- /dev/null +++ b/app/accounts/tests/test_signup_form.py @@ -0,0 +1,88 @@ +import unittest + +from django.test import TestCase + +from accounts.forms import CustomUserCreationForm + + +class CustomUserCreationFormTest(TestCase): + def setUp(self): + self.username = "testuser" + self.email = "testuser@gmail.com" + self.first_name = "Test" + self.last_name = "User" + self.password1 = "sadilar2024" + self.password2 = "sadilar2024" + + def test_valid_data(self): + form = CustomUserCreationForm( + { + "username": self.username, + "email": self.email, + "first_name": self.first_name, + "last_name": self.last_name, + "password1": self.password1, + "password2": self.password2, + } + ) + + self.assertTrue(form.is_valid()) + + def test_blank_data(self): + form = CustomUserCreationForm({}) + self.assertFalse(form.is_valid()) + + self.assertEqual( + form.errors, + { + "username": ["This field is required."], + "email": ["This field is required."], + "first_name": ["This field is required."], + "last_name": ["This field is required."], + "password1": ["This field is required."], + "password2": ["This field is required."], + }, + ) + + def test_invalid_email(self): + form = CustomUserCreationForm( + { + "username": self.username, + "email": "not a valid email", + "first_name": self.first_name, + "last_name": self.last_name, + "password1": self.password1, + "password2": self.password2, + } + ) + self.assertFalse(form.is_valid()) + self.assertEqual( + form.errors, + { + "email": ["Enter a valid email address."], + }, + ) + + def test_passwords_do_not_match(self): + form = CustomUserCreationForm( + { + "username": self.username, + "email": self.email, + "first_name": self.first_name, + "last_name": self.last_name, + "password1": self.password1, + "password2": "wrong password", + } + ) + + self.assertFalse(form.is_valid()) + self.assertEqual( + form.errors, + { + "password2": ["The two password fields didn’t match."], + }, + ) + + +if __name__ == "__main__": + unittest.main()