fix: parse malformed schedule_days string from Robot Framework

Robot Framework's Create Dictionary converts ['mon', 'tue'] into a
string. Add _parse_schedule_days to handle JSON/Python-literal parsing
before Pydantic type validation.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
2026-06-27 16:50:29 +02:00
parent 4687a06374
commit e5a5a6009a
3 changed files with 37 additions and 14 deletions

View File

@@ -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`

View File

@@ -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]:

View File

@@ -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.