955
1019
def test_config_precedence(self):
956
1020
my_config = self.get_branch_config(global_config=precedence_global)
957
1021
self.assertEqual(my_config.get_user_option('option'), 'global')
958
my_config = self.get_branch_config(global_config=precedence_global,
1022
my_config = self.get_branch_config(global_config=precedence_global,
959
1023
branch_data_config=precedence_branch)
960
1024
self.assertEqual(my_config.get_user_option('option'), 'branch')
961
my_config = self.get_branch_config(global_config=precedence_global,
1025
my_config = self.get_branch_config(global_config=precedence_global,
962
1026
branch_data_config=precedence_branch,
963
1027
location_config=precedence_location)
964
1028
self.assertEqual(my_config.get_user_option('option'), 'recurse')
965
my_config = self.get_branch_config(global_config=precedence_global,
1029
my_config = self.get_branch_config(global_config=precedence_global,
966
1030
branch_data_config=precedence_branch,
967
1031
location_config=precedence_location,
968
1032
location='http://example.com/specific')
969
1033
self.assertEqual(my_config.get_user_option('option'), 'exact')
972
class TestMailAddressExtraction(TestCase):
1035
def test_get_mail_client(self):
1036
config = self.get_branch_config()
1037
client = config.get_mail_client()
1038
self.assertIsInstance(client, mail_client.DefaultMail)
1041
config.set_user_option('mail_client', 'evolution')
1042
client = config.get_mail_client()
1043
self.assertIsInstance(client, mail_client.Evolution)
1045
config.set_user_option('mail_client', 'kmail')
1046
client = config.get_mail_client()
1047
self.assertIsInstance(client, mail_client.KMail)
1049
config.set_user_option('mail_client', 'mutt')
1050
client = config.get_mail_client()
1051
self.assertIsInstance(client, mail_client.Mutt)
1053
config.set_user_option('mail_client', 'thunderbird')
1054
client = config.get_mail_client()
1055
self.assertIsInstance(client, mail_client.Thunderbird)
1058
config.set_user_option('mail_client', 'default')
1059
client = config.get_mail_client()
1060
self.assertIsInstance(client, mail_client.DefaultMail)
1062
config.set_user_option('mail_client', 'editor')
1063
client = config.get_mail_client()
1064
self.assertIsInstance(client, mail_client.Editor)
1066
config.set_user_option('mail_client', 'mapi')
1067
client = config.get_mail_client()
1068
self.assertIsInstance(client, mail_client.MAPIClient)
1070
config.set_user_option('mail_client', 'xdg-email')
1071
client = config.get_mail_client()
1072
self.assertIsInstance(client, mail_client.XDGEmail)
1074
config.set_user_option('mail_client', 'firebird')
1075
self.assertRaises(errors.UnknownMailClient, config.get_mail_client)
1078
class TestMailAddressExtraction(tests.TestCase):
974
1080
def test_extract_email_address(self):
975
1081
self.assertEqual('jane@test.com',
976
1082
config.extract_email_address('Jane <jane@test.com>'))
977
1083
self.assertRaises(errors.NoEmailInUsername,
978
1084
config.extract_email_address, 'Jane Tester')
1086
def test_parse_username(self):
1087
self.assertEqual(('', 'jdoe@example.com'),
1088
config.parse_username('jdoe@example.com'))
1089
self.assertEqual(('', 'jdoe@example.com'),
1090
config.parse_username('<jdoe@example.com>'))
1091
self.assertEqual(('John Doe', 'jdoe@example.com'),
1092
config.parse_username('John Doe <jdoe@example.com>'))
1093
self.assertEqual(('John Doe', ''),
1094
config.parse_username('John Doe'))
1095
self.assertEqual(('John Doe', 'jdoe@example.com'),
1096
config.parse_username('John Doe jdoe@example.com'))
1098
class TestTreeConfig(tests.TestCaseWithTransport):
1100
def test_get_value(self):
1101
"""Test that retreiving a value from a section is possible"""
1102
branch = self.make_branch('.')
1103
tree_config = config.TreeConfig(branch)
1104
tree_config.set_option('value', 'key', 'SECTION')
1105
tree_config.set_option('value2', 'key2')
1106
tree_config.set_option('value3-top', 'key3')
1107
tree_config.set_option('value3-section', 'key3', 'SECTION')
1108
value = tree_config.get_option('key', 'SECTION')
1109
self.assertEqual(value, 'value')
1110
value = tree_config.get_option('key2')
1111
self.assertEqual(value, 'value2')
1112
self.assertEqual(tree_config.get_option('non-existant'), None)
1113
value = tree_config.get_option('non-existant', 'SECTION')
1114
self.assertEqual(value, None)
1115
value = tree_config.get_option('non-existant', default='default')
1116
self.assertEqual(value, 'default')
1117
self.assertEqual(tree_config.get_option('key2', 'NOSECTION'), None)
1118
value = tree_config.get_option('key2', 'NOSECTION', default='default')
1119
self.assertEqual(value, 'default')
1120
value = tree_config.get_option('key3')
1121
self.assertEqual(value, 'value3-top')
1122
value = tree_config.get_option('key3', 'SECTION')
1123
self.assertEqual(value, 'value3-section')
1126
class TestAuthenticationConfigFile(tests.TestCase):
1127
"""Test the authentication.conf file matching"""
1129
def _got_user_passwd(self, expected_user, expected_password,
1130
config, *args, **kwargs):
1131
credentials = config.get_credentials(*args, **kwargs)
1132
if credentials is None:
1136
user = credentials['user']
1137
password = credentials['password']
1138
self.assertEquals(expected_user, user)
1139
self.assertEquals(expected_password, password)
1141
def test_empty_config(self):
1142
conf = config.AuthenticationConfig(_file=StringIO())
1143
self.assertEquals({}, conf._get_config())
1144
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1146
def test_broken_config(self):
1147
conf = config.AuthenticationConfig(_file=StringIO('[DEF'))
1148
self.assertRaises(errors.ParseConfigError, conf._get_config)
1150
conf = config.AuthenticationConfig(_file=StringIO(
1154
verify_certificates=askme # Error: Not a boolean
1156
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1157
conf = config.AuthenticationConfig(_file=StringIO(
1161
port=port # Error: Not an int
1163
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1165
def test_credentials_for_scheme_host(self):
1166
conf = config.AuthenticationConfig(_file=StringIO(
1167
"""# Identity on foo.net
1172
password=secret-pass
1175
self._got_user_passwd('joe', 'secret-pass', conf, 'ftp', 'foo.net')
1177
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1179
self._got_user_passwd(None, None, conf, 'ftp', 'bar.net')
1181
def test_credentials_for_host_port(self):
1182
conf = config.AuthenticationConfig(_file=StringIO(
1183
"""# Identity on foo.net
1189
password=secret-pass
1192
self._got_user_passwd('joe', 'secret-pass',
1193
conf, 'ftp', 'foo.net', port=10021)
1195
self._got_user_passwd(None, None, conf, 'ftp', 'foo.net')
1197
def test_for_matching_host(self):
1198
conf = config.AuthenticationConfig(_file=StringIO(
1199
"""# Identity on foo.net
1205
[sourceforge domain]
1212
self._got_user_passwd('georges', 'bendover',
1213
conf, 'bzr', 'foo.bzr.sf.net')
1215
self._got_user_passwd(None, None,
1216
conf, 'bzr', 'bbzr.sf.net')
1218
def test_for_matching_host_None(self):
1219
conf = config.AuthenticationConfig(_file=StringIO(
1220
"""# Identity on foo.net
1230
self._got_user_passwd('joe', 'joepass',
1231
conf, 'bzr', 'quux.net')
1232
# no host but different scheme
1233
self._got_user_passwd('georges', 'bendover',
1234
conf, 'ftp', 'quux.net')
1236
def test_credentials_for_path(self):
1237
conf = config.AuthenticationConfig(_file=StringIO(
1253
self._got_user_passwd(None, None,
1254
conf, 'http', host='bar.org', path='/dir3')
1256
self._got_user_passwd('georges', 'bendover',
1257
conf, 'http', host='bar.org', path='/dir2')
1259
self._got_user_passwd('jim', 'jimpass',
1260
conf, 'http', host='bar.org',path='/dir1/subdir')
1262
def test_credentials_for_user(self):
1263
conf = config.AuthenticationConfig(_file=StringIO(
1272
self._got_user_passwd('jim', 'jimpass',
1273
conf, 'http', 'bar.org')
1275
self._got_user_passwd('jim', 'jimpass',
1276
conf, 'http', 'bar.org', user='jim')
1277
# Don't get a different user if one is specified
1278
self._got_user_passwd(None, None,
1279
conf, 'http', 'bar.org', user='georges')
1281
def test_verify_certificates(self):
1282
conf = config.AuthenticationConfig(_file=StringIO(
1289
verify_certificates=False
1296
credentials = conf.get_credentials('https', 'bar.org')
1297
self.assertEquals(False, credentials.get('verify_certificates'))
1298
credentials = conf.get_credentials('https', 'foo.net')
1299
self.assertEquals(True, credentials.get('verify_certificates'))
1302
class TestAuthenticationConfig(tests.TestCase):
1303
"""Test AuthenticationConfig behaviour"""
1305
def _check_default_prompt(self, expected_prompt_format, scheme,
1306
host=None, port=None, realm=None, path=None):
1309
user, password = 'jim', 'precious'
1310
expected_prompt = expected_prompt_format % {
1311
'scheme': scheme, 'host': host, 'port': port,
1312
'user': user, 'realm': realm}
1314
stdout = tests.StringIOWrapper()
1315
ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
1317
# We use an empty conf so that the user is always prompted
1318
conf = config.AuthenticationConfig()
1319
self.assertEquals(password,
1320
conf.get_password(scheme, host, user, port=port,
1321
realm=realm, path=path))
1322
self.assertEquals(stdout.getvalue(), expected_prompt)
1324
def test_default_prompts(self):
1325
# HTTP prompts can't be tested here, see test_http.py
1326
self._check_default_prompt('FTP %(user)s@%(host)s password: ', 'ftp')
1327
self._check_default_prompt('FTP %(user)s@%(host)s:%(port)d password: ',
1330
self._check_default_prompt('SSH %(user)s@%(host)s:%(port)d password: ',
1332
# SMTP port handling is a bit special (it's handled if embedded in the
1334
# FIXME: should we: forbid that, extend it to other schemes, leave
1335
# things as they are that's fine thank you ?
1336
self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1338
self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1339
'smtp', host='bar.org:10025')
1340
self._check_default_prompt(
1341
'SMTP %(user)s@%(host)s:%(port)d password: ',
1345
# FIXME: Once we have a way to declare authentication to all test servers, we
1346
# can implement generic tests.
1347
# test_user_password_in_url
1348
# test_user_in_url_password_from_config
1349
# test_user_in_url_password_prompted
1350
# test_user_in_config
1351
# test_user_getpass.getuser
1352
# test_user_prompted ?
1353
class TestAuthenticationRing(tests.TestCaseWithTransport):