1069
821
# post-commit is ignored when bresent in branch data
1070
822
self.assertEqual('bzrlib.tests.test_config.post_commit',
1071
823
my_config.post_commit())
1072
my_config.set_user_option('post_commit', 'rmtree_root',
1073
store=config.STORE_LOCATION)
824
my_config.set_user_option('post_commit', 'rmtree_root', local=True)
1074
825
self.assertEqual('rmtree_root', my_config.post_commit())
1076
827
def test_config_precedence(self):
1077
828
my_config = self.get_branch_config(global_config=precedence_global)
1078
829
self.assertEqual(my_config.get_user_option('option'), 'global')
1079
my_config = self.get_branch_config(global_config=precedence_global,
830
my_config = self.get_branch_config(global_config=precedence_global,
1080
831
branch_data_config=precedence_branch)
1081
832
self.assertEqual(my_config.get_user_option('option'), 'branch')
1082
my_config = self.get_branch_config(global_config=precedence_global,
833
my_config = self.get_branch_config(global_config=precedence_global,
1083
834
branch_data_config=precedence_branch,
1084
835
location_config=precedence_location)
1085
836
self.assertEqual(my_config.get_user_option('option'), 'recurse')
1086
my_config = self.get_branch_config(global_config=precedence_global,
837
my_config = self.get_branch_config(global_config=precedence_global,
1087
838
branch_data_config=precedence_branch,
1088
839
location_config=precedence_location,
1089
840
location='http://example.com/specific')
1090
841
self.assertEqual(my_config.get_user_option('option'), 'exact')
1092
def test_get_mail_client(self):
1093
config = self.get_branch_config()
1094
client = config.get_mail_client()
1095
self.assertIsInstance(client, mail_client.DefaultMail)
1098
config.set_user_option('mail_client', 'evolution')
1099
client = config.get_mail_client()
1100
self.assertIsInstance(client, mail_client.Evolution)
1102
config.set_user_option('mail_client', 'kmail')
1103
client = config.get_mail_client()
1104
self.assertIsInstance(client, mail_client.KMail)
1106
config.set_user_option('mail_client', 'mutt')
1107
client = config.get_mail_client()
1108
self.assertIsInstance(client, mail_client.Mutt)
1110
config.set_user_option('mail_client', 'thunderbird')
1111
client = config.get_mail_client()
1112
self.assertIsInstance(client, mail_client.Thunderbird)
1115
config.set_user_option('mail_client', 'default')
1116
client = config.get_mail_client()
1117
self.assertIsInstance(client, mail_client.DefaultMail)
1119
config.set_user_option('mail_client', 'editor')
1120
client = config.get_mail_client()
1121
self.assertIsInstance(client, mail_client.Editor)
1123
config.set_user_option('mail_client', 'mapi')
1124
client = config.get_mail_client()
1125
self.assertIsInstance(client, mail_client.MAPIClient)
1127
config.set_user_option('mail_client', 'xdg-email')
1128
client = config.get_mail_client()
1129
self.assertIsInstance(client, mail_client.XDGEmail)
1131
config.set_user_option('mail_client', 'firebird')
1132
self.assertRaises(errors.UnknownMailClient, config.get_mail_client)
1135
class TestMailAddressExtraction(tests.TestCase):
844
class TestMailAddressExtraction(TestCase):
1137
846
def test_extract_email_address(self):
1138
847
self.assertEqual('jane@test.com',
1139
848
config.extract_email_address('Jane <jane@test.com>'))
1140
849
self.assertRaises(errors.NoEmailInUsername,
1141
850
config.extract_email_address, 'Jane Tester')
1143
def test_parse_username(self):
1144
self.assertEqual(('', 'jdoe@example.com'),
1145
config.parse_username('jdoe@example.com'))
1146
self.assertEqual(('', 'jdoe@example.com'),
1147
config.parse_username('<jdoe@example.com>'))
1148
self.assertEqual(('John Doe', 'jdoe@example.com'),
1149
config.parse_username('John Doe <jdoe@example.com>'))
1150
self.assertEqual(('John Doe', ''),
1151
config.parse_username('John Doe'))
1152
self.assertEqual(('John Doe', 'jdoe@example.com'),
1153
config.parse_username('John Doe jdoe@example.com'))
1155
class TestTreeConfig(tests.TestCaseWithTransport):
1157
def test_get_value(self):
1158
"""Test that retreiving a value from a section is possible"""
1159
branch = self.make_branch('.')
1160
tree_config = config.TreeConfig(branch)
1161
tree_config.set_option('value', 'key', 'SECTION')
1162
tree_config.set_option('value2', 'key2')
1163
tree_config.set_option('value3-top', 'key3')
1164
tree_config.set_option('value3-section', 'key3', 'SECTION')
1165
value = tree_config.get_option('key', 'SECTION')
1166
self.assertEqual(value, 'value')
1167
value = tree_config.get_option('key2')
1168
self.assertEqual(value, 'value2')
1169
self.assertEqual(tree_config.get_option('non-existant'), None)
1170
value = tree_config.get_option('non-existant', 'SECTION')
1171
self.assertEqual(value, None)
1172
value = tree_config.get_option('non-existant', default='default')
1173
self.assertEqual(value, 'default')
1174
self.assertEqual(tree_config.get_option('key2', 'NOSECTION'), None)
1175
value = tree_config.get_option('key2', 'NOSECTION', default='default')
1176
self.assertEqual(value, 'default')
1177
value = tree_config.get_option('key3')
1178
self.assertEqual(value, 'value3-top')
1179
value = tree_config.get_option('key3', 'SECTION')
1180
self.assertEqual(value, 'value3-section')
1183
class TestTransportConfig(tests.TestCaseWithTransport):
1185
def test_get_value(self):
1186
"""Test that retreiving a value from a section is possible"""
1187
bzrdir_config = config.TransportConfig(transport.get_transport('.'),
1189
bzrdir_config.set_option('value', 'key', 'SECTION')
1190
bzrdir_config.set_option('value2', 'key2')
1191
bzrdir_config.set_option('value3-top', 'key3')
1192
bzrdir_config.set_option('value3-section', 'key3', 'SECTION')
1193
value = bzrdir_config.get_option('key', 'SECTION')
1194
self.assertEqual(value, 'value')
1195
value = bzrdir_config.get_option('key2')
1196
self.assertEqual(value, 'value2')
1197
self.assertEqual(bzrdir_config.get_option('non-existant'), None)
1198
value = bzrdir_config.get_option('non-existant', 'SECTION')
1199
self.assertEqual(value, None)
1200
value = bzrdir_config.get_option('non-existant', default='default')
1201
self.assertEqual(value, 'default')
1202
self.assertEqual(bzrdir_config.get_option('key2', 'NOSECTION'), None)
1203
value = bzrdir_config.get_option('key2', 'NOSECTION',
1205
self.assertEqual(value, 'default')
1206
value = bzrdir_config.get_option('key3')
1207
self.assertEqual(value, 'value3-top')
1208
value = bzrdir_config.get_option('key3', 'SECTION')
1209
self.assertEqual(value, 'value3-section')
1211
def test_set_unset_default_stack_on(self):
1212
my_dir = self.make_bzrdir('.')
1213
bzrdir_config = config.BzrDirConfig(my_dir.transport)
1214
self.assertIs(None, bzrdir_config.get_default_stack_on())
1215
bzrdir_config.set_default_stack_on('Foo')
1216
self.assertEqual('Foo', bzrdir_config._config.get_option(
1217
'default_stack_on'))
1218
self.assertEqual('Foo', bzrdir_config.get_default_stack_on())
1219
bzrdir_config.set_default_stack_on(None)
1220
self.assertIs(None, bzrdir_config.get_default_stack_on())
1223
class TestAuthenticationConfigFile(tests.TestCase):
1224
"""Test the authentication.conf file matching"""
1226
def _got_user_passwd(self, expected_user, expected_password,
1227
config, *args, **kwargs):
1228
credentials = config.get_credentials(*args, **kwargs)
1229
if credentials is None:
1233
user = credentials['user']
1234
password = credentials['password']
1235
self.assertEquals(expected_user, user)
1236
self.assertEquals(expected_password, password)
1238
def test_empty_config(self):
1239
conf = config.AuthenticationConfig(_file=StringIO())
1240
self.assertEquals({}, conf._get_config())
1241
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1243
def test_missing_auth_section_header(self):
1244
conf = config.AuthenticationConfig(_file=StringIO('foo = bar'))
1245
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1247
def test_auth_section_header_not_closed(self):
1248
conf = config.AuthenticationConfig(_file=StringIO('[DEF'))
1249
self.assertRaises(errors.ParseConfigError, conf._get_config)
1251
def test_auth_value_not_boolean(self):
1252
conf = config.AuthenticationConfig(_file=StringIO(
1256
verify_certificates=askme # Error: Not a boolean
1258
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1260
def test_auth_value_not_int(self):
1261
conf = config.AuthenticationConfig(_file=StringIO(
1265
port=port # Error: Not an int
1267
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1269
def test_unknown_password_encoding(self):
1270
conf = config.AuthenticationConfig(_file=StringIO(
1274
password_encoding=unknown
1276
self.assertRaises(ValueError, conf.get_password,
1277
'ftp', 'foo.net', 'joe')
1279
def test_credentials_for_scheme_host(self):
1280
conf = config.AuthenticationConfig(_file=StringIO(
1281
"""# Identity on foo.net
1286
password=secret-pass
1289
self._got_user_passwd('joe', 'secret-pass', conf, 'ftp', 'foo.net')
1291
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1293
self._got_user_passwd(None, None, conf, 'ftp', 'bar.net')
1295
def test_credentials_for_host_port(self):
1296
conf = config.AuthenticationConfig(_file=StringIO(
1297
"""# Identity on foo.net
1303
password=secret-pass
1306
self._got_user_passwd('joe', 'secret-pass',
1307
conf, 'ftp', 'foo.net', port=10021)
1309
self._got_user_passwd(None, None, conf, 'ftp', 'foo.net')
1311
def test_for_matching_host(self):
1312
conf = config.AuthenticationConfig(_file=StringIO(
1313
"""# Identity on foo.net
1319
[sourceforge domain]
1326
self._got_user_passwd('georges', 'bendover',
1327
conf, 'bzr', 'foo.bzr.sf.net')
1329
self._got_user_passwd(None, None,
1330
conf, 'bzr', 'bbzr.sf.net')
1332
def test_for_matching_host_None(self):
1333
conf = config.AuthenticationConfig(_file=StringIO(
1334
"""# Identity on foo.net
1344
self._got_user_passwd('joe', 'joepass',
1345
conf, 'bzr', 'quux.net')
1346
# no host but different scheme
1347
self._got_user_passwd('georges', 'bendover',
1348
conf, 'ftp', 'quux.net')
1350
def test_credentials_for_path(self):
1351
conf = config.AuthenticationConfig(_file=StringIO(
1367
self._got_user_passwd(None, None,
1368
conf, 'http', host='bar.org', path='/dir3')
1370
self._got_user_passwd('georges', 'bendover',
1371
conf, 'http', host='bar.org', path='/dir2')
1373
self._got_user_passwd('jim', 'jimpass',
1374
conf, 'http', host='bar.org',path='/dir1/subdir')
1376
def test_credentials_for_user(self):
1377
conf = config.AuthenticationConfig(_file=StringIO(
1386
self._got_user_passwd('jim', 'jimpass',
1387
conf, 'http', 'bar.org')
1389
self._got_user_passwd('jim', 'jimpass',
1390
conf, 'http', 'bar.org', user='jim')
1391
# Don't get a different user if one is specified
1392
self._got_user_passwd(None, None,
1393
conf, 'http', 'bar.org', user='georges')
1395
def test_credentials_for_user_without_password(self):
1396
conf = config.AuthenticationConfig(_file=StringIO(
1403
# Get user but no password
1404
self._got_user_passwd('jim', None,
1405
conf, 'http', 'bar.org')
1407
def test_verify_certificates(self):
1408
conf = config.AuthenticationConfig(_file=StringIO(
1415
verify_certificates=False
1422
credentials = conf.get_credentials('https', 'bar.org')
1423
self.assertEquals(False, credentials.get('verify_certificates'))
1424
credentials = conf.get_credentials('https', 'foo.net')
1425
self.assertEquals(True, credentials.get('verify_certificates'))
1428
class TestAuthenticationStorage(tests.TestCaseInTempDir):
1430
def test_set_credentials(self):
1431
conf = config.AuthenticationConfig()
1432
conf.set_credentials('name', 'host', 'user', 'scheme', 'password',
1433
99, path='/foo', verify_certificates=False, realm='realm')
1434
credentials = conf.get_credentials(host='host', scheme='scheme',
1435
port=99, path='/foo',
1437
CREDENTIALS = {'name': 'name', 'user': 'user', 'password': 'password',
1438
'verify_certificates': False, 'scheme': 'scheme',
1439
'host': 'host', 'port': 99, 'path': '/foo',
1441
self.assertEqual(CREDENTIALS, credentials)
1442
credentials_from_disk = config.AuthenticationConfig().get_credentials(
1443
host='host', scheme='scheme', port=99, path='/foo', realm='realm')
1444
self.assertEqual(CREDENTIALS, credentials_from_disk)
1446
def test_reset_credentials_different_name(self):
1447
conf = config.AuthenticationConfig()
1448
conf.set_credentials('name', 'host', 'user', 'scheme', 'password'),
1449
conf.set_credentials('name2', 'host', 'user2', 'scheme', 'password'),
1450
self.assertIs(None, conf._get_config().get('name'))
1451
credentials = conf.get_credentials(host='host', scheme='scheme')
1452
CREDENTIALS = {'name': 'name2', 'user': 'user2', 'password':
1453
'password', 'verify_certificates': True,
1454
'scheme': 'scheme', 'host': 'host', 'port': None,
1455
'path': None, 'realm': None}
1456
self.assertEqual(CREDENTIALS, credentials)
1459
class TestAuthenticationConfig(tests.TestCase):
1460
"""Test AuthenticationConfig behaviour"""
1462
def _check_default_prompt(self, expected_prompt_format, scheme,
1463
host=None, port=None, realm=None, path=None):
1466
user, password = 'jim', 'precious'
1467
expected_prompt = expected_prompt_format % {
1468
'scheme': scheme, 'host': host, 'port': port,
1469
'user': user, 'realm': realm}
1471
stdout = tests.StringIOWrapper()
1472
ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
1474
# We use an empty conf so that the user is always prompted
1475
conf = config.AuthenticationConfig()
1476
self.assertEquals(password,
1477
conf.get_password(scheme, host, user, port=port,
1478
realm=realm, path=path))
1479
self.assertEquals(stdout.getvalue(), expected_prompt)
1481
def test_default_prompts(self):
1482
# HTTP prompts can't be tested here, see test_http.py
1483
self._check_default_prompt('FTP %(user)s@%(host)s password: ', 'ftp')
1484
self._check_default_prompt('FTP %(user)s@%(host)s:%(port)d password: ',
1487
self._check_default_prompt('SSH %(user)s@%(host)s:%(port)d password: ',
1489
# SMTP port handling is a bit special (it's handled if embedded in the
1491
# FIXME: should we: forbid that, extend it to other schemes, leave
1492
# things as they are that's fine thank you ?
1493
self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1495
self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1496
'smtp', host='bar.org:10025')
1497
self._check_default_prompt(
1498
'SMTP %(user)s@%(host)s:%(port)d password: ',
1501
def test_ssh_password_emits_warning(self):
1502
conf = config.AuthenticationConfig(_file=StringIO(
1510
entered_password = 'typed-by-hand'
1511
stdout = tests.StringIOWrapper()
1512
ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1515
# Since the password defined in the authentication config is ignored,
1516
# the user is prompted
1517
self.assertEquals(entered_password,
1518
conf.get_password('ssh', 'bar.org', user='jim'))
1519
self.assertContainsRe(
1520
self._get_log(keep_log_file=True),
1521
'password ignored in section \[ssh with password\]')
1523
def test_ssh_without_password_doesnt_emit_warning(self):
1524
conf = config.AuthenticationConfig(_file=StringIO(
1531
entered_password = 'typed-by-hand'
1532
stdout = tests.StringIOWrapper()
1533
ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1536
# Since the password defined in the authentication config is ignored,
1537
# the user is prompted
1538
self.assertEquals(entered_password,
1539
conf.get_password('ssh', 'bar.org', user='jim'))
1540
# No warning shoud be emitted since there is no password. We are only
1542
self.assertNotContainsRe(
1543
self._get_log(keep_log_file=True),
1544
'password ignored in section \[ssh with password\]')
1547
class TestCredentialStoreRegistry(tests.TestCase):
1549
def _get_cs_registry(self):
1550
return config.credential_store_registry
1552
def test_default_credential_store(self):
1553
r = self._get_cs_registry()
1554
default = r.get_credential_store(None)
1555
self.assertIsInstance(default, config.PlainTextCredentialStore)
1557
def test_unknown_credential_store(self):
1558
r = self._get_cs_registry()
1559
# It's hard to imagine someone creating a credential store named
1560
# 'unknown' so we use that as an never registered key.
1561
self.assertRaises(KeyError, r.get_credential_store, 'unknown')
1564
class TestPlainTextCredentialStore(tests.TestCase):
1566
def test_decode_password(self):
1567
r = config.credential_store_registry
1568
plain_text = r.get_credential_store()
1569
decoded = plain_text.decode_password(dict(password='secret'))
1570
self.assertEquals('secret', decoded)
1573
# FIXME: Once we have a way to declare authentication to all test servers, we
1574
# can implement generic tests.
1575
# test_user_password_in_url
1576
# test_user_in_url_password_from_config
1577
# test_user_in_url_password_prompted
1578
# test_user_in_config
1579
# test_user_getpass.getuser
1580
# test_user_prompted ?
1581
class TestAuthenticationRing(tests.TestCaseWithTransport):