Skip to content

Commit

Permalink
Maint/test (#205)
Browse files Browse the repository at this point in the history
* Rename regular user to some user

* Test access

* Add context to tests
  • Loading branch information
PGijsbers authored Oct 14, 2024
1 parent 23c5df2 commit c2459a4
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 38 deletions.
2 changes: 1 addition & 1 deletion tests/routers/openml/dataset_tag_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_dataset_tag_rejects_unauthorized(key: ApiKey, py_api: TestClient) -> No

@pytest.mark.parametrize(
"key",
[ApiKey.ADMIN, ApiKey.REGULAR_USER, ApiKey.OWNER_USER],
[ApiKey.ADMIN, ApiKey.SOME_USER, ApiKey.OWNER_USER],
ids=["administrator", "non-owner", "owner"],
)
def test_dataset_tag(key: ApiKey, expdb_test: Connection, py_api: TestClient) -> None:
Expand Down
8 changes: 4 additions & 4 deletions tests/routers/openml/datasets_list_datasets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_list_filter_active(status: str, amount: int, py_api: TestClient) -> Non
[
(ApiKey.ADMIN, constants.NUMBER_OF_DATASETS),
(ApiKey.OWNER_USER, constants.NUMBER_OF_DATASETS),
(ApiKey.REGULAR_USER, constants.NUMBER_OF_DATASETS - constants.NUMBER_OF_PRIVATE_DATASETS),
(ApiKey.SOME_USER, constants.NUMBER_OF_DATASETS - constants.NUMBER_OF_PRIVATE_DATASETS),
(None, constants.NUMBER_OF_DATASETS - constants.NUMBER_OF_PRIVATE_DATASETS),
],
)
Expand Down Expand Up @@ -142,7 +142,7 @@ def test_list_data_version_no_result(py_api: TestClient) -> None:

@pytest.mark.parametrize(
"key",
[ApiKey.REGULAR_USER, ApiKey.OWNER_USER, ApiKey.ADMIN],
[ApiKey.SOME_USER, ApiKey.OWNER_USER, ApiKey.ADMIN],
)
@pytest.mark.parametrize(
("user_id", "count"),
Expand All @@ -155,7 +155,7 @@ def test_list_uploader(user_id: int, count: int, key: str, py_api: TestClient) -
)
# The dataset of user 16 is private, so can not be retrieved by other users.
owner_user_id = 16
if key == ApiKey.REGULAR_USER and user_id == owner_user_id:
if key == ApiKey.SOME_USER and user_id == owner_user_id:
_assert_empty_result(response)
return

