fix: standardize UTC timestamp handling

This commit is contained in:
2026-07-08 10:07:08 +08:00
parent 22dd364405
commit 8d347cfbc2
8 changed files with 77 additions and 24 deletions
+48 -2
View File
@@ -10,16 +10,24 @@ from app import create_app
from app.config import Config
from app.extensions import db
from app.models import AppSetting, PasswordResetToken, UploadRecord, User
from app.time_utils import utc_now
class RegistrationRoutesTest(unittest.TestCase):
def create_test_app(self, temp_dir: str, *, allow_registration: bool):
def create_test_app(
self,
temp_dir: str,
*,
allow_registration: bool,
app_timezone: str = "Asia/Shanghai",
):
class TestConfig(Config):
TESTING = True
SECRET_KEY = "test-secret"
SECRET_KEY_GENERATED = False
SQLALCHEMY_DATABASE_URI = f"sqlite:///{temp_dir}/test.db"
ALLOW_REGISTRATION = allow_registration
APP_TIMEZONE = app_timezone
ADMIN_PASSWORD = None
return create_app(TestConfig, load_model_on_start=False)
@@ -422,7 +430,7 @@ class RegistrationRoutesTest(unittest.TestCase):
with app.app_context():
reset_token = PasswordResetToken.query.one()
reset_token.expires_at = datetime.utcnow() - timedelta(minutes=1)
reset_token.expires_at = utc_now() - timedelta(minutes=1)
db.session.commit()
client = app.test_client()
@@ -512,6 +520,22 @@ class RegistrationRoutesTest(unittest.TestCase):
self.assertIn("alice-file-01.xlsx", second_page)
self.assertIn("alice-file-00.xlsx", second_page)
def test_history_page_displays_utc_upload_time_in_configured_timezone(self) -> None:
with TemporaryDirectory() as temp_dir:
app = self.create_test_app(
temp_dir,
allow_registration=False,
app_timezone="America/New_York",
)
self.create_user(app, "alice", "secret123")
self.add_upload_records(app, "alice", 1)
client = app.test_client()
self.login(client, "alice", "secret123")
html = client.get("/history").get_data(as_text=True)
self.assertIn("2026-01-01 07:00:00", html)
def test_admin_page_paginates_upload_records(self) -> None:
with TemporaryDirectory() as temp_dir:
app = self.create_test_app(temp_dir, allow_registration=False)
@@ -531,6 +555,28 @@ class RegistrationRoutesTest(unittest.TestCase):
self.assertIn("alice-file-01.xlsx", second_page)
self.assertIn("alice-file-00.xlsx", second_page)
def test_admin_page_displays_utc_times_in_configured_timezone(self) -> None:
with TemporaryDirectory() as temp_dir:
app = self.create_test_app(
temp_dir,
allow_registration=False,
app_timezone="America/New_York",
)
self.create_user(app, "admin", "secret123", is_admin=True)
self.create_user(app, "alice", "secret123")
with app.app_context():
alice = User.query.filter_by(username="alice").one()
alice.created_at = datetime(2026, 1, 1, 0, 0, 0)
db.session.commit()
self.add_upload_records(app, "alice", 1)
client = app.test_client()
self.login(client, "admin", "secret123")
html = client.get("/admin").get_data(as_text=True)
self.assertIn("2025-12-31 19:00:00", html)
self.assertIn("2026-01-01 07:00:00", html)
if __name__ == "__main__":
unittest.main()