From 06b04419691661de286fdeb3279eb272d7fc4387 Mon Sep 17 00:00:00 2001 From: Mahdi Date: Thu, 7 Aug 2025 22:42:52 +0330 Subject: [PATCH] Fix code style: remove trailing whitespace and unused imports --- rest_framework/authtoken/models.py | 6 +++--- tests/test_authtoken.py | 7 +++---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/rest_framework/authtoken/models.py b/rest_framework/authtoken/models.py index 2e4040833..1465f048e 100644 --- a/rest_framework/authtoken/models.py +++ b/rest_framework/authtoken/models.py @@ -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 """ diff --git a/tests/test_authtoken.py b/tests/test_authtoken.py index 14f3414f6..f2c591fd5 100644 --- a/tests/test_authtoken.py +++ b/tests/test_authtoken.py @@ -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)