[bridgedb/master] Fix email server tests

commit cfd8bf2e03a7761264f8570da0219e74ee22d36e Author: Damian Johnson <atagar@torproject.org> Date: Sat Jan 18 16:11:51 2020 -0800 Fix email server tests Test results changed as follows... before: FAILED (skips=114, failures=16, errors=108, successes=747) after: FAILED (skips=114, failures=16, errors=82, successes=772) --- bridgedb/distributors/email/server.py | 6 +++--- bridgedb/parse/addr.py | 1 + bridgedb/schedule.py | 7 ++++--- bridgedb/test/test_email_server.py | 33 +++++++++++++++++++-------------- 4 files changed, 27 insertions(+), 20 deletions(-) diff --git a/bridgedb/distributors/email/server.py b/bridgedb/distributors/email/server.py index 8a2fbea..f9590be 100644 --- a/bridgedb/distributors/email/server.py +++ b/bridgedb/distributors/email/server.py @@ -373,10 +373,10 @@ class SMTPIncomingDelivery(smtp.SMTP): ourAddress = smtp.Address(self.context.smtpFromAddr) if not ((ourAddress.domain in recipient.domain) or - (recipient.domain == "bridgedb")): + (recipient.domain == b"bridgedb")): logging.debug(("Not our domain (%s) or subdomain, skipping" " SMTP 'RCPT TO' address: %s") - % (ourAddress.domain, str(recipient))) + % (ourAddress.domain.decode('utf-8'), str(recipient))) raise smtp.SMTPBadRcpt(str(recipient)) # The recipient's username should at least start with ours, # but it still might be a '+' address. @@ -385,7 +385,7 @@ class SMTPIncomingDelivery(smtp.SMTP): " SMTP 'RCPT TO' address: %s") % str(recipient)) raise smtp.SMTPBadRcpt(str(recipient)) # Ignore everything after the first '+', if there is one. - beforePlus = recipient.local.split('+', 1)[0] + beforePlus = recipient.local.split(b'+', 1)[0] if beforePlus != ourAddress.local: raise smtp.SMTPBadRcpt(str(recipient)) diff --git a/bridgedb/parse/addr.py b/bridgedb/parse/addr.py index 334733d..1d59c9a 100644 --- a/bridgedb/parse/addr.py +++ b/bridgedb/parse/addr.py @@ -231,6 +231,7 @@ def canonicalizeEmailDomain(domain, domainmap): :returns: The canonical domain name for the email address. """ permitted = None + domain = domain.decode('utf-8') if isinstance(domain, bytes) else domain try: permitted = domainmap.get(domain) diff --git a/bridgedb/schedule.py b/bridgedb/schedule.py index 8a7ca0f..4704130 100644 --- a/bridgedb/schedule.py +++ b/bridgedb/schedule.py @@ -200,13 +200,14 @@ class ScheduledInterval(Unscheduled): :raises UnknownInterval: if the specified **count** was invalid. """ try: - if count is None or not count > 0: + if count is None: count = 1 count = int(count) except (TypeError, ValueError): - raise UnknownInterval("%s.intervalCount: %r ist not an integer." + raise UnknownInterval("%s.intervalCount: %r is not an integer." % (self.__class__.__name__, count)) - self.intervalCount = count + + self.intervalCount = max(1, count) def _setIntervalPeriod(self, period=None): """Set our :attr:`intervalPeriod`. diff --git a/bridgedb/test/test_email_server.py b/bridgedb/test/test_email_server.py index 081a0e4..fb82cc0 100644 --- a/bridgedb/test/test_email_server.py +++ b/bridgedb/test/test_email_server.py @@ -154,7 +154,7 @@ class SMTPIncomingDeliveryTests(unittest.TestCase): """ self.helo = (domain, ipaddress) self._createProtocolWithHost(domain) - self.origin = Address('@'.join((username, domain,))) + self.origin = Address(b'@'.join((username, domain,))) self.user = User(username, self.helo, self.proto, self.origin) def _setUpMAILFROM(self): @@ -173,9 +173,9 @@ class SMTPIncomingDeliveryTests(unittest.TestCase): The default is to emulate sending: ``RCPT TO: bridges@localhost``. """ - name = username if username is not None else self.config.EMAIL_USERNAME - host = domain if domain is not None else 'localhost' - addr = ip if ip is not None else '127.0.0.1' + name = username if username is not None else self.config.EMAIL_USERNAME.encode('utf-8') + host = domain if domain is not None else b'localhost' + addr = ip if ip is not None else b'127.0.0.1' self._createUser(name, host, ip) self.delivery.setContext(self.context) @@ -203,7 +203,7 @@ class SMTPIncomingDeliveryTests(unittest.TestCase): ``'client@example.com'`` should contain a header stating: ``'Received: from example.com'``. """ - self._createUser('client', 'example.com', '127.0.0.1') + self._createUser(b'client', b'example.com', b'127.0.0.1') hdr = self.delivery.receivedHeader(self.helo, self.origin, [self.user,]) self.assertSubstring("Received: from example.com", hdr) @@ -237,14 +237,14 @@ class SMTPIncomingDeliveryTests(unittest.TestCase): def test_SMTPIncomingDelivery_validateTo_plusAddress(self): """Should return a callable that results in a SMTPMessage.""" - self._setUpRCPTTO('bridges+ar') + self._setUpRCPTTO(b'bridges+ar') validated = self.delivery.validateTo(self.user) self.assertIsInstance(validated, types.FunctionType) self.assertIsInstance(validated(), server.SMTPMessage) def test_SMTPIncomingDelivery_validateTo_badUsername_plusAddress(self): """'givemebridges+zh_cn@...' should raise an SMTPBadRcpt exception.""" - self._setUpRCPTTO('givemebridges+zh_cn') + self._setUpRCPTTO(b'givemebridges+zh_cn') self.assertRaises(SMTPBadRcpt, self.delivery.validateTo, self.user) def test_SMTPIncomingDelivery_validateTo_badUsername(self): @@ -252,20 +252,20 @@ class SMTPIncomingDeliveryTests(unittest.TestCase): ``RCPT TO: yo.mama@localhost`` should raise a ``twisted.mail.smtp.SMTPBadRcpt`` exception. """ - self._setUpRCPTTO('yo.mama') + self._setUpRCPTTO(b'yo.mama') self.assertRaises(SMTPBadRcpt, self.delivery.validateTo, self.user) def test_SMTPIncomingDelivery_validateTo_notOurDomain(self): """An SMTP ``RCPT TO: bridges@forealsi.es`` should raise an SMTPBadRcpt exception. """ - self._setUpRCPTTO('bridges', 'forealsi.es') + self._setUpRCPTTO(b'bridges', b'forealsi.es') self.assertRaises(SMTPBadRcpt, self.delivery.validateTo, self.user) def test_SMTPIncomingDelivery_validateTo_subdomain(self): """An SMTP ``RCPT TO: bridges@subdomain.localhost`` should be allowed. """ - self._setUpRCPTTO('bridges', 'subdomain.localhost') + self._setUpRCPTTO(b'bridges', b'subdomain.localhost') validated = self.delivery.validateTo(self.user) self.assertIsInstance(validated, types.FunctionType) self.assertIsInstance(validated(), server.SMTPMessage) @@ -339,7 +339,7 @@ class SMTPTestCaseMixin(util.TestCaseMixin): 'Subject: %s' % subject, '\r\n %s' % body, '.'] # SMTP DATA EOM command - emailText = self.proto.delimiter.join(contents) + emailText = self.proto.delimiter.decode('utf-8').join(contents) return emailText def _buildSMTP(self, commands): @@ -351,7 +351,7 @@ class SMTPTestCaseMixin(util.TestCaseMixin): :returns: The string for sending those **commands**, suitable for giving to a :api:`twisted.internet.Protocol.dataReceived` method. """ - data = self.proto.delimiter.join(commands) + self.proto.delimiter + data = self.proto.delimiter.decode('utf-8').join(commands) + self.proto.delimiter.decode('utf-8') return data def _test(self, commands, expected, noisy=False): @@ -369,8 +369,8 @@ class SMTPTestCaseMixin(util.TestCaseMixin): "client" and the "server" in a nicely formatted manner. """ data = self._buildSMTP(commands) - self.proto.dataReceived(data) - recv = self.transport.value() + self.proto.dataReceived(data.encode('utf-8')) + recv = self.transport.value().decode('utf-8') if noisy: client = data.replace('\r\n', '\r\n ') @@ -515,7 +515,12 @@ class EmailServerServiceTests(SMTPTestCaseMixin, unittest.TestCase): def tearDown(self): """Kill all connections with fire.""" if self.transport: + if not hasattr(self.transport, 'protocol'): + factory = server.addServer(self.config, self.dist) + self.transport.protocol = factory.buildProtocol(('127.0.0.1', 0)) + self.transport.loseConnection() + super(EmailServerServiceTests, self).tearDown() # FIXME: this is definitely not how we're supposed to do this, but it # kills the DirtyReactorAggregateErrors.
participants (1)
-
phw@torproject.org