1576
1576
self._get_log(keep_log_file=True),
1577
1577
'password ignored in section \[ssh with password\]')
1579
def test_uses_fallback_stores(self):
1580
self._old_cs_registry = config.credential_store_registry
1582
config.credential_store_registry = self._old_cs_registry
1583
self.addCleanup(restore)
1584
config.credential_store_registry = config.CredentialStoreRegistry()
1585
store = StubCredentialStore()
1586
store.add_credentials("http", "example.com", "joe", "secret")
1587
config.credential_store_registry.register("stub", store, fallback=True)
1588
conf = config.AuthenticationConfig(_file=StringIO())
1589
creds = conf.get_credentials("http", "example.com")
1590
self.assertEquals("joe", creds["user"])
1591
self.assertEquals("secret", creds["password"])
1594
class StubCredentialStore(config.CredentialStore):
1600
def add_credentials(self, scheme, host, user, password=None):
1601
self._username[(scheme, host)] = user
1602
self._password[(scheme, host)] = password
1604
def get_credentials(self, scheme, host, port=None, user=None,
1605
path=None, realm=None):
1606
key = (scheme, host)
1607
if not key in self._username:
1609
return { "scheme": scheme, "host": host, "port": port,
1610
"user": self._username[key], "password": self._password[key]}
1613
class CountingCredentialStore(config.CredentialStore):
1618
def get_credentials(self, scheme, host, port=None, user=None,
1619
path=None, realm=None):
1580
1624
class TestCredentialStoreRegistry(tests.TestCase):
1593
1637
# 'unknown' so we use that as an never registered key.
1594
1638
self.assertRaises(KeyError, r.get_credential_store, 'unknown')
1640
def test_fallback_none_registered(self):
1641
r = config.CredentialStoreRegistry()
1642
self.assertEquals(None,
1643
r.get_fallback_credentials("http", "example.com"))
1645
def test_register(self):
1646
r = config.CredentialStoreRegistry()
1647
r.register("stub", StubCredentialStore(), fallback=False)
1648
r.register("another", StubCredentialStore(), fallback=True)
1649
self.assertEquals(["another", "stub"], r.keys())
1651
def test_register_lazy(self):
1652
r = config.CredentialStoreRegistry()
1653
r.register_lazy("stub", "bzrlib.tests.test_config",
1654
"StubCredentialStore", fallback=False)
1655
self.assertEquals(["stub"], r.keys())
1656
self.assertIsInstance(r.get_credential_store("stub"),
1657
StubCredentialStore)
1659
def test_is_fallback(self):
1660
r = config.CredentialStoreRegistry()
1661
r.register("stub1", None, fallback=False)
1662
r.register("stub2", None, fallback=True)
1663
self.assertEquals(False, r.is_fallback("stub1"))
1664
self.assertEquals(True, r.is_fallback("stub2"))
1666
def test_no_fallback(self):
1667
r = config.CredentialStoreRegistry()
1668
store = CountingCredentialStore()
1669
r.register("count", store, fallback=False)
1670
self.assertEquals(None,
1671
r.get_fallback_credentials("http", "example.com"))
1672
self.assertEquals(0, store._calls)
1674
def test_fallback_credentials(self):
1675
r = config.CredentialStoreRegistry()
1676
store = StubCredentialStore()
1677
store.add_credentials("http", "example.com",
1678
"somebody", "geheim")
1679
r.register("stub", store, fallback=True)
1680
creds = r.get_fallback_credentials("http", "example.com")
1681
self.assertEquals("somebody", creds["user"])
1682
self.assertEquals("geheim", creds["password"])
1684
def test_fallback_first_wins(self):
1685
r = config.CredentialStoreRegistry()
1686
stub1 = StubCredentialStore()
1687
stub1.add_credentials("http", "example.com",
1688
"somebody", "stub1")
1689
r.register("stub1", stub1, fallback=True)
1690
stub2 = StubCredentialStore()
1691
stub2.add_credentials("http", "example.com",
1692
"somebody", "stub2")
1693
r.register("stub2", stub1, fallback=True)
1694
creds = r.get_fallback_credentials("http", "example.com")
1695
self.assertEquals("somebody", creds["user"])
1696
self.assertEquals("stub1", creds["password"])
1597
1699
class TestPlainTextCredentialStore(tests.TestCase):