[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [bridgedb/master] Fix most autoresponder tests



commit 5aa0adcbb2725011ecd7fd11eb94d0bc2c30ab42
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Tue Jan 14 16:12:15 2020 -0800

    Fix most autoresponder tests
    
    Low hanging fruit to get all the autoresponder tests aside from
    'getMailFrom_plus_address' passing. This last test involves API
    differences between the rfc822 and email modules.
    
    Errors this resolve are the usual byte/unicode differences...
    
      Traceback (most recent call last):
        File "/home/atagar/Desktop/tor/bridgedb/bridgedb/test/test_email_autoresponder.py", line 256, in test_EmailResponse_write
          response.write(self.body)
        File "/home/atagar/Desktop/tor/bridgedb/bridgedb/distributors/email/autoresponder.py", line 273, in write
          self.mailfile.write(line.encode('utf8'))
      builtins.TypeError: string argument expected, got 'bytes'
    
    Indexing into an iterable...
    
      Traceback (most recent call last):
        File "/home/atagar/Desktop/tor/bridgedb/bridgedb/test/test_email_autoresponder.py", line 50, in setUp
          self.ctx = _createMailServerContext(self.config)
        File "/home/atagar/Desktop/tor/bridgedb/bridgedb/test/email_helpers.py", line 119, in _createMailServerContext
          context = MailServerContext(config, distributor, Unscheduled())
        File "/home/atagar/Desktop/tor/bridgedb/bridgedb/distributors/email/server.py", line 142, in __init__
          self.gpg, self.gpgSignFunc = initializeGnuPG(config)
        File "/home/atagar/Desktop/tor/bridgedb/bridgedb/crypto.py", line 318, in initializeGnuPG
          for sub in primaryPK[0]['subkeys']:
      builtins.TypeError: 'filter' object is not subscriptable
    
    ... and double encoding...
    
      Traceback (most recent call last):
        File "/home/atagar/Desktop/tor/bridgedb/bridgedb/test/test_email_autoresponder.py", line 251, in test_EmailResponse_read_three_bytes
          self.assertEqual(contents, self.body[:3])
        File "/usr/local/lib/python3.5/dist-packages/twisted/trial/_synctest.py", line 432, in assertEqual
          super(_Assertions, self).assertEqual(first, second, msg)
        File "/usr/lib/python3.5/unittest/case.py", line 820, in assertEqual
          assertion_func(first, second, msg=msg)
        File "/usr/lib/python3.5/unittest/case.py", line 1193, in assertMultiLineEqual
          self.fail(self._formatMessage(msg, standardMsg))
      twisted.trial.unittest.FailTest: "b'Peo'" != 'Peo'
      - b'Peo'
      + Peo
    
    Test results changed as follows...
    
      before: FAILED (skips=109, failures=21, errors=283, successes=567)
      after:  FAILED (skips=109, failures=24, errors=225, successes=623)
---
 bridgedb/crypto.py                           |  2 +-
 bridgedb/distributors/email/autoresponder.py | 24 ++++++++++++++++--------
 bridgedb/distributors/email/server.py        |  3 ++-
 bridgedb/test/test_email_autoresponder.py    |  8 ++++----
 4 files changed, 23 insertions(+), 14 deletions(-)

diff --git a/bridgedb/crypto.py b/bridgedb/crypto.py
index bd3d783..5b1912f 100644
--- a/bridgedb/crypto.py
+++ b/bridgedb/crypto.py
@@ -315,7 +315,7 @@ def initializeGnuPG(config):
 
     if primarySK and primaryPK:
         logging.info("Found GnuPG primary key with fingerprint: %s" % primary)
-        for sub in primaryPK[0]['subkeys']:
+        for sub in list(primaryPK)[0]['subkeys']:
             logging.info("  Subkey: %s  Usage: %s" % (sub[0], sub[1].upper()))
     else:
         logging.warn("GnuPG key %s could not be found in %s!" % (primary, gpg.secring))
diff --git a/bridgedb/distributors/email/autoresponder.py b/bridgedb/distributors/email/autoresponder.py
index bc1c7b9..1d2b618 100644
--- a/bridgedb/distributors/email/autoresponder.py
+++ b/bridgedb/distributors/email/autoresponder.py
@@ -263,6 +263,9 @@ class EmailResponse(object):
 
         :param str line: Something to append into the :data:`mailfile`.
         """
+
+        line = line.decode('utf-8') if isinstance(line, bytes) else line
+
         if line.find('\r\n') != -1:
             # If **line** contains newlines, send it to :meth:`writelines` to
             # break it up so that we can replace them:
@@ -270,7 +273,7 @@ class EmailResponse(object):
             self.writelines(line)
         else:
             line += self.delimiter
-            self.mailfile.write(line.encode('utf8'))
+            self.mailfile.write(line)
             self.mailfile.flush()
 
     def writelines(self, lines):
@@ -283,7 +286,8 @@ class EmailResponse(object):
         :type lines: :any:`str` or :any:`list`
         :param lines: The lines to write to the :attr:`mailfile`.
         """
-        if isinstance(lines, str):
+        if isinstance(lines, (str, bytes)):
+            lines = lines.decode('utf-8') if isinstance(lines, bytes) else lines
             lines = lines.replace('\r\n', '\n')
             for ln in lines.split('\n'):
                 self.write(ln)
@@ -310,20 +314,24 @@ class EmailResponse(object):
         :kwargs: If given, the key will become the name of the header, and the
             value will become the contents of that header.
         """
+
+        fromAddress = fromAddress.decode('utf-8') if isinstance(fromAddress, bytes) else fromAddress
+        toAddress = toAddress.decode('utf-8') if isinstance(toAddress, bytes) else toAddress
+
         self.write("From: %s" % fromAddress)
         self.write("To: %s" % toAddress)
         if includeMessageID:
-            self.write("Message-ID: %s" % smtp.messageid().encode('utf-8'))
+            self.write("Message-ID: %s" % smtp.messageid())
         if inReplyTo:
-            self.write("In-Reply-To: %s" % inReplyTo.encode('utf-8'))
-        self.write("Content-Type: %s" % contentType.encode('utf-8'))
-        self.write("Date: %s" % smtp.rfc822date().encode('utf-8'))
+            self.write("In-Reply-To: %s" % inReplyTo)
+        self.write("Content-Type: %s" % contentType)
+        self.write("Date: %s" % smtp.rfc822date().decode('utf-8'))
 
         if not subject:
             subject = '[no subject]'
         if not subject.lower().startswith('re'):
             subject = "Re: " + subject
-        self.write("Subject: %s" % subject.encode('utf-8'))
+        self.write("Subject: %s" % subject)
 
         if kwargs:
             for headerName, headerValue in kwargs.items():
@@ -331,7 +339,7 @@ class EmailResponse(object):
                 headerName = headerName.replace(' ', '-')
                 headerName = headerName.replace('_', '-')
                 header = "%s: %s" % (headerName, headerValue)
-                self.write(header.encode('utf-8'))
+                self.write(header)
 
         # The first blank line designates that the headers have ended:
         self.write(self.delimiter)
diff --git a/bridgedb/distributors/email/server.py b/bridgedb/distributors/email/server.py
index c1ce56f..0f96812 100644
--- a/bridgedb/distributors/email/server.py
+++ b/bridgedb/distributors/email/server.py
@@ -253,7 +253,8 @@ class SMTPMessage(object):
         """
         rawMessage = io.StringIO()
         for line in self.lines:
-            rawMessage.writelines(line.decode('utf8') + '\n')
+            line = line.decode('utf-8') if isinstance(line, bytes) else line
+            rawMessage.writelines(line + '\n')
         rawMessage.seek(0)
         return rfc822.Message(rawMessage)
 
diff --git a/bridgedb/test/test_email_autoresponder.py b/bridgedb/test/test_email_autoresponder.py
index f59e5ef..c8e9624 100644
--- a/bridgedb/test/test_email_autoresponder.py
+++ b/bridgedb/test/test_email_autoresponder.py
@@ -235,7 +235,7 @@ a ball of timey-wimey, wibbly-warbly... stuff."""
         response = autoresponder.EmailResponse()
         response.write(self.body)
         response.rewind()
-        contents = str(response.read()).replace('\x00', '')
+        contents = response.read().replace(b'\x00', b'').decode('utf-8')
         # The newlines in the email body should have been replaced with
         # ``EmailResponse.delimiter``.
         delimited = self.body.replace('\n', response.delimiter) \
@@ -247,7 +247,7 @@ a ball of timey-wimey, wibbly-warbly... stuff."""
         response = autoresponder.EmailResponse()
         response.write(self.body)
         response.rewind()
-        contents = str(response.read(3)).replace('\x00', '')
+        contents = response.read(3).replace(b'\x00', b'').decode('utf-8')
         self.assertEqual(contents, self.body[:3])
 
     def test_EmailResponse_write(self):
@@ -370,7 +370,7 @@ class SMTPAutoresponderTests(unittest.TestCase):
         """
         self._getIncomingLines()
         ours = Address(self.context.fromAddr)
-        plus = '@'.join([ours.local + '+zh_cn', ours.domain])
+        plus = '@'.join([ours.local.decode('utf-8') + '+zh_cn', ours.domain.decode('utf-8')])
         self.message.lines[1] = 'To: {0}'.format(plus)
         self._setUpResponder()
         recipient = str(self.responder.getMailFrom())
@@ -383,7 +383,7 @@ class SMTPAutoresponderTests(unittest.TestCase):
         """
         self._getIncomingLines()
         ours = Address(self.context.fromAddr)
-        plus = '@'.join(['get' + ours.local + '+zh_cn', ours.domain])
+        plus = '@'.join(['get' + ours.local.decode('utf-8') + '+zh_cn', ours.domain.decode('utf-8')])
         self.message.lines[1] = 'To: {0}'.format(plus)
         self._setUpResponder()
         recipient = str(self.responder.getMailFrom())



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits