Skip to content

Commit

Permalink
fix v2 file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
jefer94 committed Oct 22, 2024
1 parent dc85913 commit d6b243e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
20 changes: 7 additions & 13 deletions breathecode/media/tests/urls/v2/tests_me_chunk_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
Test /answer
"""

import json as json_utils
import tempfile
from io import BytesIO
from typing import Callable
from unittest.mock import AsyncMock, MagicMock, PropertyMock, call

import capyc.pytest as capy
import pytest
from django.core.files.uploadedfile import InMemoryUploadedFile, SimpleUploadedFile
from django.core.files.uploadedfile import SimpleUploadedFile
from django.urls.base import reverse_lazy
from rest_framework import status

Expand Down Expand Up @@ -143,7 +141,7 @@ async def test_no_authorized(aclient: capy.AsyncClient, database: capy.Database,

data = {"operation_type": op_type}

response = await aclient.put(url, data, headers={"Authorization": f"Token {model.token.key}"}, format="multipart")
response = await aclient.put(url, data, headers={"Authorization": f"Token {model.token.key}"}, format="json")

json = response.json()
expected = {
Expand Down Expand Up @@ -175,7 +173,7 @@ async def test_no_total_chunks(aclient: capy.AsyncClient, database: capy.Databas

data = {"operation_type": op_type}

response = await aclient.put(url, data, headers={"Authorization": f"Token {model.token.key}"}, format="multipart")
response = await aclient.put(url, data, headers={"Authorization": f"Token {model.token.key}"}, format="json")

json = response.json()
expected = {
Expand Down Expand Up @@ -207,7 +205,7 @@ async def test_no_filename(aclient: capy.AsyncClient, database: capy.Database, f

data = {"operation_type": op_type, "total_chunks": 3}

response = await aclient.put(url, data, headers={"Authorization": f"Token {model.token.key}"}, format="multipart")
response = await aclient.put(url, data, headers={"Authorization": f"Token {model.token.key}"}, format="json")

json = response.json()
expected = {
Expand Down Expand Up @@ -239,7 +237,7 @@ async def test_no_mime(aclient: capy.AsyncClient, database: capy.Database, fake:

data = {"operation_type": op_type, "total_chunks": 3, "filename": "a.txt"}

response = await aclient.put(url, data, headers={"Authorization": f"Token {model.token.key}"}, format="multipart")
response = await aclient.put(url, data, headers={"Authorization": f"Token {model.token.key}"}, format="json")

json = response.json()
expected = {
Expand Down Expand Up @@ -272,9 +270,7 @@ async def test_no_chunks(self, aclient: capy.AsyncClient, database: capy.Databas

data = {"operation_type": op_type, "total_chunks": 3, "filename": "a.txt", "mime": "text/plain"}

response = await aclient.put(
url, data, headers={"Authorization": f"Token {model.token.key}"}, format="multipart"
)
response = await aclient.put(url, data, headers={"Authorization": f"Token {model.token.key}"}, format="json")

json = response.json()
expected = {
Expand Down Expand Up @@ -320,9 +316,7 @@ async def test_upload_file__schedule_deletions(

data = {"operation_type": op_type, "total_chunks": 3, "filename": filename, "mime": "text/plain"}

response = await aclient.put(
url, data, headers={"Authorization": f"Token {model.token.key}"}, format="multipart"
)
response = await aclient.put(url, data, headers={"Authorization": f"Token {model.token.key}"}, format="json")

json = response.json()
expected = {
Expand Down
6 changes: 5 additions & 1 deletion breathecode/media/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ async def upload(self, academy_id: Optional[int] = None):
)

total_chunks = request.data.get("total_chunks")
if total_chunks is None or total_chunks.isnumeric() is False or (total_chunks := float(total_chunks)) <= 0:
if (
total_chunks is None
or isinstance(total_chunks, (int, float)) is False
or (total_chunks := total_chunks) <= 0
):
raise ValidationException(
translation(
lang,
Expand Down

0 comments on commit d6b243e

Please sign in to comment.