1438
1412
self.assertEquals(True, credentials.get('verify_certificates'))
1441
class TestAuthenticationStorage(tests.TestCaseInTempDir):
1443
def test_set_credentials(self):
1444
conf = config.AuthenticationConfig()
1445
conf.set_credentials('name', 'host', 'user', 'scheme', 'password',
1446
99, path='/foo', verify_certificates=False, realm='realm')
1447
credentials = conf.get_credentials(host='host', scheme='scheme',
1448
port=99, path='/foo',
1450
CREDENTIALS = {'name': 'name', 'user': 'user', 'password': 'password',
1451
'verify_certificates': False, 'scheme': 'scheme',
1452
'host': 'host', 'port': 99, 'path': '/foo',
1454
self.assertEqual(CREDENTIALS, credentials)
1455
credentials_from_disk = config.AuthenticationConfig().get_credentials(
1456
host='host', scheme='scheme', port=99, path='/foo', realm='realm')
1457
self.assertEqual(CREDENTIALS, credentials_from_disk)
1459
def test_reset_credentials_different_name(self):
1460
conf = config.AuthenticationConfig()
1461
conf.set_credentials('name', 'host', 'user', 'scheme', 'password'),
1462
conf.set_credentials('name2', 'host', 'user2', 'scheme', 'password'),
1463
self.assertIs(None, conf._get_config().get('name'))
1464
credentials = conf.get_credentials(host='host', scheme='scheme')
1465
CREDENTIALS = {'name': 'name2', 'user': 'user2', 'password':
1466
'password', 'verify_certificates': True,
1467
'scheme': 'scheme', 'host': 'host', 'port': None,
1468
'path': None, 'realm': None}
1469
self.assertEqual(CREDENTIALS, credentials)
1472
1415
class TestAuthenticationConfig(tests.TestCase):
1473
1416
"""Test AuthenticationConfig behaviour"""
1475
def _check_default_password_prompt(self, expected_prompt_format, scheme,
1476
host=None, port=None, realm=None,
1418
def _check_default_prompt(self, expected_prompt_format, scheme,
1419
host=None, port=None, realm=None, path=None):
1478
1420
if host is None:
1479
1421
host = 'bar.org'
1480
1422
user, password = 'jim', 'precious'
1483
1425
'user': user, 'realm': realm}
1485
1427
stdout = tests.StringIOWrapper()
1486
stderr = tests.StringIOWrapper()
1487
1428
ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
1488
stdout=stdout, stderr=stderr)
1489
1430
# We use an empty conf so that the user is always prompted
1490
1431
conf = config.AuthenticationConfig()
1491
1432
self.assertEquals(password,
1492
1433
conf.get_password(scheme, host, user, port=port,
1493
1434
realm=realm, path=path))
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)
1435
self.assertEquals(stdout.getvalue(), expected_prompt)
1437
def test_default_prompts(self):
1438
# HTTP prompts can't be tested here, see test_http.py
1439
self._check_default_prompt('FTP %(user)s@%(host)s password: ', 'ftp')
1440
self._check_default_prompt('FTP %(user)s@%(host)s:%(port)d password: ',
1443
self._check_default_prompt('SSH %(user)s@%(host)s:%(port)d password: ',
1540
1445
# SMTP port handling is a bit special (it's handled if embedded in the
1542
1447
# FIXME: should we: forbid that, extend it to other schemes, leave
1543
1448
# things as they are that's fine thank you ?
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(
1449
self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1451
self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1452
'smtp', host='bar.org:10025')
1453
self._check_default_prompt(
1549
1454
'SMTP %(user)s@%(host)s:%(port)d password: ',
1550
1455
'smtp', port=10025)
1597
1499
self._get_log(keep_log_file=True),
1598
1500
'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):
1645
class TestCredentialStoreRegistry(tests.TestCase):
1647
def _get_cs_registry(self):
1648
return config.credential_store_registry
1650
def test_default_credential_store(self):
1651
r = self._get_cs_registry()
1652
default = r.get_credential_store(None)
1653
self.assertIsInstance(default, config.PlainTextCredentialStore)
1655
def test_unknown_credential_store(self):
1656
r = self._get_cs_registry()
1657
# It's hard to imagine someone creating a credential store named
1658
# 'unknown' so we use that as an never registered key.
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"])
1720
class TestPlainTextCredentialStore(tests.TestCase):
1722
def test_decode_password(self):
1723
r = config.credential_store_registry
1724
plain_text = r.get_credential_store()
1725
decoded = plain_text.decode_password(dict(password='secret'))
1726
self.assertEquals('secret', decoded)
1729
1503
# FIXME: Once we have a way to declare authentication to all test servers, we
1730
1504
# can implement generic tests.