|
1 | 1 | """Test for the document favorite_list endpoint.""" |
2 | 2 |
|
| 3 | +from datetime import timedelta |
| 4 | + |
| 5 | +from django.utils import timezone |
| 6 | + |
3 | 7 | import pytest |
4 | 8 | from rest_framework.test import APIClient |
5 | 9 |
|
@@ -116,6 +120,48 @@ def test_api_document_favorite_list_with_favorite_children(): |
116 | 120 | assert content[2]["id"] == str(access.document.id) |
117 | 121 |
|
118 | 122 |
|
| 123 | +def test_api_document_favorite_list_sorted_by_updated_at(): |
| 124 | + """ |
| 125 | + Authenticated users should receive their favorite documents including children |
| 126 | + sorted by last updated_at timestamp. |
| 127 | + """ |
| 128 | + user = factories.UserFactory() |
| 129 | + client = APIClient() |
| 130 | + client.force_login(user) |
| 131 | + |
| 132 | + root = factories.DocumentFactory(creator=user, users=[user]) |
| 133 | + children = factories.DocumentFactory.create_batch( |
| 134 | + 2, parent=root, favorited_by=[user] |
| 135 | + ) |
| 136 | + |
| 137 | + access = factories.UserDocumentAccessFactory( |
| 138 | + user=user, role=models.RoleChoices.READER, document__favorited_by=[user] |
| 139 | + ) |
| 140 | + |
| 141 | + other_root = factories.DocumentFactory(creator=user, users=[user]) |
| 142 | + factories.DocumentFactory.create_batch(2, parent=other_root) |
| 143 | + |
| 144 | + now = timezone.now() |
| 145 | + |
| 146 | + models.Document.objects.filter(pk=children[0].pk).update( |
| 147 | + updated_at=now + timedelta(seconds=2) |
| 148 | + ) |
| 149 | + models.Document.objects.filter(pk=children[1].pk).update( |
| 150 | + updated_at=now + timedelta(seconds=3) |
| 151 | + ) |
| 152 | + |
| 153 | + response = client.get("/api/v1.0/documents/favorite_list/") |
| 154 | + |
| 155 | + assert response.status_code == 200 |
| 156 | + assert response.json()["count"] == 3 |
| 157 | + |
| 158 | + content = response.json()["results"] |
| 159 | + |
| 160 | + assert content[0]["id"] == str(children[1].id) |
| 161 | + assert content[1]["id"] == str(children[0].id) |
| 162 | + assert content[2]["id"] == str(access.document.id) |
| 163 | + |
| 164 | + |
119 | 165 | def test_api_document_favorite_list_with_deleted_child(): |
120 | 166 | """ |
121 | 167 | Authenticated users should not see deleted documents in their favorite list. |
|
0 commit comments