Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Kipchirchir Sigei <arapgodsmack@gmail.com>
  • Loading branch information
KipSigei committed Mar 8, 2024
1 parent be55ebf commit 7d8bee7
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions onadata/apps/main/tests/test_user_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from onadata.apps.main.admin import CustomUserChangeForm, CustomUserCreationForm
from onadata.apps.main.tests.test_base import TestBase


class TestUserValidation(TestBase):
def test_custom_user_creation_form_invalid_username(self):
# Try to create a user with a hyphenated username
form_data = {
"username": "john-doe",
"password1": "testpassword",
"password2": "testpassword",
}
form = CustomUserCreationForm(data=form_data)
self.assertFalse(form.is_valid())
self.assertIn("username", form.errors)
errors = form.errors.get("username")[0]
self.assertEqual(str(errors), "Usernames cannot contain hyphens.")

def test_custom_user_change_form_invalid_username(self):
# Try to change a user's username to one with a hyphen
user = self._create_user("bob-user", "bob")
form_data = {"username": "bob-user-1"}
form = CustomUserChangeForm(data=form_data, instance=user)
self.assertFalse(form.is_valid())
self.assertIn("username", form.errors)
errors = form.errors.get("username")[0]
self.assertEqual(str(errors), "Usernames cannot contain hyphens.")

0 comments on commit 7d8bee7

Please sign in to comment.