~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-04-11 13:01:19 UTC
  • mfrom: (4283.1.4 fallback)
  • Revision ID: pqm@pqm.ubuntu.com-20090411130119-4kn8b6070uyqg5xx
(Jelmer, vila) Support registration of fallback credential stores.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1576
1576
            self._get_log(keep_log_file=True),
1577
1577
            'password ignored in section \[ssh with password\]')
1578
1578
 
 
1579
    def test_uses_fallback_stores(self):
 
1580
        self._old_cs_registry = config.credential_store_registry
 
1581
        def restore():
 
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"])
 
1592
 
 
1593
 
 
1594
class StubCredentialStore(config.CredentialStore):
 
1595
 
 
1596
    def __init__(self):
 
1597
        self._username = {}
 
1598
        self._password = {}
 
1599
 
 
1600
    def add_credentials(self, scheme, host, user, password=None):
 
1601
        self._username[(scheme, host)] = user
 
1602
        self._password[(scheme, host)] = password
 
1603
 
 
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:
 
1608
            return None
 
1609
        return { "scheme": scheme, "host": host, "port": port,
 
1610
                "user": self._username[key], "password": self._password[key]}
 
1611
 
 
1612
 
 
1613
class CountingCredentialStore(config.CredentialStore):
 
1614
 
 
1615
    def __init__(self):
 
1616
        self._calls = 0
 
1617
 
 
1618
    def get_credentials(self, scheme, host, port=None, user=None,
 
1619
        path=None, realm=None):
 
1620
        self._calls += 1
 
1621
        return None
 
1622
 
1579
1623
 
1580
1624
class TestCredentialStoreRegistry(tests.TestCase):
1581
1625
 
1593
1637
        # 'unknown' so we use that as an never registered key.
1594
1638
        self.assertRaises(KeyError, r.get_credential_store, 'unknown')
1595
1639
 
 
1640
    def test_fallback_none_registered(self):
 
1641
        r = config.CredentialStoreRegistry()
 
1642
        self.assertEquals(None,
 
1643
                          r.get_fallback_credentials("http", "example.com"))
 
1644
 
 
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())
 
1650
 
 
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)
 
1658
 
 
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"))
 
1665
 
 
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)
 
1673
 
 
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"])
 
1683
 
 
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"])
 
1697
 
1596
1698
 
1597
1699
class TestPlainTextCredentialStore(tests.TestCase):
1598
1700