Merge lp:~cjwatson/lazr.restfulclient/access-token-compat into lp:lazr.restfulclient

Proposed by Colin Watson
Status: Merged
Merged at revision: 172
Proposed branch: lp:~cjwatson/lazr.restfulclient/access-token-compat
Merge into: lp:lazr.restfulclient
Diff against target: 91 lines (+52/-0)
3 files modified
NEWS.rst (+7/-0)
src/lazr/restfulclient/authorize/oauth.py (+19/-0)
src/lazr/restfulclient/tests/test_oauth.py (+26/-0)
To merge this branch: bzr merge lp:~cjwatson/lazr.restfulclient/access-token-compat
Reviewer Review Type Date Requested Status
Thiago F. Pappacena (community) Approve
LAZR Developers Pending
Review via email: mp+378084@code.launchpad.net

Commit message

Restore some AccessToken methods.

Description of the change

Restore from_string, to_string, and __str__ methods of lazr.restfulclient.authorize.oauth.AccessToken, unintentionally removed in 0.14.0. This caused a test failure when attempting to upgrade Launchpad from 0.13.2 to 0.14.2.

AccessToken.from_string doesn't have particularly clean error handling, but this was true of the previous oauth-based version as well.

The test regression was caused by https://code.launchpad.net/~cjwatson/lazr.restfulclient/oauthlib/+merge/344716.

To post a comment you must log in.
Revision history for this message
Thiago F. Pappacena (pappacena) wrote :

LGTM

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'NEWS.rst'
2--- NEWS.rst 2019-11-02 21:55:16 +0000
3+++ NEWS.rst 2020-01-26 11:00:16 +0000
4@@ -2,6 +2,13 @@
5 NEWS for lazr.restfulclient
6 ===========================
7
8+0.14.3
9+======
10+
11+ - Restore from_string, to_string, and __str__ methods of
12+ lazr.restfulclient.authorize.oauth.AccessToken, unintentionally removed
13+ in 0.14.0.
14+
15 0.14.2 (2018-11-17)
16 ===================
17
18
19=== modified file 'src/lazr/restfulclient/authorize/oauth.py'
20--- src/lazr/restfulclient/authorize/oauth.py 2018-11-17 14:43:04 +0000
21+++ src/lazr/restfulclient/authorize/oauth.py 2020-01-26 11:00:16 +0000
22@@ -31,6 +31,10 @@
23
24 from oauthlib import oauth1
25 import six
26+from six.moves.urllib.parse import (
27+ parse_qs,
28+ urlencode,
29+ )
30
31 from lazr.restfulclient.authorize import HttpAuthorizer
32 from lazr.restfulclient.errors import CredentialsFileError
33@@ -75,6 +79,21 @@
34 self.secret = secret
35 self.context = context
36
37+ def to_string(self):
38+ return urlencode([
39+ ("oauth_token_secret", self.secret),
40+ ("oauth_token", self.key),
41+ ])
42+
43+ __str__ = to_string
44+
45+ @classmethod
46+ def from_string(cls, s):
47+ params = parse_qs(s, keep_blank_values=False)
48+ key = params["oauth_token"][0]
49+ secret = params["oauth_token_secret"][0]
50+ return cls(key, secret)
51+
52
53 class TruthyString(six.text_type):
54 """A Unicode string which is always true."""
55
56=== modified file 'src/lazr/restfulclient/tests/test_oauth.py'
57--- src/lazr/restfulclient/tests/test_oauth.py 2018-04-28 12:29:24 +0000
58+++ src/lazr/restfulclient/tests/test_oauth.py 2020-01-26 11:00:16 +0000
59@@ -53,6 +53,32 @@
60 self.assertEqual(consumer.application_name, None)
61
62
63+class TestAccessToken(TestCase):
64+
65+ def test_data_fields(self):
66+ access_token = AccessToken("key", "secret", "context")
67+ self.assertEqual(access_token.key, "key")
68+ self.assertEqual(access_token.secret, "secret")
69+ self.assertEqual(access_token.context, "context")
70+
71+ def test_default_context(self):
72+ # Context defaults to None.
73+ access_token = AccessToken("key", "secret")
74+ self.assertIsNone(access_token.context)
75+
76+ def test___str__(self):
77+ access_token = AccessToken("lock&key", "secret=password")
78+ self.assertEqual(
79+ "oauth_token_secret=secret%3Dpassword&oauth_token=lock%26key",
80+ str(access_token))
81+
82+ def test_from_string(self):
83+ access_token = AccessToken.from_string(
84+ "oauth_token_secret=secret%3Dpassword&oauth_token=lock%26key")
85+ self.assertEqual(access_token.key, "lock&key")
86+ self.assertEqual(access_token.secret, "secret=password")
87+
88+
89 class TestSystemWideConsumer(TestCase):
90
91 def test_useful_distro_name(self):

Subscribers

People subscribed via source and target branches