Repository: couchers-org/couchers
Issue: Fuzz joined and last active time

These should be fuzzed a bit so that you can't see exactly if someone was just active

https://github.com/Couchers-org/couchers/blob/8b9bb7ab4c5552b30acb8013050e8c11fa002d32/app/backend/src/couchers/models.py#L73-L81

Changed files:
app/backend/src/tests/test_models.py, app/backend/src/couchers/models.py

diff --git a/app/backend/src/couchers/models.py b/app/backend/src/couchers/models.py
index b8499ef78..25f5b9bd8 100644
--- a/app/backend/src/couchers/models.py
+++ b/app/backend/src/couchers/models.py
@@ -80,13 +80,18 @@ class User(Base):
 
     @property
     def display_joined(self):
-        # TODO: cruden a bit
-        return self.joined
+        """
+        Returns the last active time rounded down to the nearest hour.
+        """
+        return self.joined.replace(minute=0, second=0, microsecond=0)
 
     @property
     def display_last_active(self):
-        # TODO(aapeli): return as crude (e.g. up to 12 hour accuracy) timestamp
-        return self.last_active
+        """
+        Returns the last active time rounded down to the nearest 15 minutes.
+        """
+        return self.last_active.replace(minute=(self.last_active.minute // 15) * 15,
+                                        second=0, microsecond=0)
 
     def __repr__(self):
         return f"User(id={self.id}, email={self.email}, username={self.username})"
diff --git a/app/backend/src/tests/test_models.py b/app/backend/src/tests/test_models.py
index f88f9d5dd..eef7751a6 100644
--- a/app/backend/src/tests/test_models.py
+++ b/app/backend/src/tests/test_models.py
@@ -1,5 +1,5 @@
 from unittest.mock import patch
-from datetime import date
+from datetime import date, datetime
 
 from couchers.models import User
 
@@ -9,8 +9,28 @@ class FakeDate(date):
         return date.__new__(date, *args, **kwargs)
 
 @patch("couchers.models.date", FakeDate)
-def test_user_birthdate():
+def test_user_age():
     FakeDate.today = classmethod(lambda cls: date(2019, 7, 5))
     assert User(birthdate = date(1990, 7, 4)).age == 29
     assert User(birthdate = date(1990, 7, 31)).age == 28
     assert User(birthdate = date(1992, 2, 29)).age == 27
+
+def test_user_display_joined():
+    assert (User(joined=datetime(2020, 7, 10, 16, 34, 1, 1))
+            .display_joined == datetime(2020, 7, 10, 16, 0, 0, 0))
+    assert (User(joined=datetime(2025, 7, 10, 16, 59, 1, 1))
+            .display_joined == datetime(2025, 7, 10, 16, 0, 0, 0))
+    assert (User(joined=datetime(2020, 7, 10, 16, 0, 1, 1))
+            .display_joined == datetime(2020, 7, 10, 16, 0, 0, 0))
+    assert (User(joined=datetime(2020, 7, 10, 0, 0, 0, 0))
+            .display_joined == datetime(2020, 7, 10, 0, 0, 0, 0))
+
+def test_user_display_last_active():
+    assert (User(last_active=datetime(2020, 7, 10, 16, 34, 1, 1))
+            .display_last_active == datetime(2020, 7, 10, 16, 30, 0, 0))
+    assert (User(last_active=datetime(2025, 7, 10, 16, 59, 1, 1))
+            .display_last_active == datetime(2025, 7, 10, 16, 45, 0, 0))
+    assert (User(last_active=datetime(2020, 7, 10, 16, 16, 1, 1))
+            .display_last_active == datetime(2020, 7, 10, 16, 15, 0, 0))
+    assert (User(last_active=datetime(2020, 7, 10, 0, 0, 0, 0))
+            .display_last_active == datetime(2020, 7, 10, 0, 0, 0, 0))