-
-
Notifications
You must be signed in to change notification settings - Fork 43
Migrate POST /setup/untag endpoint (#65) #246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
igennova
wants to merge
2
commits into
openml:main
Choose a base branch
from
igennova:issue/65
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+260
−13
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| from sqlalchemy import Connection, text | ||
| from sqlalchemy.engine import Row | ||
|
|
||
|
|
||
| def get(setup_id: int, connection: Connection) -> Row | None: | ||
| row = connection.execute( | ||
| text( | ||
| """ | ||
| SELECT * | ||
| FROM algorithm_setup | ||
| WHERE sid = :setup_id | ||
| """, | ||
| ), | ||
| parameters={"setup_id": setup_id}, | ||
| ) | ||
| return row.first() | ||
|
|
||
|
|
||
| def get_tags(setup_id: int, connection: Connection) -> list[Row]: | ||
| rows = connection.execute( | ||
| text( | ||
| """ | ||
| SELECT * | ||
| FROM setup_tag | ||
| WHERE id = :setup_id | ||
| """, | ||
| ), | ||
| parameters={"setup_id": setup_id}, | ||
| ) | ||
| return list(rows.all()) | ||
|
|
||
|
|
||
| def untag(setup_id: int, tag: str, connection: Connection) -> None: | ||
| connection.execute( | ||
| text( | ||
| """ | ||
| DELETE FROM setup_tag | ||
| WHERE id = :setup_id AND tag = :tag | ||
| """, | ||
| ), | ||
| parameters={"setup_id": setup_id, "tag": tag}, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| from http import HTTPStatus | ||
| from typing import Annotated | ||
|
|
||
| from fastapi import APIRouter, Body, Depends, HTTPException | ||
| from sqlalchemy import Connection | ||
|
|
||
| import database.setups | ||
| from database.users import User, UserGroup | ||
| from routers.dependencies import expdb_connection, fetch_user_or_raise | ||
| from routers.types import SystemString64 | ||
|
|
||
| router = APIRouter(prefix="/setup", tags=["setup"]) | ||
|
|
||
|
|
||
| @router.post(path="/untag") | ||
| def untag_setup( | ||
| setup_id: Annotated[int, Body()], | ||
| tag: Annotated[str, SystemString64], | ||
| user: Annotated[User, Depends(fetch_user_or_raise)], | ||
| expdb_db: Annotated[Connection, Depends(expdb_connection)] = None, | ||
| ) -> dict[str, dict[str, str]]: | ||
| if not database.setups.get(setup_id, expdb_db): | ||
| raise HTTPException( | ||
| status_code=HTTPStatus.PRECONDITION_FAILED, | ||
| detail={"code": "472", "message": "Entity not found."}, | ||
| ) | ||
|
|
||
| setup_tags = database.setups.get_tags(setup_id, expdb_db) | ||
| matched_tag_row = next((t for t in setup_tags if t.tag.casefold() == tag.casefold()), None) | ||
|
|
||
| if not matched_tag_row: | ||
| raise HTTPException( | ||
| status_code=HTTPStatus.PRECONDITION_FAILED, | ||
| detail={"code": "475", "message": "Tag not found."}, | ||
| ) | ||
|
|
||
| if matched_tag_row.uploader != user.user_id and UserGroup.ADMIN not in user.groups: | ||
| raise HTTPException( | ||
| status_code=HTTPStatus.PRECONDITION_FAILED, | ||
| detail={"code": "476", "message": "Tag is not owned by you"}, | ||
| ) | ||
|
|
||
| database.setups.untag(setup_id, matched_tag_row.tag, expdb_db) | ||
|
|
||
| return {"setup_untag": {"id": str(setup_id)}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| from http import HTTPStatus | ||
|
|
||
| import httpx | ||
| import pytest | ||
| from starlette.testclient import TestClient | ||
|
|
||
| from tests.users import ApiKey | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "setup_id", | ||
| [1, 999999], | ||
| ids=["existing setup", "unknown setup"], | ||
| ) | ||
| @pytest.mark.parametrize( | ||
| "api_key", | ||
| [ApiKey.ADMIN, ApiKey.SOME_USER, ApiKey.OWNER_USER], | ||
| ids=["Administrator", "regular user", "possible owner"], | ||
| ) | ||
| @pytest.mark.parametrize( | ||
| "tag", | ||
| ["totally_new_tag_for_migration_testing"], | ||
| ) | ||
| def test_setup_untag_response_is_identical( | ||
| setup_id: int, | ||
| tag: str, | ||
| api_key: str, | ||
| py_api: TestClient, | ||
| php_api: httpx.Client, | ||
| ) -> None: | ||
| if setup_id == 1: | ||
| php_api.post( | ||
| "/setup/tag", | ||
| data={"api_key": ApiKey.SOME_USER, "tag": tag, "setup_id": setup_id}, | ||
| ) | ||
|
|
||
| original = php_api.post( | ||
| "/setup/untag", | ||
| data={"api_key": api_key, "tag": tag, "setup_id": setup_id}, | ||
| ) | ||
|
|
||
| if original.status_code == HTTPStatus.OK: | ||
| php_api.post( | ||
| "/setup/tag", | ||
| data={"api_key": ApiKey.SOME_USER, "tag": tag, "setup_id": setup_id}, | ||
| ) | ||
|
|
||
| new = py_api.post( | ||
| f"/setup/untag?api_key={api_key}", | ||
| json={"setup_id": setup_id, "tag": tag}, | ||
| ) | ||
|
|
||
| assert original.status_code == new.status_code | ||
|
|
||
| if new.status_code != HTTPStatus.OK: | ||
| assert original.json()["error"] == new.json()["detail"] | ||
| return | ||
|
|
||
| original_json = original.json() | ||
| new_json = new.json() | ||
|
|
||
| assert original_json == new_json | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| from collections.abc import Iterator | ||
| from http import HTTPStatus | ||
|
|
||
| import pytest | ||
| from sqlalchemy import Connection, text | ||
| from starlette.testclient import TestClient | ||
|
|
||
| from tests.users import ApiKey | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_setup_tag(expdb_test: Connection) -> Iterator[None]: | ||
| expdb_test.execute( | ||
| text("DELETE FROM setup_tag WHERE id = 1 AND tag = 'test_unit_tag_123'"), | ||
| ) | ||
| expdb_test.execute( | ||
| text("INSERT INTO setup_tag (id, tag, uploader) VALUES (1, 'test_unit_tag_123', 2)") | ||
| ) | ||
| expdb_test.commit() | ||
|
|
||
| yield | ||
|
|
||
| expdb_test.execute( | ||
| text("DELETE FROM setup_tag WHERE id = 1 AND tag = 'test_unit_tag_123'"), | ||
| ) | ||
| expdb_test.commit() | ||
|
|
||
|
|
||
| def test_setup_untag_missing_auth(py_api: TestClient) -> None: | ||
| response = py_api.post("/setup/untag", json={"setup_id": 1, "tag": "test_tag"}) | ||
| assert response.status_code == HTTPStatus.PRECONDITION_FAILED | ||
| assert response.json()["detail"] == {"code": "103", "message": "Authentication failed"} | ||
|
|
||
|
|
||
| def test_setup_untag_unknown_setup(py_api: TestClient) -> None: | ||
| response = py_api.post( | ||
| f"/setup/untag?api_key={ApiKey.SOME_USER}", | ||
| json={"setup_id": 999999, "tag": "test_tag"}, | ||
| ) | ||
| assert response.status_code == HTTPStatus.PRECONDITION_FAILED | ||
| assert response.json()["detail"] == {"code": "472", "message": "Entity not found."} | ||
|
|
||
|
|
||
| def test_setup_untag_tag_not_found(py_api: TestClient) -> None: | ||
| response = py_api.post( | ||
| f"/setup/untag?api_key={ApiKey.SOME_USER}", | ||
| json={"setup_id": 1, "tag": "non_existent_tag_12345"}, | ||
| ) | ||
| assert response.status_code == HTTPStatus.PRECONDITION_FAILED | ||
| assert response.json()["detail"] == {"code": "475", "message": "Tag not found."} | ||
|
|
||
|
|
||
| @pytest.mark.mut | ||
| @pytest.mark.usefixtures("mock_setup_tag") | ||
| def test_setup_untag_not_owned_by_you(py_api: TestClient) -> None: | ||
| response = py_api.post( | ||
| f"/setup/untag?api_key={ApiKey.OWNER_USER}", | ||
| json={"setup_id": 1, "tag": "test_unit_tag_123"}, | ||
| ) | ||
| assert response.status_code == HTTPStatus.PRECONDITION_FAILED | ||
| assert response.json()["detail"] == {"code": "476", "message": "Tag is not owned by you"} | ||
|
|
||
|
|
||
| @pytest.mark.mut | ||
| @pytest.mark.parametrize( | ||
| "api_key", | ||
| [ApiKey.SOME_USER, ApiKey.ADMIN], | ||
| ids=["Owner", "Administrator"], | ||
| ) | ||
| def test_setup_untag_success(api_key: str, py_api: TestClient, expdb_test: Connection) -> None: | ||
| expdb_test.execute(text("DELETE FROM setup_tag WHERE id = 1 AND tag = 'test_success_tag'")) | ||
| expdb_test.execute( | ||
| text("INSERT INTO setup_tag (id, tag, uploader) VALUES (1, 'test_success_tag', 2)") | ||
| ) | ||
| expdb_test.commit() | ||
|
|
||
| response = py_api.post( | ||
| f"/setup/untag?api_key={api_key}", | ||
| json={"setup_id": 1, "tag": "test_success_tag"}, | ||
| ) | ||
|
|
||
| assert response.status_code == HTTPStatus.OK | ||
| assert response.json() == {"setup_untag": {"id": "1"}} | ||
|
|
||
| rows = expdb_test.execute( | ||
| text("SELECT * FROM setup_tag WHERE id = 1 AND tag = 'test_success_tag'") | ||
| ).all() | ||
| assert len(rows) == 0 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Authentication failure (error code 103) is not covered by migration tests.
ApiKey.INVALID(or an absent key) is excluded from theapi_keyparametrize list, so thecreate_authentication_failed_errorpath in the router is never exercised here. Consider addingApiKey.INVALIDorNoneas a case to validate parity for the 103 response.✅ Suggested addition
`@pytest.mark.parametrize`( "api_key", - [ApiKey.ADMIN, ApiKey.SOME_USER, ApiKey.OWNER_USER], - ids=["Administrator", "regular user", "possible owner"], + [ApiKey.ADMIN, ApiKey.SOME_USER, ApiKey.OWNER_USER, ApiKey.INVALID], + ids=["Administrator", "regular user", "possible owner", "invalid key"], )📝 Committable suggestion
🤖 Prompt for AI Agents
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I intentionally omitted ApiKey.INVALID from the migration test parameters because the legacy PHP API has a known bug where it returns raw XML instead of JSON when authentication fails for setup queries (Code 103). Because our migration test framework strictly relies on original.json() == new.json() to prove parity, testing the invalid key pathway directly crashes Pytest with a JSONDecodeError.POST /setup/tagis migrated natively in my follow up PR and we no longer have to rely on php_api to stage the data.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@igennova, thank you for the detailed explanation! That makes perfect sense—if the legacy PHP API returns raw XML for Code 103 errors while the new API returns JSON, the migration test would indeed fail at theoriginal.json()deserialization step before any comparison could happen.Your plan to test authentication failure properly in standard unit tests once
POST /setup/tagis migrated is the right approach. At that point, you won't needphp_apito stage the data, and you'll be able to validate the 103 error path cleanly.One optional suggestion: consider adding a brief comment in the test file or a TODO note explaining why
ApiKey.INVALIDis excluded from the parameterization, so future maintainers understand the constraint.✏️ Learnings added