diff --git a/flask_mongoengine/pagination/basic_pagination.py b/flask_mongoengine/pagination/basic_pagination.py index 035077d4..9cd0e11a 100644 --- a/flask_mongoengine/pagination/basic_pagination.py +++ b/flask_mongoengine/pagination/basic_pagination.py @@ -23,6 +23,9 @@ def __init__( :param first_page_index: Option for change first page index. """ + if per_page <= 0: + raise ValueError("per_page must be a positive integer") + if page < first_page_index: abort(404, "Invalid page number.") diff --git a/flask_mongoengine/pagination/keyset_pagination.py b/flask_mongoengine/pagination/keyset_pagination.py index 3e3e48ed..804a8ef0 100644 --- a/flask_mongoengine/pagination/keyset_pagination.py +++ b/flask_mongoengine/pagination/keyset_pagination.py @@ -17,6 +17,9 @@ def __init__( :param per_page: Required number of documents per page. :param max_depth: Option for limit number of dereference documents. """ + if per_page <= 0: + raise ValueError("per_page must be a positive integer") + self.get_page(iterable, per_page, field_filter_by, last_field_value) def get_page( diff --git a/flask_mongoengine/pagination/list_field_pagination.py b/flask_mongoengine/pagination/list_field_pagination.py index f00db31e..704879c0 100644 --- a/flask_mongoengine/pagination/list_field_pagination.py +++ b/flask_mongoengine/pagination/list_field_pagination.py @@ -28,6 +28,9 @@ def __init__( first_page_index is option for change first page index. """ + if per_page <= 0: + raise ValueError("per_page must be a positive integer") + if page < first_page_index: abort(404, "Invalid page number.") diff --git a/tests/test_pagination.py b/tests/test_pagination.py index dc8c201b..5c76aad7 100644 --- a/tests/test_pagination.py +++ b/tests/test_pagination.py @@ -37,6 +37,38 @@ def test_queryset_paginator_invalid(app, todo): Pagination(iterable=Todo.objects, page=-1, per_page=10, first_page_index=0) +def test_per_page_validation(app, todo): + """Test that per_page validation works for all pagination classes""" + Todo = todo + + # Test Pagination with per_page=0 + with pytest.raises(ValueError, match="per_page must be a positive integer"): + Pagination(iterable=Todo.objects, page=1, per_page=0) + + # Test Pagination with negative per_page + with pytest.raises(ValueError, match="per_page must be a positive integer"): + Pagination(iterable=Todo.objects, page=1, per_page=-5) + + # Test KeysetPagination with per_page=0 + with pytest.raises(ValueError, match="per_page must be a positive integer"): + KeysetPagination(Todo.objects, per_page=0) + + # Test KeysetPagination with negative per_page + with pytest.raises(ValueError, match="per_page must be a positive integer"): + KeysetPagination(Todo.objects, per_page=-5) + + # Test ListFieldPagination with per_page=0 + comments = [f"comment: {i}" for i in range(10)] + test_todo = Todo(title="test", comments=comments).save() + + with pytest.raises(ValueError, match="per_page must be a positive integer"): + ListFieldPagination(Todo.objects, test_todo.id, "comments", 1, per_page=0) + + # Test ListFieldPagination with negative per_page + with pytest.raises(ValueError, match="per_page must be a positive integer"): + ListFieldPagination(Todo.objects, test_todo.id, "comments", 1, per_page=-5) + + def test_queryset_paginator(app, todo): Todo = todo paginator = Pagination(Todo.objects, 1, 10)