160
class FakeControlFilesAndTransport(object):
158
class FakeControlFiles(object):
162
160
def __init__(self, user_id=None):
165
self.files['email'] = user_id
166
self._transport = self
168
164
def get_utf8(self, filename):
170
raise AssertionError("get_utf8 should no longer be used")
165
if filename != 'email':
166
raise NotImplementedError
167
if self.email is not None:
168
return StringIO(self.email)
169
raise errors.NoSuchFile(filename)
172
171
def get(self, filename):
175
173
return StringIO(self.files[filename])
177
175
raise errors.NoSuchFile(filename)
179
def get_bytes(self, filename):
182
return self.files[filename]
184
raise errors.NoSuchFile(filename)
186
177
def put(self, filename, fileobj):
187
178
self.files[filename] = fileobj.read()
189
def put_file(self, filename, fileobj):
190
return self.put(filename, fileobj)
193
181
class InstrumentedConfig(config.Config):
194
182
"""An instrumented config that supplies stubs for template methods."""
588
576
my_config = self._get_sample_config()
589
577
self.assertEqual('help', my_config.get_alias('h'))
591
def test_get_aliases(self):
592
my_config = self._get_sample_config()
593
aliases = my_config.get_aliases()
594
self.assertEqual(2, len(aliases))
595
sorted_keys = sorted(aliases)
596
self.assertEqual('help', aliases[sorted_keys[0]])
597
self.assertEqual(sample_long_alias, aliases[sorted_keys[1]])
599
579
def test_get_no_alias(self):
600
580
my_config = self._get_sample_config()
601
581
self.assertEqual(None, my_config.get_alias('foo'))
605
585
self.assertEqual(sample_long_alias, my_config.get_alias('ll'))
608
class TestGlobalConfigSavingOptions(tests.TestCaseInTempDir):
610
def test_empty(self):
611
my_config = config.GlobalConfig()
612
self.assertEqual(0, len(my_config.get_aliases()))
614
def test_set_alias(self):
615
my_config = config.GlobalConfig()
616
alias_value = 'commit --strict'
617
my_config.set_alias('commit', alias_value)
618
new_config = config.GlobalConfig()
619
self.assertEqual(alias_value, new_config.get_alias('commit'))
621
def test_remove_alias(self):
622
my_config = config.GlobalConfig()
623
my_config.set_alias('commit', 'commit --strict')
624
# Now remove the alias again.
625
my_config.unset_alias('commit')
626
new_config = config.GlobalConfig()
627
self.assertIs(None, new_config.get_alias('commit'))
630
588
class TestLocationConfig(tests.TestCaseInTempDir):
632
590
def test_constructs(self):
951
909
self.assertIs(self.my_config.get_user_option('foo'), None)
952
910
self.my_config.set_user_option('foo', 'bar')
953
911
self.assertEqual(
954
self.my_config.branch.control_files.files['branch.conf'].strip(),
912
self.my_config.branch.control_files.files['branch.conf'],
956
914
self.assertEqual(self.my_config.get_user_option('foo'), 'bar')
957
915
self.my_config.set_user_option('foo', 'baz',
958
916
store=config.STORE_LOCATION)
1002
960
my_config = config.BranchConfig(branch)
1003
961
self.assertEqual("Robert Collins <robertc@example.net>",
1004
962
my_config.username())
1005
my_config.branch.control_files.files['email'] = "John"
963
branch.control_files.email = "John"
1006
964
my_config.set_user_option('email',
1007
965
"Robert Collins <robertc@example.org>")
1008
966
self.assertEqual("John", my_config.username())
1009
del my_config.branch.control_files.files['email']
967
branch.control_files.email = None
1010
968
self.assertEqual("Robert Collins <robertc@example.org>",
1011
969
my_config.username())
1013
971
def test_not_set_in_branch(self):
1014
972
my_config = self.get_branch_config(sample_config_text)
973
my_config.branch.control_files.email = None
1015
974
self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
1016
975
my_config._get_user_id())
1017
my_config.branch.control_files.files['email'] = "John"
976
my_config.branch.control_files.email = "John"
1018
977
self.assertEqual("John", my_config._get_user_id())
1020
979
def test_BZR_EMAIL_OVERRIDES(self):
1177
1136
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
1139
class TestAuthenticationConfigFile(tests.TestCase):
1221
1140
"""Test the authentication.conf file matching"""
1237
1156
self.assertEquals({}, conf._get_config())
1238
1157
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):
1159
def test_broken_config(self):
1245
1160
conf = config.AuthenticationConfig(_file=StringIO('[DEF'))
1246
1161
self.assertRaises(errors.ParseConfigError, conf._get_config)
1248
def test_auth_value_not_boolean(self):
1249
1163
conf = config.AuthenticationConfig(_file=StringIO(
1379
1291
self._got_user_passwd(None, None,
1380
1292
conf, 'http', 'bar.org', user='georges')
1382
def test_credentials_for_user_without_password(self):
1383
conf = config.AuthenticationConfig(_file=StringIO(
1390
# Get user but no password
1391
self._got_user_passwd('jim', None,
1392
conf, 'http', 'bar.org')
1394
1294
def test_verify_certificates(self):
1395
1295
conf = config.AuthenticationConfig(_file=StringIO(
1412
1312
self.assertEquals(True, credentials.get('verify_certificates'))
1415
class TestAuthenticationStorage(tests.TestCaseInTempDir):
1417
def test_set_credentials(self):
1418
conf = config.AuthenticationConfig()
1419
conf.set_credentials('name', 'host', 'user', 'scheme', 'password',
1420
99, path='/foo', verify_certificates=False)
1421
credentials = conf.get_credentials(host='host', scheme='scheme',
1422
port=99, path='/foo')
1423
CREDENTIALS = {'name': 'name', 'user': 'user', 'password': 'password',
1424
'verify_certificates': False,}
1425
self.assertEqual(CREDENTIALS, credentials)
1426
credentials_from_disk = config.AuthenticationConfig().get_credentials(
1427
host='host', scheme='scheme', port=99, path='/foo')
1428
self.assertEqual(CREDENTIALS, credentials_from_disk)
1430
def test_reset_credentials_different_name(self):
1431
conf = config.AuthenticationConfig()
1432
conf.set_credentials('name', 'host', 'user', 'scheme', 'password'),
1433
conf.set_credentials('name2', 'host', 'user2', 'scheme', 'password'),
1434
self.assertIs(None, conf._get_config().get('name'))
1435
credentials = conf.get_credentials(host='host', scheme='scheme')
1436
CREDENTIALS = {'name': 'name2', 'user': 'user2', 'password':
1437
'password', 'verify_certificates': True}
1438
self.assertEqual(CREDENTIALS, credentials)
1441
1315
class TestAuthenticationConfig(tests.TestCase):
1442
1316
"""Test AuthenticationConfig behaviour"""
1480
1354
'SMTP %(user)s@%(host)s:%(port)d password: ',
1481
1355
'smtp', port=10025)
1483
def test_ssh_password_emits_warning(self):
1484
conf = config.AuthenticationConfig(_file=StringIO(
1492
entered_password = 'typed-by-hand'
1493
stdout = tests.StringIOWrapper()
1494
ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1497
# Since the password defined in the authentication config is ignored,
1498
# the user is prompted
1499
self.assertEquals(entered_password,
1500
conf.get_password('ssh', 'bar.org', user='jim'))
1501
self.assertContainsRe(
1502
self._get_log(keep_log_file=True),
1503
'password ignored in section \[ssh with password\]')
1505
def test_ssh_without_password_doesnt_emit_warning(self):
1506
conf = config.AuthenticationConfig(_file=StringIO(
1513
entered_password = 'typed-by-hand'
1514
stdout = tests.StringIOWrapper()
1515
ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1518
# Since the password defined in the authentication config is ignored,
1519
# the user is prompted
1520
self.assertEquals(entered_password,
1521
conf.get_password('ssh', 'bar.org', user='jim'))
1522
# No warning shoud be emitted since there is no password. We are only
1524
self.assertNotContainsRe(
1525
self._get_log(keep_log_file=True),
1526
'password ignored in section \[ssh with password\]')
1529
1358
# FIXME: Once we have a way to declare authentication to all test servers, we
1530
1359
# can implement generic tests.