|
40 | 40 | user_directory, |
41 | 41 | ) |
42 | 42 | from synapse.server import HomeServer |
| 43 | +from synapse.storage.databases.main.client_ips import LAST_SEEN_GRANULARITY |
43 | 44 | from synapse.types import JsonDict, UserID, create_requester |
44 | 45 | from synapse.util import Clock |
45 | 46 |
|
@@ -456,6 +457,7 @@ class UsersListTestCase(unittest.HomeserverTestCase): |
456 | 457 | servlets = [ |
457 | 458 | synapse.rest.admin.register_servlets, |
458 | 459 | login.register_servlets, |
| 460 | + room.register_servlets, |
459 | 461 | ] |
460 | 462 | url = "/_synapse/admin/v2/users" |
461 | 463 |
|
@@ -506,6 +508,62 @@ def test_all_users(self) -> None: |
506 | 508 | # Check that all fields are available |
507 | 509 | self._check_fields(channel.json_body["users"]) |
508 | 510 |
|
| 511 | + def test_last_seen(self) -> None: |
| 512 | + """ |
| 513 | + Test that last_seen_ts field is properly working. |
| 514 | + """ |
| 515 | + user1 = self.register_user("u1", "pass") |
| 516 | + user1_token = self.login("u1", "pass") |
| 517 | + user2 = self.register_user("u2", "pass") |
| 518 | + user2_token = self.login("u2", "pass") |
| 519 | + user3 = self.register_user("u3", "pass") |
| 520 | + user3_token = self.login("u3", "pass") |
| 521 | + |
| 522 | + self.helper.create_room_as(self.admin_user, tok=self.admin_user_tok) |
| 523 | + self.reactor.advance(10) |
| 524 | + self.helper.create_room_as(user2, tok=user2_token) |
| 525 | + self.reactor.advance(10) |
| 526 | + self.helper.create_room_as(user1, tok=user1_token) |
| 527 | + self.reactor.advance(10) |
| 528 | + self.helper.create_room_as(user3, tok=user3_token) |
| 529 | + self.reactor.advance(10) |
| 530 | + |
| 531 | + channel = self.make_request( |
| 532 | + "GET", |
| 533 | + self.url, |
| 534 | + access_token=self.admin_user_tok, |
| 535 | + ) |
| 536 | + |
| 537 | + self.assertEqual(200, channel.code, msg=channel.json_body) |
| 538 | + self.assertEqual(4, len(channel.json_body["users"])) |
| 539 | + self.assertEqual(4, channel.json_body["total"]) |
| 540 | + |
| 541 | + admin_last_seen = channel.json_body["users"][0]["last_seen_ts"] |
| 542 | + user1_last_seen = channel.json_body["users"][1]["last_seen_ts"] |
| 543 | + user2_last_seen = channel.json_body["users"][2]["last_seen_ts"] |
| 544 | + user3_last_seen = channel.json_body["users"][3]["last_seen_ts"] |
| 545 | + self.assertTrue(admin_last_seen > 0 and admin_last_seen < 10000) |
| 546 | + self.assertTrue(user2_last_seen > 10000 and user2_last_seen < 20000) |
| 547 | + self.assertTrue(user1_last_seen > 20000 and user1_last_seen < 30000) |
| 548 | + self.assertTrue(user3_last_seen > 30000 and user3_last_seen < 40000) |
| 549 | + |
| 550 | + self._order_test([self.admin_user, user2, user1, user3], "last_seen_ts") |
| 551 | + |
| 552 | + self.reactor.advance(LAST_SEEN_GRANULARITY / 1000) |
| 553 | + self.helper.create_room_as(user1, tok=user1_token) |
| 554 | + self.reactor.advance(10) |
| 555 | + |
| 556 | + channel = self.make_request( |
| 557 | + "GET", |
| 558 | + self.url + "/" + user1, |
| 559 | + access_token=self.admin_user_tok, |
| 560 | + ) |
| 561 | + self.assertTrue( |
| 562 | + channel.json_body["last_seen_ts"] > 40000 + LAST_SEEN_GRANULARITY |
| 563 | + ) |
| 564 | + |
| 565 | + self._order_test([self.admin_user, user2, user3, user1], "last_seen_ts") |
| 566 | + |
509 | 567 | def test_search_term(self) -> None: |
510 | 568 | """Test that searching for a users works correctly""" |
511 | 569 |
|
@@ -1135,6 +1193,7 @@ def _check_fields(self, content: List[JsonDict]) -> None: |
1135 | 1193 | self.assertIn("displayname", u) |
1136 | 1194 | self.assertIn("avatar_url", u) |
1137 | 1195 | self.assertIn("creation_ts", u) |
| 1196 | + self.assertIn("last_seen_ts", u) |
1138 | 1197 |
|
1139 | 1198 | def _create_users(self, number_users: int) -> None: |
1140 | 1199 | """ |
@@ -3035,6 +3094,7 @@ def _check_fields(self, content: JsonDict) -> None: |
3035 | 3094 | self.assertIn("consent_version", content) |
3036 | 3095 | self.assertIn("consent_ts", content) |
3037 | 3096 | self.assertIn("external_ids", content) |
| 3097 | + self.assertIn("last_seen_ts", content) |
3038 | 3098 |
|
3039 | 3099 | # This key was removed intentionally. Ensure it is not accidentally re-included. |
3040 | 3100 | self.assertNotIn("password_hash", content) |
|
0 commit comments