diff --git a/src/backend/core/api/viewsets.py b/src/backend/core/api/viewsets.py index 8539ca77..b467b06b 100644 --- a/src/backend/core/api/viewsets.py +++ b/src/backend/core/api/viewsets.py @@ -12,6 +12,7 @@ from django.http import FileResponse from rest_framework import ( decorators, exceptions, + filters, mixins, pagination, status, @@ -138,6 +139,10 @@ class UserViewSet( class ResourceViewsetMixin: """Mixin with methods common to all resource viewsets that are managed with accesses.""" + filter_backends = [filters.OrderingFilter] + ordering_fields = ["created_at"] + ordering = ["-created_at"] + def get_queryset(self): """Custom queryset to get user related resources.""" queryset = super().get_queryset() diff --git a/src/backend/core/tests/documents/test_api_documents_list.py b/src/backend/core/tests/documents/test_api_documents_list.py index 561214ff..97ccb417 100644 --- a/src/backend/core/tests/documents/test_api_documents_list.py +++ b/src/backend/core/tests/documents/test_api_documents_list.py @@ -164,3 +164,59 @@ def test_api_documents_list_authenticated_distinct(): content = response.json() assert len(content["results"]) == 1 assert content["results"][0]["id"] == str(document.id) + + +def test_api_documents_order(): + """ + Test that the endpoint GET documents is sorted in 'created_at' descending order by default. + """ + user = factories.UserFactory() + client = APIClient() + client.force_login(user) + + document_ids = [ + str(document.id) + for document in factories.DocumentFactory.create_batch(5, is_public=True) + ] + + response = client.get( + "/api/v1.0/documents/", + ) + + assert response.status_code == 200 + + response_data = response.json() + response_document_ids = [document["id"] for document in response_data["results"]] + + document_ids.reverse() + assert ( + response_document_ids == document_ids + ), "created_at values are not sorted from newest to oldest" + + +def test_api_documents_order_param(): + """ + Test that the 'created_at' field is sorted in ascending order + when the 'ordering' query parameter is set. + """ + user = factories.UserFactory() + client = APIClient() + client.force_login(user) + + documents_ids = [ + str(document.id) + for document in factories.DocumentFactory.create_batch(5, is_public=True) + ] + + response = APIClient().get( + "/api/v1.0/documents/?ordering=created_at", + ) + assert response.status_code == 200 + + response_data = response.json() + + response_document_ids = [document["id"] for document in response_data["results"]] + + assert ( + response_document_ids == documents_ids + ), "created_at values are not sorted from oldest to newest" diff --git a/src/backend/core/tests/templates/test_api_templates_list.py b/src/backend/core/tests/templates/test_api_templates_list.py index c63e7b73..4b8a39f4 100644 --- a/src/backend/core/tests/templates/test_api_templates_list.py +++ b/src/backend/core/tests/templates/test_api_templates_list.py @@ -164,3 +164,59 @@ def test_api_templates_list_authenticated_distinct(): content = response.json() assert len(content["results"]) == 1 assert content["results"][0]["id"] == str(template.id) + + +def test_api_templates_order(): + """ + Test that the endpoint GET templates is sorted in 'created_at' descending order by default. + """ + user = factories.UserFactory() + client = APIClient() + client.force_login(user) + + template_ids = [ + str(template.id) + for template in factories.TemplateFactory.create_batch(5, is_public=True) + ] + + response = APIClient().get( + "/api/v1.0/templates/", + ) + + assert response.status_code == 200 + + response_data = response.json() + response_template_ids = [template["id"] for template in response_data["results"]] + + template_ids.reverse() + assert ( + response_template_ids == template_ids + ), "created_at values are not sorted from newest to oldest" + + +def test_api_templates_order_param(): + """ + Test that the 'created_at' field is sorted in ascending order + when the 'ordering' query parameter is set. + """ + user = factories.UserFactory() + client = APIClient() + client.force_login(user) + + templates_ids = [ + str(template.id) + for template in factories.TemplateFactory.create_batch(5, is_public=True) + ] + + response = APIClient().get( + "/api/v1.0/templates/?ordering=created_at", + ) + assert response.status_code == 200 + + response_data = response.json() + + response_template_ids = [template["id"] for template in response_data["results"]] + + assert ( + response_template_ids == templates_ids + ), "created_at values are not sorted from oldest to newest"