~djfroofy/twisted/5013-txAfterResponse

« back to all changes in this revision

Viewing changes to twisted/mail/test/test_options.py

  • Committer: exarkun
  • Date: 2011-03-21 13:42:30 UTC
  • Revision ID: svn-v4:bbbe8e31-12d6-0310-92fd-ac37d47ddeeb:trunk:31358
Revert r31290 - test failure on Windows, which lacks "/dev/null"

Reopens #4739

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
from twisted.trial.unittest import TestCase
9
9
 
10
10
from twisted.python.usage import UsageError
11
 
from twisted.mail import protocols
12
 
from twisted.mail.tap import Options, makeService
 
11
from twisted.mail.tap import Options
13
12
from twisted.python import deprecate
14
13
from twisted.python import versions
15
 
from twisted.internet import endpoints, defer
16
14
 
17
15
 
18
16
class OptionsTestCase(TestCase):
19
17
    """
20
 
    Tests for the command line option parser used for I{twistd mail}.
 
18
    Tests for the command line option parser used for C{twistd mail}.
21
19
    """
22
20
    def setUp(self):
23
21
        self.aliasFilename = self.mktemp()
61
59
        self.assertEquals(warnings[0]['message'], msg)
62
60
 
63
61
 
64
 
    def test_barePort(self):
65
 
        """
66
 
        A bare port passed to I{--pop3} results in deprecation warning in
67
 
        addition to a TCP4ServerEndpoint.
68
 
        """
69
 
        options = Options()
70
 
        options.parseOptions(['--pop3', '8110'])
71
 
        self.assertEquals(len(options['pop3']), 1)
72
 
        self.assertIsInstance(
73
 
            options['pop3'][0], endpoints.TCP4ServerEndpoint)
74
 
        warnings = self.flushWarnings([options.opt_pop3])
75
 
        self.assertEquals(len(warnings), 1)
76
 
        self.assertEquals(warnings[0]['category'], DeprecationWarning)
77
 
        self.assertEquals(
78
 
            warnings[0]['message'],
79
 
            "Specifying plain ports and/or a certificate is deprecated since "
80
 
            "Twisted 11.0; use endpoint descriptions instead.")
81
 
 
82
 
 
83
 
    def _endpointTest(self, service):
84
 
        """
85
 
        Use L{Options} to parse a single service configuration parameter and
86
 
        verify that an endpoint of the correct type is added to the list for
87
 
        that service.
88
 
        """
89
 
        options = Options()
90
 
        options.parseOptions(['--' + service, 'tcp:1234'])
91
 
        self.assertEquals(len(options[service]), 1)
92
 
        self.assertIsInstance(
93
 
            options[service][0], endpoints.TCP4ServerEndpoint)
94
 
 
95
 
 
96
 
    def test_endpointSMTP(self):
97
 
        """
98
 
        When I{--smtp} is given a TCP endpoint description as an argument, a
99
 
        TCPServerEndpoint is added to the list of SMTP endpoints.
100
 
        """
101
 
        self._endpointTest('smtp')
102
 
 
103
 
 
104
 
    def test_endpointPOP3(self):
105
 
        """
106
 
        When I{--pop3} is given a TCP endpoint description as an argument, a
107
 
        TCPServerEndpoint is added to the list of POP3 endpoints.
108
 
        """
109
 
        self._endpointTest('pop3')
110
 
 
111
 
 
112
 
    def test_protoDefaults(self):
113
 
        """
114
 
        POP3 and SMTP each listen on a TCP4ServerEndpoint by default.
115
 
        """
116
 
        options = Options()
117
 
        options.parseOptions([])
118
 
 
119
 
        self.assertEquals(len(options['pop3']), 1)
120
 
        self.assertIsInstance(
121
 
            options['pop3'][0], endpoints.TCP4ServerEndpoint)
122
 
 
123
 
        self.assertEquals(len(options['smtp']), 1)
124
 
        self.assertIsInstance(
125
 
            options['smtp'][0], endpoints.TCP4ServerEndpoint)
126
 
 
127
 
 
128
 
    def test_protoDisable(self):
129
 
        """
130
 
        The I{--no-pop3} and I{--no-smtp} options disable POP3 and SMTP
131
 
        respectively.
132
 
        """
