API: Upload new version of an extension #138
@ -6,6 +6,8 @@ from django.urls import reverse
|
||||
|
||||
from users.models import User
|
||||
|
||||
from typing import Tuple
|
||||
|
||||
|
||||
class UserToken(models.Model):
|
||||
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='tokens')
|
||||
@ -34,3 +36,11 @@ class UserToken(models.Model):
|
||||
def generate_token_prefix(cls, token_key: str) -> str:
|
||||
token_prefix_length = cls._meta.get_field('token_prefix').max_length
|
||||
return token_key[:token_prefix_length]
|
||||
|
||||
@classmethod
|
||||
def create_with_token(cls, *args, **kwargs) -> Tuple['UserToken', str]:
|
||||
token_key = cls.generate_token_key()
|
||||
kwargs['token_hash'] = cls.generate_hash(token_key)
|
||||
kwargs['token_prefix'] = cls.generate_token_prefix(token_key)
|
||||
token = cls.objects.create(*args, **kwargs)
|
||||
return token, token_key
|
||||
|
@ -44,13 +44,7 @@ class UserTokenTest(TestCase):
|
||||
self.assertNotContains(response, token_key)
|
||||
|
||||
def test_list_page_does_not_display_full_token_value(self):
|
||||
token_key = UserToken.generate_token_key()
|
||||
|
||||
token_prefix = UserToken.generate_token_prefix(token_key)
|
||||
token_hash = UserToken.generate_hash(token_key)
|
||||
token = UserToken.objects.create(
|
||||
user=self.user, name='Test Token', token_prefix=token_prefix, token_hash=token_hash
|
||||
)
|
||||
token, token_key = UserToken.create_with_token(user=self.user, name='Test Token')
|
||||
|
||||
response = self.client.get(reverse('apitokens:list'))
|
||||
self.assertContains(response, str(token.token_prefix))
|
||||
|
@ -17,12 +17,7 @@ TEST_FILES_DIR = Path(__file__).resolve().parent / 'files'
|
||||
class VersionUploadAPITest(APITestCase):
|
||||
def setUp(self):
|
||||
self.user = UserFactory()
|
||||
self.token_key = UserToken.generate_token_key()
|
||||
self.token = UserToken.objects.create(
|
||||
user=self.user,
|
||||
token_prefix=UserToken.generate_token_prefix(self.token_key),
|
||||
token_hash=UserToken.generate_hash(self.token_key),
|
||||
)
|
||||
self.token, self.token_key = UserToken.create_with_token(user=self.user)
|
||||
|
||||
self.client = APIClient()
|
||||
self.version = create_approved_version(
|
||||
|
Loading…
Reference in New Issue
Block a user