Skip to content
Snippets Groups Projects
Verified Commit a4c6a71a authored by Eliot Berriot's avatar Eliot Berriot
Browse files

Can now limit the number of results returned by the /domains endpoint

parent 4277e58e
No related branches found
No related tags found
No related merge requests found
......@@ -21,6 +21,7 @@ domain_filters = {
"open_registrations": fields.Bool(),
"anonymous_can_listen": fields.Bool(),
"federation_enabled": fields.Bool(),
"limit": fields.Integer(),
}
......@@ -48,11 +49,14 @@ async def index(request):
async def domains(request):
if request.method == "GET":
filters = await parser.parse(domain_filters, request)
limit = filters.pop("limit", 0)
rows = await db.get_domains(request["conn"], private=False, **filters)
total = len(rows)
if limit:
rows = rows[:limit]
payload = {
"count": len(rows),
"count": total,
"previous": None,
"next": None,
"results": [
......
......@@ -29,6 +29,29 @@ async def test_domains_get(db_conn, client, factories):
}
async def test_domains_get_page_size(db_conn, client, factories):
checks = sorted(
[
await factories["Check"].c(private=False),
await factories["Check"].c(private=False),
],
key=lambda o: o["domain"],
)
for check in checks:
domain = await db.get_domain(db_conn, check["domain"])
check["first_seen"] = domain["first_seen"]
check["node_name"] = domain["node_name"]
resp = await client.get("/api/domains", params={"limit": 1})
assert resp.status == 200
assert await resp.json() == {
"count": 2,
"next": None,
"previous": None,
"results": [serializers.serialize_domain_from_check(checks[0])],
}
@pytest.mark.parametrize(
"field", ["up", "open_registrations", "anonymous_can_listen", "federation_enabled"]
)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment