Merge lp:~alecu/ubuntu-sso-client/keyring-api-change into lp:ubuntu-sso-client
- keyring-api-change
- Merge into trunk
Proposed by
Alejandro J. Cura
Status: | Merged | ||||||||
---|---|---|---|---|---|---|---|---|---|
Approved by: | Rodrigo Moya | ||||||||
Approved revision: | 593 | ||||||||
Merged at revision: | 599 | ||||||||
Proposed branch: | lp:~alecu/ubuntu-sso-client/keyring-api-change | ||||||||
Merge into: | lp:ubuntu-sso-client | ||||||||
Diff against target: |
526 lines (+219/-152) 4 files modified
ubuntu_sso/keyring.py (+89/-38) ubuntu_sso/main.py (+1/-28) ubuntu_sso/tests/test_keyring.py (+125/-29) ubuntu_sso/tests/test_main.py (+4/-57) |
||||||||
To merge this branch: | bzr merge lp:~alecu/ubuntu-sso-client/keyring-api-change | ||||||||
Related bugs: |
|
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Rodrigo Moya (community) | Approve | ||
John Lenton (community) | Approve | ||
Review via email: mp+33840@code.launchpad.net |
Description of the change
Use the keyring unlocking gnomekeyring apis and search all keyrings for the credentials
To post a comment you must log in.
Revision history for this message
Rodrigo Moya (rodrigo-moya) : | # |
review:
Approve
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'ubuntu_sso/keyring.py' | |||
2 | --- ubuntu_sso/keyring.py 2010-08-19 13:39:50 +0000 | |||
3 | +++ ubuntu_sso/keyring.py 2010-08-26 21:18:42 +0000 | |||
4 | @@ -19,77 +19,128 @@ | |||
5 | 19 | 19 | ||
6 | 20 | """Handle keys in the gnome kerying.""" | 20 | """Handle keys in the gnome kerying.""" |
7 | 21 | 21 | ||
8 | 22 | import socket | ||
9 | 23 | import urllib | ||
10 | 24 | import urlparse | ||
11 | 25 | |||
12 | 22 | import gnomekeyring | 26 | import gnomekeyring |
13 | 23 | 27 | ||
16 | 24 | from urllib import urlencode | 28 | from ubuntu_sso.logger import setupLogging |
17 | 25 | from urlparse import parse_qsl | 29 | logger = setupLogging("ubuntu_sso.main") |
18 | 30 | |||
19 | 31 | U1_APP_NAME = "Ubuntu One" | ||
20 | 32 | U1_KEY_NAME = "UbuntuOne token for https://ubuntuone.com" | ||
21 | 33 | U1_KEY_ATTR = { | ||
22 | 34 | "oauth-consumer-key": "ubuntuone", | ||
23 | 35 | "ubuntuone-realm": "https://ubuntuone.com", | ||
24 | 36 | } | ||
25 | 37 | |||
26 | 38 | |||
27 | 39 | def get_token_name(app_name): | ||
28 | 40 | """Build the token name.""" | ||
29 | 41 | quoted_app_name = urllib.quote(app_name) | ||
30 | 42 | computer_name = socket.gethostname() | ||
31 | 43 | quoted_computer_name = urllib.quote(computer_name) | ||
32 | 44 | return "%s - %s" % (quoted_app_name, quoted_computer_name) | ||
33 | 26 | 45 | ||
34 | 27 | 46 | ||
35 | 28 | class Keyring(object): | 47 | class Keyring(object): |
37 | 29 | 48 | """A Keyring for a given application name.""" | |
38 | 30 | KEYRING_NAME = "login" | 49 | KEYRING_NAME = "login" |
39 | 31 | 50 | ||
40 | 32 | def __init__(self, app_name): | 51 | def __init__(self, app_name): |
41 | 52 | """Initialize this instance given the app_name.""" | ||
42 | 33 | if not gnomekeyring.is_available(): | 53 | if not gnomekeyring.is_available(): |
43 | 34 | raise gnomekeyring.NoKeyringDaemonError | 54 | raise gnomekeyring.NoKeyringDaemonError |
44 | 35 | self.app_name = app_name | 55 | self.app_name = app_name |
45 | 56 | self.token_name = get_token_name(self.app_name) | ||
46 | 36 | 57 | ||
47 | 37 | def _create_keyring(self, name): | 58 | def _create_keyring(self, name): |
49 | 38 | """Creates a keyring, if it already exists, do nothing.""" | 59 | """Creates a keyring, or if it already exists, it does nothing.""" |
50 | 39 | keyring_names = gnomekeyring.list_keyring_names_sync() | 60 | keyring_names = gnomekeyring.list_keyring_names_sync() |
51 | 40 | if not name in keyring_names: | 61 | if not name in keyring_names: |
52 | 41 | gnomekeyring.create_sync(name) | 62 | gnomekeyring.create_sync(name) |
53 | 42 | 63 | ||
69 | 43 | def _get_item_id_from_name(self, sync, name): | 64 | def _find_keyring_item(self): |
70 | 44 | """Return the ID for a named item.""" | 65 | """Return the keyring item or None if not found.""" |
71 | 45 | for item_id in gnomekeyring.list_item_ids_sync(sync): | 66 | try: |
72 | 46 | item_info = gnomekeyring.item_get_info_sync(sync, item_id) | 67 | items = gnomekeyring.find_items_sync( |
73 | 47 | display_name = item_info.get_display_name() | 68 | gnomekeyring.ITEM_GENERIC_SECRET, |
74 | 48 | if display_name == name: | 69 | self._get_keyring_attr()) |
75 | 49 | return item_id | 70 | except gnomekeyring.NoMatchError: |
76 | 50 | 71 | # if no items found, return None | |
77 | 51 | def _get_item_info_from_name(self, sync, name): | 72 | return None |
78 | 52 | """Return the ID for a named item.""" | 73 | |
79 | 53 | for item_id in gnomekeyring.list_item_ids_sync(sync): | 74 | # we priorize the item in the "login" keyring |
80 | 54 | item_info = gnomekeyring.item_get_info_sync(sync, item_id) | 75 | for item in items: |
81 | 55 | display_name = item_info.get_display_name() | 76 | if item.keyring == "login": |
82 | 56 | if display_name == name: | 77 | return item |
83 | 57 | return item_info | 78 | |
84 | 79 | # if not on the "login" keyring, we return the first item | ||
85 | 80 | return items[0] | ||
86 | 81 | |||
87 | 82 | def _get_keyring_attr(self): | ||
88 | 83 | """Build the keyring attributes for this credentials.""" | ||
89 | 84 | attr = {"key-type": "Ubuntu SSO credentials", | ||
90 | 85 | "token-name": self.token_name} | ||
91 | 86 | return attr | ||
92 | 58 | 87 | ||
93 | 59 | def set_ubuntusso_attr(self, cred): | 88 | def set_ubuntusso_attr(self, cred): |
94 | 60 | """Set the credentials of the Ubuntu SSO item.""" | 89 | """Set the credentials of the Ubuntu SSO item.""" |
95 | 61 | # Creates the secret from the credentials | 90 | # Creates the secret from the credentials |
97 | 62 | secret = urlencode(cred) | 91 | secret = urllib.urlencode(cred) |
98 | 63 | 92 | ||
100 | 64 | # Create the keyring | 93 | # Create the keyring if it does not exists |
101 | 65 | self._create_keyring(self.KEYRING_NAME) | 94 | self._create_keyring(self.KEYRING_NAME) |
102 | 66 | 95 | ||
103 | 67 | # No need to delete the item if it already exists | ||
104 | 68 | |||
105 | 69 | # A set of attributes for this credentials | ||
106 | 70 | attr = {"key-type": "Ubuntu SSO credentials", | ||
107 | 71 | "token-name": cred["name"].encode("utf8")} | ||
108 | 72 | |||
109 | 73 | # Add our SSO credentials to the keyring | 96 | # Add our SSO credentials to the keyring |
110 | 74 | gnomekeyring.item_create_sync(self.KEYRING_NAME, | 97 | gnomekeyring.item_create_sync(self.KEYRING_NAME, |
113 | 75 | gnomekeyring.ITEM_GENERIC_SECRET, self.app_name, attr, | 98 | gnomekeyring.ITEM_GENERIC_SECRET, self.app_name, |
114 | 76 | secret, True) | 99 | self._get_keyring_attr(), secret, True) |
115 | 77 | 100 | ||
116 | 78 | def get_ubuntusso_attr(self): | 101 | def get_ubuntusso_attr(self): |
117 | 79 | """Return the secret of the SSO item in a dictionary.""" | 102 | """Return the secret of the SSO item in a dictionary.""" |
118 | 80 | # If we have no attributes, return None | 103 | # If we have no attributes, return None |
124 | 81 | exist = self._get_item_info_from_name(self.KEYRING_NAME, self.app_name) | 104 | item = self._find_keyring_item() |
125 | 82 | if exist is not None: | 105 | if item is not None: |
126 | 83 | secret = self._get_item_info_from_name(self.KEYRING_NAME, | 106 | return dict(urlparse.parse_qsl(item.secret)) |
127 | 84 | self.app_name).get_secret() | 107 | else: |
128 | 85 | return dict(parse_qsl(secret)) | 108 | # if no item found, try getting the old credentials |
129 | 109 | if self.app_name == U1_APP_NAME: | ||
130 | 110 | return try_old_credentials(self.app_name) | ||
131 | 111 | # nothing was found | ||
132 | 112 | return None | ||
133 | 86 | 113 | ||
134 | 87 | def delete_ubuntusso_attr(self): | 114 | def delete_ubuntusso_attr(self): |
135 | 88 | """Delete a set of credentials from the keyring.""" | 115 | """Delete a set of credentials from the keyring.""" |
140 | 89 | item_id = self._get_item_id_from_name(self.KEYRING_NAME, | 116 | item = self._find_keyring_item() |
141 | 90 | self.app_name) | 117 | if item is not None: |
142 | 91 | if item_id is not None: | 118 | gnomekeyring.item_delete_sync(item.keyring, item.item_id) |
143 | 92 | gnomekeyring.item_delete_sync(self.KEYRING_NAME, item_id) | 119 | |
144 | 120 | |||
145 | 121 | class UbuntuOneOAuthKeyring(Keyring): | ||
146 | 122 | |||
147 | 123 | def _get_keyring_attr(self): | ||
148 | 124 | """Build the keyring attributes for this credentials.""" | ||
149 | 125 | return U1_KEY_ATTR | ||
150 | 126 | |||
151 | 127 | |||
152 | 128 | def try_old_credentials(app_name): | ||
153 | 129 | """Try to get old U1 credentials and format them as new.""" | ||
154 | 130 | logger.debug('trying to get old credentials.') | ||
155 | 131 | old_creds = UbuntuOneOAuthKeyring(U1_KEY_NAME).get_ubuntusso_attr() | ||
156 | 132 | if old_creds is not None: | ||
157 | 133 | # Old creds found, build a new credentials dict with them | ||
158 | 134 | creds = { | ||
159 | 135 | 'consumer_key': "ubuntuone", | ||
160 | 136 | 'consumer_secret': "hammertime", | ||
161 | 137 | 'name': U1_KEY_NAME, | ||
162 | 138 | 'token': old_creds["oauth_token"], | ||
163 | 139 | 'token_secret': old_creds["oauth_token_secret"], | ||
164 | 140 | } | ||
165 | 141 | logger.debug('found old credentials') | ||
166 | 142 | return creds | ||
167 | 143 | logger.debug('try_old_credentials: No old credentials for this app.') | ||
168 | 93 | 144 | ||
169 | 94 | 145 | ||
170 | 95 | if __name__ == "__main__": | 146 | if __name__ == "__main__": |
171 | 96 | 147 | ||
172 | === modified file 'ubuntu_sso/main.py' | |||
173 | --- ubuntu_sso/main.py 2010-08-26 13:43:31 +0000 | |||
174 | +++ ubuntu_sso/main.py 2010-08-26 21:18:42 +0000 | |||
175 | @@ -28,11 +28,9 @@ | |||
176 | 28 | """ | 28 | """ |
177 | 29 | 29 | ||
178 | 30 | import re | 30 | import re |
179 | 31 | import socket | ||
180 | 32 | import time | 31 | import time |
181 | 33 | import threading | 32 | import threading |
182 | 34 | import traceback | 33 | import traceback |
183 | 35 | import urllib | ||
184 | 36 | import urllib2 | 34 | import urllib2 |
185 | 37 | import urlparse | 35 | import urlparse |
186 | 38 | 36 | ||
187 | @@ -49,7 +47,7 @@ | |||
188 | 49 | from ubuntu_sso import (DBUS_IFACE_AUTH_NAME, DBUS_IFACE_USER_NAME, | 47 | from ubuntu_sso import (DBUS_IFACE_AUTH_NAME, DBUS_IFACE_USER_NAME, |
189 | 50 | DBUS_IFACE_CRED_NAME, DBUS_CRED_PATH, DBUS_BUS_NAME, gui) | 48 | DBUS_IFACE_CRED_NAME, DBUS_CRED_PATH, DBUS_BUS_NAME, gui) |
190 | 51 | from ubuntu_sso.config import get_config | 49 | from ubuntu_sso.config import get_config |
192 | 52 | from ubuntu_sso.keyring import Keyring | 50 | from ubuntu_sso.keyring import Keyring, get_token_name, U1_APP_NAME |
193 | 53 | from ubuntu_sso.logger import setupLogging | 51 | from ubuntu_sso.logger import setupLogging |
194 | 54 | logger = setupLogging("ubuntu_sso.main") | 52 | logger = setupLogging("ubuntu_sso.main") |
195 | 55 | 53 | ||
196 | @@ -57,8 +55,6 @@ | |||
197 | 57 | # Disable the invalid name warning, as we have a lot of DBus style names | 55 | # Disable the invalid name warning, as we have a lot of DBus style names |
198 | 58 | # pylint: disable-msg=C0103 | 56 | # pylint: disable-msg=C0103 |
199 | 59 | 57 | ||
200 | 60 | OLD_KEY_NAME = "UbuntuOne token for https://ubuntuone.com" | ||
201 | 61 | U1_APP_NAME = "Ubuntu One" | ||
202 | 62 | PING_URL = "http://edge.one.ubuntu.com/oauth/sso-finished-so-get-tokens/" | 58 | PING_URL = "http://edge.one.ubuntu.com/oauth/sso-finished-so-get-tokens/" |
203 | 63 | 59 | ||
204 | 64 | 60 | ||
205 | @@ -113,32 +109,9 @@ | |||
206 | 113 | creds = Keyring(app_name).get_ubuntusso_attr() | 109 | creds = Keyring(app_name).get_ubuntusso_attr() |
207 | 114 | logger.debug('keyring_get_credentials: Keyring returned credentials? %r', | 110 | logger.debug('keyring_get_credentials: Keyring returned credentials? %r', |
208 | 115 | creds is not None) | 111 | creds is not None) |
209 | 116 | if creds is None and app_name == U1_APP_NAME: | ||
210 | 117 | logger.debug('keyring_get_credentials: trying for old service.') | ||
211 | 118 | # No new creds, try to get old credentials | ||
212 | 119 | old_creds = Keyring(OLD_KEY_NAME).get_ubuntusso_attr() | ||
213 | 120 | if old_creds is not None: | ||
214 | 121 | # Old creds found, build a new credentials dict with them | ||
215 | 122 | creds = { | ||
216 | 123 | 'consumer_key': "ubuntuone", | ||
217 | 124 | 'consumer_secret': "hammertime", | ||
218 | 125 | 'token_name': OLD_KEY_NAME, | ||
219 | 126 | 'token': old_creds["oauth_token"], | ||
220 | 127 | 'token_secret': old_creds["oauth_token_secret"], | ||
221 | 128 | } | ||
222 | 129 | else: | ||
223 | 130 | logger.debug('keyring_get_credentials: Keyring returned credentials.') | ||
224 | 131 | return creds | 112 | return creds |
225 | 132 | 113 | ||
226 | 133 | 114 | ||
227 | 134 | def get_token_name(app_name): | ||
228 | 135 | """Build the token name.""" | ||
229 | 136 | quoted_app_name = urllib.quote(app_name) | ||
230 | 137 | computer_name = socket.gethostname() | ||
231 | 138 | quoted_computer_name = urllib.quote(computer_name) | ||
232 | 139 | return "%s - %s" % (quoted_app_name, quoted_computer_name) | ||
233 | 140 | |||
234 | 141 | |||
235 | 142 | class SSOLoginProcessor(object): | 115 | class SSOLoginProcessor(object): |
236 | 143 | """Login and register users using the Ubuntu Single Sign On service.""" | 116 | """Login and register users using the Ubuntu Single Sign On service.""" |
237 | 144 | 117 | ||
238 | 145 | 118 | ||
239 | === modified file 'ubuntu_sso/tests/test_keyring.py' | |||
240 | --- ubuntu_sso/tests/test_keyring.py 2010-08-19 05:50:41 +0000 | |||
241 | +++ ubuntu_sso/tests/test_keyring.py 2010-08-26 21:18:42 +0000 | |||
242 | @@ -17,24 +17,39 @@ | |||
243 | 17 | # with this program. If not, see <http://www.gnu.org/licenses/>. | 17 | # with this program. If not, see <http://www.gnu.org/licenses/>. |
244 | 18 | """Tests for the keyring.py module.""" | 18 | """Tests for the keyring.py module.""" |
245 | 19 | 19 | ||
246 | 20 | import itertools | ||
247 | 21 | import socket | ||
248 | 22 | import urllib | ||
249 | 23 | |||
250 | 20 | import gnomekeyring | 24 | import gnomekeyring |
251 | 21 | from twisted.trial.unittest import TestCase | 25 | from twisted.trial.unittest import TestCase |
252 | 22 | 26 | ||
253 | 23 | from ubuntu_sso import keyring | 27 | from ubuntu_sso import keyring |
254 | 24 | 28 | ||
255 | 25 | 29 | ||
260 | 26 | class MockInfoSync(object): | 30 | def build_fake_gethostname(fake_hostname): |
261 | 27 | """A mock object that represents a fake key.""" | 31 | """Return a fake hostname getter.""" |
262 | 28 | 32 | return lambda *a: fake_hostname | |
263 | 29 | def __init__(self, (key, key_name, secret)): | 33 | |
264 | 34 | |||
265 | 35 | class MockKeyringItem(object): | ||
266 | 36 | """A mock object that fakes an item found in a keyring search.""" | ||
267 | 37 | |||
268 | 38 | def __init__(self, item_id, keyring, attributes, secret): | ||
269 | 30 | """Initialize this instance.""" | 39 | """Initialize this instance.""" |
272 | 31 | self.key = key | 40 | self.item_id = item_id |
273 | 32 | self.key_name = key_name | 41 | self.keyring = keyring |
274 | 42 | self.attributes = attributes | ||
275 | 33 | self.secret = secret | 43 | self.secret = secret |
276 | 34 | 44 | ||
280 | 35 | def get_display_name(self): | 45 | def matches(self, search_attr): |
281 | 36 | """Return info on the mocked object.""" | 46 | """See if this item matches a given search.""" |
282 | 37 | return self.key_name | 47 | for k, v in search_attr.items(): |
283 | 48 | if k not in self.attributes: | ||
284 | 49 | return False | ||
285 | 50 | if self.attributes[k] != v: | ||
286 | 51 | return False | ||
287 | 52 | return True | ||
288 | 38 | 53 | ||
289 | 39 | 54 | ||
290 | 40 | class MockGnomeKeyring(object): | 55 | class MockGnomeKeyring(object): |
291 | @@ -43,28 +58,33 @@ | |||
292 | 43 | def __init__(self): | 58 | def __init__(self): |
293 | 44 | """Initialize this instance.""" | 59 | """Initialize this instance.""" |
294 | 45 | self.keyrings = set() | 60 | self.keyrings = set() |
296 | 46 | self.store = [] | 61 | self.id_counter = itertools.count() |
297 | 62 | self.store = {} | ||
298 | 47 | self.deleted = [] | 63 | self.deleted = [] |
299 | 48 | 64 | ||
300 | 65 | def _get_next_id(self): | ||
301 | 66 | """Return the next keyring id.""" | ||
302 | 67 | return self.id_counter.next() | ||
303 | 68 | |||
304 | 49 | def item_create_sync(self, keyring_name, item_type, key_name, attr, | 69 | def item_create_sync(self, keyring_name, item_type, key_name, attr, |
305 | 50 | secret, update_if_exists): | 70 | secret, update_if_exists): |
310 | 51 | """Sets a value in the keyring.""" | 71 | """Add a key to a keyring.""" |
311 | 52 | # we'll use the attr as the dict key, so make it a tuple | 72 | new_id = self._get_next_id() |
312 | 53 | key = tuple(attr.items()) | 73 | i = MockKeyringItem(new_id, keyring_name, attr, secret) |
313 | 54 | self.store.append((key, key_name, secret)) | 74 | self.store[new_id] = i |
314 | 55 | 75 | ||
315 | 56 | def item_delete_sync(self, keyring_name, item_id): | 76 | def item_delete_sync(self, keyring_name, item_id): |
327 | 57 | """Makes a list of deleted items.""" | 77 | """Delete a key from a keyring, and keep a list of deleted keys.""" |
328 | 58 | key, key_name, secret = self.store.pop(item_id) | 78 | item = self.store.pop(item_id) |
329 | 59 | self.deleted.append(key_name) | 79 | assert keyring_name == item.keyring |
330 | 60 | 80 | self.deleted.append(item) | |
331 | 61 | def list_item_ids_sync(self, keyring_name): | 81 | |
332 | 62 | """Return a list of ids for all the items.""" | 82 | def find_items_sync(self, item_type, attr): |
333 | 63 | return [n for n, v in enumerate(self.store)] | 83 | """Find all keys that match the given attributes.""" |
334 | 64 | 84 | items = [i for i in self.store.values() if i.matches(attr)] | |
335 | 65 | def item_get_info_sync(self, keyring_name, item_id): | 85 | if len(items) == 0: |
336 | 66 | """Return an info sync object for the item.""" | 86 | raise gnomekeyring.NoMatchError() |
337 | 67 | return MockInfoSync(self.store[item_id]) | 87 | return items |
338 | 68 | 88 | ||
339 | 69 | def list_keyring_names_sync(self): | 89 | def list_keyring_names_sync(self): |
340 | 70 | """The keyring you are looking for may be here.""" | 90 | """The keyring you are looking for may be here.""" |
341 | @@ -79,6 +99,32 @@ | |||
342 | 79 | return True | 99 | return True |
343 | 80 | 100 | ||
344 | 81 | 101 | ||
345 | 102 | class TestTokenNameBuilder(TestCase): | ||
346 | 103 | """Test the method that builds the token name.""" | ||
347 | 104 | |||
348 | 105 | def test_get_simple_token_name(self): | ||
349 | 106 | """A simple token name is built right.""" | ||
350 | 107 | sample_app_name = "UbuntuTwo" | ||
351 | 108 | sample_hostname = "Darkstar" | ||
352 | 109 | expected_result = "UbuntuTwo - Darkstar" | ||
353 | 110 | |||
354 | 111 | fake_gethostname = build_fake_gethostname(sample_hostname) | ||
355 | 112 | self.patch(socket, "gethostname", fake_gethostname) | ||
356 | 113 | result = keyring.get_token_name(sample_app_name) | ||
357 | 114 | self.assertEqual(result, expected_result) | ||
358 | 115 | |||
359 | 116 | def test_get_complex_token_name(self): | ||
360 | 117 | """A complex token name is built right too.""" | ||
361 | 118 | sample_app_name = "Ubuntu Eleven" | ||
362 | 119 | sample_hostname = "Mate+Cocido" | ||
363 | 120 | expected_result = "Ubuntu%20Eleven - Mate%2BCocido" | ||
364 | 121 | |||
365 | 122 | fake_gethostname = build_fake_gethostname(sample_hostname) | ||
366 | 123 | self.patch(socket, "gethostname", fake_gethostname) | ||
367 | 124 | result = keyring.get_token_name(sample_app_name) | ||
368 | 125 | self.assertEqual(result, expected_result) | ||
369 | 126 | |||
370 | 127 | |||
371 | 82 | class TestKeyring(TestCase): | 128 | class TestKeyring(TestCase): |
372 | 83 | """Test the gnome keyring related functions.""" | 129 | """Test the gnome keyring related functions.""" |
373 | 84 | def setUp(self): | 130 | def setUp(self): |
374 | @@ -89,11 +135,10 @@ | |||
375 | 89 | self.patch(gnomekeyring, "create_sync", self.mgk.create_sync) | 135 | self.patch(gnomekeyring, "create_sync", self.mgk.create_sync) |
376 | 90 | self.patch(gnomekeyring, "list_keyring_names_sync", | 136 | self.patch(gnomekeyring, "list_keyring_names_sync", |
377 | 91 | self.mgk.list_keyring_names_sync) | 137 | self.mgk.list_keyring_names_sync) |
382 | 92 | self.patch(gnomekeyring, "list_item_ids_sync", | 138 | self.patch(gnomekeyring, "find_items_sync", self.mgk.find_items_sync) |
379 | 93 | self.mgk.list_item_ids_sync) | ||
380 | 94 | self.patch(gnomekeyring, "item_get_info_sync", | ||
381 | 95 | self.mgk.item_get_info_sync) | ||
383 | 96 | self.patch(gnomekeyring, "item_delete_sync", self.mgk.item_delete_sync) | 139 | self.patch(gnomekeyring, "item_delete_sync", self.mgk.item_delete_sync) |
384 | 140 | fake_gethostname = build_fake_gethostname("darkstar") | ||
385 | 141 | self.patch(socket, "gethostname", fake_gethostname) | ||
386 | 97 | 142 | ||
387 | 98 | def test_set_ubuntusso(self): | 143 | def test_set_ubuntusso(self): |
388 | 99 | """Test that the set method does not erase previous keys.""" | 144 | """Test that the set method does not erase previous keys.""" |
389 | @@ -113,3 +158,54 @@ | |||
390 | 113 | 158 | ||
391 | 114 | self.assertEqual(len(self.mgk.store), 0) | 159 | self.assertEqual(len(self.mgk.store), 0) |
392 | 115 | self.assertEqual(len(self.mgk.deleted), 1) | 160 | self.assertEqual(len(self.mgk.deleted), 1) |
393 | 161 | |||
394 | 162 | def test_get_credentials(self): | ||
395 | 163 | """Test that credentials are properly retrieved.""" | ||
396 | 164 | sample_creds = {"name": "sample creds name"} | ||
397 | 165 | keyring.Keyring("appname").set_ubuntusso_attr(sample_creds) | ||
398 | 166 | |||
399 | 167 | result = keyring.Keyring("appname").get_ubuntusso_attr() | ||
400 | 168 | self.assertEqual(result, sample_creds) | ||
401 | 169 | |||
402 | 170 | def test_get_old_cred_found(self): | ||
403 | 171 | """The method returns a new set of creds if old creds are found.""" | ||
404 | 172 | sample_oauth_token = "sample oauth token" | ||
405 | 173 | sample_oauth_secret = "sample oauth secret" | ||
406 | 174 | old_creds = { | ||
407 | 175 | "oauth_token": sample_oauth_token, | ||
408 | 176 | "oauth_token_secret": sample_oauth_secret, | ||
409 | 177 | } | ||
410 | 178 | secret = urllib.urlencode(old_creds) | ||
411 | 179 | self.mgk.item_create_sync(keyring.U1_APP_NAME, None, | ||
412 | 180 | keyring.Keyring.KEYRING_NAME, | ||
413 | 181 | keyring.U1_KEY_ATTR, | ||
414 | 182 | secret, True) | ||
415 | 183 | |||
416 | 184 | result = keyring.Keyring(keyring.U1_APP_NAME).get_ubuntusso_attr() | ||
417 | 185 | |||
418 | 186 | self.assertIn("token", result) | ||
419 | 187 | self.assertEqual(result["token"], sample_oauth_token) | ||
420 | 188 | self.assertIn("token_secret", result) | ||
421 | 189 | self.assertEqual(result["token_secret"], sample_oauth_secret) | ||
422 | 190 | |||
423 | 191 | def test_get_old_cred_found_but_not_asked_for(self): | ||
424 | 192 | """Returns None if old creds are present but the appname is not U1""" | ||
425 | 193 | sample_oauth_token = "sample oauth token" | ||
426 | 194 | sample_oauth_secret = "sample oauth secret" | ||
427 | 195 | old_creds = { | ||
428 | 196 | "oauth_token": sample_oauth_token, | ||
429 | 197 | "oauth_token_secret": sample_oauth_secret, | ||
430 | 198 | } | ||
431 | 199 | secret = urllib.urlencode(old_creds) | ||
432 | 200 | self.mgk.item_create_sync(keyring.U1_APP_NAME, None, | ||
433 | 201 | keyring.Keyring.KEYRING_NAME, | ||
434 | 202 | keyring.U1_KEY_ATTR, | ||
435 | 203 | secret, True) | ||
436 | 204 | |||
437 | 205 | result = keyring.Keyring("Software Center").get_ubuntusso_attr() | ||
438 | 206 | self.assertEqual(result, None) | ||
439 | 207 | |||
440 | 208 | def test_get_old_cred_not_found(self): | ||
441 | 209 | """The method returns None if no old nor new credentials found.""" | ||
442 | 210 | result = keyring.Keyring(keyring.U1_APP_NAME).get_ubuntusso_attr() | ||
443 | 211 | self.assertEqual(result, None) | ||
444 | 116 | 212 | ||
445 | === modified file 'ubuntu_sso/tests/test_main.py' | |||
446 | --- ubuntu_sso/tests/test_main.py 2010-08-26 13:43:31 +0000 | |||
447 | +++ ubuntu_sso/tests/test_main.py 2010-08-26 21:18:42 +0000 | |||
448 | @@ -35,15 +35,14 @@ | |||
449 | 35 | 35 | ||
450 | 36 | from contrib.testing.testcase import MementoHandler | 36 | from contrib.testing.testcase import MementoHandler |
451 | 37 | from ubuntu_sso import config, gui | 37 | from ubuntu_sso import config, gui |
452 | 38 | from ubuntu_sso.keyring import get_token_name, U1_APP_NAME | ||
453 | 38 | from ubuntu_sso.main import ( | 39 | from ubuntu_sso.main import ( |
454 | 39 | AuthenticationError, BadRealmError, blocking, EmailTokenError, | 40 | AuthenticationError, BadRealmError, blocking, EmailTokenError, |
457 | 40 | except_to_errdict, get_token_name, | 41 | except_to_errdict, InvalidEmailError, InvalidPasswordError, |
456 | 41 | InvalidEmailError, InvalidPasswordError, | ||
458 | 42 | keyring_get_credentials, keyring_store_credentials, logger, | 42 | keyring_get_credentials, keyring_store_credentials, logger, |
460 | 43 | LoginProcessor, NewPasswordError, OLD_KEY_NAME, PING_URL, | 43 | LoginProcessor, NewPasswordError, PING_URL, |
461 | 44 | RegistrationError, ResetPasswordTokenError, | 44 | RegistrationError, ResetPasswordTokenError, |
464 | 45 | SSOCredentials, SSOLogin, SSOLoginProcessor, | 45 | SSOCredentials, SSOLogin, SSOLoginProcessor) |
463 | 46 | U1_APP_NAME) | ||
465 | 47 | 46 | ||
466 | 48 | 47 | ||
467 | 49 | APP_NAME = 'The Coolest App Ever' | 48 | APP_NAME = 'The Coolest App Ever' |
468 | @@ -842,58 +841,6 @@ | |||
469 | 842 | token = keyring_get_credentials(APP_NAME) | 841 | token = keyring_get_credentials(APP_NAME) |
470 | 843 | self.assertEqual(token, None) | 842 | self.assertEqual(token, None) |
471 | 844 | 843 | ||
472 | 845 | def test_keyring_get_old_cred_found(self): | ||
473 | 846 | """The method returns a new set of creds if old creds are found.""" | ||
474 | 847 | sample_oauth_token = "sample oauth token" | ||
475 | 848 | sample_oauth_secret = "sample oauth secret" | ||
476 | 849 | old_creds = { | ||
477 | 850 | "oauth_token": sample_oauth_token, | ||
478 | 851 | "oauth_token_secret": sample_oauth_secret, | ||
479 | 852 | } | ||
480 | 853 | |||
481 | 854 | mockKeyringClass = self.mocker.replace("ubuntu_sso.keyring.Keyring") | ||
482 | 855 | mockKeyringClass(U1_APP_NAME) | ||
483 | 856 | mockKeyring = self.mocker.mock() | ||
484 | 857 | self.mocker.result(mockKeyring) | ||
485 | 858 | mockKeyring.get_ubuntusso_attr() | ||
486 | 859 | self.mocker.result(None) | ||
487 | 860 | |||
488 | 861 | mockKeyringClass(OLD_KEY_NAME) | ||
489 | 862 | mockKeyring2 = self.mocker.mock() | ||
490 | 863 | self.mocker.result(mockKeyring2) | ||
491 | 864 | mockKeyring2.get_ubuntusso_attr() | ||
492 | 865 | self.mocker.result(old_creds) | ||
493 | 866 | |||
494 | 867 | self.mocker.replay() | ||
495 | 868 | |||
496 | 869 | new_creds = keyring_get_credentials(U1_APP_NAME) | ||
497 | 870 | self.assertIn("token", new_creds) | ||
498 | 871 | self.assertEqual(new_creds["token"], sample_oauth_token) | ||
499 | 872 | self.assertIn("token_secret", new_creds) | ||
500 | 873 | self.assertEqual(new_creds["token_secret"], sample_oauth_secret) | ||
501 | 874 | self.assertIn("token_name", new_creds) | ||
502 | 875 | self.assertEqual(new_creds["token_name"], OLD_KEY_NAME) | ||
503 | 876 | |||
504 | 877 | def test_keyring_get_old_cred_not_found(self): | ||
505 | 878 | """The method returns None if no old nor new credentials found.""" | ||
506 | 879 | mockKeyringClass = self.mocker.replace("ubuntu_sso.keyring.Keyring") | ||
507 | 880 | mockKeyringClass(U1_APP_NAME) | ||
508 | 881 | mockKeyring = self.mocker.mock() | ||
509 | 882 | self.mocker.result(mockKeyring) | ||
510 | 883 | mockKeyring.get_ubuntusso_attr() | ||
511 | 884 | self.mocker.result(None) | ||
512 | 885 | |||
513 | 886 | mockKeyringClass(OLD_KEY_NAME) | ||
514 | 887 | mockKeyring2 = self.mocker.mock() | ||
515 | 888 | self.mocker.result(mockKeyring2) | ||
516 | 889 | mockKeyring2.get_ubuntusso_attr() | ||
517 | 890 | self.mocker.result(None) | ||
518 | 891 | |||
519 | 892 | self.mocker.replay() | ||
520 | 893 | |||
521 | 894 | token = keyring_get_credentials(U1_APP_NAME) | ||
522 | 895 | self.assertEqual(token, None) | ||
523 | 896 | |||
524 | 897 | 844 | ||
525 | 898 | class RegisterSampleException(Exception): | 845 | class RegisterSampleException(Exception): |
526 | 899 | """A mock exception thrown just when testing.""" | 846 | """A mock exception thrown just when testing.""" |
tests are delicious!