1478
1479
realm=realm, path=path))
1479
1480
self.assertEquals(stdout.getvalue(), expected_prompt)
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: ',
1487
self._check_default_prompt('SSH %(user)s@%(host)s:%(port)d password: ',
1482
def _check_default_username_prompt(self, expected_prompt_format, scheme,
1483
host=None, port=None, realm=None, path=None):
1487
expected_prompt = expected_prompt_format % {
1488
'scheme': scheme, 'host': host, 'port': port,
1490
stdout = tests.StringIOWrapper()
1491
ui.ui_factory = tests.TestUIFactory(stdin=username+ '\n',
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)
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)
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"))
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
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: ',
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: ',
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)
1543
1576
self._get_log(keep_log_file=True),
1544
1577
'password ignored in section \[ssh with password\]')
1579
def test_uses_fallback_stores(self):
1580
self._old_cs_registry = config.credential_store_registry
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"])
1594
class StubCredentialStore(config.CredentialStore):
1600
def add_credentials(self, scheme, host, user, password=None):
1601
self._username[(scheme, host)] = user
1602
self._password[(scheme, host)] = password
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:
1609
return { "scheme": scheme, "host": host, "port": port,
1610
"user": self._username[key], "password": self._password[key]}
1613
class CountingCredentialStore(config.CredentialStore):
1618
def get_credentials(self, scheme, host, port=None, user=None,
1619
path=None, realm=None):
1547
1624
class TestCredentialStoreRegistry(tests.TestCase):
1560
1637
# 'unknown' so we use that as an never registered key.
1561
1638
self.assertRaises(KeyError, r.get_credential_store, 'unknown')
1640
def test_fallback_none_registered(self):
1641
r = config.CredentialStoreRegistry()
1642
self.assertEquals(None,
1643
r.get_fallback_credentials("http", "example.com"))
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())
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)
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"))
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)
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"])
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"])
1564
1699
class TestPlainTextCredentialStore(tests.TestCase):