From 1389b2c0703e11597dbb0818594d3dd666eda5eb Mon Sep 17 00:00:00 2001 From: gameswu Date: Sun, 12 Apr 2026 15:41:41 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=BA=86=E6=99=AE?= =?UTF-8?q?=E9=80=9A=E7=94=A8=E6=88=B7=E5=8F=AF=E4=BB=A5=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E5=B9=B6=E7=BC=96=E8=BE=91human=E9=9A=BE=E5=BA=A6=E6=A0=87?= =?UTF-8?q?=E7=AD=BE=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=8C=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E5=8F=98=E6=9B=B4=E4=B8=BA0.2.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 2 +- Cargo.toml | 2 +- scripts/tests/test_1_questions.py | 134 ++++++++++++++++++++++++++++++ src/api/questions/handlers.rs | 18 ++++ 4 files changed, 154 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 32199d5..608a341 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1307,7 +1307,7 @@ dependencies = [ [[package]] name = "qb_api" -version = "0.2.0" +version = "0.2.1" dependencies = [ "anyhow", "argon2", diff --git a/Cargo.toml b/Cargo.toml index f984d8c..f51f429 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "qb_api" -version = "0.2.0" +version = "0.2.1" edition = "2021" [dependencies] diff --git a/scripts/tests/test_1_questions.py b/scripts/tests/test_1_questions.py index d193de8..a2cad42 100644 --- a/scripts/tests/test_1_questions.py +++ b/scripts/tests/test_1_questions.py @@ -830,3 +830,137 @@ def test_difficulty_updated_by_tracks_editor(api, state): finally: api.login("admin", "changeme") api.delete(f"/admin/users/{reviewer_id}") + + +def test_reviewer_cannot_modify_human_difficulty(api, state): + """Assigned reviewers (user role) cannot create/update/delete the 'human' difficulty tag. + + Only leader or above should be able to operate on the 'human' tag. + """ + question_id = state.q_ids[0] + + reviewer = api.ensure_user({ + "username": "e2e_human_blocker", + "password": "blocker12345", + "role": "user", + }) + reviewer_id = reviewer["user_id"] + + try: + # Assign as reviewer so the user has general difficulty-edit access + api.post_json( + f"/questions/{question_id}/reviewers", + {"reviewer_id": reviewer_id}, + ) + + saved = api._access_token + api.login("e2e_human_blocker", "blocker12345") + + # ── CREATE human → 403 ── + # First delete existing human tag (as admin) so we can test create + api.set_token(saved) + api.delete(f"/questions/{question_id}/difficulties/human") + api.login("e2e_human_blocker", "blocker12345") + + api.post_json( + f"/questions/{question_id}/difficulties", + {"algorithm_tag": "human", "score": 5}, + expect=403, + ) + + # Reviewer CAN create a non-human tag + api.post_json( + f"/questions/{question_id}/difficulties", + {"algorithm_tag": "reviewer_tag", "score": 3}, + ) + + # ── Re-create human tag as admin for update/delete tests ── + api.set_token(saved) + api.post_json( + f"/questions/{question_id}/difficulties", + {"algorithm_tag": "human", "score": 4, "notes": "warm-up"}, + ) + api.login("e2e_human_blocker", "blocker12345") + + # ── UPDATE human → 403 ── + api.patch_json( + f"/questions/{question_id}/difficulties/human", + {"score": 9}, + expect=403, + ) + + # Reviewer CAN update their own non-human tag + api.patch_json( + f"/questions/{question_id}/difficulties/reviewer_tag", + {"score": 6}, + ) + + # ── DELETE human → 403 ── + api.delete( + f"/questions/{question_id}/difficulties/human", + expect=403, + ) + + # Reviewer CAN delete their own non-human tag + api.delete(f"/questions/{question_id}/difficulties/reviewer_tag") + + # Cleanup + api.set_token(saved) + api.delete(f"/questions/{question_id}/reviewers/{reviewer_id}") + finally: + api.login("admin", "changeme") + api.delete(f"/admin/users/{reviewer_id}") + + +def test_leader_can_modify_human_difficulty(api, state): + """Leader role can create/update/delete the 'human' difficulty tag.""" + question_id = state.q_ids[1] + + leader = api.ensure_user({ + "username": "e2e_human_leader", + "password": "leader12345", + "role": "leader", + "leader_expires_at": "2099-12-31T23:59:59Z", + }) + leader_id = leader["user_id"] + + try: + saved = api._access_token + + # Remove existing human tag so leader can create it fresh + api.delete(f"/questions/{question_id}/difficulties/human") + + api.login("e2e_human_leader", "leader12345") + + # ── CREATE human → 200 ── + _, body, _ = api.post_json( + f"/questions/{question_id}/difficulties", + {"algorithm_tag": "human", "score": 6, "notes": "leader set"}, + ) + detail = parse_json(body) + assert detail["difficulty"]["human"]["score"] == 6 + + # ── UPDATE human → 200 ── + _, body, _ = api.patch_json( + f"/questions/{question_id}/difficulties/human", + {"score": 8, "notes": "leader revised"}, + ) + detail = parse_json(body) + assert detail["difficulty"]["human"]["score"] == 8 + + # ── DELETE human → 200 ── + _, body, _ = api.delete( + f"/questions/{question_id}/difficulties/human", + ) + detail = parse_json(body) + assert "human" not in detail["difficulty"] + + # Restore the human tag for subsequent tests + api.set_token(saved) + api.post_json( + f"/questions/{question_id}/difficulties", + {"algorithm_tag": "human", "score": 7, "notes": "competition-ready"}, + ) + finally: + api.login("admin", "changeme") + api.delete(f"/admin/users/{leader_id}") diff --git a/src/api/questions/handlers.rs b/src/api/questions/handlers.rs index c98d9e8..ae2549e 100644 --- a/src/api/questions/handlers.rs +++ b/src/api/questions/handlers.rs @@ -464,6 +464,12 @@ pub(crate) async fn create_question_difficulty( "you do not have permission to create difficulty entries on this question", )); } + // "human" difficulty tag is restricted to leader or above. + if algorithm_tag == "human" && !access.is_admin_or_bot && !access.is_leader { + return Err(ApiError::forbidden( + "only leader or above can create the 'human' difficulty tag", + )); + } let mut tx = state .pool .begin() @@ -533,6 +539,12 @@ pub(crate) async fn update_question_difficulty( "you do not have permission to update difficulty entries on this question", )); } + // "human" difficulty tag is restricted to leader or above. + if algorithm_tag == "human" && !access.is_admin_or_bot && !access.is_leader { + return Err(ApiError::forbidden( + "only leader or above can modify the 'human' difficulty tag", + )); + } let mut tx = state .pool .begin() @@ -603,6 +615,12 @@ pub(crate) async fn delete_question_difficulty( "you do not have permission to delete difficulty entries on this question", )); } + // "human" difficulty tag is restricted to leader or above. + if algorithm_tag == "human" && !access.is_admin_or_bot && !access.is_leader { + return Err(ApiError::forbidden( + "only leader or above can delete the 'human' difficulty tag", + )); + } let mut tx = state .pool .begin()