← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~cjwatson/launchpad:py3-timelimitedtoken into launchpad:master

 

Colin Watson has proposed merging ~cjwatson/launchpad:py3-timelimitedtoken into launchpad:master.

Commit message:
Fix TimeLimitedToken for Python 3

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

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

TimeLimitedToken.allocate needs to return a native string, not bytes.
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:py3-timelimitedtoken into launchpad:master.
diff --git a/lib/lp/services/librarian/model.py b/lib/lp/services/librarian/model.py
index 0cd870e..d2b3d0f 100644
--- a/lib/lp/services/librarian/model.py
+++ b/lib/lp/services/librarian/model.py
@@ -16,6 +16,7 @@ import hashlib
 
 from lazr.delegates import delegate_to
 import pytz
+import six
 from six.moves.urllib.parse import urlparse
 from sqlobject import (
     BoolCol,
@@ -309,7 +310,7 @@ class TimeLimitedToken(StormBase):
     def allocate(url):
         """Allocate a token for url path in the librarian.
 
-        :param url: A url bytestring. e.g.
+        :param url: A url string. e.g.
             https://i123.restricted.launchpad-librarian.net/123/foo.txt
             Note that the token is generated for 123/foo.txt
         :return: A url fragment token ready to be attached to the url.
@@ -317,14 +318,14 @@ class TimeLimitedToken(StormBase):
         """
         store = session_store()
         path = TimeLimitedToken.url_to_token_path(url)
-        token = create_token(32).encode('ascii')
-        store.add(TimeLimitedToken(path, token))
+        token = create_token(32)
+        store.add(TimeLimitedToken(path, token.encode('ascii')))
         # The session isn't part of the main transaction model, and in fact it
         # has autocommit on. The commit here is belts and bracers: after
         # allocation the external librarian must be able to serve the file
         # immediately.
         store.commit()
-        return token
+        return six.ensure_str(token)
 
     @staticmethod
     def url_to_token_path(url):
diff --git a/lib/lp/services/librarianserver/tests/test_web.py b/lib/lp/services/librarianserver/tests/test_web.py
index 5133aa5..4366f07 100644
--- a/lib/lp/services/librarianserver/tests/test_web.py
+++ b/lib/lp/services/librarianserver/tests/test_web.py
@@ -443,7 +443,8 @@ class LibrarianWebTestCase(LibrarianWebTestMixin, TestCaseWithFactory):
         store = session_store()
         tokens = store.find(
             TimeLimitedToken,
-            TimeLimitedToken.token == hashlib.sha256(token).hexdigest())
+            TimeLimitedToken.token == hashlib.sha256(
+                token.encode('ASCII')).hexdigest())
         tokens.set(
             TimeLimitedToken.created == SQL("created - interval '1 week'"))
         # Now, as per test_restricted_no_token we should get a 404.