diff options
Diffstat (limited to 'src/mailman/rest/tests/test_root.py')
| -rw-r--r-- | src/mailman/rest/tests/test_root.py | 211 |
1 files changed, 211 insertions, 0 deletions
diff --git a/src/mailman/rest/tests/test_root.py b/src/mailman/rest/tests/test_root.py index 1e0ff6f28..840212825 100644 --- a/src/mailman/rest/tests/test_root.py +++ b/src/mailman/rest/tests/test_root.py @@ -25,9 +25,12 @@ from base64 import b64encode from httplib2 import Http from mailman.config import config from mailman.core.system import system +from mailman.database.transaction import transaction +from mailman.interfaces.template import ITemplateManager from mailman.testing.helpers import call_api from mailman.testing.layers import RESTLayer from urllib.error import HTTPError +from zope.component import getUtility class TestRoot(unittest.TestCase): @@ -167,3 +170,211 @@ class TestRoot(unittest.TestCase): 'chains': ['chain_1', 'chain_2'] }, method='PUT') self.assertEqual(cm.exception.code, 405) + + +class TestSiteTemplates(unittest.TestCase): + """Test /uris""" + + layer = RESTLayer + + def test_no_templates_for_api_30(self): + with self.assertRaises(HTTPError) as cm: + call_api('http://localhost:9001/3.0/uris') + self.assertEqual(cm.exception.code, 404) + + def test_path_too_long(self): + with self.assertRaises(HTTPError) as cm: + call_api('http://localhost:9001/3.1/uris/foo/bar') + self.assertEqual(cm.exception.code, 400) + + def test_get_unknown_uri(self): + with self.assertRaises(HTTPError) as cm: + call_api('http://localhost:9001/3.1/uris/not:a:template') + self.assertEqual(cm.exception.code, 404) + + def test_get_all_uris(self): + manager = getUtility(ITemplateManager) + with transaction(): + manager.set( + 'list:user:notice:welcome', None, + 'http://example.com/welcome') + manager.set( + 'list:user:notice:goodbye', None, + 'http://example.com/goodbye', + 'a user', 'the password', + ) + resource, response = call_api( + 'http://localhost:9001/3.1/uris') + self.assertEqual(response.status, 200) + self.assertEqual(resource['start'], 0) + self.assertEqual(resource['total_size'], 2) + self.assertEqual( + resource['self_link'], + 'http://localhost:9001/3.1/uris') + self.assertEqual(resource['entries'], [ + {'http_etag': '"063fd6635a6035a4b7e939a304fcbd16571aa662"', + 'name': 'list:user:notice:goodbye', + 'password': 'the password', + 'self_link': ('http://localhost:9001/3.1' + '/uris/list:user:notice:goodbye'), + 'uri': 'http://example.com/goodbye', + 'username': 'a user', + }, + {'http_etag': '"5c4ec63b2a0a50f96483ec85b94b80ee092af792"', + 'name': 'list:user:notice:welcome', + 'self_link': ('http://localhost:9001/3.1' + '/uris/list:user:notice:welcome'), + 'uri': 'http://example.com/welcome', + }]) + + def test_patch_uris(self): + resource, response = call_api( + 'http://localhost:9001/3.1/uris', { + 'list:user:notice:welcome': 'http://example.org/welcome', + 'list:user:notice:goodbye': 'http://example.org/goodbye', + }, method='PATCH') + self.assertEqual(response.status, 204) + manager = getUtility(ITemplateManager) + template = manager.raw('list:user:notice:welcome', None) + self.assertEqual(template.uri, 'http://example.org/welcome') + self.assertIsNone(template.username) + self.assertEqual(template.password, '') + template = manager.raw('list:user:notice:goodbye', None) + self.assertEqual(template.uri, 'http://example.org/goodbye') + self.assertIsNone(template.username) + self.assertEqual(template.password, '') + + def test_patch_uris_with_credentials(self): + resource, response = call_api( + 'http://localhost:9001/3.1/uris', { + 'list:user:notice:welcome': 'http://example.org/welcome', + 'list:user:notice:goodbye': 'http://example.org/goodbye', + 'password': 'some password', + 'username': 'anne.person', + }, method='PATCH') + self.assertEqual(response.status, 204) + manager = getUtility(ITemplateManager) + template = manager.raw('list:user:notice:welcome', None) + self.assertEqual(template.uri, 'http://example.org/welcome') + self.assertEqual(template.username, 'anne.person') + self.assertEqual(template.password, 'some password') + template = manager.raw('list:user:notice:goodbye', None) + self.assertEqual(template.uri, 'http://example.org/goodbye') + self.assertEqual(template.username, 'anne.person') + self.assertEqual(template.password, 'some password') + + def test_patch_uris_with_partial_credentials(self): + with self.assertRaises(HTTPError) as cm: + call_api( + 'http://localhost:9001/3.1/uris', { + 'list:user:notice:welcome': 'http://example.org/welcome', + 'list:user:notice:goodbye': 'http://example.org/goodbye', + 'username': 'anne.person', + }, method='PATCH') + self.assertEqual(cm.exception.code, 400) + + def test_put_all_uris(self): + resource, response = call_api( + 'http://localhost:9001/3.1/uris', { + 'domain:admin:notice:new-list': '', + 'list:admin:action:post': '', + 'list:admin:action:subscribe': '', + 'list:admin:action:unsubscribe': '', + 'list:admin:notice:subscribe': '', + 'list:admin:notice:unrecognized': '', + 'list:admin:notice:unsubscribe': '', + 'list:member:digest:footer': '', + 'list:member:digest:header': '', + 'list:member:digest:masthead': '', + 'list:member:regular:footer': 'http://example.org/footer', + 'list:member:regular:header': 'http://example.org/header', + 'list:user:action:confirm': '', + 'list:user:action:unsubscribe': '', + 'list:user:notice:goodbye': 'http://example.org/goodbye', + 'list:user:notice:hold': '', + 'list:user:notice:no-more-today': '', + 'list:user:notice:post': '', + 'list:user:notice:probe': '', + 'list:user:notice:refuse': '', + 'list:user:notice:welcome': 'http://example.org/welcome', + 'password': 'some password', + 'username': 'anne.person', + }, method='PUT') + self.assertEqual(response.status, 204) + manager = getUtility(ITemplateManager) + template = manager.raw('list:member:digest:footer', None) + self.assertIsNone(template) + template = manager.raw('list:member:digest:header', None) + self.assertIsNone(template) + template = manager.raw('list:member:regular:footer', None) + self.assertEqual(template.uri, 'http://example.org/footer') + self.assertEqual(template.username, 'anne.person') + self.assertEqual(template.password, 'some password') + template = manager.raw('list:member:regular:header', None) + self.assertEqual(template.uri, 'http://example.org/header') + self.assertEqual(template.username, 'anne.person') + self.assertEqual(template.password, 'some password') + template = manager.raw('list:user:notice:goodbye', None) + self.assertEqual(template.uri, 'http://example.org/goodbye') + self.assertEqual(template.username, 'anne.person') + self.assertEqual(template.password, 'some password') + template = manager.raw('list:user:notice:goodbye', None) + self.assertEqual(template.uri, 'http://example.org/goodbye') + self.assertEqual(template.username, 'anne.person') + self.assertEqual(template.password, 'some password') + + def test_delete_all_uris(self): + manager = getUtility(ITemplateManager) + with transaction(): + manager.set( + 'list:user:notice:welcome', None, + 'http://example.com/welcome') + manager.set( + 'list:user:notice:goodbye', None, + 'http://example.com/goodbye', + 'a user', 'the password', + ) + resource, response = call_api( + 'http://localhost:9001/3.1/uris', + method='DELETE') + self.assertEqual(response.status, 204) + self.assertIsNone(manager.raw('list:user:notice:welcome', None)) + self.assertIsNone(manager.raw('list:user:notice:goodbye', None)) + + def test_get_a_url(self): + with transaction(): + getUtility(ITemplateManager).set( + 'list:user:notice:welcome', None, + 'http://example.com/welcome') + resource, response = call_api( + 'http://localhost:9001/3.1/uris/list:user:notice:welcome') + self.assertEqual(response.status, 200) + self.assertEqual(resource, { + 'http_etag': '"86e360d83197561d50826ad6d15e9c30923b82d6"', + 'self_link': ('http://localhost:9001/3.1' + '/uris/list:user:notice:welcome'), + 'uri': 'http://example.com/welcome', + }) + + def test_get_a_bad_url(self): + with self.assertRaises(HTTPError) as cm: + call_api( + 'http://localhost:9001/3.1/uris/list:user:notice:notemplate') + self.assertEqual(cm.exception.code, 404) + + def test_get_unset_url(self): + with self.assertRaises(HTTPError) as cm: + call_api( + 'http://localhost:9001/3.1/uris/list:user:notice:welcome') + self.assertEqual(cm.exception.code, 404) + + def test_patch_url_with_too_many_parameters(self): + with self.assertRaises(HTTPError) as cm: + call_api( + 'http://localhost:9001/3.1/uris', { + 'list:user:notice:welcome': 'http://example.org/welcome', + 'list:user:notice:goodbye': 'http://example.org/goodbye', + 'secret': 'some password', + 'person': 'anne.person', + }, method='PATCH') + self.assertEqual(cm.exception.code, 400) |
