394
364
        self.failUnless(my_config._get_parser() is parser)
 
397
 
class TestGetUserOptionAs(TestIniConfig):
 
399
 
    def test_get_user_option_as_bool(self):
 
400
 
        conf, parser = self.make_config_parser("""
 
403
 
an_invalid_bool = maybe
 
404
 
a_list = hmm, who knows ? # This is interpreted as a list !
 
406
 
        get_bool = conf.get_user_option_as_bool
 
407
 
        self.assertEqual(True, get_bool('a_true_bool'))
 
408
 
        self.assertEqual(False, get_bool('a_false_bool'))
 
411
 
            warnings.append(args[0] % args[1:])
 
412
 
        self.overrideAttr(trace, 'warning', warning)
 
413
 
        msg = 'Value "%s" is not a boolean for "%s"'
 
414
 
        self.assertIs(None, get_bool('an_invalid_bool'))
 
415
 
        self.assertEquals(msg % ('maybe', 'an_invalid_bool'), warnings[0])
 
417
 
        self.assertIs(None, get_bool('not_defined_in_this_config'))
 
418
 
        self.assertEquals([], warnings)
 
420
 
    def test_get_user_option_as_list(self):
 
421
 
        conf, parser = self.make_config_parser("""
 
426
 
        get_list = conf.get_user_option_as_list
 
427
 
        self.assertEqual(['a', 'b', 'c'], get_list('a_list'))
 
428
 
        self.assertEqual(['1'], get_list('length_1'))
 
429
 
        self.assertEqual('x', conf.get_user_option('one_item'))
 
430
 
        # automatically cast to list
 
431
 
        self.assertEqual(['x'], get_list('one_item'))
 
434
 
class TestSupressWarning(TestIniConfig):
 
436
 
    def make_warnings_config(self, s):
 
437
 
        conf, parser = self.make_config_parser(s)
 
438
 
        return conf.suppress_warning
 
440
 
    def test_suppress_warning_unknown(self):
 
441
 
        suppress_warning = self.make_warnings_config('')
 
442
 
        self.assertEqual(False, suppress_warning('unknown_warning'))
 
444
 
    def test_suppress_warning_known(self):
 
445
 
        suppress_warning = self.make_warnings_config('suppress_warnings=a,b')
 
446
 
        self.assertEqual(False, suppress_warning('c'))
 
447
 
        self.assertEqual(True, suppress_warning('a'))
 
448
 
        self.assertEqual(True, suppress_warning('b'))
 
451
367
class TestGetConfig(tests.TestCase):
 
453
369
    def test_constructs(self):
 
 
1517
1370
        self.assertEquals(True, credentials.get('verify_certificates'))
 
1520
 
class TestAuthenticationStorage(tests.TestCaseInTempDir):
 
1522
 
    def test_set_credentials(self):
 
1523
 
        conf = config.AuthenticationConfig()
 
1524
 
        conf.set_credentials('name', 'host', 'user', 'scheme', 'password',
 
1525
 
        99, path='/foo', verify_certificates=False, realm='realm')
 
1526
 
        credentials = conf.get_credentials(host='host', scheme='scheme',
 
1527
 
                                           port=99, path='/foo',
 
1529
 
        CREDENTIALS = {'name': 'name', 'user': 'user', 'password': 'password',
 
1530
 
                       'verify_certificates': False, 'scheme': 'scheme', 
 
1531
 
                       'host': 'host', 'port': 99, 'path': '/foo', 
 
1533
 
        self.assertEqual(CREDENTIALS, credentials)
 
1534
 
        credentials_from_disk = config.AuthenticationConfig().get_credentials(
 
1535
 
            host='host', scheme='scheme', port=99, path='/foo', realm='realm')
 
1536
 
        self.assertEqual(CREDENTIALS, credentials_from_disk)
 
1538
 
    def test_reset_credentials_different_name(self):
 
1539
 
        conf = config.AuthenticationConfig()
 
1540
 
        conf.set_credentials('name', 'host', 'user', 'scheme', 'password'),
 
1541
 
        conf.set_credentials('name2', 'host', 'user2', 'scheme', 'password'),
 
1542
 
        self.assertIs(None, conf._get_config().get('name'))
 
1543
 
        credentials = conf.get_credentials(host='host', scheme='scheme')
 
1544
 
        CREDENTIALS = {'name': 'name2', 'user': 'user2', 'password':
 
1545
 
                       'password', 'verify_certificates': True, 
 
1546
 
                       'scheme': 'scheme', 'host': 'host', 'port': None, 
 
1547
 
                       'path': None, 'realm': None}
 
1548
 
        self.assertEqual(CREDENTIALS, credentials)
 
1551
1373
class TestAuthenticationConfig(tests.TestCase):
 
1552
1374
    """Test AuthenticationConfig behaviour"""
 
1554
 
    def _check_default_password_prompt(self, expected_prompt_format, scheme,
 
1555
 
                                       host=None, port=None, realm=None,
 
 
1376
    def _check_default_prompt(self, expected_prompt_format, scheme,
 
 
1377
                              host=None, port=None, realm=None, path=None):
 
1557
1378
        if host is None:
 
1558
1379
            host = 'bar.org'
 
1559
1380
        user, password = 'jim', 'precious'
 
 
1562
1383
            'user': user, 'realm': realm}
 
1564
1385
        stdout = tests.StringIOWrapper()
 
1565
 
        stderr = tests.StringIOWrapper()
 
1566
1386
        ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
 
1567
 
                                            stdout=stdout, stderr=stderr)
 
1568
1388
        # We use an empty conf so that the user is always prompted
 
1569
1389
        conf = config.AuthenticationConfig()
 
1570
1390
        self.assertEquals(password,
 
1571
1391
                          conf.get_password(scheme, host, user, port=port,
 
1572
1392
                                            realm=realm, path=path))
 
1573
 
        self.assertEquals(expected_prompt, stderr.getvalue())
 
1574
 
        self.assertEquals('', stdout.getvalue())
 
1576
 
    def _check_default_username_prompt(self, expected_prompt_format, scheme,
 
1577
 
                                       host=None, port=None, realm=None,
 
1582
 
        expected_prompt = expected_prompt_format % {
 
1583
 
            'scheme': scheme, 'host': host, 'port': port,
 
1585
 
        stdout = tests.StringIOWrapper()
 
1586
 
        stderr = tests.StringIOWrapper()
 
1587
 
        ui.ui_factory = tests.TestUIFactory(stdin=username+ '\n',
 
1588
 
                                            stdout=stdout, stderr=stderr)
 
1589
 
        # We use an empty conf so that the user is always prompted
 
1590
 
        conf = config.AuthenticationConfig()
 
1591
 
        self.assertEquals(username, conf.get_user(scheme, host, port=port,
 
1592
 
                          realm=realm, path=path, ask=True))
 
1593
 
        self.assertEquals(expected_prompt, stderr.getvalue())
 
1594
 
        self.assertEquals('', stdout.getvalue())
 
1596
 
    def test_username_defaults_prompts(self):
 
1597
 
        # HTTP prompts can't be tested here, see test_http.py
 
1598
 
        self._check_default_username_prompt('FTP %(host)s username: ', 'ftp')
 
1599
 
        self._check_default_username_prompt(
 
1600
 
            'FTP %(host)s:%(port)d username: ', 'ftp', port=10020)
 
1601
 
        self._check_default_username_prompt(
 
1602
 
            'SSH %(host)s:%(port)d username: ', 'ssh', port=12345)
 
1604
 
    def test_username_default_no_prompt(self):
 
1605
 
        conf = config.AuthenticationConfig()
 
1606
 
        self.assertEquals(None,
 
1607
 
            conf.get_user('ftp', 'example.com'))
 
1608
 
        self.assertEquals("explicitdefault",
 
1609
 
            conf.get_user('ftp', 'example.com', default="explicitdefault"))
 
1611
 
    def test_password_default_prompts(self):
 
1612
 
        # HTTP prompts can't be tested here, see test_http.py
 
1613
 
        self._check_default_password_prompt(
 
1614
 
            'FTP %(user)s@%(host)s password: ', 'ftp')
 
1615
 
        self._check_default_password_prompt(
 
1616
 
            'FTP %(user)s@%(host)s:%(port)d password: ', 'ftp', port=10020)
 
1617
 
        self._check_default_password_prompt(
 
1618
 
            'SSH %(user)s@%(host)s:%(port)d password: ', 'ssh', port=12345)
 
 
1393
        self.assertEquals(stdout.getvalue(), expected_prompt)
 
 
1395
    def test_default_prompts(self):
 
 
1396
        # HTTP prompts can't be tested here, see test_http.py
 
 
1397
        self._check_default_prompt('FTP %(user)s@%(host)s password: ', 'ftp')
 
 
1398
        self._check_default_prompt('FTP %(user)s@%(host)s:%(port)d password: ',
 
 
1401
        self._check_default_prompt('SSH %(user)s@%(host)s:%(port)d password: ',
 
1619
1403
        # SMTP port handling is a bit special (it's handled if embedded in the
 
1621
1405
        # FIXME: should we: forbid that, extend it to other schemes, leave
 
1622
1406
        # things as they are that's fine thank you ?
 
1623
 
        self._check_default_password_prompt('SMTP %(user)s@%(host)s password: ',
 
1625
 
        self._check_default_password_prompt('SMTP %(user)s@%(host)s password: ',
 
1626
 
                                            'smtp', host='bar.org:10025')
 
1627
 
        self._check_default_password_prompt(
 
 
1407
        self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
 
 
1409
        self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
 
 
1410
                                   'smtp', host='bar.org:10025')
 
 
1411
        self._check_default_prompt(
 
1628
1412
            'SMTP %(user)s@%(host)s:%(port)d password: ',
 
1629
1413
            'smtp', port=10025)
 
1631
 
    def test_ssh_password_emits_warning(self):
 
1632
 
        conf = config.AuthenticationConfig(_file=StringIO(
 
1640
 
        entered_password = 'typed-by-hand'
 
1641
 
        stdout = tests.StringIOWrapper()
 
1642
 
        stderr = tests.StringIOWrapper()
 
1643
 
        ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
 
1644
 
                                            stdout=stdout, stderr=stderr)
 
1646
 
        # Since the password defined in the authentication config is ignored,
 
1647
 
        # the user is prompted
 
1648
 
        self.assertEquals(entered_password,
 
1649
 
                          conf.get_password('ssh', 'bar.org', user='jim'))
 
1650
 
        self.assertContainsRe(
 
1652
 
            'password ignored in section \[ssh with password\]')
 
1654
 
    def test_ssh_without_password_doesnt_emit_warning(self):
 
1655
 
        conf = config.AuthenticationConfig(_file=StringIO(
 
1662
 
        entered_password = 'typed-by-hand'
 
1663
 
        stdout = tests.StringIOWrapper()
 
1664
 
        stderr = tests.StringIOWrapper()
 
1665
 
        ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
 
1669
 
        # Since the password defined in the authentication config is ignored,
 
1670
 
        # the user is prompted
 
1671
 
        self.assertEquals(entered_password,
 
1672
 
                          conf.get_password('ssh', 'bar.org', user='jim'))
 
1673
 
        # No warning shoud be emitted since there is no password. We are only
 
1675
 
        self.assertNotContainsRe(
 
1677
 
            'password ignored in section \[ssh with password\]')
 
1679
 
    def test_uses_fallback_stores(self):
 
1680
 
        self.overrideAttr(config, 'credential_store_registry',
 
1681
 
                          config.CredentialStoreRegistry())
 
1682
 
        store = StubCredentialStore()
 
1683
 
        store.add_credentials("http", "example.com", "joe", "secret")
 
1684
 
        config.credential_store_registry.register("stub", store, fallback=True)
 
1685
 
        conf = config.AuthenticationConfig(_file=StringIO())
 
1686
 
        creds = conf.get_credentials("http", "example.com")
 
1687
 
        self.assertEquals("joe", creds["user"])
 
1688
 
        self.assertEquals("secret", creds["password"])
 
1691
 
class StubCredentialStore(config.CredentialStore):
 
1697
 
    def add_credentials(self, scheme, host, user, password=None):
 
1698
 
        self._username[(scheme, host)] = user
 
1699
 
        self._password[(scheme, host)] = password
 
1701
 
    def get_credentials(self, scheme, host, port=None, user=None,
 
1702
 
        path=None, realm=None):
 
1703
 
        key = (scheme, host)
 
1704
 
        if not key in self._username:
 
1706
 
        return { "scheme": scheme, "host": host, "port": port,
 
1707
 
                "user": self._username[key], "password": self._password[key]}
 
1710
 
class CountingCredentialStore(config.CredentialStore):
 
1715
 
    def get_credentials(self, scheme, host, port=None, user=None,
 
1716
 
        path=None, realm=None):
 
1721
 
class TestCredentialStoreRegistry(tests.TestCase):
 
1723
 
    def _get_cs_registry(self):
 
1724
 
        return config.credential_store_registry
 
1726
 
    def test_default_credential_store(self):
 
1727
 
        r = self._get_cs_registry()
 
1728
 
        default = r.get_credential_store(None)
 
1729
 
        self.assertIsInstance(default, config.PlainTextCredentialStore)
 
1731
 
    def test_unknown_credential_store(self):
 
1732
 
        r = self._get_cs_registry()
 
1733
 
        # It's hard to imagine someone creating a credential store named
 
1734
 
        # 'unknown' so we use that as an never registered key.
 
1735
 
        self.assertRaises(KeyError, r.get_credential_store, 'unknown')
 
1737
 
    def test_fallback_none_registered(self):
 
1738
 
        r = config.CredentialStoreRegistry()
 
1739
 
        self.assertEquals(None,
 
1740
 
                          r.get_fallback_credentials("http", "example.com"))
 
1742
 
    def test_register(self):
 
1743
 
        r = config.CredentialStoreRegistry()
 
1744
 
        r.register("stub", StubCredentialStore(), fallback=False)
 
1745
 
        r.register("another", StubCredentialStore(), fallback=True)
 
1746
 
        self.assertEquals(["another", "stub"], r.keys())
 
1748
 
    def test_register_lazy(self):
 
1749
 
        r = config.CredentialStoreRegistry()
 
1750
 
        r.register_lazy("stub", "bzrlib.tests.test_config",
 
1751
 
                        "StubCredentialStore", fallback=False)
 
1752
 
        self.assertEquals(["stub"], r.keys())
 
1753
 
        self.assertIsInstance(r.get_credential_store("stub"),
 
1754
 
                              StubCredentialStore)
 
1756
 
    def test_is_fallback(self):
 
1757
 
        r = config.CredentialStoreRegistry()
 
1758
 
        r.register("stub1", None, fallback=False)
 
1759
 
        r.register("stub2", None, fallback=True)
 
1760
 
        self.assertEquals(False, r.is_fallback("stub1"))
 
1761
 
        self.assertEquals(True, r.is_fallback("stub2"))
 
1763
 
    def test_no_fallback(self):
 
1764
 
        r = config.CredentialStoreRegistry()
 
1765
 
        store = CountingCredentialStore()
 
1766
 
        r.register("count", store, fallback=False)
 
1767
 
        self.assertEquals(None,
 
1768
 
                          r.get_fallback_credentials("http", "example.com"))
 
1769
 
        self.assertEquals(0, store._calls)
 
1771
 
    def test_fallback_credentials(self):
 
1772
 
        r = config.CredentialStoreRegistry()
 
1773
 
        store = StubCredentialStore()
 
1774
 
        store.add_credentials("http", "example.com",
 
1775
 
                              "somebody", "geheim")
 
1776
 
        r.register("stub", store, fallback=True)
 
1777
 
        creds = r.get_fallback_credentials("http", "example.com")
 
1778
 
        self.assertEquals("somebody", creds["user"])
 
1779
 
        self.assertEquals("geheim", creds["password"])
 
1781
 
    def test_fallback_first_wins(self):
 
1782
 
        r = config.CredentialStoreRegistry()
 
1783
 
        stub1 = StubCredentialStore()
 
1784
 
        stub1.add_credentials("http", "example.com",
 
1785
 
                              "somebody", "stub1")
 
1786
 
        r.register("stub1", stub1, fallback=True)
 
1787
 
        stub2 = StubCredentialStore()
 
1788
 
        stub2.add_credentials("http", "example.com",
 
1789
 
                              "somebody", "stub2")
 
1790
 
        r.register("stub2", stub1, fallback=True)
 
1791
 
        creds = r.get_fallback_credentials("http", "example.com")
 
1792
 
        self.assertEquals("somebody", creds["user"])
 
1793
 
        self.assertEquals("stub1", creds["password"])
 
1796
 
class TestPlainTextCredentialStore(tests.TestCase):
 
1798
 
    def test_decode_password(self):
 
1799
 
        r = config.credential_store_registry
 
1800
 
        plain_text = r.get_credential_store()
 
1801
 
        decoded = plain_text.decode_password(dict(password='secret'))
 
1802
 
        self.assertEquals('secret', decoded)
 
1805
1416
# FIXME: Once we have a way to declare authentication to all test servers, we
 
1806
1417
# can implement generic tests.