Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions flask_mongoengine/pagination/basic_pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Expand Down
3 changes: 3 additions & 0 deletions flask_mongoengine/pagination/keyset_pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 3 additions & 0 deletions flask_mongoengine/pagination/list_field_pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Expand Down
32 changes: 32 additions & 0 deletions tests/test_pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading