~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

  • Committer: Andrew Bennetts
  • Date: 2009-07-27 05:35:00 UTC
  • mfrom: (4570 +trunk)
  • mto: (4634.6.29 2.0)
  • mto: This revision was merged to the branch mainline in revision 4680.
  • Revision ID: andrew.bennetts@canonical.com-20090727053500-q76zsn2dx33jhmj5
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2008 Canonical Ltd
2
 
#   Authors: Robert Collins <robert.collins@canonical.com>
 
1
# Copyright (C) 2005, 2006, 2008, 2009 Canonical Ltd
3
2
#
4
3
# This program is free software; you can redistribute it and/or modify
5
4
# it under the terms of the GNU General Public License as published by
13
12
#
14
13
# You should have received a copy of the GNU General Public License
15
14
# along with this program; if not, write to the Free Software
16
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
16
 
18
17
"""Tests for finding and reading the bzr config file[s]."""
19
18
# import system imports here
150
149
        self._transport = self.control_files = \
151
150
            FakeControlFilesAndTransport(user_id=user_id)
152
151
 
 
152
    def _get_config(self):
 
153
        return config.TransportConfig(self._transport, 'branch.conf')
 
154
 
153
155
    def lock_write(self):
154
156
        pass
155
157
 
364
366
        parser = my_config._get_parser(file=config_file)
365
367
        self.failUnless(my_config._get_parser() is parser)
366
368
 
 
369
    def test_get_user_option_as_bool(self):
 
370
        config_file = StringIO("""
 
371
a_true_bool = true
 
372
a_false_bool = 0
 
373
an_invalid_bool = maybe
 
374
a_list = hmm, who knows ? # This interpreted as a list !
 
375
""".encode('utf-8'))
 
376
        my_config = config.IniBasedConfig(None)
 
377
        parser = my_config._get_parser(file=config_file)
 
378
        get_option = my_config.get_user_option_as_bool
 
379
        self.assertEqual(True, get_option('a_true_bool'))
 
380
        self.assertEqual(False, get_option('a_false_bool'))
 
381
        self.assertIs(None, get_option('an_invalid_bool'))
 
382
        self.assertIs(None, get_option('not_defined_in_this_config'))
367
383
 
368
384
class TestGetConfig(tests.TestCase):
369
385
 
1207
1223
 
1208
1224
    def test_set_unset_default_stack_on(self):
1209
1225
        my_dir = self.make_bzrdir('.')
1210
 
        bzrdir_config = config.BzrDirConfig(my_dir.transport)
 
1226
        bzrdir_config = config.BzrDirConfig(my_dir)
1211
1227
        self.assertIs(None, bzrdir_config.get_default_stack_on())
1212
1228
        bzrdir_config.set_default_stack_on('Foo')
1213
1229
        self.assertEqual('Foo', bzrdir_config._config.get_option(
1427
1443
    def test_set_credentials(self):
1428
1444
        conf = config.AuthenticationConfig()
1429
1445
        conf.set_credentials('name', 'host', 'user', 'scheme', 'password',
1430
 
        99, path='/foo', verify_certificates=False)
 
1446
        99, path='/foo', verify_certificates=False, realm='realm')
1431
1447
        credentials = conf.get_credentials(host='host', scheme='scheme',
1432
 
                                           port=99, path='/foo')
 
1448
                                           port=99, path='/foo',
 
1449
                                           realm='realm')
1433
1450
        CREDENTIALS = {'name': 'name', 'user': 'user', 'password': 'password',
1434
 
                       'verify_certificates': False,}
 
1451
                       'verify_certificates': False, 'scheme': 'scheme', 
 
1452
                       'host': 'host', 'port': 99, 'path': '/foo', 
 
1453
                       'realm': 'realm'}
1435
1454
        self.assertEqual(CREDENTIALS, credentials)
1436
1455
        credentials_from_disk = config.AuthenticationConfig().get_credentials(
1437
 
            host='host', scheme='scheme', port=99, path='/foo')
 
1456
            host='host', scheme='scheme', port=99, path='/foo', realm='realm')
1438
1457
        self.assertEqual(CREDENTIALS, credentials_from_disk)
1439
1458
 
1440
1459
    def test_reset_credentials_different_name(self):
