def test_no_creds_loaded(self): self.provider1.load.return_value = None self.provider2.load.return_value = None resolver = credentials.CredentialResolver(providers=[self.provider1, self.provider2]) with self.assertRaises(NoCredentialsError): resolver.load_credentials()
def test_inject_additional_providers_after_existing(self): self.provider1.load.return_value = None self.provider2.load.return_value = self.fake_creds resolver = credentials.CredentialResolver(providers=[self.provider1, self.provider2]) # Now, if we were to call resolver.load() now, provider2 would # win because it's returning a non None response. # However we can inject a new provider before provider2 to # override this process. # Providers can be added by the METHOD name of each provider. new_provider = mock.Mock() new_provider.METHOD = 'new_provider' new_provider.load.return_value = credentials.Credentials('d', 'e', 'test') resolver.insert_after('provider1', new_provider) creds = resolver.load_credentials() self.assertIsNotNone(creds) self.assertEqual(creds.access_key_id, 'd') self.assertEqual(creds.private_key, 'e') # Provider 1 should have been called, but provider2 should # *not* have been called because new_provider already returned # a non-None response. self.provider1.load.assert_called_with() self.assertTrue(not self.provider2.called)
def test_provider_unknown(self): resolver = credentials.CredentialResolver(providers=[self.provider1, self.provider2]) # No error is raised if you try to remove an unknown provider. resolver.remove('providerFOO') # But an error IS raised if you try to insert after an unknown # provider. with self.assertRaises(UnknownCredentialError): resolver.insert_after('providerFoo', None)
def test_first_credential_non_none_wins(self): self.provider1.load.return_value = None self.provider2.load.return_value = self.fake_creds resolver = credentials.CredentialResolver(providers=[self.provider1, self.provider2]) creds = resolver.load_credentials() self.assertEqual(creds.access_key_id, 'a') self.assertEqual(creds.private_key, 'b') self.provider1.load.assert_called_with() self.provider2.load.assert_called_with()
def test_inject_provider_before_existing(self): new_provider = mock.Mock() new_provider.METHOD = 'override' new_provider.load.return_value = credentials.Credentials('x', 'y', 'test') resolver = credentials.CredentialResolver(providers=[self.provider1, self.provider2]) resolver.insert_before(self.provider1.METHOD, new_provider) creds = resolver.load_credentials() self.assertEqual(creds.access_key_id, 'x') self.assertEqual(creds.private_key, 'y')
def test_can_remove_providers(self): self.provider1.load.return_value = credentials.Credentials( 'a', 'b', 'test') self.provider2.load.return_value = credentials.Credentials( 'd', 'e', 'test') resolver = credentials.CredentialResolver(providers=[self.provider1, self.provider2]) resolver.remove('provider1') creds = resolver.load_credentials() self.assertIsNotNone(creds) self.assertEqual(creds.access_key_id, 'd') self.assertEqual(creds.private_key, 'e') self.assertTrue(not self.provider1.load.called) self.provider2.load.assert_called_with()
def test_insert_before_unknown_provider_raises_error(self): resolver = credentials.CredentialResolver(providers=[self.provider1]) with self.assertRaises(UnknownCredentialError): resolver.insert_before('unknown-foo', self.provider2)
def test_get_unknown_provider_raises_error(self): resolver = credentials.CredentialResolver(providers=[self.provider1]) with self.assertRaises(UnknownCredentialError): resolver.get_provider('unknown-foo')
def test_get_provider_by_name(self): resolver = credentials.CredentialResolver(providers=[self.provider1]) result = resolver.get_provider('provider1') self.assertIs(result, self.provider1)
def test_load_credentials_single_provider(self): self.provider1.load.return_value = self.fake_creds resolver = credentials.CredentialResolver(providers=[self.provider1]) creds = resolver.load_credentials() self.assertEqual(creds.access_key_id, 'a') self.assertEqual(creds.private_key, 'b')