mirror of
https://github.com/ae-utbm/sith.git
synced 2025-04-15 18:40:23 +00:00
Add quick upload tests
This commit is contained in:
parent
c236092c4f
commit
91b30e7550
@ -37,7 +37,12 @@ class MarkdownController(ControllerBase):
|
||||
|
||||
@api_controller("/upload")
|
||||
class UploadController(ControllerBase):
|
||||
@route.post("/image", response=UploadedFileSchema, permissions=[IsOldSubscriber])
|
||||
@route.post(
|
||||
"/image",
|
||||
response=UploadedFileSchema,
|
||||
permissions=[IsOldSubscriber],
|
||||
url_name="quick_upload_image",
|
||||
)
|
||||
def upload_image(self, file: UploadedFile):
|
||||
if file.content_type.split("/")[0] != "image":
|
||||
return self.create_response(
|
||||
|
@ -5,7 +5,7 @@ from uuid import uuid4
|
||||
|
||||
import pytest
|
||||
from django.core.cache import cache
|
||||
from django.core.files.uploadedfile import SimpleUploadedFile
|
||||
from django.core.files.uploadedfile import SimpleUploadedFile, UploadedFile
|
||||
from django.test import Client, TestCase
|
||||
from django.urls import reverse
|
||||
from model_bakery import baker
|
||||
@ -14,7 +14,8 @@ from PIL import Image
|
||||
from pytest_django.asserts import assertNumQueries
|
||||
|
||||
from core.baker_recipes import board_user, old_subscriber_user, subscriber_user
|
||||
from core.models import Group, SithFile, User
|
||||
from core.models import Group, QuickUploadImage, SithFile, User
|
||||
from core.utils import RED_PIXEL_PNG
|
||||
from sas.models import Picture
|
||||
from sith import settings
|
||||
|
||||
@ -256,3 +257,76 @@ def test_apply_rights_recursively():
|
||||
):
|
||||
assert set(file.view_groups.all()) == set(groups[:3])
|
||||
assert set(file.edit_groups.all()) == set(groups[2:6])
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
@pytest.mark.parametrize(
|
||||
("user_receipe", "file", "expected_status"),
|
||||
[
|
||||
(
|
||||
lambda: None,
|
||||
SimpleUploadedFile(
|
||||
"test.jpg", content=RED_PIXEL_PNG, content_type="image/jpg"
|
||||
),
|
||||
403,
|
||||
),
|
||||
(
|
||||
lambda: baker.make(User),
|
||||
SimpleUploadedFile(
|
||||
"test.jpg", content=RED_PIXEL_PNG, content_type="image/jpg"
|
||||
),
|
||||
403,
|
||||
),
|
||||
(
|
||||
lambda: subscriber_user.make(),
|
||||
SimpleUploadedFile(
|
||||
"test.jpg", content=RED_PIXEL_PNG, content_type="image/jpg"
|
||||
),
|
||||
200,
|
||||
),
|
||||
(
|
||||
lambda: old_subscriber_user.make(),
|
||||
SimpleUploadedFile(
|
||||
"test.jpg", content=RED_PIXEL_PNG, content_type="image/jpg"
|
||||
),
|
||||
200,
|
||||
),
|
||||
(
|
||||
lambda: old_subscriber_user.make(),
|
||||
SimpleUploadedFile(
|
||||
"test.jpg", content=b"invalid", content_type="image/jpg"
|
||||
),
|
||||
415,
|
||||
),
|
||||
(
|
||||
lambda: old_subscriber_user.make(),
|
||||
SimpleUploadedFile(
|
||||
"test.jpg", content=RED_PIXEL_PNG, content_type="invalid"
|
||||
),
|
||||
415,
|
||||
),
|
||||
(
|
||||
lambda: old_subscriber_user.make(),
|
||||
SimpleUploadedFile("test.jpg", content=b"invalid", content_type="invalid"),
|
||||
415,
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_quick_upload_image(
|
||||
client: Client,
|
||||
user_receipe: Callable[[], User | None],
|
||||
file: UploadedFile | None,
|
||||
expected_status: int,
|
||||
):
|
||||
if (user := user_receipe()) is not None:
|
||||
client.force_login(user)
|
||||
resp = client.post(
|
||||
reverse("api:quick_upload_image"), {"file": file} if file is not None else {}
|
||||
)
|
||||
|
||||
assert resp.status_code == expected_status
|
||||
|
||||
if expected_status != 200:
|
||||
return
|
||||
|
||||
assert QuickUploadImage.objects.filter(pk=resp.json()["uuid"]).exists()
|
||||
|
Loading…
x
Reference in New Issue
Block a user