Self hosted FLOSS fitness/workout, nutrition and weight tracker
Test the day derivation against a user in a far-away zone
Roland Geider9bef89b45eb3
diff --git a/wger/core/fixtures/test-user-data.README b/wger/core/fixtures/test-user-data.READMEindex 9c58f137e..bfbc58cc7 100644--- a/wger/core/fixtures/test-user-data.README+++ b/wger/core/fixtures/test-user-data.README@@ -18,7 +18,7 @@ Gym 1 - Test 123#10 manager2#12 general_manager1#2 test-#14 member1+#14 member1 (time_zone Pacific/Auckland; use for day/timezone tests)#15 member2#16 member3#17 member4diff --git a/wger/core/fixtures/test-user-data.json b/wger/core/fixtures/test-user-data.jsonindex e614f0972..45a6b41a0 100644--- a/wger/core/fixtures/test-user-data.json+++ b/wger/core/fixtures/test-user-data.json@@ -853,6 +853,7 @@"workout_reminder_active": false,"work_intensity": "1","user": 14,+ "time_zone": "Pacific/Auckland","work_hours": 8,"freetime_intensity": "1","gender": "1",diff --git a/wger/gym/tests/test_user_last_activity.py b/wger/gym/tests/test_user_last_activity.pyindex e8ff3cca2..1425203b9 100644--- a/wger/gym/tests/test_user_last_activity.py+++ b/wger/gym/tests/test_user_last_activity.py@@ -14,6 +14,7 @@# Standard Libraryimport datetime+import zoneinfo# Djangofrom django.contrib.auth.models import User@@ -91,3 +92,25 @@ class UserLastActivityTestCase(WgerTestCase):self.add_session(datetime.date(2024, 3, 10))self.assertEqual(get_user_last_activity(self.user), datetime.date(2024, 3, 10))+++class UserLastActivityTimezoneTestCase(WgerTestCase):+ """+ Test that the activity day is derived in the member's zone, not the viewer's+ """++ def test_the_day_is_the_members_one(self):+ """14:00 UTC is already the next day for member1 (Pacific/Auckland)"""++ member = User.objects.get(username='member1')+ WorkoutLog.objects.filter(user=member).delete()+ WorkoutSession.objects.filter(user=member).delete()++ WorkoutSession.objects.create(+ user=member,+ datetime_start=datetime.datetime(2024, 6, 19, 14, 0, tzinfo=datetime.timezone.utc),+ )++ # The active zone is the viewer's (e.g. a trainer) and must not matter+ with timezone.override(zoneinfo.ZoneInfo('America/Denver')):+ self.assertEqual(get_user_last_activity(member), datetime.date(2024, 6, 20))diff --git a/wger/manager/tests/test_workout_session.py b/wger/manager/tests/test_workout_session.pyindex 6b01c59a0..859952bbf 100644--- a/wger/manager/tests/test_workout_session.py+++ b/wger/manager/tests/test_workout_session.py@@ -14,8 +14,10 @@# Standard Libraryimport datetime+import zoneinfo# Django+from django.contrib.auth.models import Userfrom django.db import (IntegrityError,transaction,@@ -128,6 +130,70 @@ class WorkoutSessionDurationTestCase(WgerTestCase):self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)+class WorkoutSessionLocalDayTestCase(WgerTestCase):+ """+ Test in whose timezone the day of a session is derived+ """++ MEMBER1 = 14+ """Fixture user with time_zone Pacific/Auckland"""++ TEST_USER = 2+ """Fixture user without a reported zone"""++ @staticmethod+ def session_at(user_id, start):+ return WorkoutSession.objects.create(+ user_id=user_id,+ datetime_start=start,+ )++ def test_east_of_the_server_the_day_is_the_next_one(self):+ """14:00 UTC is already the following day in Auckland"""++ session = self.session_at(+ self.MEMBER1,+ datetime.datetime(2024, 6, 19, 14, 0, tzinfo=datetime.timezone.utc),+ )++ self.assertEqual(session.local_day, datetime.date(2024, 6, 20))++ def test_west_of_the_server_the_day_is_the_previous_one(self):+ """01:30 UTC is still the previous evening in Denver"""++ profile = User.objects.get(pk=self.TEST_USER).userprofile+ profile.time_zone = 'America/Denver'+ profile.save()++ session = self.session_at(+ self.TEST_USER,+ datetime.datetime(2024, 6, 19, 1, 30, tzinfo=datetime.timezone.utc),+ )++ self.assertEqual(session.local_day, datetime.date(2024, 6, 18))++ def test_the_viewers_zone_does_not_leak_in(self):+ """The day belongs to the session's user, not to whoever is asking"""++ session = self.session_at(+ self.MEMBER1,+ datetime.datetime(2024, 6, 19, 14, 0, tzinfo=datetime.timezone.utc),+ )++ with timezone.override(zoneinfo.ZoneInfo('America/Denver')):+ self.assertEqual(session.local_day, datetime.date(2024, 6, 20))++ def test_without_a_zone_the_instance_zone_decides(self):+ """23:30 UTC is the next day in Europe/Berlin, the instance zone"""++ session = self.session_at(+ self.TEST_USER,+ datetime.datetime(2024, 6, 19, 23, 30, tzinfo=datetime.timezone.utc),+ )++ self.assertEqual(session.local_day, datetime.date(2024, 6, 20))++class WorkoutSessionIntervalConstraintTestCase(WgerTestCase):"""Test the database constraint on the session intervaldiff --git a/wger/trophies/tests/test_services.py b/wger/trophies/tests/test_services.pyindex 81e2bda10..fec7a3462 100644--- a/wger/trophies/tests/test_services.py+++ b/wger/trophies/tests/test_services.py@@ -172,6 +172,75 @@ class UserStatisticsServiceTestCase(WgerTestCase):self.assertEqual(stats.total_weight_lifted, Decimal('0'))+class StatisticsTimezoneTestCase(WgerTestCase):+ """+ Test that the day-based statistics follow the zone of the user they belong to+ """++ def setUp(self):+ super().setUp()+ # member1 carries time_zone Pacific/Auckland in the fixtures+ self.member = User.objects.get(username='member1')+ WorkoutLog.objects.filter(user=self.member).delete()+ WorkoutSession.objects.filter(user=self.member).delete()+ UserStatistics.objects.filter(user=self.member).delete()++ def make_session(self, start, end=None):+ return WorkoutSession.objects.create(+ user=self.member,+ datetime_start=start,+ datetime_end=end,+ )++ @staticmethod+ def utc(*args):+ return datetime.datetime(*args, tzinfo=datetime.timezone.utc)++ def test_streak_days_split_in_the_users_zone(self):+ """Two sessions on one server day are two consecutive Auckland days"""++ # 23:00 and 01:00 on the Auckland clock, around midnight+ self.make_session(self.utc(2024, 6, 19, 11, 0))+ self.make_session(self.utc(2024, 6, 19, 13, 0))++ stats = UserStatisticsService.update_statistics(self.member)++ self.assertEqual(stats.longest_streak, 2)+ self.assertEqual(stats.last_workout_date, datetime.date(2024, 6, 20))++ def test_a_zone_change_moves_the_days(self):+ """A recalculation reads the whole history in the newly reported zone"""++ # 02:00 in Auckland on Jun 20, 08:00 in Denver on Jun 19+ self.make_session(self.utc(2024, 6, 19, 14, 0))++ stats = UserStatisticsService.update_statistics(self.member)+ self.assertEqual(stats.last_workout_date, datetime.date(2024, 6, 20))++ profile = self.member.userprofile+ profile.time_zone = 'America/Denver'+ profile.save()++ stats = UserStatisticsService.update_statistics(self.member)+ self.assertEqual(stats.last_workout_date, datetime.date(2024, 6, 19))++ def test_the_workout_time_follows_the_local_clock_across_dst(self):+ """01:30 UTC on the spring-forward day is 03:30 in Berlin, not 02:30"""++ profile = self.member.userprofile+ profile.time_zone = 'Europe/Berlin'+ profile.save()++ self.make_session(+ self.utc(2024, 3, 31, 1, 30),+ self.utc(2024, 3, 31, 2, 30),+ )++ stats = UserStatisticsService.update_statistics(self.member)++ self.assertEqual(stats.earliest_workout_time, datetime.time(3, 30))++class TrophyServiceTestCase(WgerTestCase):"""Test the TrophyService