diff --git a/frigate/test/http_api/base_http_test.py b/frigate/test/http_api/base_http_test.py index 1efa3acdb1..fabd76a7e4 100644 --- a/frigate/test/http_api/base_http_test.py +++ b/frigate/test/http_api/base_http_test.py @@ -1,7 +1,10 @@ import datetime import logging import os +import struct +import tempfile import unittest +import zoneinfo from unittest.mock import patch from fastapi import Request @@ -20,6 +23,13 @@ from frigate.models import Event, Recordings, ReviewSegment from frigate.review.types import SeverityEnum from frigate.test.const import TEST_DB, TEST_DB_CLEANUPS, TEST_DB_PRAGMAS +# Minimal TZif file for a zone fixed at UTC+0 named "UTC". +UTC_TZIF = ( + struct.pack(">4s16x6l", b"TZif", 0, 0, 0, 0, 1, 4) + + struct.pack(">lBB", 0, 0, 0) + + b"UTC\0" +) + class AuthTestClient(TestClient): """TestClient that automatically adds auth headers to all requests.""" @@ -36,6 +46,23 @@ class AuthTestClient(TestClient): class BaseTestHttp(unittest.TestCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + + # Docker mounts of the host's /etc/localtime can overwrite the system + # zoneinfo/Etc/UTC file with a local zone. Shadow "UTC" with a known + # good file so the API's ZoneInfo("UTC") is real UTC. + tz_dir = tempfile.TemporaryDirectory() + with open(os.path.join(tz_dir.name, "UTC"), "wb") as f: + f.write(UTC_TZIF) + + zoneinfo.reset_tzpath(to=[tz_dir.name, *zoneinfo.TZPATH]) + zoneinfo.ZoneInfo.clear_cache() + cls.addClassCleanup(tz_dir.cleanup) + cls.addClassCleanup(zoneinfo.ZoneInfo.clear_cache) + cls.addClassCleanup(zoneinfo.reset_tzpath) + def setUp(self, models): # setup clean database for each test run migrate_db = SqliteDatabase(TEST_DB, pragmas=TEST_DB_PRAGMAS) diff --git a/frigate/test/http_api/test_http_review.py b/frigate/test/http_api/test_http_review.py index 8f24ba24d4..8afe242263 100644 --- a/frigate/test/http_api/test_http_review.py +++ b/frigate/test/http_api/test_http_review.py @@ -1,4 +1,4 @@ -from datetime import datetime, timedelta +from datetime import UTC, datetime, timedelta from fastapi import Request from peewee import DoesNotExist @@ -9,6 +9,11 @@ from frigate.review.types import SeverityEnum from frigate.test.http_api.base_http_test import AuthTestClient, BaseTestHttp +def utc_day(dt: datetime) -> str: + """Return the UTC calendar day the summary endpoint buckets dt into.""" + return dt.astimezone(UTC).strftime("%Y-%m-%d") + + class TestHttpReview(BaseTestHttp): def setUp(self): super().setUp([Event, Recordings, ReviewSegment, UserReviewStatus]) @@ -362,7 +367,7 @@ class TestHttpReview(BaseTestHttp): assert response_json["last24Hours"]["total_alert"] == 1 assert response_json["last24Hours"]["total_detection"] == 0 - today_formatted = datetime.today().strftime("%Y-%m-%d") + today_formatted = utc_day(datetime.now()) assert response_json[today_formatted]["total_alert"] == 1 assert response_json[today_formatted]["total_detection"] == 0 @@ -379,7 +384,7 @@ class TestHttpReview(BaseTestHttp): assert response.status_code == 200 response_json = response.json() # e.g. '2024-11-24' - today_formatted = datetime.today().strftime("%Y-%m-%d") + today_formatted = utc_day(datetime.now()) expected_response = { "last24Hours": { "reviewed_alert": 0, @@ -404,7 +409,7 @@ class TestHttpReview(BaseTestHttp): assert response.status_code == 200 response_json = response.json() # e.g. '2024-11-24' - today_formatted = datetime.today().strftime("%Y-%m-%d") + today_formatted = utc_day(datetime.now()) expected_response = { "last24Hours": { "reviewed_alert": 0, @@ -439,9 +444,9 @@ class TestHttpReview(BaseTestHttp): assert response.status_code == 200 response_json = response.json() # e.g. '2024-11-24' - today_formatted = now.strftime("%Y-%m-%d") + today_formatted = utc_day(now) # e.g. '2024-11-19' - five_days_ago_formatted = five_days_ago.strftime("%Y-%m-%d") + five_days_ago_formatted = utc_day(five_days_ago) expected_response = { "last24Hours": { "reviewed_alert": 0, @@ -491,9 +496,9 @@ class TestHttpReview(BaseTestHttp): assert response.status_code == 200 response_json = response.json() # e.g. '2024-11-24' - today_formatted = now.strftime("%Y-%m-%d") + today_formatted = utc_day(now) # e.g. '2024-11-19' - five_days_ago_formatted = five_days_ago.strftime("%Y-%m-%d") + five_days_ago_formatted = utc_day(five_days_ago) expected_response = { "last24Hours": { "reviewed_alert": 0, @@ -549,7 +554,7 @@ class TestHttpReview(BaseTestHttp): assert response.status_code == 200 response_json = response.json() # e.g. '2024-11-19' - five_days_ago_formatted = five_days_ago.strftime("%Y-%m-%d") + five_days_ago_formatted = utc_day(five_days_ago) expected_response = { "last24Hours": { "reviewed_alert": None,