Skip to content

Commit

Permalink
Add docstrings to testcases
Browse files Browse the repository at this point in the history
Signed-off-by: Kipchirchir Sigei <arapgodsmack@gmail.com>
  • Loading branch information
KipSigei committed Jun 27, 2024
1 parent 962acc9 commit 33a8fb6
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions onadata/libs/tests/utils/test_password_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,32 @@
from onadata.apps.main.models.password_history import PasswordHistory
from onadata.libs.utils.validators import PreviousPasswordValidator


class PreviousPasswordValidatorTestCase(TestCase):
def test_validator_does_not_raise_valueerror_missing_pk(self):
"""
Test case for the PreviousPasswordValidator class.
Ensures correct behavior of password validation.
"""

def test_missing_pk(self):
"""Validator does not raise ValueError for missing pk"""
# Create a validator instance
validator = PreviousPasswordValidator()

# Create a user instance without saving it to the database
user = User(username='testuser')
user = User(username="testuser")

# Call the validate method and ensure it does not raise a ValueError
try:
validator.validate('somepassword', user=user)
validator.validate("somepassword", user=user)
except ValueError:
self.fail("PreviousPasswordValidator raised ValueError unexpectedly!")

def test_validator_raises_validationerror_for_reused_password(self):
def test_reused_password(self):
"""Test ValidationError exception thrown on reused password"""
# Create and save a user to the database
user = User.objects.create(username='testuser')
user.set_password('oldpassword')
user = User.objects.create(username="testuser")
user.set_password("oldpassword")
user.save()

# Add the old password to password history
Expand All @@ -33,15 +41,17 @@ def test_validator_raises_validationerror_for_reused_password(self):

# Try using an old password
with self.assertRaises(ValidationError) as cm:
validator.validate('oldpassword', user=user)
validator.validate("oldpassword", user=user)

self.assertEqual(
str(cm.exception.message), "You cannot use a previously used password.")
str(cm.exception.message), "You cannot use a previously used password."
)

def test_validator_allows_new_password(self):
def test_allows_new_password(self):
"""Test validator allows new password not used before"""
# Create and save a user to the database
user = User.objects.create(username='testuser')
user.set_password('oldpassword')
user = User.objects.create(username="testuser")
user.set_password("oldpassword")
user.save()

# Add the old password to password history
Expand All @@ -52,6 +62,6 @@ def test_validator_allows_new_password(self):

# Try using a new password
try:
validator.validate('newpassword@123', user=user)
validator.validate("newpassword@123", user=user)
except ValidationError:
self.fail("PreviousPasswordValidator raised ValidationError unexpectedly!")

0 comments on commit 33a8fb6

Please sign in to comment.