364
366
parser = my_config._get_parser(file=config_file)
365
367
self.failUnless(my_config._get_parser() is parser)
369
def test_get_user_option_as_bool(self):
370
config_file = StringIO("""
373
an_invalid_bool = maybe
374
a_list = hmm, who knows ? # This interpreted as a list !
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'))
368
384
class TestGetConfig(tests.TestCase):
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',
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',
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)
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)
1451
1472
class TestAuthenticationConfig(tests.TestCase):
1452
1473
"""Test AuthenticationConfig behaviour"""
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,
1456
1478
if host is None:
1457
1479
host = 'bar.org'
1458
1480
user, password = 'jim', 'precious'
1461
1483
'user': user, 'realm': realm}
1463
1485
stdout = tests.StringIOWrapper()
1486
stderr = tests.StringIOWrapper()
1464
1487
ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
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)
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: ',
1479
self._check_default_prompt('SSH %(user)s@%(host)s:%(port)d password: ',
1494
self.assertEquals(expected_prompt, stderr.getvalue())
1495
self.assertEquals('', stdout.getvalue())
1497
def _check_default_username_prompt(self, expected_prompt_format, scheme,
1498
host=None, port=None, realm=None,
1503
expected_prompt = expected_prompt_format % {
1504
'scheme': scheme, 'host': host, 'port': port,
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())
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)
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"))
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
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: ',
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: ',
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)
1535
1597
self._get_log(keep_log_file=True),
1536
1598
'password ignored in section \[ssh with password\]')
1600
def test_uses_fallback_stores(self):
1601
self._old_cs_registry = config.credential_store_registry
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"])
1615
class StubCredentialStore(config.CredentialStore):
1621
def add_credentials(self, scheme, host, user, password=None):
1622
self._username[(scheme, host)] = user
1623
self._password[(scheme, host)] = password
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:
1630
return { "scheme": scheme, "host": host, "port": port,
1631
"user": self._username[key], "password": self._password[key]}
1634
class CountingCredentialStore(config.CredentialStore):
1639
def get_credentials(self, scheme, host, port=None, user=None,
1640
path=None, realm=None):
1539
1645
class TestCredentialStoreRegistry(tests.TestCase):
1552
1658
# 'unknown' so we use that as an never registered key.
1553
1659
self.assertRaises(KeyError, r.get_credential_store, 'unknown')
1661
def test_fallback_none_registered(self):
1662
r = config.CredentialStoreRegistry()
1663
self.assertEquals(None,
1664
r.get_fallback_credentials("http", "example.com"))
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())
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)
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"))
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)
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"])
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"])
1556
1720
class TestPlainTextCredentialStore(tests.TestCase):