1077
955
def test_config_precedence(self):
1078
956
my_config = self.get_branch_config(global_config=precedence_global)
1079
957
self.assertEqual(my_config.get_user_option('option'), 'global')
1080
my_config = self.get_branch_config(global_config=precedence_global,
958
my_config = self.get_branch_config(global_config=precedence_global,
1081
959
branch_data_config=precedence_branch)
1082
960
self.assertEqual(my_config.get_user_option('option'), 'branch')
1083
my_config = self.get_branch_config(global_config=precedence_global,
961
my_config = self.get_branch_config(global_config=precedence_global,
1084
962
branch_data_config=precedence_branch,
1085
963
location_config=precedence_location)
1086
964
self.assertEqual(my_config.get_user_option('option'), 'recurse')
1087
my_config = self.get_branch_config(global_config=precedence_global,
965
my_config = self.get_branch_config(global_config=precedence_global,
1088
966
branch_data_config=precedence_branch,
1089
967
location_config=precedence_location,
1090
968
location='http://example.com/specific')
1091
969
self.assertEqual(my_config.get_user_option('option'), 'exact')
1093
def test_get_mail_client(self):
1094
config = self.get_branch_config()
1095
client = config.get_mail_client()
1096
self.assertIsInstance(client, mail_client.DefaultMail)
1099
config.set_user_option('mail_client', 'evolution')
1100
client = config.get_mail_client()
1101
self.assertIsInstance(client, mail_client.Evolution)
1103
config.set_user_option('mail_client', 'kmail')
1104
client = config.get_mail_client()
1105
self.assertIsInstance(client, mail_client.KMail)
1107
config.set_user_option('mail_client', 'mutt')
1108
client = config.get_mail_client()
1109
self.assertIsInstance(client, mail_client.Mutt)
1111
config.set_user_option('mail_client', 'thunderbird')
1112
client = config.get_mail_client()
1113
self.assertIsInstance(client, mail_client.Thunderbird)
1116
config.set_user_option('mail_client', 'default')
1117
client = config.get_mail_client()
1118
self.assertIsInstance(client, mail_client.DefaultMail)
1120
config.set_user_option('mail_client', 'editor')
1121
client = config.get_mail_client()
1122
self.assertIsInstance(client, mail_client.Editor)
1124
config.set_user_option('mail_client', 'mapi')
1125
client = config.get_mail_client()
1126
self.assertIsInstance(client, mail_client.MAPIClient)
1128
config.set_user_option('mail_client', 'xdg-email')
1129
client = config.get_mail_client()
1130
self.assertIsInstance(client, mail_client.XDGEmail)
1132
config.set_user_option('mail_client', 'firebird')
1133
self.assertRaises(errors.UnknownMailClient, config.get_mail_client)
1136
class TestMailAddressExtraction(tests.TestCase):
972
class TestMailAddressExtraction(TestCase):
1138
974
def test_extract_email_address(self):
1139
975
self.assertEqual('jane@test.com',
1140
976
config.extract_email_address('Jane <jane@test.com>'))
1141
977
self.assertRaises(errors.NoEmailInUsername,
1142
978
config.extract_email_address, 'Jane Tester')
1144
def test_parse_username(self):
1145
self.assertEqual(('', 'jdoe@example.com'),
1146
config.parse_username('jdoe@example.com'))
1147
self.assertEqual(('', 'jdoe@example.com'),
1148
config.parse_username('<jdoe@example.com>'))
1149
self.assertEqual(('John Doe', 'jdoe@example.com'),
1150
config.parse_username('John Doe <jdoe@example.com>'))
1151
self.assertEqual(('John Doe', ''),
1152
config.parse_username('John Doe'))
1153
self.assertEqual(('John Doe', 'jdoe@example.com'),
1154
config.parse_username('John Doe jdoe@example.com'))
1156
class TestTreeConfig(tests.TestCaseWithTransport):
1158
def test_get_value(self):
1159
"""Test that retreiving a value from a section is possible"""
1160
branch = self.make_branch('.')
1161
tree_config = config.TreeConfig(branch)
1162
tree_config.set_option('value', 'key', 'SECTION')
1163
tree_config.set_option('value2', 'key2')
1164
tree_config.set_option('value3-top', 'key3')
1165
tree_config.set_option('value3-section', 'key3', 'SECTION')
1166
value = tree_config.get_option('key', 'SECTION')
1167
self.assertEqual(value, 'value')
1168
value = tree_config.get_option('key2')
1169
self.assertEqual(value, 'value2')
1170
self.assertEqual(tree_config.get_option('non-existant'), None)
1171
value = tree_config.get_option('non-existant', 'SECTION')
1172
self.assertEqual(value, None)
1173
value = tree_config.get_option('non-existant', default='default')
1174
self.assertEqual(value, 'default')
1175
self.assertEqual(tree_config.get_option('key2', 'NOSECTION'), None)
1176
value = tree_config.get_option('key2', 'NOSECTION', default='default')
1177
self.assertEqual(value, 'default')
1178
value = tree_config.get_option('key3')
1179
self.assertEqual(value, 'value3-top')
1180
value = tree_config.get_option('key3', 'SECTION')
1181
self.assertEqual(value, 'value3-section')
1184
class TestTransportConfig(tests.TestCaseWithTransport):
1186
def test_get_value(self):
1187
"""Test that retreiving a value from a section is possible"""
1188
bzrdir_config = config.TransportConfig(transport.get_transport('.'),
1190
bzrdir_config.set_option('value', 'key', 'SECTION')
1191
bzrdir_config.set_option('value2', 'key2')
1192
bzrdir_config.set_option('value3-top', 'key3')
1193
bzrdir_config.set_option('value3-section', 'key3', 'SECTION')
1194
value = bzrdir_config.get_option('key', 'SECTION')
1195
self.assertEqual(value, 'value')
1196
value = bzrdir_config.get_option('key2')
1197
self.assertEqual(value, 'value2')
1198
self.assertEqual(bzrdir_config.get_option('non-existant'), None)
1199
value = bzrdir_config.get_option('non-existant', 'SECTION')
1200
self.assertEqual(value, None)
1201
value = bzrdir_config.get_option('non-existant', default='default')
1202
self.assertEqual(value, 'default')
1203
self.assertEqual(bzrdir_config.get_option('key2', 'NOSECTION'), None)
1204
value = bzrdir_config.get_option('key2', 'NOSECTION',
1206
self.assertEqual(value, 'default')
1207
value = bzrdir_config.get_option('key3')
1208
self.assertEqual(value, 'value3-top')
1209
value = bzrdir_config.get_option('key3', 'SECTION')
1210
self.assertEqual(value, 'value3-section')
1212
def test_set_unset_default_stack_on(self):
1213
my_dir = self.make_bzrdir('.')
1214
bzrdir_config = config.BzrDirConfig(my_dir)
1215
self.assertIs(None, bzrdir_config.get_default_stack_on())
1216
bzrdir_config.set_default_stack_on('Foo')
1217
self.assertEqual('Foo', bzrdir_config._config.get_option(
1218
'default_stack_on'))
1219
self.assertEqual('Foo', bzrdir_config.get_default_stack_on())
1220
bzrdir_config.set_default_stack_on(None)
1221
self.assertIs(None, bzrdir_config.get_default_stack_on())
1224
class TestAuthenticationConfigFile(tests.TestCase):
1225
"""Test the authentication.conf file matching"""
1227
def _got_user_passwd(self, expected_user, expected_password,
1228
config, *args, **kwargs):
1229
credentials = config.get_credentials(*args, **kwargs)
1230
if credentials is None:
1234
user = credentials['user']
1235
password = credentials['password']
1236
self.assertEquals(expected_user, user)
1237
self.assertEquals(expected_password, password)
1239
def test_empty_config(self):
1240
conf = config.AuthenticationConfig(_file=StringIO())
1241
self.assertEquals({}, conf._get_config())
1242
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1244
def test_missing_auth_section_header(self):
1245
conf = config.AuthenticationConfig(_file=StringIO('foo = bar'))
1246
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1248
def test_auth_section_header_not_closed(self):
1249
conf = config.AuthenticationConfig(_file=StringIO('[DEF'))
1250
self.assertRaises(errors.ParseConfigError, conf._get_config)
1252
def test_auth_value_not_boolean(self):
1253
conf = config.AuthenticationConfig(_file=StringIO(
1257
verify_certificates=askme # Error: Not a boolean
1259
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1261
def test_auth_value_not_int(self):
1262
conf = config.AuthenticationConfig(_file=StringIO(
1266
port=port # Error: Not an int
1268
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1270
def test_unknown_password_encoding(self):
1271
conf = config.AuthenticationConfig(_file=StringIO(
1275
password_encoding=unknown
1277
self.assertRaises(ValueError, conf.get_password,
1278
'ftp', 'foo.net', 'joe')
1280
def test_credentials_for_scheme_host(self):
1281
conf = config.AuthenticationConfig(_file=StringIO(
1282
"""# Identity on foo.net
1287
password=secret-pass
1290
self._got_user_passwd('joe', 'secret-pass', conf, 'ftp', 'foo.net')
1292
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1294
self._got_user_passwd(None, None, conf, 'ftp', 'bar.net')
1296
def test_credentials_for_host_port(self):
1297
conf = config.AuthenticationConfig(_file=StringIO(
1298
"""# Identity on foo.net
1304
password=secret-pass
1307
self._got_user_passwd('joe', 'secret-pass',
1308
conf, 'ftp', 'foo.net', port=10021)
1310
self._got_user_passwd(None, None, conf, 'ftp', 'foo.net')
1312
def test_for_matching_host(self):
1313
conf = config.AuthenticationConfig(_file=StringIO(
1314
"""# Identity on foo.net
1320
[sourceforge domain]
1327
self._got_user_passwd('georges', 'bendover',
1328
conf, 'bzr', 'foo.bzr.sf.net')
1330
self._got_user_passwd(None, None,
1331
conf, 'bzr', 'bbzr.sf.net')
1333
def test_for_matching_host_None(self):
1334
conf = config.AuthenticationConfig(_file=StringIO(
1335
"""# Identity on foo.net
1345
self._got_user_passwd('joe', 'joepass',
1346
conf, 'bzr', 'quux.net')
1347
# no host but different scheme
1348
self._got_user_passwd('georges', 'bendover',
1349
conf, 'ftp', 'quux.net')
1351
def test_credentials_for_path(self):
1352
conf = config.AuthenticationConfig(_file=StringIO(
1368
self._got_user_passwd(None, None,
1369
conf, 'http', host='bar.org', path='/dir3')
1371
self._got_user_passwd('georges', 'bendover',
1372
conf, 'http', host='bar.org', path='/dir2')
1374
self._got_user_passwd('jim', 'jimpass',
1375
conf, 'http', host='bar.org',path='/dir1/subdir')
1377
def test_credentials_for_user(self):
1378
conf = config.AuthenticationConfig(_file=StringIO(
1387
self._got_user_passwd('jim', 'jimpass',
1388
conf, 'http', 'bar.org')
1390
self._got_user_passwd('jim', 'jimpass',
1391
conf, 'http', 'bar.org', user='jim')
1392
# Don't get a different user if one is specified
1393
self._got_user_passwd(None, None,
1394
conf, 'http', 'bar.org', user='georges')
1396
def test_credentials_for_user_without_password(self):
1397
conf = config.AuthenticationConfig(_file=StringIO(
1404
# Get user but no password
1405
self._got_user_passwd('jim', None,
1406
conf, 'http', 'bar.org')
1408
def test_verify_certificates(self):
1409
conf = config.AuthenticationConfig(_file=StringIO(
1416
verify_certificates=False
1423
credentials = conf.get_credentials('https', 'bar.org')
1424
self.assertEquals(False, credentials.get('verify_certificates'))
1425
credentials = conf.get_credentials('https', 'foo.net')
1426
self.assertEquals(True, credentials.get('verify_certificates'))
1429
class TestAuthenticationStorage(tests.TestCaseInTempDir):
1431
def test_set_credentials(self):
1432
conf = config.AuthenticationConfig()
1433
conf.set_credentials('name', 'host', 'user', 'scheme', 'password',
1434
99, path='/foo', verify_certificates=False, realm='realm')
1435
credentials = conf.get_credentials(host='host', scheme='scheme',
1436
port=99, path='/foo',
1438
CREDENTIALS = {'name': 'name', 'user': 'user', 'password': 'password',
1439
'verify_certificates': False, 'scheme': 'scheme',
1440
'host': 'host', 'port': 99, 'path': '/foo',
1442
self.assertEqual(CREDENTIALS, credentials)
1443
credentials_from_disk = config.AuthenticationConfig().get_credentials(
1444
host='host', scheme='scheme', port=99, path='/foo', realm='realm')
1445
self.assertEqual(CREDENTIALS, credentials_from_disk)
1447
def test_reset_credentials_different_name(self):
1448
conf = config.AuthenticationConfig()
1449
conf.set_credentials('name', 'host', 'user', 'scheme', 'password'),
1450
conf.set_credentials('name2', 'host', 'user2', 'scheme', 'password'),
1451
self.assertIs(None, conf._get_config().get('name'))
1452
credentials = conf.get_credentials(host='host', scheme='scheme')
1453
CREDENTIALS = {'name': 'name2', 'user': 'user2', 'password':
1454
'password', 'verify_certificates': True,
1455
'scheme': 'scheme', 'host': 'host', 'port': None,
1456
'path': None, 'realm': None}
1457
self.assertEqual(CREDENTIALS, credentials)
1460
class TestAuthenticationConfig(tests.TestCase):
1461
"""Test AuthenticationConfig behaviour"""
1463
def _check_default_password_prompt(self, expected_prompt_format, scheme,
1464
host=None, port=None, realm=None, path=None):
1467
user, password = 'jim', 'precious'
1468
expected_prompt = expected_prompt_format % {
1469
'scheme': scheme, 'host': host, 'port': port,
1470
'user': user, 'realm': realm}
1472
stdout = tests.StringIOWrapper()
1473
ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
1475
# We use an empty conf so that the user is always prompted
1476
conf = config.AuthenticationConfig()
1477
self.assertEquals(password,
1478
conf.get_password(scheme, host, user, port=port,
1479
realm=realm, path=path))
1480
self.assertEquals(stdout.getvalue(), expected_prompt)
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)
1522
# SMTP port handling is a bit special (it's handled if embedded in the
1524
# FIXME: should we: forbid that, extend it to other schemes, leave
1525
# things as they are that's fine thank you ?
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(
1531
'SMTP %(user)s@%(host)s:%(port)d password: ',
1534
def test_ssh_password_emits_warning(self):
1535
conf = config.AuthenticationConfig(_file=StringIO(
1543
entered_password = 'typed-by-hand'
1544
stdout = tests.StringIOWrapper()
1545
ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1548
# Since the password defined in the authentication config is ignored,
1549
# the user is prompted
1550
self.assertEquals(entered_password,
1551
conf.get_password('ssh', 'bar.org', user='jim'))
1552
self.assertContainsRe(
1553
self._get_log(keep_log_file=True),
1554
'password ignored in section \[ssh with password\]')
1556
def test_ssh_without_password_doesnt_emit_warning(self):
1557
conf = config.AuthenticationConfig(_file=StringIO(
1564
entered_password = 'typed-by-hand'
1565
stdout = tests.StringIOWrapper()
1566
ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1569
# Since the password defined in the authentication config is ignored,
1570
# the user is prompted
1571
self.assertEquals(entered_password,
1572
conf.get_password('ssh', 'bar.org', user='jim'))
1573
# No warning shoud be emitted since there is no password. We are only
1575
self.assertNotContainsRe(
1576
self._get_log(keep_log_file=True),
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):
1624
class TestCredentialStoreRegistry(tests.TestCase):
1626
def _get_cs_registry(self):
1627
return config.credential_store_registry
1629
def test_default_credential_store(self):
1630
r = self._get_cs_registry()
1631
default = r.get_credential_store(None)
1632
self.assertIsInstance(default, config.PlainTextCredentialStore)
1634
def test_unknown_credential_store(self):
1635
r = self._get_cs_registry()
1636
# It's hard to imagine someone creating a credential store named
1637
# 'unknown' so we use that as an never registered key.
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"])
1699
class TestPlainTextCredentialStore(tests.TestCase):
1701
def test_decode_password(self):
1702
r = config.credential_store_registry
1703
plain_text = r.get_credential_store()
1704
decoded = plain_text.decode_password(dict(password='secret'))
1705
self.assertEquals('secret', decoded)
1708
# FIXME: Once we have a way to declare authentication to all test servers, we
1709
# can implement generic tests.
1710
# test_user_password_in_url
1711
# test_user_in_url_password_from_config
1712
# test_user_in_url_password_prompted
1713
# test_user_in_config
1714
# test_user_getpass.getuser
1715
# test_user_prompted ?
1716
class TestAuthenticationRing(tests.TestCaseWithTransport):