Skip to content

Commit

Permalink
Rename endpoint 'old' to 'v1' and <unnamed> to "v2" (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
PGijsbers authored Nov 8, 2023
1 parent ff10593 commit 10f6809
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from fastapi import FastAPI
from routers.datasets import router as datasets_router
from routers.mldcat_ap.dataset import router as mldcat_ap_router
from routers.old.datasets import router as datasets_router_old_format
from routers.v1.datasets import router as datasets_router_old_format
from routers.v2.datasets import router as datasets_router

app = FastAPI()

Expand Down
2 changes: 1 addition & 1 deletion src/routers/mldcat_ap/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from schemas.datasets.mldcat_ap import JsonLDGraph, convert_to_mldcat_ap
from sqlalchemy import Connection

from routers.datasets import get_dataset
from routers.dependencies import expdb_connection, userdb_connection
from routers.v2.datasets import get_dataset

router = APIRouter(prefix="/mldcat_ap/datasets", tags=["datasets"])

Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/routers/old/datasets.py → src/routers/v1/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy import Connection

from routers.datasets import get_dataset
from routers.dependencies import expdb_connection, fetch_user, userdb_connection
from routers.types import SystemString64
from routers.v2.datasets import get_dataset

router = APIRouter(prefix="/old/datasets", tags=["datasets"])
router = APIRouter(prefix="/v1/datasets", tags=["datasets"])


@router.get(
Expand Down
5 changes: 2 additions & 3 deletions src/routers/datasets.py → src/routers/v2/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
)
from database.users import APIKey, get_user_groups_for, get_user_id_for
from fastapi import APIRouter, Depends, HTTPException
from routers.dependencies import expdb_connection, userdb_connection
from schemas.datasets.openml import (
DatasetFileFormat,
DatasetMetadata,
Expand All @@ -22,9 +23,7 @@
)
from sqlalchemy import Connection

from routers.dependencies import expdb_connection, userdb_connection

router = APIRouter(prefix="/datasets", tags=["datasets"])
router = APIRouter(prefix="/v2/datasets", tags=["datasets"])


class DatasetError(IntEnum):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_dataset_tag_rejects_unauthorized(key: ApiKey, api_client: FastAPI) -> N
response = cast(
httpx.Response,
api_client.post(
f"/old/datasets/tag?data_id=130&tag=test{apikey}",
f"/v1/datasets/tag?data_id=130&tag=test{apikey}",
),
)
assert response.status_code == http.client.PRECONDITION_FAILED
Expand All @@ -37,7 +37,7 @@ def test_dataset_tag(key: ApiKey, expdb_test: Connection, api_client: FastAPI) -
response = cast(
httpx.Response,
api_client.post(
f"/old/datasets/tag?data_id={dataset_id}&tag={tag}&api_key={key}",
f"/v1/datasets/tag?data_id={dataset_id}&tag={tag}&api_key={key}",
),
)
assert response.status_code == http.client.OK
Expand All @@ -52,7 +52,7 @@ def test_dataset_tag_returns_existing_tags(api_client: FastAPI) -> None:
response = cast(
httpx.Response,
api_client.post(
f"/old/datasets/tag?data_id={dataset_id}&tag={tag}&api_key={ApiKey.ADMIN}",
f"/v1/datasets/tag?data_id={dataset_id}&tag={tag}&api_key={ApiKey.ADMIN}",
),
)
assert response.status_code == http.client.OK
Expand All @@ -64,7 +64,7 @@ def test_dataset_tag_fails_if_tag_exists(api_client: FastAPI) -> None:
response = cast(
httpx.Response,
api_client.post(
f"/old/datasets/tag?data_id={dataset_id}&tag={tag}&api_key={ApiKey.ADMIN}",
f"/v1/datasets/tag?data_id={dataset_id}&tag={tag}&api_key={ApiKey.ADMIN}",
),
)
assert response.status_code == http.client.INTERNAL_SERVER_ERROR
Expand All @@ -88,7 +88,7 @@ def test_dataset_tag_invalid_tag_is_rejected(
api_client: FastAPI,
) -> None:
query = f"data_id=1&tag={tag}&api_key={ApiKey.ADMIN}"
new = cast(httpx.Response, api_client.post(f"/old/datasets/tag?{query}"))
new = cast(httpx.Response, api_client.post(f"/v1/datasets/tag?{query}"))

assert new.status_code == http.client.UNPROCESSABLE_ENTITY
assert ["query", "tag"] == new.json()["detail"][0]["loc"]
Expand Down Expand Up @@ -131,7 +131,7 @@ def test_dataset_tag_response_is_identical(
"http://server-api-php-api-1:80/api/v1/json/data/untag",
data={"api_key": api_key, "tag": tag, "data_id": dataset_id},
)
new = cast(httpx.Response, api_client.post(f"/old/datasets/tag?{query}"))
new = cast(httpx.Response, api_client.post(f"/v1/datasets/tag?{query}"))

assert original.status_code == new.status_code, original.json()
if new.status_code != http.client.OK:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
)
def test_dataset_response_is_identical(dataset_id: int, api_client: FastAPI) -> None:
original = httpx.get(f"http://server-api-php-api-1:80/api/v1/json/data/{dataset_id}")
new = cast(httpx.Response, api_client.get(f"/old/datasets/{dataset_id}"))
new = cast(httpx.Response, api_client.get(f"/v1/datasets/{dataset_id}"))
assert original.status_code == new.status_code
assert new.json()

