Merge lp:~dobey/ubuntuone-client/cp-quota-info into lp:ubuntuone-client

Proposed by dobey
Status: Merged
Approved by: Rick McBride
Approved revision: not available
Merged at revision: not available
Proposed branch: lp:~dobey/ubuntuone-client/cp-quota-info
Merge into: lp:ubuntuone-client
Diff against target: 198 lines (+95/-4)
2 files modified
bin/ubuntuone-preferences (+40/-2)
tests/test_preferences.py (+55/-2)
To merge this branch: bzr merge lp:~dobey/ubuntuone-client/cp-quota-info
Reviewer Review Type Date Requested Status
Rick McBride (community) Approve
Eric Casteleijn (community) Approve
Review via email: mp+19462@code.launchpad.net

Commit message

Get real quota info from the server and display it in the control panel

To post a comment you must log in.
Revision history for this message
Eric Casteleijn (thisfred) wrote :

looks good, all tests green

review: Approve
Revision history for this message
Rick McBride (rmcbride) wrote :

looks good

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bin/ubuntuone-preferences'
2--- bin/ubuntuone-preferences 2010-02-17 02:19:29 +0000
3+++ bin/ubuntuone-preferences 2010-02-17 04:05:23 +0000
4@@ -26,6 +26,10 @@
5 import gtk
6 import os
7 import gettext
8+import gnomekeyring
9+import httplib2
10+import simplejson
11+from oauth import oauth
12 from ubuntuone import clientdefs
13
14 import dbus.service
15@@ -79,6 +83,8 @@
16 self.dn_limit = 2097152
17
18 self.__bus = dbus.SessionBus()
19+ self.keyring = gnomekeyring
20+ self.httpclient = httplib2.Http()
21
22 try:
23 client = self.__bus.get_object(DBUS_IFACE_NAME, "/config",
24@@ -198,7 +204,37 @@
25 'type' : real_type,
26 'percent' : percent })
27 self.usage_graph.set_fraction(percent / 100)
28-
29+
30+ def request_quota_info(self):
31+ """Request new quota info from server, and update display."""
32+ consumer = oauth.OAuthConsumer("ubuntuone", "hammertime")
33+ items = []
34+ items = self.keyring.find_items_sync(
35+ gnomekeyring.ITEM_GENERIC_SECRET,
36+ {'ubuntuone-realm': "https://ubuntuone.com",
37+ 'oauth-consumer-key': consumer.key})
38+ token = oauth.OAuthToken.from_string(items[0].secret)
39+ request = oauth.OAuthRequest.from_consumer_and_token(
40+ http_url='https://one.ubuntu.com/api/quota/',
41+ http_method='GET',
42+ oauth_consumer=consumer,
43+ token=token)
44+ request.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(),
45+ consumer, token)
46+ client = self.httpclient
47+ headers = {}
48+ headers.update(request.to_header())
49+ resp, content = client.request(request.http_url,
50+ request.http_method,
51+ headers=headers)
52+ if resp['status'] == '200':
53+ quota = simplejson.loads(content)
54+ self.update_quota_display(quota['used'], quota['total'])
55+ # FIXME: Log json parsing failures
56+ else:
57+ # FIXME: Log errors
58+ return
59+
60 def __construct(self):
61 """Construct the dialog's layout."""
62 area = self.get_content_area()
63@@ -324,6 +360,7 @@
64 """Show our dialog, since we can do stuff now."""
65 self.disconnect_signal_handlers()
66 dialog = UbuntuOneDialog()
67+ dialog.request_quota_info()
68 dialog.show()
69
70 def got_oautherror(self, message=None):
71@@ -372,7 +409,7 @@
72 DBUS_IFACE_AUTH_PATH,
73 follow_name_owner_changes=True)
74 iface = dbus.Interface(client, DBUS_IFACE_AUTH_NAME)
75- iface.login('https://one.ubuntu.com/', 'ubuntuone',
76+ iface.login('https://ubuntuone.com', 'ubuntuone',
77 reply_handler=dbus_async,
78 error_handler=self.got_dbus_error)
79 except DBusException, e:
80@@ -384,6 +421,7 @@
81 gettext.textdomain(clientdefs.GETTEXT_PACKAGE)
82
83 gtk.rc_parse_string(RCSTYLE)
84+ gobject.set_application_name("Ubuntu One")
85
86 try:
87 login = UbuntuoneLoginHandler()
88
89=== modified file 'tests/test_preferences.py'
90--- tests/test_preferences.py 2010-02-17 02:19:29 +0000
91+++ tests/test_preferences.py 2010-02-17 04:05:23 +0000
92@@ -19,7 +19,9 @@
93
94 import new
95 import os
96+import gnomekeyring
97
98+from contrib.mocker import MockerTestCase, KWARGS
99 from contrib.testing.testcase import DBusTwistedTestCase, FakeLogin
100 from twisted.internet import defer
101 from twisted.python.failure import Failure
102@@ -29,7 +31,7 @@
103 """Exception for when we get the wrong signal called."""
104 pass
105
106-class PreferencesTests(DBusTwistedTestCase):
107+class PreferencesTests(MockerTestCase, DBusTwistedTestCase):
108 """Basic tests for the ubuntuone-preferences app."""
109
110 _path = os.path.join(os.getcwd(), "bin", "ubuntuone-preferences")
111@@ -38,11 +40,39 @@
112 u1prefs.DBUS_IFACE_AUTH_PATH = '/oauthdesktop'
113
114 def setUp(self):
115+ MockerTestCase.setUp(self)
116 DBusTwistedTestCase.setUp(self)
117 self.oauth = FakeLogin(self.bus)
118 self._old_path = dbus_interface.DBUS_PATH_AUTH
119 dbus_interface.DBUS_PATH_AUTH = '/oauthdesktop'
120
121+ # For testing keyring queries
122+ self.keyring = self.mocker.mock()
123+ self.item = self.mocker.mock(gnomekeyring.Found)
124+
125+ self.item_id = 999
126+
127+ ex = self.expect(self.item.item_id)
128+ ex.result(self.item_id)
129+ ex.count(0, None)
130+
131+ ex = self.expect(self.item.secret)
132+ ex.result('oauth_token=access_key&oauth_token_secret=access_secret')
133+ ex.count(0, None)
134+
135+ def expect_token_query(self):
136+ """Expects the keyring to be queried for a token."""
137+ return self.expect(
138+ self.keyring.find_items_sync(
139+ gnomekeyring.ITEM_GENERIC_SECRET,
140+ {'ubuntuone-realm': 'https://ubuntuone.com',
141+ 'oauth-consumer-key': 'ubuntuone'})
142+ )
143+
144+ def mock_has_token(self):
145+ """Mocks a cached token in the keyring."""
146+ self.expect_token_query().result([self.item])
147+
148 def tearDown(self):
149 # collect all signal receivers registered during the test
150 signal_receivers = set()
151@@ -61,23 +91,46 @@
152
153 def test_bw_throttling(self):
154 """Test that toggling bw throttling works correctly."""
155-
156+ self.mocker.replay()
157 dialog = self.u1prefs.UbuntuOneDialog()
158 self.assertTrue(dialog is not None)
159+ dialog.notebook.set_current_page(1)
160 self.assertFalse(dialog.bw_table.get_property('sensitive'))
161 dialog.limit_check.set_active(True)
162 self.assertTrue(dialog.bw_table.get_property('sensitive'))
163+ dialog.destroy()
164
165 def test_quota_display(self):
166 """Test that quota display works correctly."""
167+ self.mocker.replay()
168 dialog = self.u1prefs.UbuntuOneDialog()
169 self.assertTrue(dialog is not None)
170 self.assertEqual(dialog.usage_graph.get_fraction(), 0.0)
171 dialog.update_quota_display(1024, 2048)
172 self.assertEqual(dialog.usage_graph.get_fraction(), 0.5)
173+ dialog.destroy()
174+
175+ def test_request_quota_info(self):
176+ """Test that we can request the quota info properly."""
177+ self.mock_has_token()
178+ dialog = self.u1prefs.UbuntuOneDialog()
179+ dialog.keyring = self.keyring
180+ self.assertEqual(dialog.usage_graph.get_fraction(), 0.0)
181+ self.assertTrue(dialog is not None)
182+ response = { 'status' : '200' }
183+ content = '{"total":2048, "used":1024}'
184+ client = self.mocker.mock()
185+ dialog.httpclient = client
186+ self.expect(client.request('https://one.ubuntu.com/api/quota/',
187+ 'GET', KWARGS)).result((response, content))
188+ self.mocker.replay()
189+ dialog.request_quota_info()
190+ self.assertEqual(dialog.usage_graph.get_fraction(), 0.5)
191+ dialog.destroy()
192
193 def test_login_check(self):
194 """Test that our login check works correctly."""
195+ self.mocker.replay()
196 def got_new_creds(realm=None, consumer_key=None, sender=None):
197 """ Override the callback """
198 d.callback(True)

Subscribers

People subscribed via source and target branches