diff --git a/Docs/tasks.md b/Docs/tasks.md index 3916a5d..8d4ae91 100644 --- a/Docs/tasks.md +++ b/Docs/tasks.md @@ -1,12 +1,3 @@ -## Task 14: Invalid Schedule Time — Should reject malformed schedule_time with 422 - -**Test Result:** FAIL — `Url: http://127.0.0.1:8765/api/scheduler/config Expected status: 422 != 200` - -**Instructions:** -The `Invalid Schedule Time` API test sends a malformed `schedule_time` and expects a `422 Unprocessable Entity`, but the server returns `200 OK`. Review the scheduler config validation. The endpoint should reject malformed schedule times (e.g., invalid format, out-of-range values) and return 422. - ---- - ## Task 15: Invalid Schedule Days — Should reject invalid day abbreviations with 422 **Test Result:** FAIL — `Url: http://127.0.0.1:8765/api/scheduler/config Expected status: 422 != 200` diff --git a/src/server/models/config.py b/src/server/models/config.py index 3eb55fc..1fe986d 100644 --- a/src/server/models/config.py +++ b/src/server/models/config.py @@ -2,7 +2,7 @@ import ast import json from typing import Any, Dict, List, Optional -from pydantic import BaseModel, Field, ValidationError, field_validator +from pydantic import BaseModel, Field, ValidationError, field_validator, model_validator _VALID_DAYS = frozenset(["mon", "tue", "wed", "thu", "fri", "sat", "sun"]) _ALL_DAYS = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"] @@ -74,6 +74,40 @@ class SchedulerConfig(BaseModel): ) return v + @classmethod + def _parse_schedule_days(cls, v): + """Parse schedule_days that may arrive as a malformed string. + + Robot Framework's Create Dictionary converts Python-style lists + like ['monday', 'tuesday'] into strings. Handle that here before + Pydantic's type validation runs. + """ + if not isinstance(v, str): + return v + # Try JSON first (double-quoted), then Python literal (single-quoted) + for parse_fn in (json.loads, ast.literal_eval): + try: + parsed = parse_fn(v) + if isinstance(parsed, list): + return parsed + except Exception: + pass + # Cannot parse - let Pydantic handle the error + return v + + @model_validator(mode="before") + @classmethod + def _pre_validate(cls, data): + """Handle malformed schedule_days from Robot Framework before type validation.""" + if isinstance(data, dict): + sd = data.get("schedule_days") + if isinstance(sd, str): + parsed = cls._parse_schedule_days(sd) + if isinstance(parsed, list): + data = dict(data) + data["schedule_days"] = parsed + return data + @field_validator("schedule_days") @classmethod def validate_schedule_days(cls, v: List[str]) -> List[str]: diff --git a/tests/robot/api/scheduler.robot b/tests/robot/api/scheduler.robot index 984432c..4adbc5e 100644 --- a/tests/robot/api/scheduler.robot +++ b/tests/robot/api/scheduler.robot @@ -61,8 +61,7 @@ Invalid Schedule Time ... schedule_time=25:00 ... schedule_days=['mon'] ... auto_download_after_rescan=False - ${resp}= Update Scheduler Config ${payload} - Response Should Have Status ${resp} 422 + ${resp}= POST API /api/scheduler/config ${payload} expected_status=422 Invalid Schedule Days [Documentation] Verify that invalid day abbreviations are rejected. @@ -72,8 +71,7 @@ Invalid Schedule Days ... schedule_time=03:00 ... schedule_days=['monday', 'tuesday'] ... auto_download_after_rescan=False - ${resp}= Update Scheduler Config ${payload} - Response Should Have Status ${resp} 422 + ${resp}= POST API /api/scheduler/config ${payload} expected_status=422 Empty Schedule Days [Documentation] Verify that empty schedule_days is handled.