API: Upload new version of an extension #138

Merged
Dalai Felinto merged 12 commits from tokens-version-api into main 2024-05-27 16:18:05 +02:00
3 changed files with 12 additions and 13 deletions
Showing only changes of commit 9ac16a529f - Show all commits

View File

@ -6,6 +6,8 @@ from django.urls import reverse
from users.models import User from users.models import User
from typing import Tuple
class UserToken(models.Model): class UserToken(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='tokens') 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: def generate_token_prefix(cls, token_key: str) -> str:
token_prefix_length = cls._meta.get_field('token_prefix').max_length token_prefix_length = cls._meta.get_field('token_prefix').max_length
return token_key[:token_prefix_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

View File

@ -44,13 +44,7 @@ class UserTokenTest(TestCase):
self.assertNotContains(response, token_key) self.assertNotContains(response, token_key)
def test_list_page_does_not_display_full_token_value(self): def test_list_page_does_not_display_full_token_value(self):
token_key = UserToken.generate_token_key() token, token_key = UserToken.create_with_token(user=self.user, name='Test Token')
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
)
response = self.client.get(reverse('apitokens:list')) response = self.client.get(reverse('apitokens:list'))
self.assertContains(response, str(token.token_prefix)) self.assertContains(response, str(token.token_prefix))

View File

@ -17,12 +17,7 @@ TEST_FILES_DIR = Path(__file__).resolve().parent / 'files'
class VersionUploadAPITest(APITestCase): class VersionUploadAPITest(APITestCase):
def setUp(self): def setUp(self):
self.user = UserFactory() self.user = UserFactory()
self.token_key = UserToken.generate_token_key() self.token, self.token_key = UserToken.create_with_token(user=self.user)
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.client = APIClient() self.client = APIClient()
self.version = create_approved_version( self.version = create_approved_version(