mirror of
https://github.com/barkeser2002/offline-db.git
synced 2026-09-25 05:20:06 +03:00
Implemented task 1.3 from the development plan. Overridden the `username` field on the custom `User` model to use a `RegexValidator` restricting usernames to alphanumeric characters, underscores, and hyphens (`^[\w-]+$`). This effectively mitigates potential XSS attacks or injection vulnerabilities via the username field. Updated the field's `help_text` to accurately reflect the restrictions. Added full test coverage for valid and invalid username inputs in `users/tests/test_validation.py`. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
28 lines
1.2 KiB
Python
28 lines
1.2 KiB
Python
from django.test import TestCase
|
|
from django.core.exceptions import ValidationError
|
|
from users.models import User
|
|
|
|
class UserValidationTest(TestCase):
|
|
def test_valid_username(self):
|
|
user = User(username='valid_user-123', password='password123')
|
|
user.full_clean() # Should not raise ValidationError
|
|
self.assertEqual(user.username, 'valid_user-123')
|
|
|
|
def test_invalid_username_special_chars(self):
|
|
user = User(username='invalid@user!', password='password123')
|
|
with self.assertRaises(ValidationError) as context:
|
|
user.full_clean()
|
|
self.assertIn('username', context.exception.message_dict)
|
|
|
|
def test_invalid_username_spaces(self):
|
|
user = User(username='invalid user', password='password123')
|
|
with self.assertRaises(ValidationError) as context:
|
|
user.full_clean()
|
|
self.assertIn('username', context.exception.message_dict)
|
|
|
|
def test_invalid_username_xss(self):
|
|
user = User(username='<script>alert(1)</script>', password='password123')
|
|
with self.assertRaises(ValidationError) as context:
|
|
user.full_clean()
|
|
self.assertIn('username', context.exception.message_dict)
|