133
 
        options = Options()
134
 
        options.parseOptions(['--no-pop3'])
135
 
        self.assertEquals(options._getEndpoints(None, 'pop3'), [])
136
 
        self.assertNotEquals(options._getEndpoints(None, 'smtp'), [])
137
 
 
138
 
        options = Options()
139
 
        options.parseOptions(['--no-smtp'])
140
 
        self.assertNotEquals(options._getEndpoints(None, 'pop3'), [])
141
 
        self.assertEquals(options._getEndpoints(None, 'smtp'), [])
142
 
 
143
 
 
144
 
    def test_allProtosDisabledError(self):
145
 
        """
146
 
        If all protocols are disabled, L{UsageError} is raised.
147
 
        """
148
 
        options = Options()
149
 
        self.assertRaises(
150
 
            UsageError, options.parseOptions, (['--no-pop3', '--no-smtp']))
151
 
 
152
 
 
153
 
    def test_pop3sBackwardCompatibility(self):
154
 
        """
155
 
        The deprecated I{--pop3s} and I{--certificate} options set up a POP3 SSL
156
 
        server.
157
 
        """
158
 
        options = Options()
159
 
        options.parseOptions(['--pop3s', '8995',
160
 
                              '--certificate', '/dev/null'])
161
 
        self.assertEquals(len(options['pop3']), 2)
162
 
        self.assertIsInstance(
163
 
            options['pop3'][0], endpoints.SSL4ServerEndpoint)
164
 
        self.assertIsInstance(
165
 
            options['pop3'][1], endpoints.TCP4ServerEndpoint)
166
 
 
167
 
        warnings = self.flushWarnings([options.postOptions])
168
 
        self.assertEquals(len(warnings), 1)
169
 
        self.assertEquals(warnings[0]['category'], DeprecationWarning)
170
 
        self.assertEquals(
171
 
            warnings[0]['message'],
172
 
            "Specifying plain ports and/or a certificate is deprecated since "
173
 
            "Twisted 11.0; use endpoint descriptions instead.")
174
 
 
175
 
 
176
 
 
177
 
class SpyEndpoint(object):
178
 
    """
179
 
    SpyEndpoint remembers what factory it is told to listen with.
180
 
    """
181
 
    listeningWith = None
182
 
    def listen(self, factory):
183
 
        self.listeningWith = factory
184
 
        return defer.succeed(None)
185
 
 
186
 
 
187
 
 
188
 
class MakeServiceTests(TestCase):
189
 
    """
190
 
    Tests for L{twisted.mail.tap.makeService}
191
 
    """
192
 
    def _endpointServerTest(self, key, factoryClass):
193
 
        """
194
 
        Configure a service with two endpoints for the protocol associated with
195
 
        C{key} and verify that when the service is started a factory of type
196
 
        C{factoryClass} is used to listen on each of them.
197
 
        """
198
 
        cleartext = SpyEndpoint()
199
 
        secure = SpyEndpoint()
200
 
        config = Options()
201
 
        config[key] = [cleartext, secure]
202
 
        service = makeService(config)
203
 
        service.privilegedStartService()
204
 
        service.startService()
205
 
        self.addCleanup(service.stopService)
206
 
        self.assertIsInstance(cleartext.listeningWith, factoryClass)
207
 
        self.assertIsInstance(secure.listeningWith, factoryClass)
208
 
 
209
 
 
210
 
    def test_pop3(self):
211
 
        """
212
 
        If one or more endpoints is included in the configuration passed to
213
 
        L{makeService} for the C{"pop3"} key, a service for starting a POP3
214
 
        server is constructed for each of them and attached to the returned
215
 
        service.
216
 
        """
217
 
        self._endpointServerTest("pop3", protocols.POP3Factory)
218
 
 
219
 
 
220
 
    def test_smtp(self):
221
 
        """
222
 
        If one or more endpoints is included in the configuration passed to
223
 
        L{makeService} for the C{"smtp"} key, a service for starting an SMTP
224
 
        server is constructed for each of them and attached to the returned
225
 
        service.
226
 
        """
227
 
        self._endpointServerTest("smtp", protocols.SMTPFactory)