Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Add Test
Browse files Browse the repository at this point in the history
  • Loading branch information
erikjohnston committed Dec 9, 2022
1 parent b53fef9 commit 99eadb2
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions tests/handlers/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

from synapse.api.errors import NotFoundError, SynapseError
from synapse.handlers.device import MAX_DEVICE_DISPLAY_NAME_LEN, DeviceHandler
from synapse.rest import admin
from synapse.rest.client import account, login
from synapse.server import HomeServer
from synapse.util import Clock

Expand All @@ -30,6 +32,12 @@


class DeviceTestCase(unittest.HomeserverTestCase):
servlets = [
login.register_servlets,
admin.register_servlets,
account.register_servlets,
]

def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
hs = self.setup_test_homeserver("server", federation_http_client=None)
handler = hs.get_device_handler()
Expand Down Expand Up @@ -229,6 +237,29 @@ def test_update_unknown_device(self) -> None:
NotFoundError,
)

def test_login_delete_old_devices(self) -> None:
"""Delete old devices if the user already has too many."""

user_id = self.register_user("user", "pass")

# Create a bunch of devices
for _ in range(50):
self.login("user", "pass")
self.reactor.advance(1)

# Advance the clock for ages (as we only delete old devices)
self.reactor.advance(60 * 60 * 24 * 300)

# Log in again to start the pruning
self.login("user", "pass")

# Give the background job time to do its thing
self.reactor.pump([1.0] * 100)

# We should now only have the most recent device.
devices = self.get_success(self.handler.get_devices_by_user(user_id))
self.assertEqual(len(devices), 1)

def _record_users(self) -> None:
# check this works for both devices which have a recorded client_ip,
# and those which don't.
Expand Down

0 comments on commit 99eadb2

Please sign in to comment.