From c7074204018693425a1ba9de5129b374d918d8c6 Mon Sep 17 00:00:00 2001 From: Barry Warsaw Date: Mon, 28 Dec 2009 08:11:37 -0500 Subject: More tests. --- src/mailman/interfaces/rest.py | 10 ++++++ src/mailman/rest/adapters.py | 17 +++++----- src/mailman/rest/configure.zcml | 20 +++++++++--- src/mailman/rest/docs/membership.txt | 61 ++++++++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/mailman/interfaces/rest.py b/src/mailman/interfaces/rest.py index 83a458c1d..4fbb0f5ef 100644 --- a/src/mailman/interfaces/rest.py +++ b/src/mailman/interfaces/rest.py @@ -21,12 +21,22 @@ from __future__ import absolute_import, unicode_literals __metaclass__ = type __all__ = [ + 'APIValueError', 'IResolvePathNames', ] +from lazr.restful.declarations import error_status from zope.interface import Interface +from mailman.core.errors import MailmanError + + + +@error_status(400) +class APIValueError(MailmanError, ValueError): + """A `ValueError` from the REST API.""" + class IResolvePathNames(Interface): diff --git a/src/mailman/rest/adapters.py b/src/mailman/rest/adapters.py index ab0279d6e..afa371c5d 100644 --- a/src/mailman/rest/adapters.py +++ b/src/mailman/rest/adapters.py @@ -38,7 +38,7 @@ from mailman.interfaces.domain import IDomainCollection, IDomainManager from mailman.interfaces.listmanager import IListManager, NoSuchListError from mailman.interfaces.member import DeliveryMode, NotAMemberError from mailman.interfaces.membership import ISubscriptionService -from mailman.interfaces.rest import IResolvePathNames +from mailman.interfaces.rest import APIValueError, IResolvePathNames @@ -102,12 +102,15 @@ class SubscriptionService: mlist = getUtility(IListManager).get(fqdn_listname) if mlist is None: raise NoSuchListError(fqdn_listname) - # Convert from string to enum. Allow ValueError exceptions to be - # returned by the API. XXX 2009-12-30 Does lazr.restful intelligently - # handle ValueError? - mode = (DeliveryMode.regular - if delivery_mode is None - else DeliveryMode(delivery_mode)) + # Convert from string to enum. Turn Python's ValueErrors into one + # suitable for the REST API. + try: + mode = (DeliveryMode.regular + if delivery_mode is None + else DeliveryMode(delivery_mode)) + except ValueError: + raise APIValueError( + 'Invalid delivery_mode: {0}'.format(delivery_mode)) if real_name is None: real_name, at, domain = address.partition('@') if len(at) == 0: diff --git a/src/mailman/rest/configure.zcml b/src/mailman/rest/configure.zcml index cf08b331e..b12d42f46 100644 --- a/src/mailman/rest/configure.zcml +++ b/src/mailman/rest/configure.zcml @@ -11,6 +11,7 @@ + @@ -52,11 +53,12 @@ /> + + + + + >> transaction.abort() + >>> dump_json('http://localhost:8001/3.0/members', { + ... 'ws.op': 'join', + ... 'fqdn_listname': 'alpha@example.com', + ... 'address': 'fperson@example.com', + ... 'real_name': 'Fred Person', + ... 'delivery_mode': 'mime_digests', + ... }) + http_etag: ... + resource_type_link: http://localhost:8001/3.0/#member + self_link: http://localhost:8001/3.0/lists/alpha@example.com/member/fperson@example.com + + >>> fred = user_manager.get_user('fperson@example.com') + >>> memberships = list(fred.memberships.members) + >>> len(memberships) + 1 + >>> memberships[0] + + on alpha@example.com as MemberRole.member> + + Corner cases ============ @@ -258,6 +284,17 @@ Then, she tries to leave a mailing list that does not exist. ... HTTPError: HTTP Error 400: Bad Request +She then tries to leave a mailing list with a bogus address. + + >>> dump_json('http://localhost:8001/3.0/members', { + ... 'ws.op': 'leave', + ... 'fqdn_listname': 'alpha@example.com', + ... 'address': 'elly', + ... }) + Traceback (most recent call last): + ... + HTTPError: HTTP Error 400: Bad Request + For some reason, Elly tries to leave the mailing list again, but she's already been unsubscribed. @@ -269,3 +306,27 @@ been unsubscribed. Traceback (most recent call last): ... HTTPError: HTTP Error 400: Bad Request + +Anna tries to join a mailing list she's already a member of. + + >>> dump_json('http://localhost:8001/3.0/members', { + ... 'ws.op': 'join', + ... 'fqdn_listname': 'alpha@example.com', + ... 'address': 'aperson@example.com', + ... }) + Traceback (most recent call last): + ... + HTTPError: HTTP Error 400: Bad Request + +Gwen tries to join the alpha mailing list using an invalid delivery mode. + + >>> dump_json('http://localhost:8001/3.0/members', { + ... 'ws.op': 'join', + ... 'fqdn_listname': 'alpha@example.com', + ... 'address': 'gperson@example.com', + ... 'real_name': 'Gwen Person', + ... 'delivery_mode': 'in_digests', + ... }) + Traceback (most recent call last): + ... + HTTPError: HTTP Error 400: Bad Request -- cgit v1.2.3-70-g09d2