Refactor duty authentication and event type handling
- Introduced a new function `get_authenticated_username` to centralize Mini App authentication logic, improving code readability and maintainability. - Updated the duty fetching logic to map unknown event types to "duty" for consistent API responses. - Enhanced the `get_duties` function to include duties starting on the last day of the specified date range. - Improved session management in the database layer to ensure rollback on exceptions. - Added tests to validate the new authentication flow and event type handling.
This commit is contained in:
@@ -131,3 +131,31 @@ def test_duties_e2e_auth_real_validation(client, monkeypatch):
|
||||
assert r.status_code == 200
|
||||
assert r.json() == []
|
||||
mock_fetch.assert_called_once_with("2025-01-01", "2025-01-31")
|
||||
|
||||
|
||||
@patch("api.app.config.MINI_APP_SKIP_AUTH", True)
|
||||
def test_duties_200_with_unknown_event_type_mapped_to_duty(client):
|
||||
"""When DB returns duty with event_type not in (duty, unavailable, vacation), API returns 200 with event_type='duty'."""
|
||||
from types import SimpleNamespace
|
||||
|
||||
fake_duty = SimpleNamespace(
|
||||
id=1,
|
||||
user_id=10,
|
||||
start_at="2025-01-15T09:00:00Z",
|
||||
end_at="2025-01-15T18:00:00Z",
|
||||
event_type="unknown",
|
||||
)
|
||||
|
||||
def fake_get_duties(session, from_date, to_date):
|
||||
return [(fake_duty, "User A")]
|
||||
|
||||
with patch("api.app.get_duties", side_effect=fake_get_duties):
|
||||
r = client.get(
|
||||
"/api/duties",
|
||||
params={"from": "2025-01-01", "to": "2025-01-31"},
|
||||
)
|
||||
assert r.status_code == 200
|
||||
data = r.json()
|
||||
assert len(data) == 1
|
||||
assert data[0]["event_type"] == "duty"
|
||||
assert data[0]["full_name"] == "User A"
|
||||
|
||||
@@ -91,3 +91,17 @@ def test_delete_duties_in_range_other_user_unchanged(session, user_a):
|
||||
remaining = get_duties(session, "2026-02-01", "2026-02-28")
|
||||
assert len(remaining) == 1
|
||||
assert remaining[0][1] == "User B"
|
||||
|
||||
|
||||
def test_get_duties_includes_duty_starting_on_last_day_of_range(session, user_a):
|
||||
"""Duty starting on to_date (e.g. 2026-01-31T09:00:00Z) must be included when to_date is 2026-01-31."""
|
||||
insert_duty(
|
||||
session,
|
||||
user_a.id,
|
||||
"2026-01-31T09:00:00Z",
|
||||
"2026-02-01T09:00:00Z",
|
||||
)
|
||||
rows = get_duties(session, "2026-01-01", "2026-01-31")
|
||||
assert len(rows) == 1
|
||||
assert rows[0][0].start_at == "2026-01-31T09:00:00Z"
|
||||
assert rows[0][1] == "User A"
|
||||
|
||||
Reference in New Issue
Block a user