Expand Down Expand Up @@ -61,7 +61,7 @@ def test_error_unknown_dataset(
dataset_id: int,
api_client: FastAPI,
) -> None:
response = cast(httpx.Response, api_client.get(f"old/datasets/{dataset_id}"))
response = cast(httpx.Response, api_client.get(f"v1/datasets/{dataset_id}"))

assert response.status_code == http.client.PRECONDITION_FAILED
assert {"code": "111", "message": "Unknown dataset"} == response.json()["detail"]
Expand All @@ -76,7 +76,7 @@ def test_private_dataset_no_user_no_access(
api_key: str | None,
) -> None:
query = f"?api_key={api_key}" if api_key else ""
response = cast(httpx.Response, api_client.get(f"old/datasets/130{query}"))
response = cast(httpx.Response, api_client.get(f"v1/datasets/130{query}"))

assert response.status_code == http.client.PRECONDITION_FAILED
assert {"code": "112", "message": "No access granted"} == response.json()["detail"]
Expand All @@ -87,12 +87,12 @@ def test_private_dataset_owner_access(
api_client: FastAPI,
dataset_130: dict[str, Any],
) -> None:
response = cast(httpx.Response, api_client.get("/old/datasets/130?api_key=..."))
response = cast(httpx.Response, api_client.get("/v1/datasets/130?api_key=..."))
assert response.status_code == http.client.OK
assert dataset_130 == response.json()


@pytest.mark.skip("Not sure how to include apikey in test yet.")
def test_private_dataset_admin_access(api_client: FastAPI) -> None:
cast(httpx.Response, api_client.get("/old/datasets/130?api_key=..."))
cast(httpx.Response, api_client.get("/v1/datasets/130?api_key=..."))
# test against cached response
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,38 @@


@pytest.mark.parametrize(
("endpoint", "dataset_id", "response_code"),
("dataset_id", "response_code"),
[
("datasets/", -1, http.client.NOT_FOUND),
("datasets/", 138, http.client.NOT_FOUND),
("datasets/", 100_000, http.client.NOT_FOUND),
(-1, http.client.NOT_FOUND),
(138, http.client.NOT_FOUND),
(100_000, http.client.NOT_FOUND),
],
)
def test_error_unknown_dataset(
endpoint: str,
dataset_id: int,
response_code: int,
api_client: FastAPI,
) -> None:
response = cast(httpx.Response, api_client.get(f"{endpoint}/{dataset_id}"))
response = cast(httpx.Response, api_client.get(f"v2/datasets/{dataset_id}"))

assert response.status_code == response_code
assert {"code": "111", "message": "Unknown dataset"} == response.json()["detail"]


@pytest.mark.parametrize(
("endpoint", "api_key", "response_code"),
("api_key", "response_code"),
[
("datasets", None, http.client.FORBIDDEN),
("datasets", "a" * 32, http.client.FORBIDDEN),
(None, http.client.FORBIDDEN),
("a" * 32, http.client.FORBIDDEN),
],
)
def test_private_dataset_no_user_no_access(
api_client: FastAPI,
endpoint: str,
api_key: str | None,
response_code: int,
) -> None:
query = f"?api_key={api_key}" if api_key else ""
response = cast(httpx.Response, api_client.get(f"{endpoint}/130{query}"))
response = cast(httpx.Response, api_client.get(f"v2/datasets/130{query}"))

assert response.status_code == response_code
assert {"code": "112", "message": "No access granted"} == response.json()["detail"]
Expand All @@ -51,12 +49,12 @@ def test_private_dataset_owner_access(
api_client: FastAPI,
dataset_130: dict[str, Any],
) -> None:
response = cast(httpx.Response, api_client.get("/datasets/130?api_key=..."))
response = cast(httpx.Response, api_client.get("/v2/datasets/130?api_key=..."))
assert response.status_code == http.client.OK
assert dataset_130 == response.json()


@pytest.mark.skip("Not sure how to include apikey in test yet.")
def test_private_dataset_admin_access(api_client: FastAPI) -> None:
cast(httpx.Response, api_client.get("/datasets/130?api_key=..."))
cast(httpx.Response, api_client.get("/v2/datasets/130?api_key=..."))
# test against cached response

0 comments on commit 10f6809

Please sign in to comment.