1444
1463
        self.assertIs(None, conf._get_config().get('name'))
1445
1464
        credentials = conf.get_credentials(host='host', scheme='scheme')
1446
1465
        CREDENTIALS = {'name': 'name2', 'user': 'user2', 'password':
1447
 
                       'password', 'verify_certificates': True}
 
1466
                       'password', 'verify_certificates': True, 
 
1467
                       'scheme': 'scheme', 'host': 'host', 'port': None, 
 
1468
                       'path': None, 'realm': None}
1448
1469
        self.assertEqual(CREDENTIALS, credentials)
1449
1470
 
1450
1471
 
1451
1472
class TestAuthenticationConfig(tests.TestCase):
1452
1473
    """Test AuthenticationConfig behaviour"""
1453
1474
 
1454
 
    def _check_default_prompt(self, expected_prompt_format, scheme,
1455
 
                              host=None, port=None, realm=None, path=None):
 
1475
    def _check_default_password_prompt(self, expected_prompt_format, scheme,
 
1476
                                       host=None, port=None, realm=None,
 
1477
                                       path=None):
1456
1478
        if host is None:
1457
1479
            host = 'bar.org'
1458
1480
        user, password = 'jim', 'precious'
1461
1483
            'user': user, 'realm': realm}
1462
1484
 
1463
1485
        stdout = tests.StringIOWrapper()
 
1486
        stderr = tests.StringIOWrapper()
1464
1487
        ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
1465
 
                                            stdout=stdout)
 
1488
                                            stdout=stdout, stderr=stderr)
1466
1489
        # We use an empty conf so that the user is always prompted
1467
1490
        conf = config.AuthenticationConfig()
1468
1491
        self.assertEquals(password,
1469
1492
                          conf.get_password(scheme, host, user, port=port,
1470
1493
                                            realm=realm, path=path))
1471
 
        self.assertEquals(stdout.getvalue(), expected_prompt)
1472
 
 
1473
 
    def test_default_prompts(self):
1474
 
        # HTTP prompts can't be tested here, see test_http.py
1475
 
        self._check_default_prompt('FTP %(user)s@%(host)s password: ', 'ftp')
1476
 
        self._check_default_prompt('FTP %(user)s@%(host)s:%(port)d password: ',
1477
 
                                   'ftp', port=10020)
1478
 
 
1479
 
        self._check_default_prompt('SSH %(user)s@%(host)s:%(port)d password: ',
1480
 
                                   'ssh', port=12345)
 
1494
        self.assertEquals(expected_prompt, stderr.getvalue())
 
1495
        self.assertEquals('', stdout.getvalue())
 
1496
 
 
1497
    def _check_default_username_prompt(self, expected_prompt_format, scheme,
 
1498
                                       host=None, port=None, realm=None,
 
1499
                                       path=None):
 
1500
        if host is None:
 
1501
            host = 'bar.org'
 
1502
        username = 'jim'
 
1503
        expected_prompt = expected_prompt_format % {
 
1504
            'scheme': scheme, 'host': host, 'port': port,
 
1505
            'realm': realm}
 
1506
        stdout = tests.StringIOWrapper()
 
1507
        stderr = tests.StringIOWrapper()
 
1508
        ui.ui_factory = tests.TestUIFactory(stdin=username+ '\n',
 
1509
                                            stdout=stdout, stderr=stderr)
 
1510
        # We use an empty conf so that the user is always prompted
 
1511
        conf = config.AuthenticationConfig()
 
1512
        self.assertEquals(username, conf.get_user(scheme, host, port=port,
 
1513
                          realm=realm, path=path, ask=True))
 
1514
        self.assertEquals(expected_prompt, stderr.getvalue())
 
1515
        self.assertEquals('', stdout.getvalue())
 
1516
 
 
1517
    def test_username_defaults_prompts(self):
 
1518
        # HTTP prompts can't be tested here, see test_http.py
 
1519
        self._check_default_username_prompt('FTP %(host)s username: ', 'ftp')
 
1520
        self._check_default_username_prompt(
 
1521
            'FTP %(host)s:%(port)d username: ', 'ftp', port=10020)
 
1522
        self._check_default_username_prompt(
 
1523
            'SSH %(host)s:%(port)d username: ', 'ssh', port=12345)
 
