Skip to content

Commit

Permalink
Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
FledgeXu committed Sep 8, 2023
1 parent fbe957a commit 65751bc
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 64 deletions.
3 changes: 2 additions & 1 deletion backend/api/routes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import hashlib
from collections.abc import Iterator
from http import HTTPStatus
from pathlib import Path
from typing import Annotated, BinaryIO, Iterator
from typing import Annotated, BinaryIO
from uuid import UUID

from fastapi import Cookie, Depends, HTTPException, Response
Expand Down
3 changes: 1 addition & 2 deletions backend/api/routes/archives.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
import datetime
from enum import Enum
from http import HTTPStatus
from io import BytesIO
from typing import Any
from uuid import UUID

import zimscraperlib.image
from fastapi import APIRouter, Depends, HTTPException, UploadFile
from pydantic import BaseModel, ConfigDict, TypeAdapter
from sqlalchemy import select, update
from sqlalchemy.orm import Session
from zimscraperlib import filesystem
import zimscraperlib.image

from api.constants import constants, logger
from api.database import gen_session
Expand Down
5 changes: 1 addition & 4 deletions backend/api/routes/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import hashlib
from enum import Enum
from http import HTTPStatus
from pathlib import Path
from typing import BinaryIO
from uuid import UUID

from fastapi import APIRouter, Depends, HTTPException, UploadFile
Expand All @@ -14,13 +12,12 @@

from api.constants import constants, logger
from api.database import Session as DBSession
from api.database import gen_session, get_local_fpath_for
from api.database import gen_session
from api.database.models import File, Project
from api.redis import task_queue
from api.routes import (
calculate_file_size,
generate_file_hash,
read_file_in_chunks,
save_file,
validated_project,
)
Expand Down
32 changes: 23 additions & 9 deletions backend/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import pytest
from fastapi.testclient import TestClient

from api.constants import constants
from api.database import Session
from api.database.models import Archive, File, Project, User
from api.entrypoint import app
Expand Down Expand Up @@ -110,6 +109,22 @@ def test_file():
return b"TDw\xd1\x01\xc8\xdfBE8\x80\x08;}6\xd3O9\xefm\xc5"


@pytest.fixture
def test_png_image():
return (
b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00"
b"\x01\x08\x04\x00\x00\x00\xb5\x1c\x0c\x02\x00\x00\x00\x0bIDATx\xdacd`\x00\x00\x00\x06\x00\x020\x81\xd0/\x00\x00\x00\x00IEND\xaeB`\x82"
)


@pytest.fixture
def test_jpeg_image():
return (
b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00"
b"\x01\x08\x04\x00\x00\x00\xb5\x1c\x0c\x02\x00\x00\x00\x0bIDATx\xdacd`\x00\x00\x00\x06\x00\x020\x81\xd0/\x00\x00\x00\x00IEND\xaeB`\x82"
)


@pytest.fixture
def test_file_hash():
return "9e56d33da489a4ba0fe1f02ed4b0b5984854845dfd666a92e112262b8e7ea0dc"
Expand Down Expand Up @@ -149,16 +164,15 @@ def project_id(test_project_name, user_id):
def archive_id(test_archive_name, project_id):
now = datetime.datetime.now(datetime.UTC)
new_archive = Archive(
filename=test_archive_name,
filesize=0,
created_on=now,
requested_on=datetime.datetime.min,
download_url="",
collection_json_path="",
status=ArchiveStatus.CREATED,
zimfarm_task_id=constants.empty_uuid,
status=ArchiveStatus.PENDING,
config={"filename": test_archive_name},
filesize=None,
requested_on=None,
download_url=None,
collection_json_path=None,
zimfarm_task_id=None,
email=None,
config={},
)
with Session.begin() as session:
project = session.get(Project, project_id)
Expand Down
121 changes: 73 additions & 48 deletions backend/tests/routes/test_archives.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,6 @@
from api.constants import constants


def test_create_archive_correct_data(logged_in_client, project_id):
data = {
"filename": "test_name",
"email": "[email protected]",
"config": {
"title": "test_title",
"description": "test_description",
"name": "test_name",
"publisher": "test_publisher",
"creator": "test_creator",
"languages": ["en"],
"tags": ["test_tags"],
},
}
response = logged_in_client.post(
f"{constants.api_version_prefix}/projects/{project_id}/archives", json=data
)
assert response.status_code == HTTPStatus.CREATED
json_result = response.json()
assert uuid.UUID(json_result.get("id"))
assert json_result.get("filename") == "test_name"
assert json_result.get("email") == "[email protected]"
assert json_result.get("config").get("title") == "test_title"
assert json_result.get("config").get("description") == "test_description"
assert json_result.get("config").get("name") == "test_name"
assert json_result.get("config").get("publisher") == "test_publisher"
assert json_result.get("config").get("creator") == "test_creator"
assert json_result.get("config").get("languages")[0] == "en"
assert json_result.get("config").get("tags")[0] == "test_tags"


