Debian Codegraph @

src/launchpadlib/tests/test_credential_store.py

raw
1 # Copyright 2010-2011 Canonical Ltd.
2
3 # This file is part of launchpadlib.
4 #
5 # launchpadlib is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as published by the
7 # Free Software Foundation, version 3 of the License.
8 #
9 # launchpadlib is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
12 # for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public License
15 # along with launchpadlib. If not, see <http://www.gnu.org/licenses/>.
16
17 """Tests for the credential store classes."""
18
19 import os
20 import tempfile
21 import unittest
22
23 from base64 import b64decode
24
25 if bytes is str:
26 # Python 2
27 unicode_type = unicode # noqa: F821
28 else:
29 unicode_type = str
30
31 from launchpadlib.testing.helpers import (
32 fake_keyring,
33 InMemoryKeyring,
34 )
35
36 from launchpadlib.credentials import (
37 AccessToken,
38 Credentials,
39 KeyringCredentialStore,
40 UnencryptedFileCredentialStore,
41 )
42
43
44 class TestAccessToken(unittest.TestCase):
45 """Tests for the AccessToken class."""
46
47 def test_from_string(self):
48 access_token = AccessToken.from_string(
49 "oauth_token_secret=secret%3Dpassword&oauth_token=lock%26key"
50 )
51 self.assertEqual("lock&key", access_token.key)
52 self.assertEqual("secret=password", access_token.secret)
53 self.assertIsNone(access_token.context)
54
55 def test_from_string_with_context(self):
56 access_token = AccessToken.from_string(
57 "oauth_token_secret=secret%3Dpassword&oauth_token=lock%26key&"
58 "lp.context=firefox"
59 )
60 self.assertEqual("lock&key", access_token.key)
61 self.assertEqual("secret=password", access_token.secret)
62 self.assertEqual("firefox", access_token.context)
63
64
65 class CredentialStoreTestCase(unittest.TestCase):
66 def make_credential(self, consumer_key):
67 """Helper method to make a fake credential."""
68 return Credentials(
69 "app name",
70 consumer_secret="consumer_secret:42",
71 access_token=AccessToken(consumer_key, "access_secret:168"),
72 )
73
74
75 class TestUnencryptedFileCredentialStore(CredentialStoreTestCase):
76 """Tests for the UnencryptedFileCredentialStore class."""
77
78 def setUp(self):
79 ignore, self.filename = tempfile.mkstemp()
80 self.store = UnencryptedFileCredentialStore(self.filename)
81
82 def tearDown(self):
83 if os.path.exists(self.filename):
84 os.remove(self.filename)
85
86 def test_save_and_load(self):
87 # Make sure you can save and load credentials to a file.
88 credential = self.make_credential("consumer key")
89 self.store.save(credential, "unique key")
90 credential2 = self.store.load("unique key")
91 self.assertEqual(credential.consumer.key, credential2.consumer.key)
92
93 def test_unique_id_doesnt_matter(self):
94 # If a file contains a credential, that credential will be
95 # accessed no matter what unique ID you specify.
96 credential = self.make_credential("consumer key")
97 self.store.save(credential, "some key")
98 credential2 = self.store.load("some other key")
99 self.assertEqual(credential.consumer.key, credential2.consumer.key)
100
101 def test_file_only_contains_one_credential(self):
102 # A credential file may contain only one credential. If you
103 # write two credentials with different unique IDs to the same
104 # file, the first credential will be overwritten with the
105 # second.
106 credential1 = self.make_credential("consumer key")
107 credential2 = self.make_credential("consumer key2")
108 self.store.save(credential1, "unique key 1")
109 self.store.save(credential1, "unique key 2")
110 loaded = self.store.load("unique key 1")
111 self.assertEqual(loaded.consumer.key, credential2.consumer.key)
112
113
114 class TestKeyringCredentialStore(CredentialStoreTestCase):
115 """Tests for the KeyringCredentialStore class."""
116
117 def setUp(self):
118 self.keyring = InMemoryKeyring()
119 self.store = KeyringCredentialStore()
120
121 def test_save_and_load(self):
122 # Make sure you can save and load credentials to a keyring.
123 with fake_keyring(self.keyring):
124 credential = self.make_credential("consumer key")
125 self.store.save(credential, "unique key")
126 credential2 = self.store.load("unique key")
127 self.assertEqual(credential.consumer.key, credential2.consumer.key)
128
129 def test_lookup_by_unique_key(self):
130 # Credentials in the keyring are looked up by the unique ID
131 # under which they were stored.
132 with fake_keyring(self.keyring):
133 credential1 = self.make_credential("consumer key1")
134 self.store.save(credential1, "key 1")
135
136 credential2 = self.make_credential("consumer key2")
137 self.store.save(credential2, "key 2")
138
139 loaded1 = self.store.load("key 1")
140 self.assertTrue(loaded1)
141 self.assertEqual(credential1.consumer.key, loaded1.consumer.key)
142
143 loaded2 = self.store.load("key 2")
144 self.assertEqual(credential2.consumer.key, loaded2.consumer.key)
145
146 def test_reused_unique_id_overwrites_old_credential(self):
147 # Writing a credential to the keyring with a given unique ID
148 # will overwrite any credential stored under that ID.
149
150 with fake_keyring(self.keyring):
151 credential1 = self.make_credential("consumer key1")
152 self.store.save(credential1, "the only key")
153
154 credential2 = self.make_credential("consumer key2")
155 self.store.save(credential2, "the only key")
156
157 loaded = self.store.load("the only key")
158 self.assertEqual(credential2.consumer.key, loaded.consumer.key)
159
160 def test_bad_unique_id_returns_none(self):
161 # Trying to load a credential without providing a good unique
162 # ID will get you None.
163 with fake_keyring(self.keyring):
164 self.assertIsNone(self.store.load("no such key"))
165
166 def test_keyring_returns_unicode(self):
167 # Kwallet is reported to sometimes return Unicode, which broke the
168 # credentials parsing. This test ensures a Unicode password is
169 # handled correctly. (See bug lp:877374)
170 class UnicodeInMemoryKeyring(InMemoryKeyring):
171 def get_password(self, service, username):
172 password = super().get_password(service, username)
173 if isinstance(password, unicode_type):
174 password = password.encode("utf-8")
175 return password
176
177 self.keyring = UnicodeInMemoryKeyring()
178 with fake_keyring(self.keyring):
179 credential = self.make_credential("consumer key")
180 self.assertTrue(credential)
181 # Shouldn't this test actually use a unicodish key?!
182 self.store.save(credential, "unique key")
183 credential2 = self.store.load("unique key")
184 self.assertTrue(credential2)
185 self.assertEqual(credential.consumer.key, credential2.consumer.key)
186 self.assertEqual(
187 credential.consumer.secret, credential2.consumer.secret
188 )
189
190 def test_nonencoded_key_handled(self):
191 # For backwards compatibility with keys that are not base 64 encoded.
192
193 class UnencodedInMemoryKeyring(InMemoryKeyring):
194 def get_password(self, service, username):
195 pw = super().get_password(service, username)
196 return b64decode(pw[5:])
197
198 self.keyring = UnencodedInMemoryKeyring()
199 with fake_keyring(self.keyring):
200 credential = self.make_credential("consumer key")
201 self.store.save(credential, "unique key")
202 credential2 = self.store.load("unique key")
203 self.assertEqual(credential.consumer.key, credential2.consumer.key)
204 self.assertEqual(
205 credential.consumer.secret, credential2.consumer.secret
206 )
207
208 def test_corrupted_key_handled(self):
209 # A corrupted password results in None being returned.
210
211 class CorruptedInMemoryKeyring(InMemoryKeyring):
212 def get_password(self, service, username):
213 return "bad"
214
215 self.keyring = CorruptedInMemoryKeyring()
216 with fake_keyring(self.keyring):
217 credential = self.make_credential("consumer key")
218 self.store.save(credential, "unique key")
219 credential2 = self.store.load("unique key")
220 self.assertIsNone(credential2)