Fom

Merge lp:~terrycojones/fom/add-oauth-calls-899576 into lp:fom

Proposed by Terry Jones
Status: Merged
Merge reported by: Nicholas Tollervey
Merged at revision: not available
Proposed branch: lp:~terrycojones/fom/add-oauth-calls-899576
Merge into: lp:fom
Diff against target: 82 lines (+44/-5)
3 files modified
fom/db.py (+11/-1)
fom/session.py (+14/-4)
tests/test_db.py (+19/-0)
To merge this branch: bzr merge lp:~terrycojones/fom/add-oauth-calls-899576
Reviewer Review Type Date Requested Status
Nicholas Tollervey Approve
Review via email: mp+84364@code.launchpad.net

Description of the change

Makes it possible to make OAuth2 calls to Fluidinfo.

To post a comment you must log in.
Revision history for this message
Nicholas Tollervey (ntoll) wrote :

All the tests pass. I've also tried running some requests via the main instance using the new oAuth functionality and it works. +1 LGTM.

Now I just need to remember how to merge the bloody thing.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'fom/db.py'
2--- fom/db.py 2011-08-30 11:40:04 +0000
3+++ fom/db.py 2011-12-03 13:18:24 +0000
4@@ -246,7 +246,17 @@
5 auth = 'Basic ' + userpass.encode('base64').strip()
6 self.headers['Authorization'] = auth
7
8+ def login_oauth2(self, token):
9+ """Prepare to make OAuth2 calls to Fluidinfo.
10+
11+ :param token: The OAuth token to pass in calls to Fluidinfo.
12+ """
13+ self.headers['Authorization'] = 'oauth2'
14+ self.headers['X-FluidDB-Access-Token'] = token
15+
16 def logout(self):
17 """Log out of this FluidDB instance
18 """
19- del self.headers['Authorization']
20+ # Use pop here, to avoid catching KeyError if login was never called.
21+ self.headers.pop('Authorization', None)
22+ self.headers.pop('X-FluidDB-Access-Token', None)
23
24=== modified file 'fom/session.py'
25--- fom/session.py 2010-02-06 22:29:08 +0000
26+++ fom/session.py 2011-12-03 13:18:24 +0000
27@@ -54,11 +54,21 @@
28 """
29 return self.db.login(username, password)
30
31+ def login_oauth2(self, token):
32+ """Log in to the connected FluidDB using OAuth2
33+
34+ The login is maintained for the session until :meth:`~Fluid.logout`
35+ is called.
36+
37+ >>> fdb = Fluid()
38+ >>> fdb.login_oauth2(u'token')
39+ >>> fdb.users.get(u'aliafshar') # this is now authenticated
40+
41+ :param token: The OAuth token to pass in calls to Fluidinfo.
42+ """
43+ return self.db.login_oauth2(token)
44+
45 def logout(self):
46 """Log out of the FluidDB session
47 """
48 return self.db.logout()
49-
50-
51-
52-
53
54=== modified file 'tests/test_db.py'
55--- tests/test_db.py 2010-12-24 15:30:04 +0000
56+++ tests/test_db.py 2011-12-03 13:18:24 +0000
57@@ -184,6 +184,25 @@
58 db.logout()
59 self.assertFalse('Authorization' in db.headers)
60
61+ def testLoginLogoutOAuth2(self):
62+ """
63+ Make sure login and logout functions set things up correctly
64+ when we use OAuth2.
65+ """
66+ token = 'kajfjowijmssafuwoisflsjlfsoieuossfh'
67+ db = FluidDB(TEST_INSTANCE)
68+ # start from a blank slate
69+ self.assertFalse('Authorization' in db.headers)
70+ self.assertFalse('X-FluidDB-Access-Token' in db.headers)
71+ # Login
72+ db.login_oauth2(token)
73+ self.assertEquals(db.headers['Authorization'], 'oauth2')
74+ self.assertEquals(db.headers['X-FluidDB-Access-Token'], token)
75+ # Logout
76+ db.logout()
77+ self.assertFalse('Authorization' in db.headers)
78+ self.assertFalse('X-FluidDB-Access-Token' in db.headers)
79+
80
81 if __name__ == '__main__':
82 unittest.main()

Subscribers

People subscribed via source and target branches

to all changes: