Fix code style: remove trailing whitespace and unused imports

This commit is contained in:
Mahdi 2025-08-07 22:42:52 +03:30
parent d381901de4
commit 06b0441969
2 changed files with 6 additions and 7 deletions

View File

@ -29,7 +29,7 @@ class Token(models.Model):
def save(self, *args, **kwargs):
"""
Save the token instance.
If no key is provided, generates a cryptographically secure key.
For existing tokens with cleared keys, regenerates the key.
For new tokens, ensures they are inserted as new (not updated).
@ -45,10 +45,10 @@ class Token(models.Model):
def generate_key(cls):
"""
Generate a cryptographically secure token key.
Uses secrets.token_hex(20) which provides 40 hexadecimal characters
(160 bits of entropy) suitable for authentication tokens.
Returns:
str: A 40-character hexadecimal string
"""

View File

@ -1,5 +1,4 @@
import importlib
import secrets
from io import StringIO
from unittest import mock
@ -75,7 +74,7 @@ class AuthTokenTests(TestCase):
"""
user2 = User.objects.create_user('user2', 'user2@example.com', 'p')
existing_token = Token.objects.create(user=user2)
# Try to create another token with the same key
with self.assertRaises(IntegrityError):
Token.objects.create(key=existing_token.key, user=self.user)
@ -90,11 +89,11 @@ class AuthTokenTests(TestCase):
token = Token(user=self.user)
token.key = "" # Explicitly clear the key
token.save()
# Verify the key was generated
self.assertEqual(len(token.key), 40)
self.assertEqual(token.user, self.user)
# Verify it's saved in the database
token.refresh_from_db()
self.assertEqual(len(token.key), 40)