1524
 
 
1525
    def test_username_default_no_prompt(self):
 
1526
        conf = config.AuthenticationConfig()
 
1527
        self.assertEquals(None,
 
1528
            conf.get_user('ftp', 'example.com'))
 
1529
        self.assertEquals("explicitdefault",
 
1530
            conf.get_user('ftp', 'example.com', default="explicitdefault"))
 
1531
 
 
1532
    def test_password_default_prompts(self):
 
1533
        # HTTP prompts can't be tested here, see test_http.py
 
1534
        self._check_default_password_prompt(
 
1535
            'FTP %(user)s@%(host)s password: ', 'ftp')
 
1536
        self._check_default_password_prompt(
 
1537
            'FTP %(user)s@%(host)s:%(port)d password: ', 'ftp', port=10020)
 
1538
        self._check_default_password_prompt(
 
1539
            'SSH %(user)s@%(host)s:%(port)d password: ', 'ssh', port=12345)
1481
1540
        # SMTP port handling is a bit special (it's handled if embedded in the
1482
1541
        # host too)
1483
1542
        # FIXME: should we: forbid that, extend it to other schemes, leave
1484
1543
        # things as they are that's fine thank you ?
1485
 
        self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1486
 
                                   'smtp')
1487
 
        self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1488
 
                                   'smtp', host='bar.org:10025')
1489
 
        self._check_default_prompt(
 
1544
        self._check_default_password_prompt('SMTP %(user)s@%(host)s password: ',
 
1545
                                            'smtp')
 
1546
        self._check_default_password_prompt('SMTP %(user)s@%(host)s password: ',
 
1547
                                            'smtp', host='bar.org:10025')
 
1548
        self._check_default_password_prompt(
1490
1549
            'SMTP %(user)s@%(host)s:%(port)d password: ',
1491
1550
            'smtp', port=10025)
1492
1551
 
1501
1560
"""))
1502
1561
        entered_password = 'typed-by-hand'
1503
1562
        stdout = tests.StringIOWrapper()
 
1563
        stderr = tests.StringIOWrapper()
1504
1564
        ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1505
 
                                            stdout=stdout)
 
1565
                                            stdout=stdout, stderr=stderr)
1506
1566
 
1507
1567
        # Since the password defined in the authentication config is ignored,
1508
1568
        # the user is prompted
1522
1582
"""))
1523
1583
        entered_password = 'typed-by-hand'
1524
1584
        stdout = tests.StringIOWrapper()
 
1585
        stderr = tests.StringIOWrapper()
1525
1586
        ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1526
 
                                            stdout=stdout)
 
1587
                                            stdout=stdout,
 
1588
                                            stderr=stderr)
1527
1589
 
1528
1590
        # Since the password defined in the authentication config is ignored,
1529
1591
        # the user is prompted
1535
1597
            self._get_log(keep_log_file=True),
1536
1598
            'password ignored in section \[ssh with password\]')
1537
1599
 
 
1600
    def test_uses_fallback_stores(self):
 
1601
        self._old_cs_registry = config.credential_store_registry
 
1602
        def restore():
 
1603
            config.credential_store_registry = self._old_cs_registry
 
1604
        self.addCleanup(restore)
 
1605
        config.credential_store_registry = config.CredentialStoreRegistry()
 
1606
        store = StubCredentialStore()
 
1607
        store.add_credentials("http", "example.com", "joe", "secret")
 
1608
        config.credential_store_registry.register("stub", store, fallback=True)
 
1609
        conf = config.AuthenticationConfig(_file=StringIO())
 
1610
        creds = conf.get_credentials("http", "example.com")
 
1611
        self.assertEquals("joe", creds["user"])
 
1612
        self.assertEquals("secret", creds["password"])
 
1613
 
 
1614
 
 
1615
class StubCredentialStore(config.CredentialStore):
 
1616
 
 
1617
    def __init__(self):
 
1618
        self._username = {}
 
1619
        self._password = {}
 
1620
 
 
1621
    def add_credentials(self, scheme, host, user, password=None):
 
1622
        self._username[(scheme, host)] = user
 
1623
        self._password[(scheme, host)] = password
 
