Files
offline-db/users/tests/test_serializers.py
Barış Keserandgoogle-labs-jules[bot] 4387d28d5e fix: Add username XSS pattern validation (#306)
Added a `validate_username` method to `UserProfileUpdateSerializer` to
ensure usernames only contain alphanumeric characters, hyphens, and
underscores (`^[\w-]+$`).

Updated the development plan to mark this issue as resolved.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
2026-03-19 13:36:47 +00:00

22 lines
1018 B
Python

from django.test import TestCase
from users.serializers import UserProfileUpdateSerializer
from users.models import User
class UserProfileUpdateSerializerTest(TestCase):
def test_username_xss_validation(self):
user = User.objects.create(username="testuser")
serializer = UserProfileUpdateSerializer(user, data={'username': '<script>alert(1)</script>'})
self.assertFalse(serializer.is_valid())
self.assertIn('username', serializer.errors)
def test_username_xss_validation_spaces(self):
user = User.objects.create(username="testuser2")
serializer = UserProfileUpdateSerializer(user, data={'username': 'test user'})
self.assertFalse(serializer.is_valid())
self.assertIn('username', serializer.errors)
def test_username_xss_validation_valid(self):
user = User.objects.create(username="testuser3")
serializer = UserProfileUpdateSerializer(user, data={'username': 'test-user_123'})
self.assertTrue(serializer.is_valid())