Add field `total` to device list in admin API (#8644)

pull/8657/head
Dirk Klimpel 2020-10-26 15:07:51 +01:00 committed by GitHub
parent 7b13780c54
commit 913f8a06e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 2 deletions

1
changelog.d/8644.misc Normal file
View File

@ -0,0 +1 @@
Add field `total` to device list in admin API.

View File

@ -375,7 +375,8 @@ A response body like the following is returned:
"last_seen_ts": 1474491775025,
"user_id": "<user_id>"
}
]
],
"total": 2
}
**Parameters**
@ -400,6 +401,8 @@ The following fields are returned in the JSON response body:
devices was last seen. (May be a few minutes out of date, for efficiency reasons).
- ``user_id`` - Owner of device.
- ``total`` - Total number of user's devices.
Delete multiple devices
------------------
Deletes the given devices for a specific ``user_id``, and invalidates

View File

@ -119,7 +119,7 @@ class DevicesRestServlet(RestServlet):
raise NotFoundError("Unknown user")
devices = await self.device_handler.get_devices_by_user(target_user.to_string())
return 200, {"devices": devices}
return 200, {"devices": devices, "total": len(devices)}
class DeleteDevicesRestServlet(RestServlet):

View File

@ -393,6 +393,22 @@ class DevicesRestTestCase(unittest.HomeserverTestCase):
self.assertEqual(400, channel.code, msg=channel.json_body)
self.assertEqual("Can only lookup local users", channel.json_body["error"])
def test_user_has_no_devices(self):
"""
Tests that a normal lookup for devices is successfully
if user has no devices
"""
# Get devices
request, channel = self.make_request(
"GET", self.url, access_token=self.admin_user_tok,
)
self.render(request)
self.assertEqual(200, channel.code, msg=channel.json_body)
self.assertEqual(0, channel.json_body["total"])
self.assertEqual(0, len(channel.json_body["devices"]))
def test_get_devices(self):
"""
Tests that a normal lookup for devices is successfully
@ -409,6 +425,7 @@ class DevicesRestTestCase(unittest.HomeserverTestCase):
self.render(request)
self.assertEqual(200, channel.code, msg=channel.json_body)
self.assertEqual(number_devices, channel.json_body["total"])
self.assertEqual(number_devices, len(channel.json_body["devices"]))
self.assertEqual(self.other_user, channel.json_body["devices"][0]["user_id"])
# Check that all fields are available