Repository: Kinto/kinto

Issue:
Paginated delete missing last page token
```bash
# create three records
> http post localhost:8888/v1/buckets -a a:a
> http post localhost:8888/v1/buckets -a a:a
> http post localhost:8888/v1/buckets -a a:a
(...)

gsurita-30820:~ gsurita$ http delete localhost:8888/v1/buckets?_limit=1 -a a:a
HTTP/1.1 200 OK
Access-Control-Expose-Headers: Backoff, Retry-After, Content-Length, Alert
Content-Length: 73
Content-Type: application/json
Date: Sun, 26 Mar 2017 12:51:21 GMT
Etag: "1490532681567"
Last-Modified: Sun, 26 Mar 2017 12:51:21 GMT
Next-Page: http://localhost:8888/v1/buckets?_limit=1&_token=eyJsYXN0X3JlY29yZCI6eyJsYXN0X21vZGlmaWVkIjoxNDkwNTMyNjgxNTY3fSwib2Zmc2V0IjoxfQ%3D%3D
Server: waitress
Total-Records: 3
X-Content-Type-Options: nosniff

{
    "data": [
        {
            "deleted": true,
            "id": "29Bbx5Bu",
            "last_modified": 1490532681567
        }
    ]
}

gsurita-30820:~ gsurita$ http delete "http://localhost:8888/v1/buckets?_limit=1&_token=eyJsYXN0X3JlY29yZCI6eyJsYXN0X21vZGlmaWVkIjoxNDkwNTMyNjgxNTY3fSwib2Zmc2V0IjoxfQ%3D%3D" -a a:a
HTTP/1.1 200 OK
Access-Control-Expose-Headers: Backoff, Retry-After, Content-Length, Alert
Content-Length: 73
Content-Type: application/json
Date: Sun, 26 Mar 2017 12:52:05 GMT
Etag: "1490532725067"
Last-Modified: Sun, 26 Mar 2017 12:52:05 GMT
Server: waitress
Total-Records: 2
X-Content-Type-Options: nosniff

{
    "data": [
        {
            "deleted": true,
            "id": "IrkBjOFr",
            "last_modified": 1490532725067
        }
    ]
}



Discussion:
The `Total-Records` count change is concerning.
I don't think total records changing is an issue, as long as the pagination is right. 
In that case that is why the pagination is not right.

Resolving patch:
diff --git a/kinto/core/resource/__init__.py b/kinto/core/resource/__init__.py
--- a/kinto/core/resource/__init__.py
+++ b/kinto/core/resource/__init__.py
@@ -397,8 +397,7 @@ def collection_delete(self):
             self._add_timestamp_header(self.request.response, timestamp=timestamp)
 
             # Add pagination header
-            offset = offset + len(deleted)
-            if limit and len(deleted) == limit and offset < total_records:
+            if limit and len(deleted) == limit and total_records > 1:
                 next_page = self._next_page_url(sorting, limit, lastrecord, offset)
                 self.request.response.headers['Next-Page'] = next_page
         else:


Test patch:
diff --git a/tests/core/resource/test_pagination.py b/tests/core/resource/test_pagination.py
--- a/tests/core/resource/test_pagination.py
+++ b/tests/core/resource/test_pagination.py
@@ -239,6 +239,24 @@ def test_return_total_records_in_headers_matching_deletable(self):
         count = headers['Total-Records']
         self.assertEquals(int(count), 20)
 
+    def test_paginated_delete_second_to_last_gets_next_header(self):
+        self.resource.collection_get()
+        get_all_headers = self.last_response.headers
+        count = int(get_all_headers['Total-Records']) - 1
+
+        self.validated['querystring'] = {'_limit': 1}
+        headers = []
+        for i in range(count):
+            self.resource.collection_delete()
+            headers.append(self.last_response.headers)
+            self._setup_next_page()
+
+        self.resource.collection_delete()
+        headers.append(self.last_response.headers)
+
+        self.assertIn('Next-Page', headers[count - 1])
+        self.assertNotIn('Next-Page', headers[count])
+
     def test_token_cannot_be_reused_twice(self):
         self.resource.request.method = "DELETE"
         self.validated['querystring']['_limit'] = 3