Skip to content

Commit

Permalink
code coverage for admin endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
JBorrow committed Feb 16, 2024
1 parent a1c35b1 commit 433ede3
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
9 changes: 9 additions & 0 deletions librarian_server/api/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

from ..database import yield_session
from ..orm import File, Instance, StoreMetadata
from ..stores import StoreNames
from .auth import AdminUserDependency

router = APIRouter(prefix="/api/v2/admin")
Expand Down Expand Up @@ -50,6 +51,14 @@ def add_file(
suggested_remedy="Create the store first. Maybe you need to run DB migration?",
)

# TODO: Can't do code coverage until we add nonlocal stores.
if store.store_type != StoreNames["local"]: # pragma: no cover
response.status_code = status.HTTP_400_BAD_REQUEST
return AdminRequestFailedResponse(
reason=f"Store {request.store_name} is not a local store.",
suggested_remedy="Use a local store for this operation.",
)

# Check if the file exists already.
existing_file = session.get(File, request.name)

Expand Down
88 changes: 87 additions & 1 deletion tests/server_unit_test/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from hera_librarian.utils import get_md5_from_path, get_size_from_path


def test_add_file(test_client, test_server, garbage_file):
def test_add_file(test_client, test_server, garbage_file, test_orm):
"""
Tests that we can add a file with no row in database.
"""
Expand Down Expand Up @@ -48,3 +48,89 @@ def test_add_file(test_client, test_server, garbage_file):
response = AdminCreateFileResponse.model_validate_json(response.content)

assert response.success

# Now can check what happens if we upload the file again...

response = test_client.post_with_auth(
"/api/v2/admin/add_file", content=request.model_dump_json()
)

assert response.status_code == 200

response = AdminCreateFileResponse.model_validate_json(response.content)

assert response.already_exists

# Ok, now validate the actual db.

get_session = test_server[1]

with get_session() as session:
file = session.get(test_orm.File, "test_upload_without_uploading.txt")

assert file is not None

instance = file.instances[0]

assert instance is not None

assert instance.path == str(full_path)
assert instance.store.name == "local_store"


def test_add_flie_no_file_exists(test_client, test_server, test_orm):
"""
Tests that we can't add a file if the file doesn't exist.
"""

request = AdminCreateFileRequest(
name="non_existent_file.txt",
create_time=0,
size=0,
checksum="",
uploader="test",
source="test",
path="/this/file/does/not/exist",
store_name="local_store",
)

response = test_client.post_with_auth(
"/api/v2/admin/add_file", content=request.model_dump_json()
)

assert response.status_code == 400

response = AdminRequestFailedResponse.model_validate_json(response.content)

assert response.reason == "File /this/file/does/not/exist does not exist."
assert (
response.suggested_remedy
== "Create the file first, or make sure that you are using a local store."
)


def test_add_file_no_store_exists(test_client):
"""
Tests the case where the store does not exist and we try to add a file.
"""

request = AdminCreateFileRequest(
name="non_existent_file.txt",
create_time=0,
size=0,
checksum="",
uploader="test",
source="test",
path="/this/file/does/not/exist",
store_name="not_a_store",
)

response = test_client.post_with_auth(
"/api/v2/admin/add_file", content=request.model_dump_json()
)

assert response.status_code == 400

response = AdminRequestFailedResponse.model_validate_json(response.content)

assert response.reason == "Store not_a_store does not exist."

0 comments on commit 433ede3

Please sign in to comment.