~stephen-xemacs/mailman/sprint-2012-overview

« back to all changes in this revision

Viewing changes to src/mailman/rest/tests/test_moderation.py

  • Committer: Barry Warsaw
  • Date: 2012-01-30 15:37:16 UTC
  • Revision ID: barry@list.org-20120130153716-s9qx07i6i0rltyax
 * Held messages can now be moderated through the REST API.  Mailing list
   resources now accept a `held` path component.  GETing this returns all held
   messages for the mailing list.  POSTing to a specific request id under this
   url can dispose of the message using `Action` enums.
 * `IRequests` interface is removed.  Now just use adaptation from
   `IListRequests` directly (which takes an `IMailingList` object).
 * `handle_message()` now allows for `Action.hold` which is synonymous with
   `Action.defer` (since the message is already being held).
 * `IListRequests.get_request()` now takes an optional `request_type`
   argument to narrow the search for the given request.

- also, print_function is now a standard __future__ import.  The template has
  been updated, but add this to modules as you edit them.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2012 by the Free Software Foundation, Inc.
 
2
#
 
3
# This file is part of GNU Mailman.
 
4
#
 
5
# GNU Mailman is free software: you can redistribute it and/or modify it under
 
6
# the terms of the GNU General Public License as published by the Free
 
7
# Software Foundation, either version 3 of the License, or (at your option)
 
8
# any later version.
 
9
#
 
10
# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
 
11
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
12
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 
13
# more details.
 
14
#
 
15
# You should have received a copy of the GNU General Public License along with
 
16
# GNU Mailman.  If not, see <http://www.gnu.org/licenses/>.
 
17
 
 
18
"""REST moderation tests."""
 
19
 
 
20
from __future__ import absolute_import, print_function, unicode_literals
 
21
 
 
22
__metaclass__ = type
 
23
__all__ = [
 
24
    ]
 
25
 
 
26
 
 
27
import unittest
 
28
 
 
29
from urllib2 import HTTPError
 
30
 
 
31
from mailman.app.lifecycle import create_list
 
32
from mailman.app.moderator import hold_message, hold_subscription
 
33
from mailman.config import config
 
34
from mailman.interfaces.member import DeliveryMode
 
35
from mailman.testing.helpers import (
 
36
    call_api, specialized_message_from_string as mfs)
 
37
from mailman.testing.layers import RESTLayer
 
38
 
 
39
 
 
40
 
 
41
class TestModeration(unittest.TestCase):
 
42
    layer = RESTLayer
 
43
 
 
44
    def setUp(self):
 
45
        self._mlist = create_list('ant@example.com')
 
46
        self._msg = mfs("""\
 
47
From: anne@example.com
 
48
To: ant@example.com
 
49
Subject: Something
 
50
Message-ID: <alpha>
 
51
 
 
52
Something else.
 
53
""")
 
54
        config.db.commit()
 
55
 
 
56
    def test_not_found(self):
 
57
        # When a bogus mailing list is given, 404 should result.
 
58
        try:
 
59
            # For Python 2.6
 
60
            call_api('http://localhost:9001/3.0/lists/bee@example.com/held')
 
61
        except HTTPError as exc:
 
62
            self.assertEqual(exc.code, 404)
 
63
        else:
 
64
            raise AssertionError('Expected HTTPError')
 
65
 
 
66
    def test_bad_request_id(self):
 
67
        # Bad request when request_id is not an integer.
 
68
        try:
 
69
            # For Python 2.6
 
70
            call_api(
 
71
                'http://localhost:9001/3.0/lists/ant@example.com/held/bogus')
 
72
        except HTTPError as exc:
 
73
            self.assertEqual(exc.code, 400)
 
74
        else:
 
75
            raise AssertionError('Expected HTTPError')
 
76
 
 
77
    def test_subscription_request_as_held_message(self):
 
78
        # Provide the request id of a subscription request using the held
 
79
        # message API returns a not-found even though the request id is
 
80
        # in the database.
 
81
        held_id = hold_message(self._mlist, self._msg)
 
82
        subscribe_id = hold_subscription(
 
83
            self._mlist, 'bperson@example.net', 'Bart Person', 'xyz',
 
84
            DeliveryMode.regular, 'en')
 
85
        config.db.store.commit()
 
86
        url = 'http://localhost:9001/3.0/lists/ant@example.com/held/{0}'
 
87
        try:
 
88
            call_api(url.format(subscribe_id))
 
89
        except HTTPError as exc:
 
90
            self.assertEqual(exc.code, 404)
 
91
        else:
 
92
            raise AssertionError('Expected HTTPError')
 
93
        # But using the held_id returns a valid response.
 
94
        response, content = call_api(url.format(held_id))
 
95
        self.assertEqual(response['key'], '<alpha>')
 
96
 
 
97
    def test_bad_action(self):
 
98
        # POSTing to a held message with a bad action.
 
99
        held_id = hold_message(self._mlist, self._msg)
 
100
        url = 'http://localhost:9001/3.0/lists/ant@example.com/held/{0}'
 
101
        try:
 
102
            call_api(url.format(held_id), {'action': 'bogus'})
 
103
        except HTTPError as exc:
 
104
            self.assertEqual(exc.code, 400)
 
105
            self.assertEqual(exc.msg, 'Cannot convert parameters: action')
 
106
        else:
 
107
            raise AssertionError('Expected HTTPError')