def test_create_archive_wrong_authorization(client, project_id, missing_user_cookie):
response = client.post(
f"{constants.api_version_prefix}/projects/{project_id}/archives"
)
assert response.status_code == HTTPStatus.UNAUTHORIZED

client.cookies = missing_user_cookie
response = client.post(
f"{constants.api_version_prefix}/projects/{project_id}/archives"
)
assert response.status_code == HTTPStatus.UNAUTHORIZED


def test_get_all_archive_correct_data(logged_in_client, project_id, archive_id):
response = logged_in_client.get(
f"{constants.api_version_prefix}/projects/{project_id}/archives"
Expand Down Expand Up @@ -105,9 +61,9 @@ def test_get_archive_wrong_authorization(

def test_update_archive_correct_data(logged_in_client, project_id, archive_id):
data = {
"filename": "test_name",
"email": "[email protected]",
"config": {
"filename": "test_name",
"title": "test_title",
"description": "test_description",
"name": "test_name",
Expand All @@ -128,8 +84,8 @@ def test_update_archive_correct_data(logged_in_client, project_id, archive_id):
f"{constants.api_version_prefix}/projects/{project_id}/archives/{archive_id}",
)
json_result = response.json()
assert json_result.get("filename") == "test_name"
assert json_result.get("email") == "[email protected]"
assert json_result.get("config").get("filename") == "test_name"
assert json_result.get("config").get("title") == "test_title"
assert json_result.get("config").get("description") == "test_description"
assert json_result.get("config").get("name") == "test_name"
Expand All @@ -141,9 +97,9 @@ def test_update_archive_correct_data(logged_in_client, project_id, archive_id):

def test_update_archive_wrong_id(logged_in_client, project_id, missing_archive_id):
data = {
"filename": "test_name",
"email": "[email protected]",
"config": {
"filename": "test_name",
"title": "test_title",
"description": "test_description",
"name": "test_name",
Expand All @@ -165,9 +121,9 @@ def test_update_archive_wrong_authorization(
client, missing_user_cookie, project_id, archive_id
):
data = {
"filename": "test_name",
"email": "[email protected]",
"config": {
"filename": "test_name",
"title": "test_title",
"description": "test_description",
"name": "test_name",
Expand All @@ -189,3 +145,72 @@ def test_update_archive_wrong_authorization(
json=data,
)
assert response.status_code == HTTPStatus.UNAUTHORIZED


def test_upload_illustration_correct_data(
logged_in_client, project_id, test_png_image, archive_id
):
file = {"uploaded_illustration": test_png_image}
response = logged_in_client.post(
f"{constants.api_version_prefix}/projects/{project_id}/archives/{archive_id}/illustration",
files=file,
)
assert response.status_code == HTTPStatus.CREATED


def test_upload_illustration_other_format(logged_in_client, project_id, archive_id):
test_image = (
b"GIF89a\x01\x00\x01\x00\x80\x00\x00\xff\xff\xff\x00\x00\x00,"
b"\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;"
)
file = {"uploaded_illustration": test_image}
response = logged_in_client.post(
f"{constants.api_version_prefix}/projects/{project_id}/archives/{archive_id}/illustration",
files=file,
)
assert response.status_code == HTTPStatus.CREATED


def test_upload_illustration_empty_file(logged_in_client, project_id, archive_id):
file = {"uploaded_illustration": b""}
response = logged_in_client.post(
f"{constants.api_version_prefix}/projects/{project_id}/archives/{archive_id}/illustration",
files=file,
)
assert response.status_code == HTTPStatus.BAD_REQUEST


def test_upload_too_large_illustration(logged_in_client, project_id, archive_id):
file = {"uploaded_illustration": b"\xff" * (constants.illustration_quota + 1)}
response = logged_in_client.post(
f"{constants.api_version_prefix}/projects/{project_id}/archives/{archive_id}/illustration",
files=file,
)
assert response.status_code == HTTPStatus.REQUEST_ENTITY_TOO_LARGE


def test_upload_none_image_illustration(logged_in_client, project_id, archive_id):
file = {"uploaded_illustration": b"\xff"}
response = logged_in_client.post(
f"{constants.api_version_prefix}/projects/{project_id}/archives/{archive_id}/illustration",
files=file,
)
assert response.status_code == HTTPStatus.BAD_REQUEST


def test_upload_illustration_without_wrong_authorization(
client, missing_user_cookie, project_id, archive_id, test_png_image
):
file = {"uploaded_illustration": test_png_image}
response = client.post(
f"{constants.api_version_prefix}/projects/{project_id}/archives/{archive_id}/illustration",
files=file,
)
assert response.status_code == HTTPStatus.UNAUTHORIZED

client.cookies = missing_user_cookie
response = client.post(
f"{constants.api_version_prefix}/projects/{project_id}/archives/{archive_id}/illustration",
files=file,
)
assert response.status_code == HTTPStatus.UNAUTHORIZED

0 comments on commit 65751bc

Please sign in to comment.