|
13 | 13 | # limitations under the License.
|
14 | 14 | from http import HTTPStatus
|
15 | 15 |
|
| 16 | +from twisted.test.proto_helpers import MemoryReactor |
| 17 | + |
| 18 | +from synapse.api.errors import NotFoundError |
16 | 19 | from synapse.rest import admin, devices, room, sync
|
17 | 20 | from synapse.rest.client import account, login, register
|
| 21 | +from synapse.server import HomeServer |
| 22 | +from synapse.util import Clock |
18 | 23 |
|
19 | 24 | from tests import unittest
|
20 | 25 |
|
@@ -157,3 +162,41 @@ def test_not_receiving_local_device_list_changes(self) -> None:
|
157 | 162 | self.assertNotIn(
|
158 | 163 | alice_user_id, changed_device_lists, bob_sync_channel.json_body
|
159 | 164 | )
|
| 165 | + |
| 166 | + |
| 167 | +class DevicesTestCase(unittest.HomeserverTestCase): |
| 168 | + servlets = [ |
| 169 | + admin.register_servlets, |
| 170 | + login.register_servlets, |
| 171 | + sync.register_servlets, |
| 172 | + ] |
| 173 | + |
| 174 | + def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None: |
| 175 | + self.handler = hs.get_device_handler() |
| 176 | + |
| 177 | + @unittest.override_config({"delete_stale_devices_after": 72000000}) |
| 178 | + def test_delete_stale_devices(self) -> None: |
| 179 | + """Tests that stale devices are automatically removed after a set time of |
| 180 | + inactivity. |
| 181 | + The configuration is set to delete devices that haven't been used in the past 20h. |
| 182 | + """ |
| 183 | + # Register a user and creates 2 devices for them. |
| 184 | + user_id = self.register_user("user", "password") |
| 185 | + tok1 = self.login("user", "password", device_id="abc") |
| 186 | + tok2 = self.login("user", "password", device_id="def") |
| 187 | + |
| 188 | + # Sync them so they have a last_seen value. |
| 189 | + self.make_request("GET", "/sync", access_token=tok1) |
| 190 | + self.make_request("GET", "/sync", access_token=tok2) |
| 191 | + |
| 192 | + # Advance half a day and sync again with one of the devices, so that the next |
| 193 | + # time the background job runs we don't delete this device (since it will look |
| 194 | + # for devices that haven't been used for over an hour). |
| 195 | + self.reactor.advance(43200) |
| 196 | + self.make_request("GET", "/sync", access_token=tok1) |
| 197 | + |
| 198 | + # Advance another half a day, and check that the device that has synced still |
| 199 | + # exists but the one that hasn't has been removed. |
| 200 | + self.reactor.advance(43200) |
| 201 | + self.get_success(self.handler.get_device(user_id, "abc")) |
| 202 | + self.get_failure(self.handler.get_device(user_id, "def"), NotFoundError) |
0 commit comments