Skip to content

Commit

Permalink
Add server tests to meet 100% coverage again
Browse files Browse the repository at this point in the history
  • Loading branch information
bkis committed Jan 13, 2024
1 parent 02b16a9 commit a222077
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 29 deletions.
18 changes: 17 additions & 1 deletion Tekst-API/tests/test_api_browse.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

@pytest.mark.anyio
async def test_get_unit_siblings(
test_client: AsyncClient, insert_sample_data, status_fail_msg, wrong_id
test_client: AsyncClient, insert_sample_data, status_fail_msg, wrong_id, login
):
await insert_sample_data("texts", "nodes", "resources", "units")
text = await TextDocument.find_one(TextDocument.slug == "pond")
Expand All @@ -33,6 +33,22 @@ async def test_get_unit_siblings(
)
assert resp.status_code == 404, status_fail_msg(404, resp)

# siblings of resource version
await login()
resp = await test_client.post(
f"/resources/{str(resource.id)}/version",
)
assert resp.status_code == 201, status_fail_msg(201, resp)
assert "id" in resp.json()
version_id = resp.json()["id"]
resp = await test_client.get(
"/browse/unit-siblings",
params={"res": version_id},
)
assert resp.status_code == 200, status_fail_msg(200, resp)
assert isinstance(resp.json(), list)
assert len(resp.json()) == 3


@pytest.mark.anyio
async def test_get_location_data(
Expand Down
114 changes: 86 additions & 28 deletions Tekst-API/tests/test_api_resources.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from httpx import AsyncClient
from tekst.models.common import PydanticObjectId
from tekst.models.resource import ResourceBaseDocument


Expand Down Expand Up @@ -94,6 +95,64 @@ async def test_create_resource_w_wrong_text_id(
assert resp.status_code == 400, status_fail_msg(400, resp)


@pytest.mark.anyio
async def test_create_resource_with_forged_owner_id(
test_client: AsyncClient,
insert_sample_data,
status_fail_msg,
login,
):
text_id = (await insert_sample_data("texts", "nodes"))["texts"][0]
await login()

# create new resource with made up owner ID
payload = {
"title": "Foo Bar Baz",
"textId": text_id,
"level": 0,
"resourceType": "plaintext",
"ownerId": "643d3cdc21efd6c46ae1527e",
}
resp = await test_client.post(
"/resources",
json=payload,
)
assert resp.status_code == 201, status_fail_msg(201, resp)
assert resp.json()["ownerId"] != payload["ownerId"]


@pytest.mark.anyio
async def test_create_resource_version(
test_client: AsyncClient, insert_sample_data, status_fail_msg, login, wrong_id
):
resource_id = (await insert_sample_data("texts", "nodes", "resources"))[
"resources"
][0]
user = await login()

# create new resource version (fail with wrong resource ID)
resp = await test_client.post(
f"/resources/{wrong_id}/version",
)
assert resp.status_code == 404, status_fail_msg(404, resp)

# create new resource version
resp = await test_client.post(
f"/resources/{resource_id}/version",
)
assert resp.status_code == 201, status_fail_msg(201, resp)
assert "id" in resp.json()
assert "originalId" in resp.json()
assert "ownerId" in resp.json()
assert resp.json()["ownerId"] == user.get("id")

# fail to create new resource version of another version
resp = await test_client.post(
f"/resources/{resp.json()['id']}/version",
)
assert resp.status_code == 400, status_fail_msg(400, resp)


@pytest.mark.anyio
async def test_update_resource(
test_client: AsyncClient,
Expand Down Expand Up @@ -241,32 +300,6 @@ async def test_set_shares_for_public_resource(
assert len(resp.json()["sharedRead"]) == 0 # bc API should clear shares updates!


@pytest.mark.anyio
async def test_create_resource_with_forged_owner_id(
test_client: AsyncClient,
insert_sample_data,
status_fail_msg,
login,
):
text_id = (await insert_sample_data("texts", "nodes"))["texts"][0]
await login()

# create new resource with made up owner ID
payload = {
"title": "Foo Bar Baz",
"textId": text_id,
"level": 0,
"resourceType": "plaintext",
"ownerId": "643d3cdc21efd6c46ae1527e",
}
resp = await test_client.post(
"/resources",
json=payload,
)
assert resp.status_code == 201, status_fail_msg(201, resp)
assert resp.json()["ownerId"] != payload["ownerId"]


@pytest.mark.anyio
async def test_get_resource(
test_client: AsyncClient, insert_sample_data, status_fail_msg, wrong_id, login
Expand All @@ -288,10 +321,10 @@ async def test_get_resource(

# fail to get resource without read permissions
await ResourceBaseDocument.find_one(
ResourceBaseDocument.id == resource_id, with_children=True
ResourceBaseDocument.id == PydanticObjectId(resource_id), with_children=True
).set({ResourceBaseDocument.public: False})
await login(is_superuser=False)
resp = await test_client.get(f"/resources/{wrong_id}")
resp = await test_client.get(f"/resources/{resource_id}")
assert resp.status_code == 404, status_fail_msg(404, resp)


Expand Down Expand Up @@ -400,6 +433,19 @@ async def test_propose_unpropose_publish_unpublish_resource(
)
assert resp.status_code == 404, status_fail_msg(404, resp)

# fail to propose resource version
# create new resource version
resp = await test_client.post(
f"/resources/{resource_id}/version",
)
assert resp.status_code == 201, status_fail_msg(201, resp)
assert "id" in resp.json()
version_id = resp.json()["id"]
resp = await test_client.post(
f"/resources/{version_id}/propose",
)
assert resp.status_code == 400, status_fail_msg(400, resp)

# get all accessible resources, check if ours is proposed
resp = await test_client.get("/resources", params={"txt": text_id})
assert resp.status_code == 200, status_fail_msg(200, resp)
Expand All @@ -420,6 +466,18 @@ async def test_propose_unpropose_publish_unpublish_resource(
)
assert resp.status_code == 404, status_fail_msg(404, resp)

# fail to publish resource version
# (this should be actually be impossible anyway,
# because we can't even propose a version... so we create
# an invalid resource state on purpose, here)
await ResourceBaseDocument.find_one(
ResourceBaseDocument.id == PydanticObjectId(version_id), with_children=True
).set({ResourceBaseDocument.proposed: True})
resp = await test_client.post(
f"/resources/{version_id}/publish",
)
assert resp.status_code == 400, status_fail_msg(400, resp)

# publish resource
resp = await test_client.post(
f"/resources/{resource_id}/publish",
Expand Down
19 changes: 19 additions & 0 deletions Tekst-API/tests/test_api_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,26 @@ async def test_get_unit(
)
assert resp.status_code == 404, status_fail_msg(404, resp)


@pytest.mark.anyio
async def test_find_units(
test_client: AsyncClient, insert_sample_data, status_fail_msg, login, wrong_id
):
resource_id = (await insert_sample_data("texts", "nodes", "resources", "units"))[
"resources"
][0]
await login(is_superuser=True)

# find all units
resp = await test_client.get(
"/units",
params={"res": [resource_id], "limit": 100},
)
assert resp.status_code == 200, status_fail_msg(200, resp)
assert isinstance(resp.json(), list)
assert len(resp.json()) > 0

# find all units of resource
resp = await test_client.get(
"/units",
params={"limit": 100},
Expand Down

0 comments on commit a222077

Please sign in to comment.