203
201
nonactive = False
207
class TestConfigObj(tests.TestCase):
203
class TestConfigObj(TestCase):
209
204
def test_get_bool(self):
210
co = config.ConfigObj(StringIO(bool_config))
205
from bzrlib.config import ConfigObj
206
co = ConfigObj(StringIO(bool_config))
211
207
self.assertIs(co.get_bool('DEFAULT', 'active'), True)
212
208
self.assertIs(co.get_bool('DEFAULT', 'inactive'), False)
213
209
self.assertIs(co.get_bool('UPPERCASE', 'active'), True)
214
210
self.assertIs(co.get_bool('UPPERCASE', 'nonactive'), False)
216
def test_hash_sign_in_value(self):
218
Before 4.5.0, ConfigObj did not quote # signs in values, so they'd be
219
treated as comments when read in again. (#86838)
221
co = config.ConfigObj()
222
co['test'] = 'foo#bar'
224
self.assertEqual(lines, ['test = "foo#bar"'])
225
co2 = config.ConfigObj(lines)
226
self.assertEqual(co2['test'], 'foo#bar')
229
erroneous_config = """[section] # line 1
232
whocares=notme # line 4
236
class TestConfigObjErrors(tests.TestCase):
238
def test_duplicate_section_name_error_line(self):
240
co = configobj.ConfigObj(StringIO(erroneous_config),
242
except config.configobj.DuplicateError, e:
243
self.assertEqual(3, e.line_number)
245
self.fail('Error in config file not detected')
248
class TestConfig(tests.TestCase):
213
class TestConfig(TestCase):
250
215
def test_constructs(self):
253
218
def test_no_default_editor(self):
254
219
self.assertRaises(NotImplementedError, config.Config().get_editor)
309
274
if sys.platform == 'win32':
310
275
os.environ['BZR_HOME'] = \
311
276
r'C:\Documents and Settings\bogus\Application Data'
313
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0'
315
self.bzr_home = '/home/bogus/.bazaar'
317
278
def test_config_dir(self):
318
self.assertEqual(config.config_dir(), self.bzr_home)
279
if sys.platform == 'win32':
280
self.assertEqual(config.config_dir(),
281
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0')
283
self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
320
285
def test_config_filename(self):
321
self.assertEqual(config.config_filename(),
322
self.bzr_home + '/bazaar.conf')
286
if sys.platform == 'win32':
287
self.assertEqual(config.config_filename(),
288
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/bazaar.conf')
290
self.assertEqual(config.config_filename(),
291
'/home/bogus/.bazaar/bazaar.conf')
324
293
def test_branches_config_filename(self):
325
self.assertEqual(config.branches_config_filename(),
326
self.bzr_home + '/branches.conf')
294
if sys.platform == 'win32':
295
self.assertEqual(config.branches_config_filename(),
296
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/branches.conf')
298
self.assertEqual(config.branches_config_filename(),
299
'/home/bogus/.bazaar/branches.conf')
328
301
def test_locations_config_filename(self):
329
self.assertEqual(config.locations_config_filename(),
330
self.bzr_home + '/locations.conf')
332
def test_authentication_config_filename(self):
333
self.assertEqual(config.authentication_config_filename(),
334
self.bzr_home + '/authentication.conf')
337
class TestIniConfig(tests.TestCase):
302
if sys.platform == 'win32':
303
self.assertEqual(config.locations_config_filename(),
304
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/locations.conf')
306
self.assertEqual(config.locations_config_filename(),
307
'/home/bogus/.bazaar/locations.conf')
309
class TestIniConfig(TestCase):
339
311
def test_contructs(self):
340
312
my_config = config.IniBasedConfig("nothing")
1032
992
def test_config_precedence(self):
1033
993
my_config = self.get_branch_config(global_config=precedence_global)
1034
994
self.assertEqual(my_config.get_user_option('option'), 'global')
1035
my_config = self.get_branch_config(global_config=precedence_global,
995
my_config = self.get_branch_config(global_config=precedence_global,
1036
996
branch_data_config=precedence_branch)
1037
997
self.assertEqual(my_config.get_user_option('option'), 'branch')
1038
my_config = self.get_branch_config(global_config=precedence_global,
998
my_config = self.get_branch_config(global_config=precedence_global,
1039
999
branch_data_config=precedence_branch,
1040
1000
location_config=precedence_location)
1041
1001
self.assertEqual(my_config.get_user_option('option'), 'recurse')
1042
my_config = self.get_branch_config(global_config=precedence_global,
1002
my_config = self.get_branch_config(global_config=precedence_global,
1043
1003
branch_data_config=precedence_branch,
1044
1004
location_config=precedence_location,
1045
1005
location='http://example.com/specific')
1046
1006
self.assertEqual(my_config.get_user_option('option'), 'exact')
1048
def test_get_mail_client(self):
1049
config = self.get_branch_config()
1050
client = config.get_mail_client()
1051
self.assertIsInstance(client, mail_client.DefaultMail)
1054
config.set_user_option('mail_client', 'evolution')
1055
client = config.get_mail_client()
1056
self.assertIsInstance(client, mail_client.Evolution)
1058
config.set_user_option('mail_client', 'kmail')
1059
client = config.get_mail_client()
1060
self.assertIsInstance(client, mail_client.KMail)
1062
config.set_user_option('mail_client', 'mutt')
1063
client = config.get_mail_client()
1064
self.assertIsInstance(client, mail_client.Mutt)
1066
config.set_user_option('mail_client', 'thunderbird')
1067
client = config.get_mail_client()
1068
self.assertIsInstance(client, mail_client.Thunderbird)
1071
config.set_user_option('mail_client', 'default')
1072
client = config.get_mail_client()
1073
self.assertIsInstance(client, mail_client.DefaultMail)
1075
config.set_user_option('mail_client', 'editor')
1076
client = config.get_mail_client()
1077
self.assertIsInstance(client, mail_client.Editor)
1079
config.set_user_option('mail_client', 'mapi')
1080
client = config.get_mail_client()
1081
self.assertIsInstance(client, mail_client.MAPIClient)
1083
config.set_user_option('mail_client', 'xdg-email')
1084
client = config.get_mail_client()
1085
self.assertIsInstance(client, mail_client.XDGEmail)
1087
config.set_user_option('mail_client', 'firebird')
1088
self.assertRaises(errors.UnknownMailClient, config.get_mail_client)
1091
class TestMailAddressExtraction(tests.TestCase):
1009
class TestMailAddressExtraction(TestCase):
1093
1011
def test_extract_email_address(self):
1094
1012
self.assertEqual('jane@test.com',
1134
1041
self.assertEqual(value, 'value3-top')
1135
1042
value = tree_config.get_option('key3', 'SECTION')
1136
1043
self.assertEqual(value, 'value3-section')
1139
class TestAuthenticationConfigFile(tests.TestCase):
1140
"""Test the authentication.conf file matching"""
1142
def _got_user_passwd(self, expected_user, expected_password,
1143
config, *args, **kwargs):
1144
credentials = config.get_credentials(*args, **kwargs)
1145
if credentials is None:
1149
user = credentials['user']
1150
password = credentials['password']
1151
self.assertEquals(expected_user, user)
1152
self.assertEquals(expected_password, password)
1154
def test_empty_config(self):
1155
conf = config.AuthenticationConfig(_file=StringIO())
1156
self.assertEquals({}, conf._get_config())
1157
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1159
def test_broken_config(self):
1160
conf = config.AuthenticationConfig(_file=StringIO('[DEF'))
1161
self.assertRaises(errors.ParseConfigError, conf._get_config)
1163
conf = config.AuthenticationConfig(_file=StringIO(
1167
verify_certificates=askme # Error: Not a boolean
1169
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1170
conf = config.AuthenticationConfig(_file=StringIO(
1174
port=port # Error: Not an int
1176
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1178
def test_credentials_for_scheme_host(self):
1179
conf = config.AuthenticationConfig(_file=StringIO(
1180
"""# Identity on foo.net
1185
password=secret-pass
1188
self._got_user_passwd('joe', 'secret-pass', conf, 'ftp', 'foo.net')
1190
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1192
self._got_user_passwd(None, None, conf, 'ftp', 'bar.net')
1194
def test_credentials_for_host_port(self):
1195
conf = config.AuthenticationConfig(_file=StringIO(
1196
"""# Identity on foo.net
1202
password=secret-pass
1205
self._got_user_passwd('joe', 'secret-pass',
1206
conf, 'ftp', 'foo.net', port=10021)
1208
self._got_user_passwd(None, None, conf, 'ftp', 'foo.net')
1210
def test_for_matching_host(self):
1211
conf = config.AuthenticationConfig(_file=StringIO(
1212
"""# Identity on foo.net
1218
[sourceforge domain]
1225
self._got_user_passwd('georges', 'bendover',
1226
conf, 'bzr', 'foo.bzr.sf.net')
1228
self._got_user_passwd(None, None,
1229
conf, 'bzr', 'bbzr.sf.net')
1231
def test_for_matching_host_None(self):
1232
conf = config.AuthenticationConfig(_file=StringIO(
1233
"""# Identity on foo.net
1243
self._got_user_passwd('joe', 'joepass',
1244
conf, 'bzr', 'quux.net')
1245
# no host but different scheme
1246
self._got_user_passwd('georges', 'bendover',
1247
conf, 'ftp', 'quux.net')
1249
def test_credentials_for_path(self):
1250
conf = config.AuthenticationConfig(_file=StringIO(
1266
self._got_user_passwd(None, None,
1267
conf, 'http', host='bar.org', path='/dir3')
1269
self._got_user_passwd('georges', 'bendover',
1270
conf, 'http', host='bar.org', path='/dir2')
1272
self._got_user_passwd('jim', 'jimpass',
1273
conf, 'http', host='bar.org',path='/dir1/subdir')
1275
def test_credentials_for_user(self):
1276
conf = config.AuthenticationConfig(_file=StringIO(
1285
self._got_user_passwd('jim', 'jimpass',
1286
conf, 'http', 'bar.org')
1288
self._got_user_passwd('jim', 'jimpass',
1289
conf, 'http', 'bar.org', user='jim')
1290
# Don't get a different user if one is specified
1291
self._got_user_passwd(None, None,
1292
conf, 'http', 'bar.org', user='georges')
1294
def test_verify_certificates(self):
1295
conf = config.AuthenticationConfig(_file=StringIO(
1302
verify_certificates=False
1309
credentials = conf.get_credentials('https', 'bar.org')
1310
self.assertEquals(False, credentials.get('verify_certificates'))
1311
credentials = conf.get_credentials('https', 'foo.net')
1312
self.assertEquals(True, credentials.get('verify_certificates'))
1315
class TestAuthenticationConfig(tests.TestCase):
1316
"""Test AuthenticationConfig behaviour"""
1318
def _check_default_prompt(self, expected_prompt_format, scheme,
1319
host=None, port=None, realm=None, path=None):
1322
user, password = 'jim', 'precious'
1323
expected_prompt = expected_prompt_format % {
1324
'scheme': scheme, 'host': host, 'port': port,
1325
'user': user, 'realm': realm}
1327
stdout = tests.StringIOWrapper()
1328
ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
1330
# We use an empty conf so that the user is always prompted
1331
conf = config.AuthenticationConfig()
1332
self.assertEquals(password,
1333
conf.get_password(scheme, host, user, port=port,
1334
realm=realm, path=path))
1335
self.assertEquals(stdout.getvalue(), expected_prompt)
1337
def test_default_prompts(self):
1338
# HTTP prompts can't be tested here, see test_http.py
1339
self._check_default_prompt('FTP %(user)s@%(host)s password: ', 'ftp')
1340
self._check_default_prompt('FTP %(user)s@%(host)s:%(port)d password: ',
1343
self._check_default_prompt('SSH %(user)s@%(host)s:%(port)d password: ',
1345
# SMTP port handling is a bit special (it's handled if embedded in the
1347
# FIXME: should we: forbid that, extend it to other schemes, leave
1348
# things as they are that's fine thank you ?
1349
self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1351
self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1352
'smtp', host='bar.org:10025')
1353
self._check_default_prompt(
1354
'SMTP %(user)s@%(host)s:%(port)d password: ',
1358
# FIXME: Once we have a way to declare authentication to all test servers, we
1359
# can implement generic tests.
1360
# test_user_password_in_url
1361
# test_user_in_url_password_from_config
1362
# test_user_in_url_password_prompted
1363
# test_user_in_config
1364
# test_user_getpass.getuser
1365
# test_user_prompted ?
1366
class TestAuthenticationRing(tests.TestCaseWithTransport):