From a2c17632afae875cd3c486f9eb0608d0821e8c8d Mon Sep 17 00:00:00 2001 From: Abhilash Raj Date: Wed, 2 May 2018 21:29:03 -0700 Subject: [PATCH] Check for MailingList exists error. Do not render the generic error page when the Mailing List already exists. Fixes #237 --- ...ionTest.test_duplicate_listname_validation.yaml | 166 +++++++++++++++++++++ .../tests/mailman_api_tests/test_list_new.py | 16 ++ src/postorius/views/list.py | 8 + 3 files changed, 190 insertions(+) create mode 100644 src/postorius/tests/fixtures/vcr_cassettes/ListCreationTest.test_duplicate_listname_validation.yaml diff --git a/src/postorius/tests/fixtures/vcr_cassettes/ListCreationTest.test_duplicate_listname_validation.yaml b/src/postorius/tests/fixtures/vcr_cassettes/ListCreationTest.test_duplicate_listname_validation.yaml new file mode 100644 index 00000000..69452fa7 --- /dev/null +++ b/src/postorius/tests/fixtures/vcr_cassettes/ListCreationTest.test_duplicate_listname_validation.yaml @@ -0,0 +1,166 @@ +interactions: +- request: + body: mail_host=example.com + headers: + accept-encoding: ['gzip, deflate'] + content-type: [application/x-www-form-urlencoded] + method: POST + uri: http://localhost:9001/3.0/domains + response: + body: {string: ''} + headers: + content-length: ['0'] + content-type: [application/json; charset=UTF-8] + location: ['http://localhost:9001/3.0/domains/example.com'] + status: {code: 201, message: Created} +- request: + body: null + headers: + accept-encoding: ['gzip, deflate'] + method: GET + uri: http://localhost:9001/3.0/domains/example.com + response: + body: {string: '{"alias_domain": null, "description": null, "http_etag": "\"492652d581dc93af101db450d81ae93a074bff49\"", + "mail_host": "example.com", "self_link": "http://localhost:9001/3.0/domains/example.com"}'} + headers: + content-length: ['194'] + content-type: [application/json; charset=UTF-8] + status: {code: 200, message: OK} +- request: + body: fqdn_listname=foo%40example.com + headers: + accept-encoding: ['gzip, deflate'] + content-type: [application/x-www-form-urlencoded] + method: POST + uri: http://localhost:9001/3.0/lists + response: + body: {string: ''} + headers: + content-length: ['0'] + content-type: [application/json; charset=UTF-8] + location: ['http://localhost:9001/3.0/lists/foo.example.com'] + status: {code: 201, message: Created} +- request: + body: null + headers: + accept-encoding: ['gzip, deflate'] + method: GET + uri: http://localhost:9001/3.0/domains + response: + body: {string: '{"entries": [{"alias_domain": null, "description": null, "http_etag": + "\"492652d581dc93af101db450d81ae93a074bff49\"", "mail_host": "example.com", + "self_link": "http://localhost:9001/3.0/domains/example.com"}], "http_etag": + "\"5868fb6d2442c0d163e0edb2bfd7a92b6c812796\"", "start": 0, "total_size": + 1}'} + headers: + content-length: ['299'] + content-type: [application/json; charset=UTF-8] + status: {code: 200, message: OK} +- request: + body: null + headers: + accept-encoding: ['gzip, deflate'] + method: GET + uri: http://localhost:9001/3.0/domains/example.com + response: + body: {string: '{"alias_domain": null, "description": null, "http_etag": "\"492652d581dc93af101db450d81ae93a074bff49\"", + "mail_host": "example.com", "self_link": "http://localhost:9001/3.0/domains/example.com"}'} + headers: + content-length: ['194'] + content-type: [application/json; charset=UTF-8] + status: {code: 200, message: OK} +- request: + body: null + headers: + accept-encoding: ['gzip, deflate'] + method: GET + uri: http://localhost:9001/3.0/lists/styles + response: + body: {string: '{"default": "legacy-default", "http_etag": "\"21a4b8b38716167b458ed2d8e88579233938274b\"", + "style_names": ["legacy-announce", "legacy-default"], "styles": [{"description": + "Announce only mailing list style.", "name": "legacy-announce"}, {"description": + "Ordinary discussion mailing list style.", "name": "legacy-default"}]}'} + headers: + content-length: ['323'] + content-type: [application/json; charset=UTF-8] + status: {code: 200, message: OK} +- request: + body: null + headers: + accept-encoding: ['gzip, deflate'] + method: GET + uri: http://localhost:9001/3.0/domains/example.com + response: + body: {string: '{"alias_domain": null, "description": null, "http_etag": "\"492652d581dc93af101db450d81ae93a074bff49\"", + "mail_host": "example.com", "self_link": "http://localhost:9001/3.0/domains/example.com"}'} + headers: + content-length: ['194'] + content-type: [application/json; charset=UTF-8] + status: {code: 200, message: OK} +- request: + body: null + headers: + accept-encoding: ['gzip, deflate'] + method: GET + uri: http://localhost:9001/3.0/domains/example.com + response: + body: {string: '{"alias_domain": null, "description": null, "http_etag": "\"492652d581dc93af101db450d81ae93a074bff49\"", + "mail_host": "example.com", "self_link": "http://localhost:9001/3.0/domains/example.com"}'} + headers: + content-length: ['194'] + content-type: [application/json; charset=UTF-8] + status: {code: 200, message: OK} +- request: + body: fqdn_listname=foo%40example.com&style_name=legacy-default + headers: + accept-encoding: ['gzip, deflate'] + content-type: [application/x-www-form-urlencoded] + method: POST + uri: http://localhost:9001/3.0/lists + response: + body: {string: Mailing list exists} + headers: + content-length: ['19'] + content-type: [application/json; charset=UTF-8] + status: {code: 400, message: Bad Request} +- request: + body: null + headers: + accept-encoding: ['gzip, deflate'] + method: GET + uri: http://localhost:9001/3.0/domains + response: + body: {string: '{"entries": [{"alias_domain": null, "description": null, "http_etag": + "\"492652d581dc93af101db450d81ae93a074bff49\"", "mail_host": "example.com", + "self_link": "http://localhost:9001/3.0/domains/example.com"}], "http_etag": + "\"5868fb6d2442c0d163e0edb2bfd7a92b6c812796\"", "start": 0, "total_size": + 1}'} + headers: + content-length: ['299'] + content-type: [application/json; charset=UTF-8] + status: {code: 200, message: OK} +- request: + body: null + headers: + accept-encoding: ['gzip, deflate'] + method: DELETE + uri: http://localhost:9001/3.0/domains/example.com + response: + body: {string: ''} + headers: + Content-Length: ['0'] + status: {code: 204, message: No Content} +- request: + body: null + headers: + accept-encoding: ['gzip, deflate'] + method: GET + uri: http://localhost:9001/3.0/users + response: + body: {string: '{"http_etag": "\"32223434a0f3af4cdc4673d1fbc5bac1f6d98fd3\"", + "start": 0, "total_size": 0}'} + headers: + content-length: ['90'] + content-type: [application/json; charset=UTF-8] + status: {code: 200, message: OK} +version: 1 diff --git a/src/postorius/tests/mailman_api_tests/test_list_new.py b/src/postorius/tests/mailman_api_tests/test_list_new.py index 810a886a..64614067 100644 --- a/src/postorius/tests/mailman_api_tests/test_list_new.py +++ b/src/postorius/tests/mailman_api_tests/test_list_new.py @@ -41,6 +41,22 @@ class ListCreationTest(ViewTestCase): response = self.client.get(reverse('list_new')) self.assertEqual(response.status_code, 403) + def test_duplicate_listname_validation(self): + # First, let's create a mailing list. + self.domain.create_list('foo') + self.client.login(username='su', password='pwd') + post_data = {'listname': 'foo', + 'mail_host': 'example.com', + 'list_owner': 'owner@example.com', + 'advertised': 'True', + 'list_style': 'legacy-default', + 'description': 'A new list.'} + response = self.client.post(reverse('list_new'), post_data) + self.assertEqual(response.status_code, 200) + self.assertContains(response, 'Mailing List already exists') + # Make sure this is the list create form and not just error page. + self.assertContains(response, 'Create a new list') + def test_new_list_created_with_owner(self): self.client.login(username='su', password='pwd') post_data = {'listname': 'a_new_list', diff --git a/src/postorius/views/list.py b/src/postorius/views/list.py index 97b1cdad..6dd9f25b 100644 --- a/src/postorius/views/list.py +++ b/src/postorius/views/list.py @@ -579,6 +579,14 @@ def list_new(request, template='postorius/lists/new.html'): list_id=mailing_list.list_id) # TODO catch correct Error class: except HTTPError as e: + # Right now, there is no good way to detect that this is a + # duplicate mailing list request other than checking the + # reason for 400 error. + if e.reason == b'Mailing list exists': + form.add_error( + 'listname', _('Mailing List already exists.')) + return render(request, template, {'form': form}) + # Otherwise just render the generic error page. return render(request, 'postorius/errors/generic.html', {'error': e}) else: -- 2.11.4.GIT