-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* update username validator * lint fix
- Loading branch information
Showing
5 changed files
with
92 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Generated by Django 4.2.8 on 2024-01-31 14:00 | ||
|
||
from django.db import migrations, models | ||
|
||
import care.utils.models.validators | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("users", "0013_staff_to_nurse"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="user", | ||
name="username", | ||
field=models.CharField( | ||
error_messages={"unique": "A user with that username already exists."}, | ||
help_text="Username must be 4 to 16 characters long. It may only contain lowercase alphabets, numbers, underscores, hyphens and dots. It shouldn't start or end with underscores, hyphens or dots. It shouldn't contain consecutive underscores, hyphens or dots.", | ||
max_length=150, | ||
unique=True, | ||
validators=[care.utils.models.validators.UsernameValidator()], | ||
verbose_name="username", | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from django.core.exceptions import ValidationError | ||
from django.test import TestCase | ||
|
||
from care.utils.models.validators import UsernameValidator | ||
|
||
|
||
class UsernameValidatorTests(TestCase): | ||
username_validator = UsernameValidator() | ||
|
||
valid_usernames = [ | ||
"user", | ||
"user123", | ||
"user_123", | ||
"user_123-456", | ||
"useruseruseruser", | ||
"11useruseruser11", | ||
] | ||
|
||
invalid_characters = ["user@123", "user#123", "user?123", "user!123"] | ||
|
||
consecutive_characters = ["user__123", "user--123", "user..123", "user__" "..user"] | ||
|
||
invalid_case = ["User", "USER", "uSeR"] | ||
|
||
invalid_length = ["usr", "user12345678901234567890", "useruseruseruseru"] | ||
|
||
def test_valid_usernames(self): | ||
for username in self.valid_usernames: | ||
self.assertIsNone(self.username_validator(username)) | ||
|
||
def test_invalid_characters(self): | ||
for username in self.invalid_characters: | ||
with self.assertRaises(ValidationError): | ||
self.username_validator(username) | ||
|
||
def test_consecutive_characters(self): | ||
for username in self.consecutive_characters: | ||
with self.assertRaises(ValidationError): | ||
self.username_validator(username) | ||
|
||
def test_invalid_case(self): | ||
for username in self.invalid_case: | ||
with self.assertRaises(ValidationError): | ||
self.username_validator(username) | ||
|
||
def test_invalid_length(self): | ||
for username in self.invalid_length: | ||
with self.assertRaises(ValidationError): | ||
self.username_validator(username) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters