821
1066
# post-commit is ignored when bresent in branch data
822
1067
self.assertEqual('bzrlib.tests.test_config.post_commit',
823
1068
my_config.post_commit())
824
my_config.set_user_option('post_commit', 'rmtree_root', local=True)
1069
my_config.set_user_option('post_commit', 'rmtree_root',
1070
store=config.STORE_LOCATION)
825
1071
self.assertEqual('rmtree_root', my_config.post_commit())
827
1073
def test_config_precedence(self):
828
1074
my_config = self.get_branch_config(global_config=precedence_global)
829
1075
self.assertEqual(my_config.get_user_option('option'), 'global')
830
my_config = self.get_branch_config(global_config=precedence_global,
1076
my_config = self.get_branch_config(global_config=precedence_global,
831
1077
branch_data_config=precedence_branch)
832
1078
self.assertEqual(my_config.get_user_option('option'), 'branch')
833
my_config = self.get_branch_config(global_config=precedence_global,
1079
my_config = self.get_branch_config(global_config=precedence_global,
834
1080
branch_data_config=precedence_branch,
835
1081
location_config=precedence_location)
836
1082
self.assertEqual(my_config.get_user_option('option'), 'recurse')
837
my_config = self.get_branch_config(global_config=precedence_global,
1083
my_config = self.get_branch_config(global_config=precedence_global,
838
1084
branch_data_config=precedence_branch,
839
1085
location_config=precedence_location,
840
1086
location='http://example.com/specific')
841
1087
self.assertEqual(my_config.get_user_option('option'), 'exact')
844
class TestMailAddressExtraction(TestCase):
1089
def test_get_mail_client(self):
1090
config = self.get_branch_config()
1091
client = config.get_mail_client()
1092
self.assertIsInstance(client, mail_client.DefaultMail)
1095
config.set_user_option('mail_client', 'evolution')
1096
client = config.get_mail_client()
1097
self.assertIsInstance(client, mail_client.Evolution)
1099
config.set_user_option('mail_client', 'kmail')
1100
client = config.get_mail_client()
1101
self.assertIsInstance(client, mail_client.KMail)
1103
config.set_user_option('mail_client', 'mutt')
1104
client = config.get_mail_client()
1105
self.assertIsInstance(client, mail_client.Mutt)
1107
config.set_user_option('mail_client', 'thunderbird')
1108
client = config.get_mail_client()
1109
self.assertIsInstance(client, mail_client.Thunderbird)
1112
config.set_user_option('mail_client', 'default')
1113
client = config.get_mail_client()
1114
self.assertIsInstance(client, mail_client.DefaultMail)
1116
config.set_user_option('mail_client', 'editor')
1117
client = config.get_mail_client()
1118
self.assertIsInstance(client, mail_client.Editor)
1120
config.set_user_option('mail_client', 'mapi')
1121
client = config.get_mail_client()
1122
self.assertIsInstance(client, mail_client.MAPIClient)
1124
config.set_user_option('mail_client', 'xdg-email')
1125
client = config.get_mail_client()
1126
self.assertIsInstance(client, mail_client.XDGEmail)
1128
config.set_user_option('mail_client', 'firebird')
1129
self.assertRaises(errors.UnknownMailClient, config.get_mail_client)
1132
class TestMailAddressExtraction(tests.TestCase):
846
1134
def test_extract_email_address(self):
847
1135
self.assertEqual('jane@test.com',
848
1136
config.extract_email_address('Jane <jane@test.com>'))
849
1137
self.assertRaises(errors.NoEmailInUsername,
850
1138
config.extract_email_address, 'Jane Tester')
1140
def test_parse_username(self):
1141
self.assertEqual(('', 'jdoe@example.com'),
1142
config.parse_username('jdoe@example.com'))
1143
self.assertEqual(('', 'jdoe@example.com'),
1144
config.parse_username('<jdoe@example.com>'))
1145
self.assertEqual(('John Doe', 'jdoe@example.com'),
1146
config.parse_username('John Doe <jdoe@example.com>'))
1147
self.assertEqual(('John Doe', ''),
1148
config.parse_username('John Doe'))
1149
self.assertEqual(('John Doe', 'jdoe@example.com'),
1150
config.parse_username('John Doe jdoe@example.com'))
1152
class TestTreeConfig(tests.TestCaseWithTransport):
1154
def test_get_value(self):
1155
"""Test that retreiving a value from a section is possible"""
1156
branch = self.make_branch('.')
1157
tree_config = config.TreeConfig(branch)
1158
tree_config.set_option('value', 'key', 'SECTION')
1159
tree_config.set_option('value2', 'key2')
1160
tree_config.set_option('value3-top', 'key3')
1161
tree_config.set_option('value3-section', 'key3', 'SECTION')
1162
value = tree_config.get_option('key', 'SECTION')
1163
self.assertEqual(value, 'value')
1164
value = tree_config.get_option('key2')
1165
self.assertEqual(value, 'value2')
1166
self.assertEqual(tree_config.get_option('non-existant'), None)
1167
value = tree_config.get_option('non-existant', 'SECTION')
1168
self.assertEqual(value, None)
1169
value = tree_config.get_option('non-existant', default='default')
1170
self.assertEqual(value, 'default')
1171
self.assertEqual(tree_config.get_option('key2', 'NOSECTION'), None)
1172
value = tree_config.get_option('key2', 'NOSECTION', default='default')
1173
self.assertEqual(value, 'default')
1174
value = tree_config.get_option('key3')
1175
self.assertEqual(value, 'value3-top')
1176
value = tree_config.get_option('key3', 'SECTION')
1177
self.assertEqual(value, 'value3-section')
1180
class TestTransportConfig(tests.TestCaseWithTransport):
1182
def test_get_value(self):
1183
"""Test that retreiving a value from a section is possible"""
1184
bzrdir_config = config.TransportConfig(transport.get_transport('.'),
1186
bzrdir_config.set_option('value', 'key', 'SECTION')
1187
bzrdir_config.set_option('value2', 'key2')
1188
bzrdir_config.set_option('value3-top', 'key3')
1189
bzrdir_config.set_option('value3-section', 'key3', 'SECTION')
1190
value = bzrdir_config.get_option('key', 'SECTION')
1191
self.assertEqual(value, 'value')
1192
value = bzrdir_config.get_option('key2')
1193
self.assertEqual(value, 'value2')
1194
self.assertEqual(bzrdir_config.get_option('non-existant'), None)
1195
value = bzrdir_config.get_option('non-existant', 'SECTION')
1196
self.assertEqual(value, None)
1197
value = bzrdir_config.get_option('non-existant', default='default')
1198
self.assertEqual(value, 'default')
1199
self.assertEqual(bzrdir_config.get_option('key2', 'NOSECTION'), None)
1200
value = bzrdir_config.get_option('key2', 'NOSECTION',
1202
self.assertEqual(value, 'default')
1203
value = bzrdir_config.get_option('key3')
1204
self.assertEqual(value, 'value3-top')
1205
value = bzrdir_config.get_option('key3', 'SECTION')
1206
self.assertEqual(value, 'value3-section')
1208
def test_set_unset_default_stack_on(self):
1209
my_dir = self.make_bzrdir('.')
1210
bzrdir_config = config.BzrDirConfig(my_dir.transport)
1211
self.assertIs(None, bzrdir_config.get_default_stack_on())
1212
bzrdir_config.set_default_stack_on('Foo')
1213
self.assertEqual('Foo', bzrdir_config._config.get_option(
1214
'default_stack_on'))
1215
self.assertEqual('Foo', bzrdir_config.get_default_stack_on())
1216
bzrdir_config.set_default_stack_on(None)
1217
self.assertIs(None, bzrdir_config.get_default_stack_on())
1220
class TestAuthenticationConfigFile(tests.TestCase):
1221
"""Test the authentication.conf file matching"""
1223
def _got_user_passwd(self, expected_user, expected_password,
1224
config, *args, **kwargs):
1225
credentials = config.get_credentials(*args, **kwargs)
1226
if credentials is None:
1230
user = credentials['user']
1231
password = credentials['password']
1232
self.assertEquals(expected_user, user)
1233
self.assertEquals(expected_password, password)
1235
def test_empty_config(self):
1236
conf = config.AuthenticationConfig(_file=StringIO())
1237
self.assertEquals({}, conf._get_config())
1238
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1240
def test_missing_auth_section_header(self):
1241
conf = config.AuthenticationConfig(_file=StringIO('foo = bar'))
1242
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1244
def test_auth_section_header_not_closed(self):
1245
conf = config.AuthenticationConfig(_file=StringIO('[DEF'))
1246
self.assertRaises(errors.ParseConfigError, conf._get_config)
1248
def test_auth_value_not_boolean(self):
1249
conf = config.AuthenticationConfig(_file=StringIO(
1253
verify_certificates=askme # Error: Not a boolean
1255
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1257
def test_auth_value_not_int(self):
1258
conf = config.AuthenticationConfig(_file=StringIO(
1262
port=port # Error: Not an int
1264
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1266
def test_unknown_password_encoding(self):
1267
conf = config.AuthenticationConfig(_file=StringIO(
1271
password_encoding=unknown
1273
self.assertRaises(ValueError, conf.get_password,
1274
'ftp', 'foo.net', 'joe')
1276
def test_credentials_for_scheme_host(self):
1277
conf = config.AuthenticationConfig(_file=StringIO(
1278
"""# Identity on foo.net
1283
password=secret-pass
1286
self._got_user_passwd('joe', 'secret-pass', conf, 'ftp', 'foo.net')
1288
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1290
self._got_user_passwd(None, None, conf, 'ftp', 'bar.net')
1292
def test_credentials_for_host_port(self):
1293
conf = config.AuthenticationConfig(_file=StringIO(
1294
"""# Identity on foo.net
1300
password=secret-pass
1303
self._got_user_passwd('joe', 'secret-pass',
1304
conf, 'ftp', 'foo.net', port=10021)
1306
self._got_user_passwd(None, None, conf, 'ftp', 'foo.net')
1308
def test_for_matching_host(self):
1309
conf = config.AuthenticationConfig(_file=StringIO(
1310
"""# Identity on foo.net
1316
[sourceforge domain]
1323
self._got_user_passwd('georges', 'bendover',
1324
conf, 'bzr', 'foo.bzr.sf.net')
1326
self._got_user_passwd(None, None,
1327
conf, 'bzr', 'bbzr.sf.net')
1329
def test_for_matching_host_None(self):
1330
conf = config.AuthenticationConfig(_file=StringIO(
1331
"""# Identity on foo.net
1341
self._got_user_passwd('joe', 'joepass',
1342
conf, 'bzr', 'quux.net')
1343
# no host but different scheme
1344
self._got_user_passwd('georges', 'bendover',
1345
conf, 'ftp', 'quux.net')
1347
def test_credentials_for_path(self):
1348
conf = config.AuthenticationConfig(_file=StringIO(
1364
self._got_user_passwd(None, None,
1365
conf, 'http', host='bar.org', path='/dir3')
1367
self._got_user_passwd('georges', 'bendover',
1368
conf, 'http', host='bar.org', path='/dir2')
1370
self._got_user_passwd('jim', 'jimpass',
1371
conf, 'http', host='bar.org',path='/dir1/subdir')
1373
def test_credentials_for_user(self):
1374
conf = config.AuthenticationConfig(_file=StringIO(
1383
self._got_user_passwd('jim', 'jimpass',
1384
conf, 'http', 'bar.org')
1386
self._got_user_passwd('jim', 'jimpass',
1387
conf, 'http', 'bar.org', user='jim')
1388
# Don't get a different user if one is specified
1389
self._got_user_passwd(None, None,
1390
conf, 'http', 'bar.org', user='georges')
1392
def test_credentials_for_user_without_password(self):
1393
conf = config.AuthenticationConfig(_file=StringIO(
1400
# Get user but no password
1401
self._got_user_passwd('jim', None,
1402
conf, 'http', 'bar.org')
1404
def test_verify_certificates(self):
1405
conf = config.AuthenticationConfig(_file=StringIO(
1412
verify_certificates=False
1419
credentials = conf.get_credentials('https', 'bar.org')
1420
self.assertEquals(False, credentials.get('verify_certificates'))
1421
credentials = conf.get_credentials('https', 'foo.net')
1422
self.assertEquals(True, credentials.get('verify_certificates'))
1425
class TestAuthenticationStorage(tests.TestCaseInTempDir):
1427
def test_set_credentials(self):
1428
conf = config.AuthenticationConfig()
1429
conf.set_credentials('name', 'host', 'user', 'scheme', 'password',
1430
99, path='/foo', verify_certificates=False)
1431
credentials = conf.get_credentials(host='host', scheme='scheme',
1432
port=99, path='/foo')
1433
CREDENTIALS = {'name': 'name', 'user': 'user', 'password': 'password',
1434
'verify_certificates': False,}
1435
self.assertEqual(CREDENTIALS, credentials)
1436
credentials_from_disk = config.AuthenticationConfig().get_credentials(
1437
host='host', scheme='scheme', port=99, path='/foo')
1438
self.assertEqual(CREDENTIALS, credentials_from_disk)
1440
def test_reset_credentials_different_name(self):
1441
conf = config.AuthenticationConfig()
1442
conf.set_credentials('name', 'host', 'user', 'scheme', 'password'),
1443
conf.set_credentials('name2', 'host', 'user2', 'scheme', 'password'),
1444
self.assertIs(None, conf._get_config().get('name'))
1445
credentials = conf.get_credentials(host='host', scheme='scheme')
1446
CREDENTIALS = {'name': 'name2', 'user': 'user2', 'password':
1447
'password', 'verify_certificates': True}
1448
self.assertEqual(CREDENTIALS, credentials)
1451
class TestAuthenticationConfig(tests.TestCase):
1452
"""Test AuthenticationConfig behaviour"""
1454
def _check_default_prompt(self, expected_prompt_format, scheme,
1455
host=None, port=None, realm=None, path=None):
1458
user, password = 'jim', 'precious'
1459
expected_prompt = expected_prompt_format % {
1460
'scheme': scheme, 'host': host, 'port': port,
1461
'user': user, 'realm': realm}
1463
stdout = tests.StringIOWrapper()
1464
ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
1466
# We use an empty conf so that the user is always prompted
1467
conf = config.AuthenticationConfig()
1468
self.assertEquals(password,
1469
conf.get_password(scheme, host, user, port=port,
1470
realm=realm, path=path))
1471
self.assertEquals(stdout.getvalue(), expected_prompt)
1473
def test_default_prompts(self):
1474
# HTTP prompts can't be tested here, see test_http.py
1475
self._check_default_prompt('FTP %(user)s@%(host)s password: ', 'ftp')
1476
self._check_default_prompt('FTP %(user)s@%(host)s:%(port)d password: ',
1479
self._check_default_prompt('SSH %(user)s@%(host)s:%(port)d password: ',
1481
# SMTP port handling is a bit special (it's handled if embedded in the
1483
# FIXME: should we: forbid that, extend it to other schemes, leave
1484
# things as they are that's fine thank you ?
1485
self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1487
self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1488
'smtp', host='bar.org:10025')
1489
self._check_default_prompt(
1490
'SMTP %(user)s@%(host)s:%(port)d password: ',
1493
def test_ssh_password_emits_warning(self):
1494
conf = config.AuthenticationConfig(_file=StringIO(
1502
entered_password = 'typed-by-hand'
1503
stdout = tests.StringIOWrapper()
1504
ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1507
# Since the password defined in the authentication config is ignored,
1508
# the user is prompted
1509
self.assertEquals(entered_password,
1510
conf.get_password('ssh', 'bar.org', user='jim'))
1511
self.assertContainsRe(
1512
self._get_log(keep_log_file=True),
1513
'password ignored in section \[ssh with password\]')
1515
def test_ssh_without_password_doesnt_emit_warning(self):
1516
conf = config.AuthenticationConfig(_file=StringIO(
1523
entered_password = 'typed-by-hand'
1524
stdout = tests.StringIOWrapper()
1525
ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1528
# Since the password defined in the authentication config is ignored,
1529
# the user is prompted
1530
self.assertEquals(entered_password,
1531
conf.get_password('ssh', 'bar.org', user='jim'))
1532
# No warning shoud be emitted since there is no password. We are only
1534
self.assertNotContainsRe(
1535
self._get_log(keep_log_file=True),
1536
'password ignored in section \[ssh with password\]')
1539
class TestCredentialStoreRegistry(tests.TestCase):
1541
def _get_cs_registry(self):
1542
return config.credential_store_registry
1544
def test_default_credential_store(self):
1545
r = self._get_cs_registry()
1546
default = r.get_credential_store(None)
1547
self.assertIsInstance(default, config.PlainTextCredentialStore)
1549
def test_unknown_credential_store(self):
1550
r = self._get_cs_registry()
1551
# It's hard to imagine someone creating a credential store named
1552
# 'unknown' so we use that as an never registered key.
1553
self.assertRaises(KeyError, r.get_credential_store, 'unknown')
1556
class TestPlainTextCredentialStore(tests.TestCase):
1558
def test_decode_password(self):
1559
r = config.credential_store_registry
1560
plain_text = r.get_credential_store()
1561
decoded = plain_text.decode_password(dict(password='secret'))
1562
self.assertEquals('secret', decoded)
1565
# FIXME: Once we have a way to declare authentication to all test servers, we
1566
# can implement generic tests.
1567
# test_user_password_in_url
1568
# test_user_in_url_password_from_config
1569
# test_user_in_url_password_prompted
1570
# test_user_in_config
1571
# test_user_getpass.getuser
1572
# test_user_prompted ?
1573
class TestAuthenticationRing(tests.TestCaseWithTransport):