Skip to content

Commit 7d8a6aa

Browse files
Fix multipage downloads quota
1 parent fbe14e3 commit 7d8a6aa

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

src/django/api/facilities_download_view_set.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ def list(self, request):
107107
payload['count'] = base_qs.count()
108108

109109
if is_last_page and limit:
110-
returned_count = len(items)
110+
# Charge for the full result set, not just the last page size
111+
returned_count = base_qs.count()
111112

112113
prev_free_amount = getattr(limit, 'free_download_records', 0)
113114
prev_paid_amount = getattr(limit, 'paid_download_records', 0)

src/django/api/tests/test_facilities_download_viewset.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,60 @@ def test_is_same_contributor_with_combine_contributors_and_logic(self):
631631
response.data['results']['is_same_contributor']
632632
)
633633

634+
def test_multi_page_download_decrements_free_by_total_count(self):
635+
user = self.create_user()
636+
self.login_user(user)
637+
638+
limit = FacilityDownloadLimit.objects.create(
639+
user=user,
640+
free_download_records=20,
641+
paid_download_records=0,
642+
)
643+
644+
# Request first page to get the total count from the payload
645+
resp_page1 = self.get_facility_downloads({"pageSize": 10, "page": 1})
646+
self.assertEqual(resp_page1.status_code, status.HTTP_200_OK)
647+
total_count = resp_page1.data.get("count")
648+
self.assertIsNotNone(total_count)
649+
650+
# Quotas should remain unchanged after first page
651+
limit.refresh_from_db()
652+
self.assertEqual(limit.free_download_records, 20)
653+
self.assertEqual(limit.paid_download_records, 0)
654+
655+
# Request last page to trigger quota registration using total_count
656+
resp_page2 = self.get_facility_downloads({"pageSize": 10, "page": 2})
657+
self.assertEqual(resp_page2.status_code, status.HTTP_200_OK)
658+
659+
limit.refresh_from_db()
660+
expected_free = max(20 - total_count, 0)
661+
self.assertEqual(limit.free_download_records, expected_free)
662+
self.assertEqual(limit.paid_download_records, 0)
663+
664+
def test_multi_page_download_consumes_paid_when_free_insufficient(self):
665+
user = self.create_user()
666+
self.login_user(user)
667+
668+
limit = FacilityDownloadLimit.objects.create(
669+
user=user,
670+
free_download_records=5,
671+
paid_download_records=20,
672+
)
673+
674+
resp_page1 = self.get_facility_downloads({"pageSize": 10, "page": 1})
675+
self.assertEqual(resp_page1.status_code, status.HTTP_200_OK)
676+
total_count = resp_page1.data.get("count")
677+
self.assertIsNotNone(total_count)
678+
679+
# Trigger decrement on last page
680+
resp_page2 = self.get_facility_downloads({"pageSize": 10, "page": 2})
681+
self.assertEqual(resp_page2.status_code, status.HTTP_200_OK)
682+
683+
limit.refresh_from_db()
684+
self.assertEqual(limit.free_download_records, 0)
685+
expected_paid = max(20 - max(total_count - 5, 0), 0)
686+
self.assertEqual(limit.paid_download_records, expected_paid)
687+
634688
def test_is_same_contributor_with_empty_queryset(self):
635689
"""Test is_same_contributor with empty queryset."""
636690
user = self.create_user()

0 commit comments

Comments
 (0)