diff options
| author | Barry Warsaw | 2014-08-13 12:55:24 -0400 |
|---|---|---|
| committer | Barry Warsaw | 2014-08-13 12:55:24 -0400 |
| commit | 5be40dfb86ceaed9a47e1efff108fdeaf7a568fd (patch) | |
| tree | 0acd2d883e20b8f0cd712499c993f5a4eb1ed1d7 /src/mailman/rest/tests | |
| parent | 45691d23d4fb4dca8e7a3d7442186a333e7f9663 (diff) | |
| download | mailman-5be40dfb86ceaed9a47e1efff108fdeaf7a568fd.tar.gz mailman-5be40dfb86ceaed9a47e1efff108fdeaf7a568fd.tar.zst mailman-5be40dfb86ceaed9a47e1efff108fdeaf7a568fd.zip | |
Lists and pagination are ported.
Diffstat (limited to 'src/mailman/rest/tests')
| -rw-r--r-- | src/mailman/rest/tests/test_lists.py | 82 | ||||
| -rw-r--r-- | src/mailman/rest/tests/test_paginate.py | 37 |
2 files changed, 95 insertions, 24 deletions
diff --git a/src/mailman/rest/tests/test_lists.py b/src/mailman/rest/tests/test_lists.py index f4cafa3f3..9426e7f27 100644 --- a/src/mailman/rest/tests/test_lists.py +++ b/src/mailman/rest/tests/test_lists.py @@ -22,6 +22,7 @@ from __future__ import absolute_import, print_function, unicode_literals __metaclass__ = type __all__ = [ 'TestListArchivers', + 'TestListPagination', 'TestLists', 'TestListsMissing', ] @@ -228,3 +229,84 @@ class TestListArchivers(unittest.TestCase): method='PATCH') self.assertEqual(cm.exception.code, 400) self.assertEqual(cm.exception.reason, 'Invalid boolean value: sure') + + + +class TestListPagination(unittest.TestCase): + """Test mailing list pagination functionality. + + We create a bunch of mailing lists within a domain. When we want to + get all the lists in that domain via the REST API, we need to + paginate over them, otherwise there could be too many for display. + """ + + layer = RESTLayer + + def setUp(self): + with transaction(): + # Create a bunch of mailing lists in the example.com domain. + create_list('ant@example.com') + create_list('bee@example.com') + create_list('cat@example.com') + create_list('dog@example.com') + create_list('emu@example.com') + create_list('fly@example.com') + + def test_first_page(self): + resource, response = call_api( + 'http://localhost:9001/3.0/domains/example.com/lists' + '?count=1&page=1') + # There are 6 total lists, but only the first one in the page. + self.assertEqual(resource['total_size'], 1) + self.assertEqual(resource['start'], 0) + self.assertEqual(len(resource['entries']), 1) + entry = resource['entries'][0] + self.assertEqual(entry['fqdn_listname'], 'ant@example.com') + + def test_second_page(self): + resource, response = call_api( + 'http://localhost:9001/3.0/domains/example.com/lists' + '?count=1&page=2') + # There are 6 total lists, but only the first one in the page. + self.assertEqual(resource['total_size'], 1) + self.assertEqual(resource['start'], 0) + self.assertEqual(len(resource['entries']), 1) + entry = resource['entries'][0] + self.assertEqual(entry['fqdn_listname'], 'bee@example.com') + + def test_last_page(self): + resource, response = call_api( + 'http://localhost:9001/3.0/domains/example.com/lists' + '?count=1&page=6') + # There are 6 total lists, but only the first one in the page. + self.assertEqual(resource['total_size'], 1) + self.assertEqual(resource['start'], 0) + self.assertEqual(len(resource['entries']), 1) + entry = resource['entries'][0] + self.assertEqual(entry['fqdn_listname'], 'fly@example.com') + + def test_zeroth_page(self): + # Page numbers start at one. + with self.assertRaises(HTTPError) as cm: + resource, response = call_api( + 'http://localhost:9001/3.0/domains/example.com/lists' + '?count=1&page=0') + self.assertEqual(cm.exception.code, 400) + + def test_negative_page(self): + # Negative pages are not allowed. + with self.assertRaises(HTTPError) as cm: + resource, response = call_api( + 'http://localhost:9001/3.0/domains/example.com/lists' + '?count=1&page=-1') + self.assertEqual(cm.exception.code, 400) + + def test_past_last_page(self): + # The 7th page doesn't exist so the collection is empty. + resource, response = call_api( + 'http://localhost:9001/3.0/domains/example.com/lists' + '?count=1&page=7') + # There are 6 total lists, but only the first one in the page. + self.assertEqual(resource['total_size'], 0) + self.assertEqual(resource['start'], 0) + self.assertNotIn('entries', resource) diff --git a/src/mailman/rest/tests/test_paginate.py b/src/mailman/rest/tests/test_paginate.py index 0774125bb..3a166d9d2 100644 --- a/src/mailman/rest/tests/test_paginate.py +++ b/src/mailman/rest/tests/test_paginate.py @@ -27,6 +27,7 @@ __all__ = [ import unittest +from falcon import InvalidParam, Request from mailman.app.lifecycle import create_list from mailman.database.transaction import transaction from mailman.rest.helpers import paginate @@ -34,15 +35,15 @@ from mailman.testing.layers import RESTLayer -class _FakeRequest: +class _FakeRequest(Request): """Fake restish.http.Request object.""" def __init__(self, count=None, page=None): - self.GET = {} + self._params = {} if count is not None: - self.GET['count'] = count + self._params['count'] = count if page is not None: - self.GET['page'] = page + self._params['page'] = page @@ -105,41 +106,29 @@ class TestPaginateHelper(unittest.TestCase): @paginate def get_collection(self, request): return [] - response = get_collection(None, _FakeRequest('two', 1)) - self.assertEqual(response.status, '400 Bad Request') - - def test_no_get_attr_returns_bad_request(self): - # ?count=two&page=2 are not valid values so a bad request is returned. - @paginate - def get_collection(self, request): - return [] - request = _FakeRequest() - del request.GET - # The request object has no GET attribute. - self.assertIsNone(getattr(request, 'GET', None)) - response = get_collection(None, request) - self.assertEqual(response.status, '400 Bad Request') + self.assertRaises(InvalidParam, get_collection, + None, _FakeRequest('two', 1)) def test_negative_count(self): # ?count=-1&page=1 @paginate def get_collection(self, request): return ['one', 'two', 'three', 'four', 'five'] - response = get_collection(None, _FakeRequest(-1, 1)) - self.assertEqual(response.status, '400 Bad Request') + self.assertRaises(InvalidParam, get_collection, + None, _FakeRequest(-1, 1)) def test_negative_page(self): # ?count=1&page=-1 @paginate def get_collection(self, request): return ['one', 'two', 'three', 'four', 'five'] - response = get_collection(None, _FakeRequest(1, -1)) - self.assertEqual(response.status, '400 Bad Request') + self.assertRaises(InvalidParam, get_collection, + None, _FakeRequest(1, -1)) def test_negative_page_and_count(self): # ?count=1&page=-1 @paginate def get_collection(self, request): return ['one', 'two', 'three', 'four', 'five'] - response = get_collection(None, _FakeRequest(-1, -1)) - self.assertEqual(response.status, '400 Bad Request') + self.assertRaises(InvalidParam, get_collection, + None, _FakeRequest(-1, -1)) |
