refactor: streamline code formatting in API and tests
Some checks failed
CI / lint-and-test (push) Failing after 1m17s
Some checks failed
CI / lint-and-test (push) Failing after 1m17s
- Consolidated function call formatting in `admin_reassign_duty` for improved readability. - Enhanced test cases by standardizing header formatting in API requests, ensuring consistency across tests. - Improved overall code clarity and maintainability by adhering to established coding style guidelines.
This commit is contained in:
@@ -374,9 +374,7 @@ def admin_reassign_duty(
|
|||||||
status_code=400,
|
status_code=400,
|
||||||
detail=t(lang, "admin.user_not_found"),
|
detail=t(lang, "admin.user_not_found"),
|
||||||
)
|
)
|
||||||
updated = update_duty_user(
|
updated = update_duty_user(session, duty_id, body.user_id, commit=True)
|
||||||
session, duty_id, body.user_id, commit=True
|
|
||||||
)
|
|
||||||
if updated is None:
|
if updated is None:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404,
|
status_code=404,
|
||||||
|
|||||||
@@ -39,7 +39,12 @@ def test_admin_me_returns_is_admin_true_when_admin(
|
|||||||
mock_get_user.return_value = SimpleNamespace(full_name="Admin", username="admin")
|
mock_get_user.return_value = SimpleNamespace(full_name="Admin", username="admin")
|
||||||
mock_can_access.return_value = True
|
mock_can_access.return_value = True
|
||||||
mock_is_admin.return_value = True
|
mock_is_admin.return_value = True
|
||||||
r = client.get("/api/admin/me", headers={"X-Telegram-Init-Data": "auth_date=1&user=%7B%22id%22%3A100%7D&hash=x"})
|
r = client.get(
|
||||||
|
"/api/admin/me",
|
||||||
|
headers={
|
||||||
|
"X-Telegram-Init-Data": "auth_date=1&user=%7B%22id%22%3A100%7D&hash=x"
|
||||||
|
},
|
||||||
|
)
|
||||||
assert r.status_code == 200
|
assert r.status_code == 200
|
||||||
assert r.json() == {"is_admin": True}
|
assert r.json() == {"is_admin": True}
|
||||||
|
|
||||||
@@ -59,7 +64,12 @@ def test_admin_me_returns_is_admin_false_when_not_admin(
|
|||||||
mock_get_user.return_value = SimpleNamespace(full_name="User", username="user")
|
mock_get_user.return_value = SimpleNamespace(full_name="User", username="user")
|
||||||
mock_can_access.return_value = True
|
mock_can_access.return_value = True
|
||||||
mock_is_admin.return_value = False
|
mock_is_admin.return_value = False
|
||||||
r = client.get("/api/admin/me", headers={"X-Telegram-Init-Data": "auth_date=1&user=%7B%22id%22%3A200%7D&hash=x"})
|
r = client.get(
|
||||||
|
"/api/admin/me",
|
||||||
|
headers={
|
||||||
|
"X-Telegram-Init-Data": "auth_date=1&user=%7B%22id%22%3A200%7D&hash=x"
|
||||||
|
},
|
||||||
|
)
|
||||||
assert r.status_code == 200
|
assert r.status_code == 200
|
||||||
assert r.json() == {"is_admin": False}
|
assert r.json() == {"is_admin": False}
|
||||||
|
|
||||||
@@ -91,7 +101,9 @@ def test_admin_users_403_when_not_admin(
|
|||||||
mock_is_admin.return_value = False # not admin
|
mock_is_admin.return_value = False # not admin
|
||||||
r = client.get(
|
r = client.get(
|
||||||
"/api/admin/users",
|
"/api/admin/users",
|
||||||
headers={"X-Telegram-Init-Data": "auth_date=1&user=%7B%22id%22%3A100%7D&hash=x"},
|
headers={
|
||||||
|
"X-Telegram-Init-Data": "auth_date=1&user=%7B%22id%22%3A100%7D&hash=x"
|
||||||
|
},
|
||||||
)
|
)
|
||||||
assert r.status_code == 403
|
assert r.status_code == 403
|
||||||
detail = r.json()["detail"]
|
detail = r.json()["detail"]
|
||||||
@@ -239,7 +251,9 @@ def test_admin_reassign_400_when_user_not_found(
|
|||||||
r = client.patch(
|
r = client.patch(
|
||||||
"/api/admin/duties/1",
|
"/api/admin/duties/1",
|
||||||
json={"user_id": 999},
|
json={"user_id": 999},
|
||||||
headers={"X-Telegram-Init-Data": "auth_date=1&user=%7B%22id%22%3A1%7D&hash=x"},
|
headers={
|
||||||
|
"X-Telegram-Init-Data": "auth_date=1&user=%7B%22id%22%3A1%7D&hash=x"
|
||||||
|
},
|
||||||
)
|
)
|
||||||
assert r.status_code == 400
|
assert r.status_code == 400
|
||||||
mock_update.assert_not_called()
|
mock_update.assert_not_called()
|
||||||
@@ -293,7 +307,9 @@ def test_admin_reassign_200_updates_and_invalidates(
|
|||||||
r = client.patch(
|
r = client.patch(
|
||||||
"/api/admin/duties/1",
|
"/api/admin/duties/1",
|
||||||
json={"user_id": 2},
|
json={"user_id": 2},
|
||||||
headers={"X-Telegram-Init-Data": "auth_date=1&user=%7B%22id%22%3A1%7D&hash=x"},
|
headers={
|
||||||
|
"X-Telegram-Init-Data": "auth_date=1&user=%7B%22id%22%3A1%7D&hash=x"
|
||||||
|
},
|
||||||
)
|
)
|
||||||
assert r.status_code == 200
|
assert r.status_code == 200
|
||||||
data = r.json()
|
data = r.json()
|
||||||
|
|||||||
Reference in New Issue
Block a user