1401
1443
self.assertEquals(True, credentials.get('verify_certificates'))
1446
class TestAuthenticationStorage(tests.TestCaseInTempDir):
1448
def test_set_credentials(self):
1449
conf = config.AuthenticationConfig()
1450
conf.set_credentials('name', 'host', 'user', 'scheme', 'password',
1451
99, path='/foo', verify_certificates=False, realm='realm')
1452
credentials = conf.get_credentials(host='host', scheme='scheme',
1453
port=99, path='/foo',
1455
CREDENTIALS = {'name': 'name', 'user': 'user', 'password': 'password',
1456
'verify_certificates': False, 'scheme': 'scheme',
1457
'host': 'host', 'port': 99, 'path': '/foo',
1459
self.assertEqual(CREDENTIALS, credentials)
1460
credentials_from_disk = config.AuthenticationConfig().get_credentials(
1461
host='host', scheme='scheme', port=99, path='/foo', realm='realm')
1462
self.assertEqual(CREDENTIALS, credentials_from_disk)
1464
def test_reset_credentials_different_name(self):
1465
conf = config.AuthenticationConfig()
1466
conf.set_credentials('name', 'host', 'user', 'scheme', 'password'),
1467
conf.set_credentials('name2', 'host', 'user2', 'scheme', 'password'),
1468
self.assertIs(None, conf._get_config().get('name'))
1469
credentials = conf.get_credentials(host='host', scheme='scheme')
1470
CREDENTIALS = {'name': 'name2', 'user': 'user2', 'password':
1471
'password', 'verify_certificates': True,
1472
'scheme': 'scheme', 'host': 'host', 'port': None,
1473
'path': None, 'realm': None}
1474
self.assertEqual(CREDENTIALS, credentials)
1404
1477
class TestAuthenticationConfig(tests.TestCase):
1405
1478
"""Test AuthenticationConfig behaviour"""
1407
def _check_default_prompt(self, expected_prompt_format, scheme,
1408
host=None, port=None, realm=None, path=None):
1480
def _check_default_password_prompt(self, expected_prompt_format, scheme,
1481
host=None, port=None, realm=None,
1409
1483
if host is None:
1410
1484
host = 'bar.org'
1411
1485
user, password = 'jim', 'precious'
1414
1488
'user': user, 'realm': realm}
1416
1490
stdout = tests.StringIOWrapper()
1491
stderr = tests.StringIOWrapper()
1417
1492
ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
1493
stdout=stdout, stderr=stderr)
1419
1494
# We use an empty conf so that the user is always prompted
1420
1495
conf = config.AuthenticationConfig()
1421
1496
self.assertEquals(password,
1422
1497
conf.get_password(scheme, host, user, port=port,
1423
1498
realm=realm, path=path))
1424
self.assertEquals(stdout.getvalue(), expected_prompt)
1426
def test_default_prompts(self):
1427
# HTTP prompts can't be tested here, see test_http.py
1428
self._check_default_prompt('FTP %(user)s@%(host)s password: ', 'ftp')
1429
self._check_default_prompt('FTP %(user)s@%(host)s:%(port)d password: ',
1432
self._check_default_prompt('SSH %(user)s@%(host)s:%(port)d password: ',
1499
self.assertEquals(expected_prompt, stderr.getvalue())
1500
self.assertEquals('', stdout.getvalue())
1502
def _check_default_username_prompt(self, expected_prompt_format, scheme,
1503
host=None, port=None, realm=None,
1508
expected_prompt = expected_prompt_format % {
1509
'scheme': scheme, 'host': host, 'port': port,
1511
stdout = tests.StringIOWrapper()
1512
stderr = tests.StringIOWrapper()
1513
ui.ui_factory = tests.TestUIFactory(stdin=username+ '\n',
1514
stdout=stdout, stderr=stderr)
1515
# We use an empty conf so that the user is always prompted
1516
conf = config.AuthenticationConfig()
1517
self.assertEquals(username, conf.get_user(scheme, host, port=port,
1518
realm=realm, path=path, ask=True))
1519
self.assertEquals(expected_prompt, stderr.getvalue())
1520
self.assertEquals('', stdout.getvalue())
1522
def test_username_defaults_prompts(self):
1523
# HTTP prompts can't be tested here, see test_http.py
1524
self._check_default_username_prompt('FTP %(host)s username: ', 'ftp')
1525
self._check_default_username_prompt(
1526
'FTP %(host)s:%(port)d username: ', 'ftp', port=10020)
1527
self._check_default_username_prompt(
1528
'SSH %(host)s:%(port)d username: ', 'ssh', port=12345)
1530
def test_username_default_no_prompt(self):
1531
conf = config.AuthenticationConfig()
1532
self.assertEquals(None,
1533
conf.get_user('ftp', 'example.com'))
1534
self.assertEquals("explicitdefault",
1535
conf.get_user('ftp', 'example.com', default="explicitdefault"))
1537
def test_password_default_prompts(self):
1538
# HTTP prompts can't be tested here, see test_http.py
1539
self._check_default_password_prompt(
1540
'FTP %(user)s@%(host)s password: ', 'ftp')
1541
self._check_default_password_prompt(
1542
'FTP %(user)s@%(host)s:%(port)d password: ', 'ftp', port=10020)
1543
self._check_default_password_prompt(
1544
'SSH %(user)s@%(host)s:%(port)d password: ', 'ssh', port=12345)
1434
1545
# SMTP port handling is a bit special (it's handled if embedded in the
1436
1547
# FIXME: should we: forbid that, extend it to other schemes, leave
1437
1548
# things as they are that's fine thank you ?
1438
self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1440
self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1441
'smtp', host='bar.org:10025')
1442
self._check_default_prompt(
1549
self._check_default_password_prompt('SMTP %(user)s@%(host)s password: ',
1551
self._check_default_password_prompt('SMTP %(user)s@%(host)s password: ',
1552
'smtp', host='bar.org:10025')
1553
self._check_default_password_prompt(
1443
1554
'SMTP %(user)s@%(host)s:%(port)d password: ',
1444
1555
'smtp', port=10025)
1488
1602
self._get_log(keep_log_file=True),
1489
1603
'password ignored in section \[ssh with password\]')
1605
def test_uses_fallback_stores(self):
1606
self._old_cs_registry = config.credential_store_registry
1608
config.credential_store_registry = self._old_cs_registry
1609
self.addCleanup(restore)
1610
config.credential_store_registry = config.CredentialStoreRegistry()
1611
store = StubCredentialStore()
1612
store.add_credentials("http", "example.com", "joe", "secret")
1613
config.credential_store_registry.register("stub", store, fallback=True)
1614
conf = config.AuthenticationConfig(_file=StringIO())
1615
creds = conf.get_credentials("http", "example.com")
1616
self.assertEquals("joe", creds["user"])
1617
self.assertEquals("secret", creds["password"])
1620
class StubCredentialStore(config.CredentialStore):
1626
def add_credentials(self, scheme, host, user, password=None):
1627
self._username[(scheme, host)] = user
1628
self._password[(scheme, host)] = password
1630
def get_credentials(self, scheme, host, port=None, user=None,
1631
path=None, realm=None):
1632
key = (scheme, host)
1633
if not key in self._username:
1635
return { "scheme": scheme, "host": host, "port": port,
1636
"user": self._username[key], "password": self._password[key]}
1639
class CountingCredentialStore(config.CredentialStore):
1644
def get_credentials(self, scheme, host, port=None, user=None,
1645
path=None, realm=None):
1650
class TestCredentialStoreRegistry(tests.TestCase):
1652
def _get_cs_registry(self):
1653
return config.credential_store_registry
1655
def test_default_credential_store(self):
1656
r = self._get_cs_registry()
1657
default = r.get_credential_store(None)
1658
self.assertIsInstance(default, config.PlainTextCredentialStore)
1660
def test_unknown_credential_store(self):
1661
r = self._get_cs_registry()
1662
# It's hard to imagine someone creating a credential store named
1663
# 'unknown' so we use that as an never registered key.
1664
self.assertRaises(KeyError, r.get_credential_store, 'unknown')
1666
def test_fallback_none_registered(self):
1667
r = config.CredentialStoreRegistry()
1668
self.assertEquals(None,
1669
r.get_fallback_credentials("http", "example.com"))
1671
def test_register(self):
1672
r = config.CredentialStoreRegistry()
1673
r.register("stub", StubCredentialStore(), fallback=False)
1674
r.register("another", StubCredentialStore(), fallback=True)
1675
self.assertEquals(["another", "stub"], r.keys())
1677
def test_register_lazy(self):
1678
r = config.CredentialStoreRegistry()
1679
r.register_lazy("stub", "bzrlib.tests.test_config",
1680
"StubCredentialStore", fallback=False)
1681
self.assertEquals(["stub"], r.keys())
1682
self.assertIsInstance(r.get_credential_store("stub"),
1683
StubCredentialStore)
1685
def test_is_fallback(self):
1686
r = config.CredentialStoreRegistry()
1687
r.register("stub1", None, fallback=False)
1688
r.register("stub2", None, fallback=True)
1689
self.assertEquals(False, r.is_fallback("stub1"))
1690
self.assertEquals(True, r.is_fallback("stub2"))
1692
def test_no_fallback(self):
1693
r = config.CredentialStoreRegistry()
1694
store = CountingCredentialStore()
1695
r.register("count", store, fallback=False)
1696
self.assertEquals(None,
1697
r.get_fallback_credentials("http", "example.com"))
1698
self.assertEquals(0, store._calls)
1700
def test_fallback_credentials(self):
1701
r = config.CredentialStoreRegistry()
1702
store = StubCredentialStore()
1703
store.add_credentials("http", "example.com",
1704
"somebody", "geheim")
1705
r.register("stub", store, fallback=True)
1706
creds = r.get_fallback_credentials("http", "example.com")
1707
self.assertEquals("somebody", creds["user"])
1708
self.assertEquals("geheim", creds["password"])
1710
def test_fallback_first_wins(self):
1711
r = config.CredentialStoreRegistry()
1712
stub1 = StubCredentialStore()
1713
stub1.add_credentials("http", "example.com",
1714
"somebody", "stub1")
1715
r.register("stub1", stub1, fallback=True)
1716
stub2 = StubCredentialStore()
1717
stub2.add_credentials("http", "example.com",
1718
"somebody", "stub2")
1719
r.register("stub2", stub1, fallback=True)
1720
creds = r.get_fallback_credentials("http", "example.com")
1721
self.assertEquals("somebody", creds["user"])
1722
self.assertEquals("stub1", creds["password"])
1725
class TestPlainTextCredentialStore(tests.TestCase):
1727
def test_decode_password(self):
1728
r = config.credential_store_registry
1729
plain_text = r.get_credential_store()
1730
decoded = plain_text.decode_password(dict(password='secret'))
1731
self.assertEquals('secret', decoded)
1492
1734
# FIXME: Once we have a way to declare authentication to all test servers, we
1493
1735
# can implement generic tests.