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 11 additions and 13 deletions
Showing only changes of commit 8c11df9e5d - Show all commits

View File

@ -26,16 +26,19 @@ class VersionUploadAPITest(APITestCase):
file__user=self.user, file__user=self.user,
) )
self.extension = self.version.extension self.extension = self.version.extension
self.upload_url = reverse('extensions:upload-extension-version')
self.file_path = TEST_FILES_DIR / "amaranth-1.0.8.zip" self.file_path = TEST_FILES_DIR / "amaranth-1.0.8.zip"
@staticmethod
def _get_upload_url(extension_id):
upload_url = reverse('extensions:upload-extension-version', args=(extension_id,))
return upload_url
def test_version_upload_unauthenticated(self): def test_version_upload_unauthenticated(self):
with open(self.file_path, 'rb') as version_file: with open(self.file_path, 'rb') as version_file:
response = self.client.post( response = self.client.post(
self.upload_url, self._get_upload_url(self.extension.extension_id),
{ {
'version_file': version_file, 'version_file': version_file,
'extension_id': self.extension.extension_id,
'release_notes': 'These are the release notes', 'release_notes': 'These are the release notes',
}, },
format='multipart', format='multipart',
@ -51,10 +54,9 @@ class VersionUploadAPITest(APITestCase):
with open(self.file_path, 'rb') as version_file: with open(self.file_path, 'rb') as version_file:
response = self.client.post( response = self.client.post(
self.upload_url, self._get_upload_url(other_extension.extension_id),
{ {
'version_file': version_file, 'version_file': version_file,
'extension_id': other_extension.extension_id,
'release_notes': 'These are the release notes', 'release_notes': 'These are the release notes',
}, },
format='multipart', format='multipart',
@ -71,10 +73,9 @@ class VersionUploadAPITest(APITestCase):
extension_name = 'extension_do_not_exist' extension_name = 'extension_do_not_exist'
with open(self.file_path, 'rb') as version_file: with open(self.file_path, 'rb') as version_file:
response = self.client.post( response = self.client.post(
self.upload_url, self._get_upload_url(extension_name),
{ {
'version_file': version_file, 'version_file': version_file,
'extension_id': extension_name,
'release_notes': 'These are the release notes', 'release_notes': 'These are the release notes',
}, },
format='multipart', format='multipart',
@ -88,10 +89,9 @@ class VersionUploadAPITest(APITestCase):
self.assertEqual(Version.objects.filter(extension=self.extension).count(), 1) self.assertEqual(Version.objects.filter(extension=self.extension).count(), 1)
with open(self.file_path, 'rb') as version_file: with open(self.file_path, 'rb') as version_file:
response = self.client.post( response = self.client.post(
self.upload_url, self._get_upload_url(self.extension.extension_id),
{ {
'version_file': version_file, 'version_file': version_file,
'extension_id': self.extension.extension_id,
'release_notes': 'These are the release notes', 'release_notes': 'These are the release notes',
}, },
format='multipart', format='multipart',

View File

@ -17,7 +17,7 @@ urlpatterns = [
# API # API
path('api/v1/extensions/', api.ExtensionsAPIView.as_view(), name='api'), path('api/v1/extensions/', api.ExtensionsAPIView.as_view(), name='api'),
path( path(
'api/v1/version-upload/', 'api/v1/extensions/<str:extension_id>/versions/new/',
Oleg-Komarov marked this conversation as resolved Outdated

sorry, haven't thought of this before, let's restructure the parameters: make them more in line with REST conventions, and get a nice bonus for logs transparency

the url becomes api/v1/extensions/<extension_id>/versions/new/, and the extension_id in the body is not needed

sorry, haven't thought of this before, let's restructure the parameters: make them more in line with REST conventions, and get a nice bonus for logs transparency the url becomes `api/v1/extensions/<extension_id>/versions/new/`, and the `extension_id` in the body is not needed
api.UploadExtensionVersionView.as_view(), api.UploadExtensionVersionView.as_view(),
name='upload-extension-version', name='upload-extension-version',
), ),

View File

@ -158,7 +158,6 @@ class ExtensionsAPIView(APIView):
class ExtensionVersionSerializer(serializers.Serializer): class ExtensionVersionSerializer(serializers.Serializer):
extension_id = serializers.CharField(max_length=255)
version_file = serializers.FileField() version_file = serializers.FileField()
release_notes = serializers.CharField(max_length=1024, required=False) release_notes = serializers.CharField(max_length=1024, required=False)
@ -170,13 +169,12 @@ class UploadExtensionVersionView(APIView):
request=ExtensionVersionSerializer, request=ExtensionVersionSerializer,
responses={201: 'Extension version uploaded successfully!'}, responses={201: 'Extension version uploaded successfully!'},
) )
def post(self, request, *args, **kwargs): def post(self, request, extension_id, *args, **kwargs):
serializer = ExtensionVersionSerializer(data=request.data) serializer = ExtensionVersionSerializer(data=request.data)
if not serializer.is_valid(): if not serializer.is_valid():
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
user = request.user user = request.user
extension_id = serializer.validated_data['extension_id']
version_file = serializer.validated_data['version_file'] version_file = serializer.validated_data['version_file']
release_notes = serializer.validated_data.get('release_notes', '') release_notes = serializer.validated_data.get('release_notes', '')