1263
1041
self.assertEqual(value, 'value3-top')
1264
1042
value = tree_config.get_option('key3', 'SECTION')
1265
1043
self.assertEqual(value, 'value3-section')
1268
class TestTransportConfig(tests.TestCaseWithTransport):
1270
def test_get_value(self):
1271
"""Test that retreiving a value from a section is possible"""
1272
bzrdir_config = config.TransportConfig(transport.get_transport('.'),
1274
bzrdir_config.set_option('value', 'key', 'SECTION')
1275
bzrdir_config.set_option('value2', 'key2')
1276
bzrdir_config.set_option('value3-top', 'key3')
1277
bzrdir_config.set_option('value3-section', 'key3', 'SECTION')
1278
value = bzrdir_config.get_option('key', 'SECTION')
1279
self.assertEqual(value, 'value')
1280
value = bzrdir_config.get_option('key2')
1281
self.assertEqual(value, 'value2')
1282
self.assertEqual(bzrdir_config.get_option('non-existant'), None)
1283
value = bzrdir_config.get_option('non-existant', 'SECTION')
1284
self.assertEqual(value, None)
1285
value = bzrdir_config.get_option('non-existant', default='default')
1286
self.assertEqual(value, 'default')
1287
self.assertEqual(bzrdir_config.get_option('key2', 'NOSECTION'), None)
1288
value = bzrdir_config.get_option('key2', 'NOSECTION',
1290
self.assertEqual(value, 'default')
1291
value = bzrdir_config.get_option('key3')
1292
self.assertEqual(value, 'value3-top')
1293
value = bzrdir_config.get_option('key3', 'SECTION')
1294
self.assertEqual(value, 'value3-section')
1296
def test_set_unset_default_stack_on(self):
1297
my_dir = self.make_bzrdir('.')
1298
bzrdir_config = config.BzrDirConfig(my_dir)
1299
self.assertIs(None, bzrdir_config.get_default_stack_on())
1300
bzrdir_config.set_default_stack_on('Foo')
1301
self.assertEqual('Foo', bzrdir_config._config.get_option(
1302
'default_stack_on'))
1303
self.assertEqual('Foo', bzrdir_config.get_default_stack_on())
1304
bzrdir_config.set_default_stack_on(None)
1305
self.assertIs(None, bzrdir_config.get_default_stack_on())
1308
class TestAuthenticationConfigFile(tests.TestCase):
1309
"""Test the authentication.conf file matching"""
1311
def _got_user_passwd(self, expected_user, expected_password,
1312
config, *args, **kwargs):
1313
credentials = config.get_credentials(*args, **kwargs)
1314
if credentials is None:
1318
user = credentials['user']
1319
password = credentials['password']
1320
self.assertEquals(expected_user, user)
1321
self.assertEquals(expected_password, password)
1323
def test_empty_config(self):
1324
conf = config.AuthenticationConfig(_file=StringIO())
1325
self.assertEquals({}, conf._get_config())
1326
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1328
def test_missing_auth_section_header(self):
1329
conf = config.AuthenticationConfig(_file=StringIO('foo = bar'))
1330
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1332
def test_auth_section_header_not_closed(self):
1333
conf = config.AuthenticationConfig(_file=StringIO('[DEF'))
1334
self.assertRaises(errors.ParseConfigError, conf._get_config)
1336
def test_auth_value_not_boolean(self):
1337
conf = config.AuthenticationConfig(_file=StringIO(
1341
verify_certificates=askme # Error: Not a boolean
1343
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1345
def test_auth_value_not_int(self):
1346
conf = config.AuthenticationConfig(_file=StringIO(
1350
port=port # Error: Not an int
1352
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1354
def test_unknown_password_encoding(self):
1355
conf = config.AuthenticationConfig(_file=StringIO(
1359
password_encoding=unknown
1361
self.assertRaises(ValueError, conf.get_password,
1362
'ftp', 'foo.net', 'joe')
1364
def test_credentials_for_scheme_host(self):
1365
conf = config.AuthenticationConfig(_file=StringIO(
1366
"""# Identity on foo.net
1371
password=secret-pass
1374
self._got_user_passwd('joe', 'secret-pass', conf, 'ftp', 'foo.net')
1376
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1378
self._got_user_passwd(None, None, conf, 'ftp', 'bar.net')
1380
def test_credentials_for_host_port(self):
1381
conf = config.AuthenticationConfig(_file=StringIO(
1382
"""# Identity on foo.net
1388
password=secret-pass
1391
self._got_user_passwd('joe', 'secret-pass',
1392
conf, 'ftp', 'foo.net', port=10021)
1394
self._got_user_passwd(None, None, conf, 'ftp', 'foo.net')
1396
def test_for_matching_host(self):
1397
conf = config.AuthenticationConfig(_file=StringIO(
1398
"""# Identity on foo.net
1404
[sourceforge domain]
1411
self._got_user_passwd('georges', 'bendover',
1412
conf, 'bzr', 'foo.bzr.sf.net')
1414
self._got_user_passwd(None, None,
1415
conf, 'bzr', 'bbzr.sf.net')
1417
def test_for_matching_host_None(self):
1418
conf = config.AuthenticationConfig(_file=StringIO(
1419
"""# Identity on foo.net
1429
self._got_user_passwd('joe', 'joepass',
1430
conf, 'bzr', 'quux.net')
1431
# no host but different scheme
1432
self._got_user_passwd('georges', 'bendover',
1433
conf, 'ftp', 'quux.net')
1435
def test_credentials_for_path(self):
1436
conf = config.AuthenticationConfig(_file=StringIO(
1452
self._got_user_passwd(None, None,
1453
conf, 'http', host='bar.org', path='/dir3')
1455
self._got_user_passwd('georges', 'bendover',
1456
conf, 'http', host='bar.org', path='/dir2')
1458
self._got_user_passwd('jim', 'jimpass',
1459
conf, 'http', host='bar.org',path='/dir1/subdir')
1461
def test_credentials_for_user(self):
1462
conf = config.AuthenticationConfig(_file=StringIO(
1471
self._got_user_passwd('jim', 'jimpass',
1472
conf, 'http', 'bar.org')
1474
self._got_user_passwd('jim', 'jimpass',
1475
conf, 'http', 'bar.org', user='jim')
1476
# Don't get a different user if one is specified
1477
self._got_user_passwd(None, None,
1478
conf, 'http', 'bar.org', user='georges')
1480
def test_credentials_for_user_without_password(self):
1481
conf = config.AuthenticationConfig(_file=StringIO(
1488
# Get user but no password
1489
self._got_user_passwd('jim', None,
1490
conf, 'http', 'bar.org')
1492
def test_verify_certificates(self):
1493
conf = config.AuthenticationConfig(_file=StringIO(
1500
verify_certificates=False
1507
credentials = conf.get_credentials('https', 'bar.org')
1508
self.assertEquals(False, credentials.get('verify_certificates'))
1509
credentials = conf.get_credentials('https', 'foo.net')
1510
self.assertEquals(True, credentials.get('verify_certificates'))
1513
class TestAuthenticationStorage(tests.TestCaseInTempDir):
1515
def test_set_credentials(self):
1516
conf = config.AuthenticationConfig()
1517
conf.set_credentials('name', 'host', 'user', 'scheme', 'password',
1518
99, path='/foo', verify_certificates=False, realm='realm')
1519
credentials = conf.get_credentials(host='host', scheme='scheme',
1520
port=99, path='/foo',
1522
CREDENTIALS = {'name': 'name', 'user': 'user', 'password': 'password',
1523
'verify_certificates': False, 'scheme': 'scheme',
1524
'host': 'host', 'port': 99, 'path': '/foo',
1526
self.assertEqual(CREDENTIALS, credentials)
1527
credentials_from_disk = config.AuthenticationConfig().get_credentials(
1528
host='host', scheme='scheme', port=99, path='/foo', realm='realm')
1529
self.assertEqual(CREDENTIALS, credentials_from_disk)
1531
def test_reset_credentials_different_name(self):
1532
conf = config.AuthenticationConfig()
1533
conf.set_credentials('name', 'host', 'user', 'scheme', 'password'),
1534
conf.set_credentials('name2', 'host', 'user2', 'scheme', 'password'),
1535
self.assertIs(None, conf._get_config().get('name'))
1536
credentials = conf.get_credentials(host='host', scheme='scheme')
1537
CREDENTIALS = {'name': 'name2', 'user': 'user2', 'password':
1538
'password', 'verify_certificates': True,
1539
'scheme': 'scheme', 'host': 'host', 'port': None,
1540
'path': None, 'realm': None}
1541
self.assertEqual(CREDENTIALS, credentials)
1544
class TestAuthenticationConfig(tests.TestCase):
1545
"""Test AuthenticationConfig behaviour"""
1547
def _check_default_password_prompt(self, expected_prompt_format, scheme,
1548
host=None, port=None, realm=None,
1552
user, password = 'jim', 'precious'
1553
expected_prompt = expected_prompt_format % {
1554
'scheme': scheme, 'host': host, 'port': port,
1555
'user': user, 'realm': realm}
1557
stdout = tests.StringIOWrapper()
1558
stderr = tests.StringIOWrapper()
1559
ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
1560
stdout=stdout, stderr=stderr)
1561
# We use an empty conf so that the user is always prompted
1562
conf = config.AuthenticationConfig()
1563
self.assertEquals(password,
1564
conf.get_password(scheme, host, user, port=port,
1565
realm=realm, path=path))
1566
self.assertEquals(expected_prompt, stderr.getvalue())
1567
self.assertEquals('', stdout.getvalue())
1569
def _check_default_username_prompt(self, expected_prompt_format, scheme,
1570
host=None, port=None, realm=None,
1575
expected_prompt = expected_prompt_format % {
1576
'scheme': scheme, 'host': host, 'port': port,
1578
stdout = tests.StringIOWrapper()
1579
stderr = tests.StringIOWrapper()
1580
ui.ui_factory = tests.TestUIFactory(stdin=username+ '\n',
1581
stdout=stdout, stderr=stderr)
1582
# We use an empty conf so that the user is always prompted
1583
conf = config.AuthenticationConfig()
1584
self.assertEquals(username, conf.get_user(scheme, host, port=port,
1585
realm=realm, path=path, ask=True))
1586
self.assertEquals(expected_prompt, stderr.getvalue())
1587
self.assertEquals('', stdout.getvalue())
1589
def test_username_defaults_prompts(self):
1590
# HTTP prompts can't be tested here, see test_http.py
1591
self._check_default_username_prompt('FTP %(host)s username: ', 'ftp')
1592
self._check_default_username_prompt(
1593
'FTP %(host)s:%(port)d username: ', 'ftp', port=10020)
1594
self._check_default_username_prompt(
1595
'SSH %(host)s:%(port)d username: ', 'ssh', port=12345)
1597
def test_username_default_no_prompt(self):
1598
conf = config.AuthenticationConfig()
1599
self.assertEquals(None,
1600
conf.get_user('ftp', 'example.com'))
1601
self.assertEquals("explicitdefault",
1602
conf.get_user('ftp', 'example.com', default="explicitdefault"))
1604
def test_password_default_prompts(self):
1605
# HTTP prompts can't be tested here, see test_http.py
1606
self._check_default_password_prompt(
1607
'FTP %(user)s@%(host)s password: ', 'ftp')
1608
self._check_default_password_prompt(
1609
'FTP %(user)s@%(host)s:%(port)d password: ', 'ftp', port=10020)
1610
self._check_default_password_prompt(
1611
'SSH %(user)s@%(host)s:%(port)d password: ', 'ssh', port=12345)
1612
# SMTP port handling is a bit special (it's handled if embedded in the
1614
# FIXME: should we: forbid that, extend it to other schemes, leave
1615
# things as they are that's fine thank you ?
1616
self._check_default_password_prompt('SMTP %(user)s@%(host)s password: ',
1618
self._check_default_password_prompt('SMTP %(user)s@%(host)s password: ',
1619
'smtp', host='bar.org:10025')
1620
self._check_default_password_prompt(
1621
'SMTP %(user)s@%(host)s:%(port)d password: ',
1624
def test_ssh_password_emits_warning(self):
1625
conf = config.AuthenticationConfig(_file=StringIO(
1633
entered_password = 'typed-by-hand'
1634
stdout = tests.StringIOWrapper()
1635
stderr = tests.StringIOWrapper()
1636
ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1637
stdout=stdout, stderr=stderr)
1639
# Since the password defined in the authentication config is ignored,
1640
# the user is prompted
1641
self.assertEquals(entered_password,
1642
conf.get_password('ssh', 'bar.org', user='jim'))
1643
self.assertContainsRe(
1645
'password ignored in section \[ssh with password\]')
1647
def test_ssh_without_password_doesnt_emit_warning(self):
1648
conf = config.AuthenticationConfig(_file=StringIO(
1655
entered_password = 'typed-by-hand'
1656
stdout = tests.StringIOWrapper()
1657
stderr = tests.StringIOWrapper()
1658
ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1662
# Since the password defined in the authentication config is ignored,
1663
# the user is prompted
1664
self.assertEquals(entered_password,
1665
conf.get_password('ssh', 'bar.org', user='jim'))
1666
# No warning shoud be emitted since there is no password. We are only
1668
self.assertNotContainsRe(
1670
'password ignored in section \[ssh with password\]')
1672
def test_uses_fallback_stores(self):
1673
self._old_cs_registry = config.credential_store_registry
1675
config.credential_store_registry = self._old_cs_registry
1676
self.addCleanup(restore)
1677
config.credential_store_registry = config.CredentialStoreRegistry()
1678
store = StubCredentialStore()
1679
store.add_credentials("http", "example.com", "joe", "secret")
1680
config.credential_store_registry.register("stub", store, fallback=True)
1681
conf = config.AuthenticationConfig(_file=StringIO())
1682
creds = conf.get_credentials("http", "example.com")
1683
self.assertEquals("joe", creds["user"])
1684
self.assertEquals("secret", creds["password"])
1687
class StubCredentialStore(config.CredentialStore):
1693
def add_credentials(self, scheme, host, user, password=None):
1694
self._username[(scheme, host)] = user
1695
self._password[(scheme, host)] = password
1697
def get_credentials(self, scheme, host, port=None, user=None,
1698
path=None, realm=None):
1699
key = (scheme, host)
1700
if not key in self._username:
1702
return { "scheme": scheme, "host": host, "port": port,
1703
"user": self._username[key], "password": self._password[key]}
1706
class CountingCredentialStore(config.CredentialStore):
1711
def get_credentials(self, scheme, host, port=None, user=None,
1712
path=None, realm=None):
1717
class TestCredentialStoreRegistry(tests.TestCase):
1719
def _get_cs_registry(self):
1720
return config.credential_store_registry
1722
def test_default_credential_store(self):
1723
r = self._get_cs_registry()
1724
default = r.get_credential_store(None)
1725
self.assertIsInstance(default, config.PlainTextCredentialStore)
1727
def test_unknown_credential_store(self):
1728
r = self._get_cs_registry()
1729
# It's hard to imagine someone creating a credential store named
1730
# 'unknown' so we use that as an never registered key.
1731
self.assertRaises(KeyError, r.get_credential_store, 'unknown')
1733
def test_fallback_none_registered(self):
1734
r = config.CredentialStoreRegistry()
1735
self.assertEquals(None,
1736
r.get_fallback_credentials("http", "example.com"))
1738
def test_register(self):
1739
r = config.CredentialStoreRegistry()
1740
r.register("stub", StubCredentialStore(), fallback=False)
1741
r.register("another", StubCredentialStore(), fallback=True)
1742
self.assertEquals(["another", "stub"], r.keys())
1744
def test_register_lazy(self):
1745
r = config.CredentialStoreRegistry()
1746
r.register_lazy("stub", "bzrlib.tests.test_config",
1747
"StubCredentialStore", fallback=False)
1748
self.assertEquals(["stub"], r.keys())
1749
self.assertIsInstance(r.get_credential_store("stub"),
1750
StubCredentialStore)
1752
def test_is_fallback(self):
1753
r = config.CredentialStoreRegistry()
1754
r.register("stub1", None, fallback=False)
1755
r.register("stub2", None, fallback=True)
1756
self.assertEquals(False, r.is_fallback("stub1"))
1757
self.assertEquals(True, r.is_fallback("stub2"))
1759
def test_no_fallback(self):
1760
r = config.CredentialStoreRegistry()
1761
store = CountingCredentialStore()
1762
r.register("count", store, fallback=False)
1763
self.assertEquals(None,
1764
r.get_fallback_credentials("http", "example.com"))
1765
self.assertEquals(0, store._calls)
1767
def test_fallback_credentials(self):
1768
r = config.CredentialStoreRegistry()
1769
store = StubCredentialStore()
1770
store.add_credentials("http", "example.com",
1771
"somebody", "geheim")
1772
r.register("stub", store, fallback=True)
1773
creds = r.get_fallback_credentials("http", "example.com")
1774
self.assertEquals("somebody", creds["user"])
1775
self.assertEquals("geheim", creds["password"])
1777
def test_fallback_first_wins(self):
1778
r = config.CredentialStoreRegistry()
1779
stub1 = StubCredentialStore()
1780
stub1.add_credentials("http", "example.com",
1781
"somebody", "stub1")
1782
r.register("stub1", stub1, fallback=True)
1783
stub2 = StubCredentialStore()
1784
stub2.add_credentials("http", "example.com",
1785
"somebody", "stub2")
1786
r.register("stub2", stub1, fallback=True)
1787
creds = r.get_fallback_credentials("http", "example.com")
1788
self.assertEquals("somebody", creds["user"])
1789
self.assertEquals("stub1", creds["password"])
1792
class TestPlainTextCredentialStore(tests.TestCase):
1794
def test_decode_password(self):
1795
r = config.credential_store_registry
1796
plain_text = r.get_credential_store()
1797
decoded = plain_text.decode_password(dict(password='secret'))
1798
self.assertEquals('secret', decoded)
1801
# FIXME: Once we have a way to declare authentication to all test servers, we
1802
# can implement generic tests.
1803
# test_user_password_in_url
1804
# test_user_in_url_password_from_config
1805
# test_user_in_url_password_prompted
1806
# test_user_in_config
1807
# test_user_getpass.getuser
1808
# test_user_prompted ?
1809
class TestAuthenticationRing(tests.TestCaseWithTransport):