mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-13 16:54:47 +03:00
Fix code style: remove trailing whitespace and unused imports
This commit is contained in:
parent
d381901de4
commit
06b0441969
|
@ -29,7 +29,7 @@ class Token(models.Model):
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
Save the token instance.
|
Save the token instance.
|
||||||
|
|
||||||
If no key is provided, generates a cryptographically secure key.
|
If no key is provided, generates a cryptographically secure key.
|
||||||
For existing tokens with cleared keys, regenerates the key.
|
For existing tokens with cleared keys, regenerates the key.
|
||||||
For new tokens, ensures they are inserted as new (not updated).
|
For new tokens, ensures they are inserted as new (not updated).
|
||||||
|
@ -45,10 +45,10 @@ class Token(models.Model):
|
||||||
def generate_key(cls):
|
def generate_key(cls):
|
||||||
"""
|
"""
|
||||||
Generate a cryptographically secure token key.
|
Generate a cryptographically secure token key.
|
||||||
|
|
||||||
Uses secrets.token_hex(20) which provides 40 hexadecimal characters
|
Uses secrets.token_hex(20) which provides 40 hexadecimal characters
|
||||||
(160 bits of entropy) suitable for authentication tokens.
|
(160 bits of entropy) suitable for authentication tokens.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
str: A 40-character hexadecimal string
|
str: A 40-character hexadecimal string
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
import importlib
|
import importlib
|
||||||
import secrets
|
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
|
||||||
|
@ -75,7 +74,7 @@ class AuthTokenTests(TestCase):
|
||||||
"""
|
"""
|
||||||
user2 = User.objects.create_user('user2', 'user2@example.com', 'p')
|
user2 = User.objects.create_user('user2', 'user2@example.com', 'p')
|
||||||
existing_token = Token.objects.create(user=user2)
|
existing_token = Token.objects.create(user=user2)
|
||||||
|
|
||||||
# Try to create another token with the same key
|
# Try to create another token with the same key
|
||||||
with self.assertRaises(IntegrityError):
|
with self.assertRaises(IntegrityError):
|
||||||
Token.objects.create(key=existing_token.key, user=self.user)
|
Token.objects.create(key=existing_token.key, user=self.user)
|
||||||
|
@ -90,11 +89,11 @@ class AuthTokenTests(TestCase):
|
||||||
token = Token(user=self.user)
|
token = Token(user=self.user)
|
||||||
token.key = "" # Explicitly clear the key
|
token.key = "" # Explicitly clear the key
|
||||||
token.save()
|
token.save()
|
||||||
|
|
||||||
# Verify the key was generated
|
# Verify the key was generated
|
||||||
self.assertEqual(len(token.key), 40)
|
self.assertEqual(len(token.key), 40)
|
||||||
self.assertEqual(token.user, self.user)
|
self.assertEqual(token.user, self.user)
|
||||||
|
|
||||||
# Verify it's saved in the database
|
# Verify it's saved in the database
|
||||||
token.refresh_from_db()
|
token.refresh_from_db()
|
||||||
self.assertEqual(len(token.key), 40)
|
self.assertEqual(len(token.key), 40)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user