← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~cjwatson/launchpad:py3-logintoken-url-from-bytes into launchpad:master

 

Colin Watson has proposed merging ~cjwatson/launchpad:py3-logintoken-url-from-bytes into launchpad:master.

Commit message:
Rename get_token_url_from_string to get_token_url_from_bytes

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/397700

Make it accept and require bytes, as that's easier to deal with on Python 3.
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:py3-logintoken-url-from-bytes into launchpad:master.
diff --git a/lib/lp/registry/stories/gpg-coc/xx-gpg-coc.txt b/lib/lp/registry/stories/gpg-coc/xx-gpg-coc.txt
index 950706d..2227cb9 100644
--- a/lib/lp/registry/stories/gpg-coc/xx-gpg-coc.txt
+++ b/lib/lp/registry/stories/gpg-coc/xx-gpg-coc.txt
@@ -113,8 +113,8 @@ message.
 Extract the token URL from the email:
 
     >>> from lp.services.verification.tests.logintoken import (
-    ...     get_token_url_from_string)
-    >>> token_url = get_token_url_from_string(body)
+    ...     get_token_url_from_bytes)
+    >>> token_url = get_token_url_from_bytes(body)
 
 Go to the link sent by email, to validate the email address.
 
@@ -203,7 +203,7 @@ Sample Person should visit to verify their ownership of the key.
     ...
         http://launchpad.test/token/...
 
-    >>> token_url = get_token_url_from_string(body)
+    >>> token_url = get_token_url_from_bytes(body)
 
 Side note: in a little while, Sample User will be asked to sign some
 text which includes the date the token was generated (to avoid replay
diff --git a/lib/lp/services/verification/tests/logintoken.py b/lib/lp/services/verification/tests/logintoken.py
index 0de7219..4f7d3ee 100644
--- a/lib/lp/services/verification/tests/logintoken.py
+++ b/lib/lp/services/verification/tests/logintoken.py
@@ -3,16 +3,19 @@
 
 """Helper functions for logintoken-related tests."""
 
-import email
 import re
 
+import six
+
+from lp.services.compat import message_from_bytes
+
 
 def get_token_url_from_email(email_msg):
     """Return the logintoken URL contained in the given email message."""
-    msg = email.message_from_string(email_msg)
-    return get_token_url_from_string(msg.get_payload())
+    msg = message_from_bytes(email_msg)
+    return get_token_url_from_bytes(msg.get_payload(decode=True))
 
 
-def get_token_url_from_string(s):
-    """Return the logintoken URL contained in the given string."""
-    return re.findall(r'http.*/token/.*', s)[0]
+def get_token_url_from_bytes(buf):
+    """Return the logintoken URL contained in the given byte string."""
+    return six.ensure_str(re.findall(br'http.*/token/.*', buf)[0])