|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Copyright (C) 2025 CERN. |
| 4 | +# |
| 5 | +# ZenodoRDM is free software; you can redistribute it and/or modify it |
| 6 | +# under the terms of the MIT License; see LICENSE file for more details. |
| 7 | + |
| 8 | +"""Test search API page size validation.""" |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def search_url(test_app): |
| 15 | + """Search API URL.""" |
| 16 | + host = test_app.config["SITE_API_URL"] |
| 17 | + return f"{host}/records" |
| 18 | + |
| 19 | + |
| 20 | +def test_guest_page_size_validation(client, search_url): |
| 21 | + """Guest users limited to 25 results per page.""" |
| 22 | + # Guest: size=25 works |
| 23 | + res = client.get(f"{search_url}?size=25") |
| 24 | + assert res.status_code == 200 |
| 25 | + |
| 26 | + # Guest: size=26 exceeds limit |
| 27 | + res = client.get(f"{search_url}?size=26") |
| 28 | + assert res.status_code == 400 |
| 29 | + assert res.json == { |
| 30 | + "status": 400, |
| 31 | + "message": "A validation error occurred.", |
| 32 | + "errors": [ |
| 33 | + { |
| 34 | + "field": "size", |
| 35 | + "messages": [ |
| 36 | + "Page size cannot be greater than 25. Please use authenticated requests to increase the limit to 100." |
| 37 | + ], |
| 38 | + } |
| 39 | + ], |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | +def test_authenticated_page_size_validation(client_with_login, search_url): |
| 44 | + """Authenticated users limited to 100 results per page.""" |
| 45 | + # Auth: size=100 works |
| 46 | + res = client_with_login.get(f"{search_url}?size=100") |
| 47 | + assert res.status_code == 200 |
| 48 | + |
| 49 | + # Auth: size=101 exceeds limit |
| 50 | + res = client_with_login.get(f"{search_url}?size=101") |
| 51 | + assert res.status_code == 400 |
| 52 | + assert res.json == { |
| 53 | + "status": 400, |
| 54 | + "message": "A validation error occurred.", |
| 55 | + "errors": [ |
| 56 | + { |
| 57 | + "field": "size", |
| 58 | + "messages": ["Page size cannot be greater than 100."], |
| 59 | + } |
| 60 | + ], |
| 61 | + } |
0 commit comments