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
4 changed files with 16 additions and 13 deletions
Showing only changes of commit f72b6c217c - Show all commits

View File

@ -6,8 +6,6 @@ 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')
@ -36,11 +34,3 @@ 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

View File

@ -6,6 +6,7 @@ from django.urls import reverse
from apitokens.models import UserToken
from common.tests.factories.users import UserFactory
from common.tests.utils import create_user_token
class UserTokenTest(TestCase):
@ -44,7 +45,7 @@ class UserTokenTest(TestCase):
self.assertNotContains(response, token_key)
def test_list_page_does_not_display_full_token_value(self):
token, token_key = UserToken.create_with_token(user=self.user, name='Test Token')
token, token_key = create_user_token(user=self.user, name='Test Token')
response = self.client.get(reverse('apitokens:list'))
self.assertContains(response, str(token.token_prefix))

View File

@ -1,9 +1,13 @@
import itertools
from typing import Tuple
import django.urls as urls
from django.utils.functional import cached_property
from django.utils.regex_helper import normalize
from apitokens.models import UserToken
try: # Django 2.0
url_resolver_types = (urls.URLResolver,)
DJANGO_2 = True
@ -109,3 +113,11 @@ class CheckFilePropertiesMixin:
self.assertEqual(file.original_name, kwargs.get('original_name'))
if 'size_bytes' in kwargs:
self.assertEqual(file.size_bytes, kwargs.get('size_bytes'))
def create_user_token(*args, **kwargs) -> Tuple['UserToken', str]:
token_key = UserToken.generate_token_key()
kwargs['token_hash'] = UserToken.generate_hash(token_key)
kwargs['token_prefix'] = UserToken.generate_token_prefix(token_key)
token = UserToken.objects.create(*args, **kwargs)
return token, token_key

View File

@ -6,9 +6,9 @@ from rest_framework.test import APITestCase, APIClient
from common.tests.factories.users import UserFactory
from common.tests.factories.extensions import create_approved_version
from common.tests.utils import create_user_token
from extensions.models import Version
from apitokens.models import UserToken
TEST_FILES_DIR = Path(__file__).resolve().parent / 'files'
@ -17,7 +17,7 @@ TEST_FILES_DIR = Path(__file__).resolve().parent / 'files'
class VersionUploadAPITest(APITestCase):
dfelinto marked this conversation as resolved Outdated

should include a test for a non-authenticated request

should include a test for a non-authenticated request

returning 403 is ok: maybe not 100% correct from a purist perspective, but it serves the purpose

returning 403 is ok: maybe not 100% correct from a purist perspective, but it serves the purpose
def setUp(self):
self.user = UserFactory()
self.token, self.token_key = UserToken.create_with_token(user=self.user)
self.token, self.token_key = create_user_token(user=self.user)
self.client = APIClient()
self.version = create_approved_version(