From 0b07ef819f538731da892b14a47155722d66afc7 Mon Sep 17 00:00:00 2001 From: EdgeNeko Date: Mon, 20 May 2024 19:14:56 +0800 Subject: [PATCH] Add negative testcases for upload api --- tests/api/test_upload.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 tests/api/test_upload.py diff --git a/tests/api/test_upload.py b/tests/api/test_upload.py new file mode 100644 index 0000000..a6fae14 --- /dev/null +++ b/tests/api/test_upload.py @@ -0,0 +1,27 @@ +import io +import random + +from .conftest import TEST_ADMIN_TOKEN + + +def test_upload_bad_img_file(test_client): + bad_img_file = io.BytesIO(bytearray(random.getrandbits(8) for _ in range(1024 * 1024))) + bad_img_file.name = 'bad_image.jpg' + + resp = test_client.post('/admin/upload', + files={'image_file': bad_img_file}, + headers={'x-admin-token': TEST_ADMIN_TOKEN}, + params={'local': True}) + print(resp.content) + assert resp.status_code == 422 + + +def test_upload_unsupported_types(test_client): + bad_img_file = io.BytesIO(bytearray(random.getrandbits(8) for _ in range(1024 * 1024))) + bad_img_file.name = 'bad_image.tga' + + resp = test_client.post('/admin/upload', + files={'image_file': ('bad_img.tga', bad_img_file, 'image/tga')}, + headers={'x-admin-token': TEST_ADMIN_TOKEN}, + params={'local': True}) + assert resp.status_code == 415