@@ -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