summaryrefslogtreecommitdiff
path: root/src/mailman/rest
diff options
context:
space:
mode:
Diffstat (limited to 'src/mailman/rest')
-rw-r--r--src/mailman/rest/tests/test_addresses.py36
-rw-r--r--src/mailman/rest/tests/test_domains.py16
-rw-r--r--src/mailman/rest/tests/test_lists.py32
-rw-r--r--src/mailman/rest/tests/test_membership.py95
-rw-r--r--src/mailman/rest/tests/test_moderation.py32
-rw-r--r--src/mailman/rest/tests/test_root.py32
-rw-r--r--src/mailman/rest/tests/test_users.py8
7 files changed, 69 insertions, 182 deletions
diff --git a/src/mailman/rest/tests/test_addresses.py b/src/mailman/rest/tests/test_addresses.py
index 01ce710b2..35a19f77f 100644
--- a/src/mailman/rest/tests/test_addresses.py
+++ b/src/mailman/rest/tests/test_addresses.py
@@ -48,34 +48,24 @@ class TestAddresses(unittest.TestCase):
def test_membership_of_missing_address(self):
# Try to get the memberships of a missing address.
- try:
- # For Python 2.6.
+ with self.assertRaises(HTTPError) as cm:
call_api('http://localhost:9001/3.0/addresses/'
'nobody@example.com/memberships')
- except HTTPError as exc:
- self.assertEqual(exc.code, 404)
- else:
- raise AssertionError('Expected HTTPError 404')
+ self.assertEqual(cm.exception.code, 404)
def test_verify_a_missing_address(self):
# POSTing to the 'verify' sub-resource returns a 404.
- try:
+ with self.assertRaises(HTTPError) as cm:
call_api('http://localhost:9001/3.0/addresses/'
'nobody@example.com/verify', {})
- except HTTPError as exc:
- self.assertEqual(exc.code, 404)
- else:
- raise AssertionError('Expected HTTPError 404')
+ self.assertEqual(cm.exception.code, 404)
def test_unverify_a_missing_address(self):
# POSTing to the 'unverify' sub-resource returns a 404.
- try:
+ with self.assertRaises(HTTPError) as cm:
call_api('http://localhost:9001/3.0/addresses/'
'nobody@example.com/unverify', {})
- except HTTPError as exc:
- self.assertEqual(exc.code, 404)
- else:
- raise AssertionError('Expected HTTPError 404')
+ self.assertEqual(cm.exception.code, 404)
def test_verify_already_verified(self):
# It's okay to verify an already verified; it just doesn't change the
@@ -105,23 +95,17 @@ class TestAddresses(unittest.TestCase):
with transaction():
anne = getUtility(IUserManager).create_address('anne@example.com')
self.assertEqual(anne.verified_on, None)
- try:
+ with self.assertRaises(HTTPError) as cm:
call_api('http://localhost:9001/3.0/addresses/'
'anne@example.com/verify/foo', {})
- except HTTPError as exc:
- self.assertEqual(exc.code, 400)
- else:
- raise AssertionError('Expected HTTPError 400')
+ self.assertEqual(cm.exception.code, 400)
def test_unverify_bad_request(self):
# Too many segments after /verify.
with transaction():
anne = getUtility(IUserManager).create_address('anne@example.com')
self.assertEqual(anne.verified_on, None)
- try:
+ with self.assertRaises(HTTPError) as cm:
call_api('http://localhost:9001/3.0/addresses/'
'anne@example.com/unverify/foo', {})
- except HTTPError as exc:
- self.assertEqual(exc.code, 400)
- else:
- raise AssertionError('Expected HTTPError 400')
+ self.assertEqual(cm.exception.code, 400)
diff --git a/src/mailman/rest/tests/test_domains.py b/src/mailman/rest/tests/test_domains.py
index a86768481..dea6a0aa6 100644
--- a/src/mailman/rest/tests/test_domains.py
+++ b/src/mailman/rest/tests/test_domains.py
@@ -47,24 +47,16 @@ class TestDomains(unittest.TestCase):
def test_bogus_endpoint_extension(self):
# /domains/<domain>/lists/<anything> is not a valid endpoint.
- try:
- # For Python 2.6.
+ with self.assertRaises(HTTPError) as cm:
call_api('http://localhost:9001/3.0/domains/example.com'
'/lists/wrong')
- except HTTPError as exc:
- self.assertEqual(exc.code, 400)
- else:
- raise AssertionError('Expected HTTPError')
+ self.assertEqual(cm.exception.code, 400)
def test_bogus_endpoint(self):
# /domains/<domain>/<!lists> does not exist.
- try:
- # For Python 2.6.
+ with self.assertRaises(HTTPError) as cm:
call_api('http://localhost:9001/3.0/domains/example.com/wrong')
- except HTTPError as exc:
- self.assertEqual(exc.code, 404)
- else:
- raise AssertionError('Expected HTTPError')
+ self.assertEqual(cm.exception.code, 404)
def test_lists_are_deleted_when_domain_is_deleted(self):
# /domains/<domain> DELETE removes all associated mailing lists.
diff --git a/src/mailman/rest/tests/test_lists.py b/src/mailman/rest/tests/test_lists.py
index cd0ebaf8e..9686ce6a8 100644
--- a/src/mailman/rest/tests/test_lists.py
+++ b/src/mailman/rest/tests/test_lists.py
@@ -46,47 +46,31 @@ class TestListsMissing(unittest.TestCase):
def test_missing_list_roster_member_404(self):
# /lists/<missing>/roster/member gives 404
- try:
- # For Python 2.6.
+ with self.assertRaises(HTTPError) as cm:
call_api('http://localhost:9001/3.0/lists/missing@example.com'
'/roster/member')
- except HTTPError as exc:
- self.assertEqual(exc.code, 404)
- else:
- raise AssertionError('Expected HTTPError')
+ self.assertEqual(cm.exception.code, 404)
def test_missing_list_roster_owner_404(self):
# /lists/<missing>/roster/owner gives 404
- try:
- # For Python 2.6.
+ with self.assertRaises(HTTPError) as cm:
call_api('http://localhost:9001/3.0/lists/missing@example.com'
'/roster/owner')
- except HTTPError as exc:
- self.assertEqual(exc.code, 404)
- else:
- raise AssertionError('Expected HTTPError')
+ self.assertEqual(cm.exception.code, 404)
def test_missing_list_roster_moderator_404(self):
# /lists/<missing>/roster/member gives 404
- try:
- # For Python 2.6.
+ with self.assertRaises(HTTPError) as cm:
call_api('http://localhost:9001/3.0/lists/missing@example.com'
'/roster/moderator')
- except HTTPError as exc:
- self.assertEqual(exc.code, 404)
- else:
- raise AssertionError('Expected HTTPError')
+ self.assertEqual(cm.exception.code, 404)
def test_missing_list_configuration_404(self):
# /lists/<missing>/config gives 404
- try:
- # For Python 2.6.
+ with self.assertRaises(HTTPError) as cm:
call_api(
'http://localhost:9001/3.0/lists/missing@example.com/config')
- except HTTPError as exc:
- self.assertEqual(exc.code, 404)
- else:
- raise AssertionError('Expected HTTPError')
+ self.assertEqual(cm.exception.code, 404)
diff --git a/src/mailman/rest/tests/test_membership.py b/src/mailman/rest/tests/test_membership.py
index 18469e537..3bbe821ac 100644
--- a/src/mailman/rest/tests/test_membership.py
+++ b/src/mailman/rest/tests/test_membership.py
@@ -50,41 +50,29 @@ class TestMembership(unittest.TestCase):
def test_try_to_join_missing_list(self):
# A user tries to join a non-existent list.
- try:
- # For Python 2.6.
+ with self.assertRaises(HTTPError) as cm:
call_api('http://localhost:9001/3.0/members', {
'list_id': 'missing.example.com',
'subscriber': 'nobody@example.com',
})
- except HTTPError as exc:
- self.assertEqual(exc.code, 400)
- self.assertEqual(exc.msg, 'No such list')
- else:
- raise AssertionError('Expected HTTPError')
+ self.assertEqual(cm.exception.code, 400)
+ self.assertEqual(cm.exception.msg, 'No such list')
def test_try_to_leave_missing_list(self):
# A user tries to leave a non-existent list.
- try:
- # For Python 2.6.
+ with self.assertRaises(HTTPError) as cm:
call_api('http://localhost:9001/3.0/lists/missing@example.com'
'/member/nobody@example.com',
method='DELETE')
- except HTTPError as exc:
- self.assertEqual(exc.code, 404)
- self.assertEqual(exc.msg, '404 Not Found')
- else:
- raise AssertionError('Expected HTTPError')
+ self.assertEqual(cm.exception.code, 404)
+ self.assertEqual(cm.exception.msg, '404 Not Found')
def test_try_to_leave_list_with_bogus_address(self):
# Try to leave a mailing list using an invalid membership address.
- try:
- # For Python 2.6.
+ with self.assertRaises(HTTPError) as cm:
call_api('http://localhost:9001/3.0/members/1', method='DELETE')
- except HTTPError as exc:
- self.assertEqual(exc.code, 404)
- self.assertEqual(exc.msg, '404 Not Found')
- else:
- raise AssertionError('Expected HTTPError')
+ self.assertEqual(cm.exception.code, 404)
+ self.assertEqual(cm.exception.msg, '404 Not Found')
def test_try_to_leave_a_list_twice(self):
with transaction():
@@ -96,45 +84,34 @@ class TestMembership(unittest.TestCase):
# content.
self.assertEqual(content, None)
self.assertEqual(response.status, 204)
- try:
- # For Python 2.6.
+ with self.assertRaises(HTTPError) as cm:
call_api(url, method='DELETE')
- except HTTPError as exc:
- self.assertEqual(exc.code, 404)
- self.assertEqual(exc.msg, '404 Not Found')
- else:
- raise AssertionError('Expected HTTPError')
+ self.assertEqual(cm.exception.code, 404)
+ self.assertEqual(cm.exception.msg, '404 Not Found')
def test_try_to_join_a_list_twice(self):
with transaction():
anne = self._usermanager.create_address('anne@example.com')
self._mlist.subscribe(anne)
- try:
- # For Python 2.6.
+ with self.assertRaises(HTTPError) as cm:
call_api('http://localhost:9001/3.0/members', {
'list_id': 'test.example.com',
'subscriber': 'anne@example.com',
})
- except HTTPError as exc:
- self.assertEqual(exc.code, 409)
- self.assertEqual(exc.msg, 'Member already subscribed')
- else:
- raise AssertionError('Expected HTTPError')
+ self.assertEqual(cm.exception.code, 409)
+ self.assertEqual(cm.exception.msg, 'Member already subscribed')
def test_join_with_invalid_delivery_mode(self):
- try:
+ with self.assertRaises(HTTPError) as cm:
call_api('http://localhost:9001/3.0/members', {
'list_id': 'test.example.com',
'subscriber': 'anne@example.com',
'display_name': 'Anne Person',
'delivery_mode': 'invalid-mode',
})
- except HTTPError as exc:
- self.assertEqual(exc.code, 400)
- self.assertEqual(exc.msg,
- 'Cannot convert parameters: delivery_mode')
- else:
- raise AssertionError('Expected HTTPError')
+ self.assertEqual(cm.exception.code, 400)
+ self.assertEqual(cm.exception.msg,
+ 'Cannot convert parameters: delivery_mode')
def test_join_email_contains_slash(self):
content, response = call_api('http://localhost:9001/3.0/members', {
@@ -196,47 +173,31 @@ class TestMembership(unittest.TestCase):
def test_get_nonexistent_member(self):
# /members/<bogus> returns 404
- try:
- # For Python 2.6
+ with self.assertRaises(HTTPError) as cm:
call_api('http://localhost:9001/3.0/members/bogus')
- except HTTPError as exc:
- self.assertEqual(exc.code, 404)
- else:
- raise AssertionError('Expected HTTPError')
+ self.assertEqual(cm.exception.code, 404)
def test_patch_nonexistent_member(self):
# /members/<missing> PATCH returns 404
- try:
- # For Python 2.6
+ with self.assertRaises(HTTPError) as cm:
call_api('http://localhost:9001/3.0/members/801', method='PATCH')
- except HTTPError as exc:
- self.assertEqual(exc.code, 404)
- else:
- raise AssertionError('Expected HTTPError')
+ self.assertEqual(cm.exception.code, 404)
def test_patch_member_bogus_attribute(self):
# /members/<id> PATCH 'bogus' returns 400
with transaction():
anne = self._usermanager.create_address('anne@example.com')
self._mlist.subscribe(anne)
- try:
- # For Python 2.6
+ with self.assertRaises(HTTPError) as cm:
call_api('http://localhost:9001/3.0/members/1', {
'powers': 'super',
}, method='PATCH')
- except HTTPError as exc:
- self.assertEqual(exc.code, 400)
- self.assertEqual(exc.msg, 'Unexpected parameters: powers')
- else:
- raise AssertionError('Expected HTTPError')
+ self.assertEqual(cm.exception.code, 400)
+ self.assertEqual(cm.exception.msg, 'Unexpected parameters: powers')
def test_member_all_without_preferences(self):
# /members/<id>/all should return a 404 when it isn't trailed by
# `preferences`
- try:
- # For Python 2.6
+ with self.assertRaises(HTTPError) as cm:
call_api('http://localhost:9001/3.0/members/1/all')
- except HTTPError as exc:
- self.assertEqual(exc.code, 404)
- else:
- raise AssertionError('Expected HTTPError')
+ self.assertEqual(cm.exception.code, 404)
diff --git a/src/mailman/rest/tests/test_moderation.py b/src/mailman/rest/tests/test_moderation.py
index dfcedef05..2ee796c87 100644
--- a/src/mailman/rest/tests/test_moderation.py
+++ b/src/mailman/rest/tests/test_moderation.py
@@ -56,24 +56,16 @@ Something else.
def test_not_found(self):
# When a bogus mailing list is given, 404 should result.
- try:
- # For Python 2.6
+ with self.assertRaises(HTTPError) as cm:
call_api('http://localhost:9001/3.0/lists/bee@example.com/held')
- except HTTPError as exc:
- self.assertEqual(exc.code, 404)
- else:
- raise AssertionError('Expected HTTPError')
+ self.assertEqual(cm.exception.code, 404)
def test_bad_request_id(self):
# Bad request when request_id is not an integer.
- try:
- # For Python 2.6
+ with self.assertRaises(HTTPError) as cm:
call_api(
'http://localhost:9001/3.0/lists/ant@example.com/held/bogus')
- except HTTPError as exc:
- self.assertEqual(exc.code, 400)
- else:
- raise AssertionError('Expected HTTPError')
+ self.assertEqual(cm.exception.code, 400)
def test_subscription_request_as_held_message(self):
# Provide the request id of a subscription request using the held
@@ -85,12 +77,9 @@ Something else.
DeliveryMode.regular, 'en')
config.db.store.commit()
url = 'http://localhost:9001/3.0/lists/ant@example.com/held/{0}'
- try:
+ with self.assertRaises(HTTPError) as cm:
call_api(url.format(subscribe_id))
- except HTTPError as exc:
- self.assertEqual(exc.code, 404)
- else:
- raise AssertionError('Expected HTTPError')
+ self.assertEqual(cm.exception.code, 404)
# But using the held_id returns a valid response.
response, content = call_api(url.format(held_id))
self.assertEqual(response['key'], '<alpha>')
@@ -99,10 +88,7 @@ Something else.
# POSTing to a held message with a bad action.
held_id = hold_message(self._mlist, self._msg)
url = 'http://localhost:9001/3.0/lists/ant@example.com/held/{0}'
- try:
+ with self.assertRaises(HTTPError) as cm:
call_api(url.format(held_id), {'action': 'bogus'})
- except HTTPError as exc:
- self.assertEqual(exc.code, 400)
- self.assertEqual(exc.msg, 'Cannot convert parameters: action')
- else:
- raise AssertionError('Expected HTTPError')
+ self.assertEqual(cm.exception.code, 400)
+ self.assertEqual(cm.exception.msg, 'Cannot convert parameters: action')
diff --git a/src/mailman/rest/tests/test_root.py b/src/mailman/rest/tests/test_root.py
index 90d30bd80..4a9ba0dd1 100644
--- a/src/mailman/rest/tests/test_root.py
+++ b/src/mailman/rest/tests/test_root.py
@@ -38,38 +38,25 @@ class TestSystem(unittest.TestCase):
def test_system_url_too_long(self):
# /system/foo/bar is not allowed.
- try:
- # For Python 2.6.
+ with self.assertRaises(HTTPError) as cm:
call_api('http://localhost:9001/3.0/system/foo/bar')
- except HTTPError as exc:
- self.assertEqual(exc.code, 400)
- else:
- raise AssertionError('Expected HTTPError')
+ self.assertEqual(cm.exception.code, 400)
def test_system_url_not_preferences(self):
# /system/foo where `foo` is not `preferences`.
- try:
- # For Python 2.6.
+ with self.assertRaises(HTTPError) as cm:
call_api('http://localhost:9001/3.0/system/foo')
- except HTTPError as exc:
- self.assertEqual(exc.code, 400)
- else:
- raise AssertionError('Expected HTTPError')
+ self.assertEqual(cm.exception.code, 400)
def test_system_preferences_are_read_only(self):
# /system/preferences are read-only.
- try:
- # For Python 2.6.
+ with self.assertRaises(HTTPError) as cm:
call_api('http://localhost:9001/3.0/system/preferences', {
'acknowledge_posts': True,
}, method='PATCH')
- except HTTPError as exc:
- self.assertEqual(exc.code, 405)
- else:
- raise AssertionError('Expected HTTPError')
+ self.assertEqual(cm.exception.code, 405)
# /system/preferences are read-only.
- try:
- # For Python 2.6.
+ with self.assertRaises(HTTPError) as cm:
call_api('http://localhost:9001/3.0/system/preferences', {
'acknowledge_posts': False,
'delivery_mode': 'regular',
@@ -79,7 +66,4 @@ class TestSystem(unittest.TestCase):
'receive_list_copy': True,
'receive_own_postings': True,
}, method='PUT')
- except HTTPError as exc:
- self.assertEqual(exc.code, 405)
- else:
- raise AssertionError('Expected HTTPError')
+ self.assertEqual(cm.exception.code, 405)
diff --git a/src/mailman/rest/tests/test_users.py b/src/mailman/rest/tests/test_users.py
index 301027885..4595c69d8 100644
--- a/src/mailman/rest/tests/test_users.py
+++ b/src/mailman/rest/tests/test_users.py
@@ -45,10 +45,6 @@ class TestUsers(unittest.TestCase):
def test_delete_bogus_user(self):
# Try to delete a user that does not exist.
- try:
- # For Python 2.6.
+ with self.assertRaises(HTTPError) as cm:
call_api('http://localhost:9001/3.0/users/99', method='DELETE')
- except HTTPError as exc:
- self.assertEqual(exc.code, 404)
- else:
- raise AssertionError('Expected HTTPError')
+ self.assertEqual(cm.exception.code, 404)