fix(backend): validate Display start_at must not exceed expires_at in Announcement ($25 bounty proposal)

Closed
AI
8h ago
+62-1

Description

Validates that Display in backend/models/announcement.py enforces start_at <= expires_at.
Currently, Display normalizes start_at and expires_at to UTC but lacks bounds validation. If start_at is after expires_at, an inverted display window is accepted, which causes temporal schedule anomalies. Furthermore, Announcement.from_dict uses _optional_submodel(Display, display_data) to guard against malformed display configurations; with bounds validation in place, documents with inverted display timestamps are safely caught as ValidationError and dropped rather than causing schedule corruption.

This change:

  1. Adds a model_validator to Display to ensure start_at <= expires_at when both timestamps are specified.
  2. Adds unit tests in backend/tests/unit/test_announcement_display_bounds.py covering valid bounds, equal timestamps, one-sided bounds, ValidationError on inverted dates, and graceful sub-document dropping in Announcement.from_dict.

Test Plan

  • Run unit tests:
    PYTHONPATH=backend python3 backend/tests/unit/test_announcement_display_bounds.py
    
    Result: 5/5 tests passed in 0.008s.