~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

  • Committer: Robert J. Tanner
  • Date: 2009-04-30 22:40:42 UTC
  • mfrom: (4323 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4324.
  • Revision ID: tanner@real-time.com-20090430224042-53v45axtue5bw45l
Merge 1.14.1 back to trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
"""Tests for finding and reading the bzr config file[s]."""
19
19
# import system imports here
20
20
from cStringIO import StringIO
 
21
import getpass
21
22
import os
22
23
import sys
23
24
 
1210
1211
 
1211
1212
    def test_set_unset_default_stack_on(self):
1212
1213
        my_dir = self.make_bzrdir('.')
1213
 
        bzrdir_config = config.BzrDirConfig(my_dir.transport)
 
1214
        bzrdir_config = config.BzrDirConfig(my_dir)
1214
1215
        self.assertIs(None, bzrdir_config.get_default_stack_on())
1215
1216
        bzrdir_config.set_default_stack_on('Foo')
1216
1217
        self.assertEqual('Foo', bzrdir_config._config.get_option(
1459
1460
class TestAuthenticationConfig(tests.TestCase):
1460
1461
    """Test AuthenticationConfig behaviour"""
1461
1462
 
1462
 
    def _check_default_prompt(self, expected_prompt_format, scheme,
 
1463
    def _check_default_password_prompt(self, expected_prompt_format, scheme,
1463
1464
                              host=None, port=None, realm=None, path=None):
1464
1465
        if host is None:
1465
1466
            host = 'bar.org'
1478
1479
                                            realm=realm, path=path))
1479
1480
        self.assertEquals(stdout.getvalue(), expected_prompt)
1480
1481
 
1481
 
    def test_default_prompts(self):
1482
 
        # HTTP prompts can't be tested here, see test_http.py
1483
 
        self._check_default_prompt('FTP %(user)s@%(host)s password: ', 'ftp')
1484
 
        self._check_default_prompt('FTP %(user)s@%(host)s:%(port)d password: ',
1485
 
                                   'ftp', port=10020)
1486
 
 
1487
 
        self._check_default_prompt('SSH %(user)s@%(host)s:%(port)d password: ',
1488
 
                                   'ssh', port=12345)
 
1482
    def _check_default_username_prompt(self, expected_prompt_format, scheme,
 
1483
                              host=None, port=None, realm=None, path=None):
 
1484
        if host is None:
 
1485
            host = 'bar.org'
 
1486
        username = 'jim'
 
1487
        expected_prompt = expected_prompt_format % {
 
1488
            'scheme': scheme, 'host': host, 'port': port,
 
1489
            'realm': realm}
 
1490
        stdout = tests.StringIOWrapper()
 
1491
        ui.ui_factory = tests.TestUIFactory(stdin=username+ '\n',
 
1492
                                            stdout=stdout)
 
1493
        # We use an empty conf so that the user is always prompted
 
1494
        conf = config.AuthenticationConfig()
 
1495
        self.assertEquals(username, conf.get_user(scheme, host, port=port,
 
1496
                          realm=realm, path=path, ask=True))
 
1497
        self.assertEquals(stdout.getvalue(), expected_prompt)
 
1498
 
 
1499
    def test_username_defaults_prompts(self):
 
1500
        # HTTP prompts can't be tested here, see test_http.py
 
1501
        self._check_default_username_prompt('FTP %(host)s username: ', 'ftp')
 
1502
        self._check_default_username_prompt(
 
1503
            'FTP %(host)s:%(port)d username: ', 'ftp', port=10020)
 
1504
        self._check_default_username_prompt(
 
1505
            'SSH %(host)s:%(port)d username: ', 'ssh', port=12345)
 
1506
 
 
1507
    def test_username_default_no_prompt(self):
 
1508
        conf = config.AuthenticationConfig()
 
1509
        self.assertEquals(None,
 
1510
            conf.get_user('ftp', 'example.com'))
 
1511
        self.assertEquals("explicitdefault",
 
1512
            conf.get_user('ftp', 'example.com', default="explicitdefault"))
 
1513
 
 
1514
    def test_password_default_prompts(self):
 
1515
        # HTTP prompts can't be tested here, see test_http.py
 
1516
        self._check_default_password_prompt(
 
1517
            'FTP %(user)s@%(host)s password: ', 'ftp')
 
1518
        self._check_default_password_prompt(
 
1519
            'FTP %(user)s@%(host)s:%(port)d password: ', 'ftp', port=10020)
 
1520
        self._check_default_password_prompt(
 
1521
            'SSH %(user)s@%(host)s:%(port)d password: ', 'ssh', port=12345)
1489
1522
        # SMTP port handling is a bit special (it's handled if embedded in the
1490
1523
        # host too)
1491
1524
        # FIXME: should we: forbid that, extend it to other schemes, leave
1492
1525
        # things as they are that's fine thank you ?
1493
 
        self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1494
 
                                   'smtp')
1495
 
        self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1496
 
                                   'smtp', host='bar.org:10025')
1497
 
        self._check_default_prompt(
 
1526
        self._check_default_password_prompt('SMTP %(user)s@%(host)s password: ',
 
1527
                                            'smtp')
 
1528
        self._check_default_password_prompt('SMTP %(user)s@%(host)s password: ',
 
1529
                                            'smtp', host='bar.org:10025')
 
1530
        self._check_default_password_prompt(
1498
1531
            'SMTP %(user)s@%(host)s:%(port)d password: ',
1499
1532
            'smtp', port=10025)
1500
1533
 
1543
1576
            self._get_log(keep_log_file=True),
1544
1577
            'password ignored in section \[ssh with password\]')
1545
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
 
1546
1623
 
1547
1624
class TestCredentialStoreRegistry(tests.TestCase):
1548
1625
 
1560
1637
        # 'unknown' so we use that as an never registered key.
1561
1638
        self.assertRaises(KeyError, r.get_credential_store, 'unknown')
1562
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
 
1563
1698
 
1564
1699
class TestPlainTextCredentialStore(tests.TestCase):
1565
1700