Expand Down Expand Up @@ -242,7 +242,7 @@ def test_list_data_quality(quality: str, range_: str, count: int, py_api: TestCl
data_version=st.sampled_from([None, 2, 4]),
tag=st.sampled_from([None, "study_14", "study_not_in_db"]),
# We don't test ADMIN user, as we fixed a bug which treated them as a regular user
api_key=st.sampled_from([None, ApiKey.REGULAR_USER, ApiKey.OWNER_USER]),
api_key=st.sampled_from([None, ApiKey.SOME_USER, ApiKey.OWNER_USER]),
) # type: ignore[misc] # https://github.com/openml/server-api/issues/108
def test_list_data_identical(
py_api: TestClient,
Expand Down
27 changes: 12 additions & 15 deletions tests/routers/openml/datasets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from database.users import User
from routers.openml.datasets import get_dataset
from schemas.datasets.openml import DatasetMetadata, DatasetStatus
from tests.users import NO_USER, OWNER_USER, SOME_USER, ApiKey
from tests.users import ADMIN_USER, NO_USER, OWNER_USER, SOME_USER, ApiKey


@pytest.mark.parametrize(
Expand Down Expand Up @@ -75,7 +75,7 @@ def test_get_dataset(py_api: TestClient) -> None:
SOME_USER,
],
)
def test_private_dataset_no_owner_no_access(
def test_private_dataset_no_access(
user: User | None,
expdb_test: Connection,
) -> None:
Expand All @@ -90,22 +90,19 @@ def test_private_dataset_no_owner_no_access(
assert e.value.detail == {"code": "112", "message": "No access granted"} # type: ignore[comparison-overlap]


def test_private_dataset_owner_access(expdb_test: Connection, user_test: Connection) -> None:
@pytest.mark.parametrize(
"user", [OWNER_USER, ADMIN_USER, pytest.param(SOME_USER, marks=pytest.mark.xfail)]
)
def test_private_dataset_access(user: User, expdb_test: Connection, user_test: Connection) -> None:
dataset = get_dataset(
dataset_id=130,
user=OWNER_USER,
user=user,
user_db=user_test,
expdb_db=expdb_test,
)
assert isinstance(dataset, DatasetMetadata)


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


def test_dataset_features(py_api: TestClient) -> None:
# Dataset 4 has both nominal and numerical features, so provides reasonable coverage
response = py_api.get("/datasets/features/4")
Expand Down Expand Up @@ -254,11 +251,11 @@ def test_dataset_status_update_deactivated_to_active(py_api: TestClient) -> None
@pytest.mark.parametrize(
("dataset_id", "api_key", "status"),
[
(1, ApiKey.REGULAR_USER, DatasetStatus.ACTIVE),
(1, ApiKey.REGULAR_USER, DatasetStatus.DEACTIVATED),
(2, ApiKey.REGULAR_USER, DatasetStatus.DEACTIVATED),
(33, ApiKey.REGULAR_USER, DatasetStatus.ACTIVE),
(131, ApiKey.REGULAR_USER, DatasetStatus.ACTIVE),
(1, ApiKey.SOME_USER, DatasetStatus.ACTIVE),
(1, ApiKey.SOME_USER, DatasetStatus.DEACTIVATED),
(2, ApiKey.SOME_USER, DatasetStatus.DEACTIVATED),
(33, ApiKey.SOME_USER, DatasetStatus.ACTIVE),
(131, ApiKey.SOME_USER, DatasetStatus.ACTIVE),
],
)
def test_dataset_status_unauthorized(
Expand Down
31 changes: 15 additions & 16 deletions tests/routers/openml/migration/datasets_migration_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
from http import HTTPStatus
from typing import Any

import constants
import httpx
import pytest
from starlette.testclient import TestClient
Expand Down Expand Up @@ -104,7 +104,7 @@ def test_error_unknown_dataset(

@pytest.mark.parametrize(
"api_key",
[None, "a" * 32],
[None, ApiKey.INVALID],
)
def test_private_dataset_no_user_no_access(
py_api: TestClient,
Expand All @@ -118,22 +118,21 @@ def test_private_dataset_no_user_no_access(
assert response.json()["detail"] == {"code": "112", "message": "No access granted"}


@pytest.mark.skip("Not sure how to include apikey in test yet.")
@pytest.mark.parametrize(
"api_key",
[ApiKey.OWNER_USER, ApiKey.ADMIN],
)
def test_private_dataset_owner_access(
py_api: TestClient,
dataset_130: dict[str, Any],
php_api: TestClient,
api_key: str,
) -> None:
response = py_api.get("/datasets/130?api_key=...")
assert response.status_code == HTTPStatus.OK
assert dataset_130 == response.json()


@pytest.mark.skip("Not sure how to include apikey in test yet.")
def test_private_dataset_admin_access(py_api: TestClient) -> None:
py_api.get("/datasets/130?api_key=...")


# test against cached response
[private_dataset] = constants.PRIVATE_DATASET_ID
new_response = py_api.get(f"/datasets/{private_dataset}?api_key={api_key}")
old_response = php_api.get(f"/data/{private_dataset}?api_key={api_key}")
assert old_response.status_code == HTTPStatus.OK
assert old_response.status_code == new_response.status_code
assert new_response.json()["id"] == private_dataset


@pytest.mark.parametrize(
Expand All @@ -142,7 +141,7 @@ def test_private_dataset_admin_access(py_api: TestClient) -> None:
)
@pytest.mark.parametrize(
"api_key",
[ApiKey.ADMIN, ApiKey.REGULAR_USER, ApiKey.OWNER_USER],
[ApiKey.ADMIN, ApiKey.SOME_USER, ApiKey.OWNER_USER],
ids=["Administrator", "regular user", "possible owner"],
)
@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion tests/routers/openml/users_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
[
(ApiKey.ADMIN, ADMIN_USER),
(ApiKey.OWNER_USER, OWNER_USER),
(ApiKey.REGULAR_USER, SOME_USER),
(ApiKey.SOME_USER, SOME_USER),
],
)
def test_fetch_user(api_key: str, user: User, user_test: Connection) -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

class ApiKey(StrEnum):
ADMIN: str = "AD000000000000000000000000000000"
REGULAR_USER: str = "00000000000000000000000000000000"
SOME_USER: str = "00000000000000000000000000000000"
OWNER_USER: str = "DA1A0000000000000000000000000000"
INVALID: str = "11111111111111111111111111111111"

0 comments on commit c2459a4

Please sign in to comment.