80 lines
2.8 KiB
Python
80 lines
2.8 KiB
Python
"""Visit times and report dates are judged in the company's timezone, not the server's UTC.
|
|
|
|
Regression: the server clock is UTC, so at 12:35 IST (07:05 UTC) an MR who logged a
|
|
09:00 visit was told it was "in the future", and around midnight IST "today" was
|
|
still yesterday.
|
|
"""
|
|
|
|
from datetime import date, datetime, time
|
|
from types import SimpleNamespace
|
|
from zoneinfo import ZoneInfo
|
|
|
|
import pytest
|
|
|
|
from app.common.enums import ReportType
|
|
from app.common.exceptions import ValidationError
|
|
from app.reports import service as svc
|
|
|
|
IST = ZoneInfo("Asia/Kolkata")
|
|
|
|
|
|
@pytest.fixture
|
|
def at(monkeypatch):
|
|
"""Freeze 'now' to a given IST moment and switch the production-only rule on."""
|
|
|
|
def _freeze(year, month, day, hour, minute):
|
|
now = datetime(year, month, day, hour, minute, tzinfo=IST)
|
|
monkeypatch.setattr(svc, "local_now", lambda: now)
|
|
monkeypatch.setattr(svc, "local_today", lambda: now.date())
|
|
monkeypatch.setattr(svc.settings, "ENVIRONMENT", "production")
|
|
return now
|
|
|
|
return _freeze
|
|
|
|
|
|
def _service() -> svc.DCRService:
|
|
return svc.DCRService.__new__(svc.DCRService) # the checks under test need no database
|
|
|
|
|
|
def _report(day: date) -> SimpleNamespace:
|
|
return SimpleNamespace(report_date=day, report_type=ReportType.ACTUAL)
|
|
|
|
|
|
class TestVisitTime:
|
|
def test_a_morning_visit_is_accepted_at_lunchtime_ist(self, at):
|
|
at(2026, 9, 21, 12, 35) # 07:05 UTC — the old code rejected everything after 07:05
|
|
_service()._validate_visit_time(_report(date(2026, 9, 21)), time(9, 0))
|
|
|
|
def test_a_visit_later_today_is_still_rejected(self, at):
|
|
at(2026, 9, 21, 12, 35)
|
|
with pytest.raises(ValidationError):
|
|
_service()._validate_visit_time(_report(date(2026, 9, 21)), time(18, 0))
|
|
|
|
def test_past_days_accept_any_time(self, at):
|
|
at(2026, 9, 21, 12, 35)
|
|
_service()._validate_visit_time(_report(date(2026, 9, 20)), time(23, 59))
|
|
|
|
|
|
class TestReportDate:
|
|
def test_today_is_todays_ist_date_just_after_midnight(self, at):
|
|
at(2026, 9, 21, 1, 0) # still 20 Sep in UTC
|
|
_service()._validate_report_date(date(2026, 9, 21), ReportType.ACTUAL)
|
|
|
|
def test_tomorrow_is_rejected_for_a_done_or_self_report(self, at):
|
|
at(2026, 9, 21, 1, 0)
|
|
for report_type in (ReportType.ACTUAL, ReportType.SELF):
|
|
with pytest.raises(ValidationError):
|
|
_service()._validate_report_date(date(2026, 9, 22), report_type)
|
|
|
|
def test_a_plan_may_be_made_up_to_two_days_ahead(self, at):
|
|
at(2026, 9, 21, 1, 0)
|
|
_service()._validate_report_date(date(2026, 9, 23), ReportType.PLAN)
|
|
with pytest.raises(ValidationError):
|
|
_service()._validate_report_date(date(2026, 9, 24), ReportType.PLAN)
|
|
|
|
|
|
def test_local_helpers_use_the_configured_timezone():
|
|
from app.common.utils import local_now
|
|
|
|
assert str(local_now().tzinfo) == "Asia/Kolkata"
|