Change Token.generate_key to a Classmethod (#7502)

* adds classmethod decorator to generate_key and test to confirm change is acceptable

* self -> cls
This commit is contained in:
Charles 2020-09-03 19:51:03 +09:00 committed by GitHub
parent f323049ecc
commit 35c0abf24e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 1 deletions

View File

@ -32,7 +32,8 @@ class Token(models.Model):
self.key = self.generate_key()
return super().save(*args, **kwargs)
def generate_key(self):
@classmethod
def generate_key(cls):
return binascii.hexlify(os.urandom(20)).decode()
def __str__(self):

View File

@ -397,6 +397,10 @@ class TokenAuthTests(BaseTokenAuthTests, TestCase):
key = token.generate_key()
assert isinstance(key, str)
def test_generate_key_accessible_as_classmethod(self):
key = self.model.generate_key()
assert isinstance(key, str)
def test_token_login_json(self):
"""Ensure token login view using JSON POST works."""
client = APIClient(enforce_csrf_checks=True)