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

Commit

Permalink
Add field total to device list in admin API (#8644)
Browse files Browse the repository at this point in the history
  • Loading branch information
dklimpel authored Oct 26, 2020
1 parent 7b13780 commit 913f8a0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog.d/8644.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add field `total` to device list in admin API.
5 changes: 4 additions & 1 deletion docs/admin_api/user_admin_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,8 @@ A response body like the following is returned:
"last_seen_ts": 1474491775025,
"user_id": "<user_id>"
}
]
],
"total": 2
}
**Parameters**
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion synapse/rest/admin/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ async def on_GET(self, request, user_id):
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):
Expand Down
17 changes: 17 additions & 0 deletions tests/rest/admin/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,22 @@ def test_user_is_not_local(self):
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
Expand All @@ -409,6 +425,7 @@ def test_get_devices(self):
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
Expand Down

0 comments on commit 913f8a0

Please sign in to comment.