Skip to content

Commit

Permalink
added form tests
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-gray-tangent committed Jul 3, 2024
1 parent f89f5fc commit ddabf34
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
3 changes: 0 additions & 3 deletions app/accounts/tests.py

This file was deleted.

Empty file added app/accounts/tests/__init__.py
Empty file.
88 changes: 88 additions & 0 deletions app/accounts/tests/test_signup_form.py
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]"
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()

0 comments on commit ddabf34

Please sign in to comment.