Merge lp:~leonardr/launchpadlib/handle-unknown-token into lp:launchpadlib

Proposed by Leonard Richardson
Status: Merged
Merge reported by: Leonard Richardson
Merged at revision: not available
Proposed branch: lp:~leonardr/launchpadlib/handle-unknown-token
Merge into: lp:launchpadlib
Diff against target: 64 lines (+22/-2)
4 files modified
src/launchpadlib/NEWS.txt (+6/-0)
src/launchpadlib/__init__.py (+1/-1)
src/launchpadlib/launchpad.py (+2/-1)
src/launchpadlib/tests/test_http.py (+13/-0)
To merge this branch: bzr merge lp:~leonardr/launchpadlib/handle-unknown-token
Reviewer Review Type Date Requested Status
Benji York (community) code Approve
Review via email: mp+51563@code.launchpad.net

Description of the change

Sometimes a launchpadlib client will present Launchpad with an OAuth access token that's expired or invalid. Launchpad sends a 401 response code and an error message. launchpadlib recognizes these error messages and knows to fetch a new access token when it gets one. But it doesn't recognize the error message "Unknown access token", which is sent when a token is valid but Launchpad simply doesn't have it in its database.

Right now access tokens are kept in the Launchpad database permanently, so in normal usage this error only shows up on staging. You'll get it if you get an access token, then the staging database is wiped, then you try to use the token.

This branch makes launchpadlib request a new token when this error occurs. It's got a unit test and I've also tested it manually.

To post a comment you must log in.
Revision history for this message
Benji York (benji) wrote :

Looks good.

review: Approve (code)
Revision history for this message
Javier Collado (javier.collado) wrote :

I had today exactly this problem and, after I updated launchpadlib to this change, it was fixed. Thanks.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/launchpadlib/NEWS.txt'
2--- src/launchpadlib/NEWS.txt 2011-02-15 12:15:15 +0000
3+++ src/launchpadlib/NEWS.txt 2011-02-28 16:18:29 +0000
4@@ -2,6 +2,12 @@
5 NEWS for launchpadlib
6 =====================
7
8+1.9.8 (2011-02-28)
9+==================
10+
11+- Detect the error Launchpad sends when it doesn't recognize an access
12+ token, and get a new token.
13+
14 1.9.7 (2011-02-15)
15 ==================
16
17
18=== modified file 'src/launchpadlib/__init__.py'
19--- src/launchpadlib/__init__.py 2011-02-15 12:15:15 +0000
20+++ src/launchpadlib/__init__.py 2011-02-28 16:18:29 +0000
21@@ -14,4 +14,4 @@
22 # You should have received a copy of the GNU Lesser General Public License
23 # along with launchpadlib. If not, see <http://www.gnu.org/licenses/>.
24
25-__version__ = '1.9.7'
26+__version__ = '1.9.8'
27
28=== modified file 'src/launchpadlib/launchpad.py'
29--- src/launchpadlib/launchpad.py 2011-02-15 12:03:58 +0000
30+++ src/launchpadlib/launchpad.py 2011-02-28 16:18:29 +0000
31@@ -118,7 +118,8 @@
32 """Helper method to detect an error caused by a bad OAuth token."""
33 return (response.status == 401 and
34 (content.startswith("Expired token")
35- or content.startswith("Invalid token")))
36+ or content.startswith("Invalid token")
37+ or content.startswith("Unknown access token")))
38
39 def _request(self, *args):
40 response, content = super(
41
42=== modified file 'src/launchpadlib/tests/test_http.py'
43--- src/launchpadlib/tests/test_http.py 2011-01-10 14:34:03 +0000
44+++ src/launchpadlib/tests/test_http.py 2011-02-28 16:18:29 +0000
45@@ -200,6 +200,19 @@
46 'application name', authorization_engine=self.engine)
47 self.assertEquals(self.engine.access_tokens_obtained, 2)
48
49+ def test_unknown_token(self):
50+ """If our token is unknown, we get another one."""
51+
52+ SimulatedResponsesLaunchpad.responses = [
53+ Response(401, "Unknown access token."),
54+ Response(200, SIMPLE_WADL),
55+ Response(200, SIMPLE_JSON)]
56+
57+ self.assertEquals(self.engine.access_tokens_obtained, 0)
58+ launchpad = SimulatedResponsesLaunchpad.login_with(
59+ 'application name', authorization_engine=self.engine)
60+ self.assertEquals(self.engine.access_tokens_obtained, 2)
61+
62 def test_delayed_error(self):
63 """We get another token no matter when the error happens."""
64 SimulatedResponsesLaunchpad.responses = [

Subscribers

People subscribed via source and target branches