1624
 
 
1625
    def get_credentials(self, scheme, host, port=None, user=None,
 
1626
        path=None, realm=None):
 
1627
        key = (scheme, host)
 
1628
        if not key in self._username:
 
1629
            return None
 
1630
        return { "scheme": scheme, "host": host, "port": port,
 
1631
                "user": self._username[key], "password": self._password[key]}
 
1632
 
 
1633
 
 
1634
class CountingCredentialStore(config.CredentialStore):
 
1635
 
 
1636
    def __init__(self):
 
1637
        self._calls = 0
 
1638
 
 
1639
    def get_credentials(self, scheme, host, port=None, user=None,
 
1640
        path=None, realm=None):
 
1641
        self._calls += 1
 
1642
        return None
 
1643
 
1538
1644
 
1539
1645
class TestCredentialStoreRegistry(tests.TestCase):
1540
1646
 
1552
1658
        # 'unknown' so we use that as an never registered key.
1553
1659
        self.assertRaises(KeyError, r.get_credential_store, 'unknown')
1554
1660
 
 
1661
    def test_fallback_none_registered(self):
 
1662
        r = config.CredentialStoreRegistry()
 
1663
        self.assertEquals(None,
 
1664
                          r.get_fallback_credentials("http", "example.com"))
 
1665
 
 
1666
    def test_register(self):
 
1667
        r = config.CredentialStoreRegistry()
 
1668
        r.register("stub", StubCredentialStore(), fallback=False)
 
1669
        r.register("another", StubCredentialStore(), fallback=True)
 
1670
        self.assertEquals(["another", "stub"], r.keys())
 
1671
 
 
1672
    def test_register_lazy(self):
 
1673
        r = config.CredentialStoreRegistry()
 
1674
        r.register_lazy("stub", "bzrlib.tests.test_config",
 
1675
                        "StubCredentialStore", fallback=False)
 
1676
        self.assertEquals(["stub"], r.keys())
 
1677
        self.assertIsInstance(r.get_credential_store("stub"),
 
1678
                              StubCredentialStore)
 
1679
 
 
1680
    def test_is_fallback(self):
 
1681
        r = config.CredentialStoreRegistry()
 
1682
        r.register("stub1", None, fallback=False)
 
1683
        r.register("stub2", None, fallback=True)
 
1684
        self.assertEquals(False, r.is_fallback("stub1"))
 
1685
        self.assertEquals(True, r.is_fallback("stub2"))
 
1686
 
 
1687
    def test_no_fallback(self):
 
1688
        r = config.CredentialStoreRegistry()
 
1689
        store = CountingCredentialStore()
 
1690
        r.register("count", store, fallback=False)
 
1691
        self.assertEquals(None,
 
1692
                          r.get_fallback_credentials("http", "example.com"))
 
1693
        self.assertEquals(0, store._calls)
 
1694
 
 
1695
    def test_fallback_credentials(self):
 
1696
        r = config.CredentialStoreRegistry()
 
1697
        store = StubCredentialStore()
 
1698
        store.add_credentials("http", "example.com",
 
1699
                              "somebody", "geheim")
 
1700
        r.register("stub", store, fallback=True)
 
1701
        creds = r.get_fallback_credentials("http", "example.com")
 
1702
        self.assertEquals("somebody", creds["user"])
 
1703
        self.assertEquals("geheim", creds["password"])
 
1704
 
 
1705
    def test_fallback_first_wins(self):
 
1706
        r = config.CredentialStoreRegistry()
 
1707
        stub1 = StubCredentialStore()
 
1708
        stub1.add_credentials("http", "example.com",
 
1709
                              "somebody", "stub1")
 
1710
        r.register("stub1", stub1, fallback=True)
 
1711
        stub2 = StubCredentialStore()
 
1712
        stub2.add_credentials("http", "example.com",
 
1713
                              "somebody", "stub2")
 
1714
        r.register("stub2", stub1, fallback=True)
 
1715
        creds = r.get_fallback_credentials("http", "example.com")
 
1716
        self.assertEquals("somebody", creds["user"])
 
1717
        self.assertEquals("stub1", creds["password"])
 
1718
 
1555
1719
 
1556
1720
class TestPlainTextCredentialStore(tests.TestCase):
1557
1721