Ejemplo n.º 1
0
 def test_cannot_specify_both_filename_and_store(self):
     ignore, filename = tempfile.mkstemp()
     store = KeyringCredentialStore()
     self.assertRaises(
         ValueError, NoNetworkLaunchpad.login_with,
         application_name='not important', credentials_file=filename,
         credential_store=store)
     os.remove(filename)
Ejemplo n.º 2
0
 def credential_store_factory(cls, credential_save_failed):
     if cls._is_sudo():
         # Do not try to store credentials under sudo;
         # it can be problematic with shared sudo access,
         # and we may not have access to the normal keyring provider
         # LP: #1862948
         return MemoryCredentialStore(credential_save_failed)
     return KeyringCredentialStore(credential_save_failed, fallback=True)
Ejemplo n.º 3
0
    def test_inconsistent_credential_save_failed(self):
        # Catch an attempt to specify inconsistent callbacks for
        # credential save failure.
        def callback1():
            pass
        store = KeyringCredentialStore(credential_save_failed=callback1)

        def callback2():
            pass
        self.assertRaises(ValueError, NoNetworkLaunchpad.login_with,
                          "app name", credential_store=store,
                          credential_save_failed=callback2)
Ejemplo n.º 4
0
 def setUp(self):
     self.keyring = InMemoryKeyring()
     self.store = KeyringCredentialStore()
Ejemplo n.º 5
0
class TestKeyringCredentialStore(CredentialStoreTestCase):
    """Tests for the KeyringCredentialStore class."""
    def setUp(self):
        self.keyring = InMemoryKeyring()
        self.store = KeyringCredentialStore()

    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_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.assertTrue(loaded1)
            self.assertEqual(credential1.consumer.key, loaded1.consumer.key)

            loaded2 = self.store.load("key 2")
            self.assertEqual(credential2.consumer.key, loaded2.consumer.key)

    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_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"))

    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_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_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 setUp(self):
     self.keyring = InMemoryKeyring()
     self.store = KeyringCredentialStore()
class TestKeyringCredentialStore(CredentialStoreTestCase):
    """Tests for the KeyringCredentialStore class."""

    def setUp(self):
        self.keyring = InMemoryKeyring()
        self.store = KeyringCredentialStore()

    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_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_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_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_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.assertEquals(
                credential.consumer.key, credential2.consumer.key)
            self.assertEquals(
                credential.consumer.secret, credential2.consumer.secret)

    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)
Ejemplo n.º 8
0
 def credential_store_factory(cls, credential_save_failed):
     return KeyringCredentialStore(credential_save_failed)
class TestKeyringCredentialStore(CredentialStoreTestCase):
    """Tests for the KeyringCredentialStore class."""

    def setUp(self):
        self.keyring = InMemoryKeyring()
        self.store = KeyringCredentialStore()

    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_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_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_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"))