def test_save_and_load(self): # Make sure you can save and load credentials to a keyring. with fake_keyring(self.keyring): credential = self.make_credential("consumer key") self.store.save(credential, "unique key") credential2 = self.store.load("unique key") self.assertEquals(credential.consumer.key, credential2.consumer.key)
def test_save_and_load(self): # Make sure you can save and load credentials to a keyring. with fake_keyring(self.keyring): credential = self.make_credential("consumer key") self.store.save(credential, "unique key") credential2 = self.store.load("unique key") self.assertEqual(credential.consumer.key, credential2.consumer.key)
def test_credentials_save_fail_under_sudo_does_not_raise_exception(self): # When running under sudo, Launchpad will not attempt to use # the keyring, so credential save failure will never happen launchpadlib_dir = os.path.join(self.temp_dir, 'launchpadlib') service_root = "http://api.example.com/" with fake_keyring(BadSaveKeyring()): NoNetworkLaunchpad.login_with('not important', service_root=service_root, launchpadlib_dir=launchpadlib_dir)
def test_default_credentials_save_failed_is_to_raise_exception(self): # If saving the credentials did not succeed and no callback was # provided, the underlying exception is raised. launchpadlib_dir = os.path.join(self.temp_dir, 'launchpadlib') service_root = "http://api.example.com/" with fake_keyring(BadSaveKeyring()): self.assertRaises( RuntimeError, NoNetworkLaunchpad.login_with, 'not important', service_root=service_root, launchpadlib_dir=launchpadlib_dir)
def test_reused_unique_id_overwrites_old_credential(self): # Writing a credential to the keyring with a given unique ID # will overwrite any credential stored under that ID. with fake_keyring(self.keyring): credential1 = self.make_credential("consumer key1") self.store.save(credential1, "the only key") credential2 = self.make_credential("consumer key2") self.store.save(credential2, "the only key") loaded = self.store.load("the only key") self.assertEqual(credential2.consumer.key, loaded.consumer.key)
def test_corrupted_key_handled(self): # A corrupted password results in None being returned. class CorruptedInMemoryKeyring(InMemoryKeyring): def get_password(self, service, username): return "bad" self.keyring = CorruptedInMemoryKeyring() with fake_keyring(self.keyring): credential = self.make_credential("consumer key") self.store.save(credential, "unique key") credential2 = self.store.load("unique key") self.assertIsNone(credential2)
def test_reused_unique_id_overwrites_old_credential(self): # Writing a credential to the keyring with a given unique ID # will overwrite any credential stored under that ID. with fake_keyring(self.keyring): credential1 = self.make_credential("consumer key1") self.store.save(credential1, "the only key") credential2 = self.make_credential("consumer key2") self.store.save(credential2, "the only key") loaded = self.store.load("the only key") self.assertEquals(credential2.consumer.key, loaded.consumer.key)
def test_lookup_by_unique_key(self): # Credentials in the keyring are looked up by the unique ID # under which they were stored. with fake_keyring(self.keyring): credential1 = self.make_credential("consumer key1") self.store.save(credential1, "key 1") credential2 = self.make_credential("consumer key2") self.store.save(credential2, "key 2") loaded1 = self.store.load("key 1") self.assertEquals(credential1.consumer.key, loaded1.consumer.key) loaded2 = self.store.load("key 2") self.assertEquals(credential2.consumer.key, loaded2.consumer.key)
def test_nonencoded_key_handled(self): # For backwards compatibility with keys that are not base 64 encoded. class UnencodedInMemoryKeyring(InMemoryKeyring): def get_password(self, service, username): pw = super(UnencodedInMemoryKeyring, self).get_password(service, username) return b64decode(pw[5:]) self.keyring = UnencodedInMemoryKeyring() with fake_keyring(self.keyring): credential = self.make_credential("consumer key") self.store.save(credential, "unique key") credential2 = self.store.load("unique key") self.assertEquals(credential.consumer.key, credential2.consumer.key) self.assertEquals(credential.consumer.secret, credential2.consumer.secret)
def test_keyring_returns_unicode(self): # Kwallet is reported to sometimes return Unicode, which broke the # credentials parsing. This test ensures a Unicode password is # handled correctly. (See bug lp:877374) class UnicodeInMemoryKeyring(InMemoryKeyring): def get_password(self, service, username): return unicode(super(UnicodeInMemoryKeyring, self).get_password(service, username)) self.keyring = UnicodeInMemoryKeyring() with fake_keyring(self.keyring): credential = self.make_credential("consumer key") self.store.save(credential, "unique key") credential2 = self.store.load("unique key") self.assertEquals(credential.consumer.key, credential2.consumer.key) self.assertEquals(credential.consumer.secret, credential2.consumer.secret)
def test_nonencoded_key_handled(self): # For backwards compatibility with keys that are not base 64 encoded. class UnencodedInMemoryKeyring(InMemoryKeyring): def get_password(self, service, username): pw = super(UnencodedInMemoryKeyring, self).get_password(service, username) return b64decode(pw[5:]) self.keyring = UnencodedInMemoryKeyring() with fake_keyring(self.keyring): credential = self.make_credential("consumer key") self.store.save(credential, "unique key") credential2 = self.store.load("unique key") self.assertEqual(credential.consumer.key, credential2.consumer.key) self.assertEqual(credential.consumer.secret, credential2.consumer.secret)
def test_credentials_save_failed(self): # If saving the credentials did not succeed and a callback was # provided, it is called. callback_called = [] def callback(): # Since we can't rebind "callback_called" here, we'll have to # settle for mutating it to signal success. callback_called.append(None) launchpadlib_dir = os.path.join(self.temp_dir, 'launchpadlib') service_root = "http://api.example.com/" with fake_keyring(BadSaveKeyring()): NoNetworkLaunchpad.login_with( 'not important', service_root=service_root, launchpadlib_dir=launchpadlib_dir, credential_save_failed=callback) self.assertEquals(len(callback_called), 1)
def test_keyring_returns_unicode(self): # Kwallet is reported to sometimes return Unicode, which broke the # credentials parsing. This test ensures a Unicode password is # handled correctly. (See bug lp:877374) class UnicodeInMemoryKeyring(InMemoryKeyring): def get_password(self, service, username): return unicode( super(UnicodeInMemoryKeyring, self).get_password( service, username)) self.keyring = UnicodeInMemoryKeyring() with fake_keyring(self.keyring): credential = self.make_credential("consumer key") self.store.save(credential, "unique key") credential2 = self.store.load("unique key") self.assertEquals( credential.consumer.key, credential2.consumer.key) self.assertEquals( credential.consumer.secret, credential2.consumer.secret)
def test_same_app_different_servers(self): launchpadlib_dir = os.path.join(self.temp_dir, 'launchpadlib') keyring = InMemoryKeyring() # Be paranoid about the keyring starting out empty. assert not keyring.data, 'oops, a fresh keyring has data in it' with fake_keyring(keyring): # Create stored credentials for the same application but against # two different sites (service roots). NoNetworkLaunchpad.login_with( 'application name', service_root='http://alpha.example.com/', launchpadlib_dir=launchpadlib_dir) NoNetworkLaunchpad.login_with( 'application name', service_root='http://beta.example.com/', launchpadlib_dir=launchpadlib_dir) # There should only be two sets of stored credentials (this assertion # is of the test mechanism, not a test assertion). assert len(keyring.data.keys()) == 2 application_key_1 = keyring.data.keys()[0][1] application_key_2 = keyring.data.keys()[1][1] self.assertNotEqual(application_key_1, application_key_2)
def test_components_of_application_key(self): launchpadlib_dir = os.path.join(self.temp_dir, 'launchpadlib') keyring = InMemoryKeyring() service_root = 'http://api.example.com/' application_name = 'Super App 3000' with fake_keyring(keyring): launchpad = NoNetworkLaunchpad.login_with( application_name, service_root=service_root, launchpadlib_dir=launchpadlib_dir) consumer_name = launchpad.credentials.consumer.key application_key = keyring.data.keys()[0][1] # Both the consumer name (normally the name of the application) and # the service root (the URL of the service being accessed) are # included in the key when storing credentials. self.assert_(service_root in application_key) self.assert_(consumer_name in application_key) # The key used to store the credentials is of this structure (and # shouldn't change between releases or stored credentials will be # "forgotten"). self.assertEquals(application_key, consumer_name + '@' + service_root)
def test_keyring_returns_unicode(self): # Kwallet is reported to sometimes return Unicode, which broke the # credentials parsing. This test ensures a Unicode password is # handled correctly. (See bug lp:877374) class UnicodeInMemoryKeyring(InMemoryKeyring): def get_password(self, service, username): password = super(UnicodeInMemoryKeyring, self).get_password(service, username) if isinstance(password, unicode_type): password = password.encode('utf-8') return password self.keyring = UnicodeInMemoryKeyring() with fake_keyring(self.keyring): credential = self.make_credential("consumer key") self.assertTrue(credential) # Shouldn't this test actually use a unicodish key?! self.store.save(credential, "unique key") credential2 = self.store.load("unique key") self.assertTrue(credential2) self.assertEqual(credential.consumer.key, credential2.consumer.key) self.assertEqual(credential.consumer.secret, credential2.consumer.secret)
def test_bad_unique_id_returns_none(self): # Trying to load a credential without providing a good unique # ID will get you None. with fake_keyring(self.keyring): self.assertEquals(None, self.store.load("no such key"))
def test_bad_unique_id_returns_none(self): # Trying to load a credential without providing a good unique # ID will get you None. with fake_keyring(self.keyring): self.assertIsNone(self.store.load("no such key"))