Production
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
"""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.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)
|
||||
|
||||
|
||||
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))
|
||||
|
||||
def test_tomorrow_is_rejected(self, at):
|
||||
at(2026, 9, 21, 1, 0)
|
||||
with pytest.raises(ValidationError):
|
||||
_service()._validate_report_date(date(2026, 9, 22))
|
||||
|
||||
|
||||
def test_local_helpers_use_the_configured_timezone():
|
||||
from app.common.utils import local_now
|
||||
|
||||
assert str(local_now().tzinfo) == "Asia/Kolkata"
|
||||
Reference in New Issue
Block a user