~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-09 20:23:07 UTC
  • mfrom: (4265.1.4 bbc-merge)
  • Revision ID: pqm@pqm.ubuntu.com-20090409202307-n0depb16qepoe21o
(jam) Change _fetch_uses_deltas = False for CHK repos until we can
        write a better fix.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2008, 2009 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2008 Canonical Ltd
 
2
#   Authors: Robert Collins <robert.collins@canonical.com>
2
3
#
3
4
# This program is free software; you can redistribute it and/or modify
4
5
# it under the terms of the GNU General Public License as published by
366
367
        parser = my_config._get_parser(file=config_file)
367
368
        self.failUnless(my_config._get_parser() is parser)
368
369
 
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'))
383
370
 
384
371
class TestGetConfig(tests.TestCase):
385
372
 
1223
1210
 
1224
1211
    def test_set_unset_default_stack_on(self):
1225
1212
        my_dir = self.make_bzrdir('.')
1226
 
        bzrdir_config = config.BzrDirConfig(my_dir)
 
1213
        bzrdir_config = config.BzrDirConfig(my_dir.transport)
1227
1214
        self.assertIs(None, bzrdir_config.get_default_stack_on())
1228
1215
        bzrdir_config.set_default_stack_on('Foo')
1229
1216
        self.assertEqual('Foo', bzrdir_config._config.get_option(
1472
1459
class TestAuthenticationConfig(tests.TestCase):
1473
1460
    """Test AuthenticationConfig behaviour"""
1474
1461
 
1475
 
    def _check_default_password_prompt(self, expected_prompt_format, scheme,
1476
 
                                       host=None, port=None, realm=None,
1477
 
                                       path=None):
 
1462
    def _check_default_prompt(self, expected_prompt_format, scheme,
 
1463
                              host=None, port=None, realm=None, path=None):
1478
1464
        if host is None:
1479
1465
            host = 'bar.org'
1480
1466
        user, password = 'jim', 'precious'
1483
1469
            'user': user, 'realm': realm}
1484
1470
 
1485
1471
        stdout = tests.StringIOWrapper()
1486
 
        stderr = tests.StringIOWrapper()
1487
1472
        ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
1488
 
                                            stdout=stdout, stderr=stderr)
 
1473
                                            stdout=stdout)
1489
1474
        # We use an empty conf so that the user is always prompted
1490
1475
        conf = config.AuthenticationConfig()
1491
1476
        self.assertEquals(password,
1492
1477
                          conf.get_password(scheme, host, user, port=port,
1493
1478
                                            realm=realm, path=path))
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)
 
1479
        self.assertEquals(stdout.getvalue(), expected_prompt)
 
1480
 
 
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)
1540
1489
        # SMTP port handling is a bit special (it's handled if embedded in the
1541
1490
        # host too)
1542
1491
        # FIXME: should we: forbid that, extend it to other schemes, leave
1543
1492
        # things as they are that's fine thank you ?
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(
 
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(
1549
1498
            'SMTP %(user)s@%(host)s:%(port)d password: ',
1550
1499
            'smtp', port=10025)
1551
1500
 
1560
1509
"""))
1561
1510
        entered_password = 'typed-by-hand'
1562
1511
        stdout = tests.StringIOWrapper()
1563
 
        stderr = tests.StringIOWrapper()
1564
1512
        ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1565
 
                                            stdout=stdout, stderr=stderr)
 
1513
                                            stdout=stdout)
1566
1514
 
1567
1515
        # Since the password defined in the authentication config is ignored,
1568
1516
        # the user is prompted
1582
1530
"""))
1583
1531
        entered_password = 'typed-by-hand'
1584
1532
        stdout = tests.StringIOWrapper()
1585
 
        stderr = tests.StringIOWrapper()
1586
1533
        ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1587
 
                                            stdout=stdout,
1588
 
                                            stderr=stderr)
 
1534
                                            stdout=stdout)
1589
1535
 
1590
1536
        # Since the password defined in the authentication config is ignored,
1591
1537
        # the user is prompted
1597
1543
            self._get_log(keep_log_file=True),
1598
1544
            'password ignored in section \[ssh with password\]')
1599
1545
 
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
 
 
1644
1546
 
1645
1547
class TestCredentialStoreRegistry(tests.TestCase):
1646
1548
 
1658
1560
        # 'unknown' so we use that as an never registered key.
1659
1561
        self.assertRaises(KeyError, r.get_credential_store, 'unknown')
1660
1562
 
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
 
 
1719
1563
 
1720
1564
class TestPlainTextCredentialStore(tests.TestCase):
1721
1565