158
class FakeControlFiles(object):
160
class FakeControlFilesAndTransport(object):
160
162
def __init__(self, user_id=None):
165
self.files['email'] = user_id
166
self._transport = self
164
168
def get_utf8(self, filename):
165
if filename != 'email':
166
raise NotImplementedError
167
if self.email is not None:
168
return StringIO(self.email)
169
raise errors.NoSuchFile(filename)
170
raise AssertionError("get_utf8 should no longer be used")
171
172
def get(self, filename):
173
175
return StringIO(self.files[filename])
175
177
raise errors.NoSuchFile(filename)
179
def get_bytes(self, filename):
182
return self.files[filename]
184
raise errors.NoSuchFile(filename)
177
186
def put(self, filename, fileobj):
178
187
self.files[filename] = fileobj.read()
189
def put_file(self, filename, fileobj):
190
return self.put(filename, fileobj)
181
193
class InstrumentedConfig(config.Config):
182
194
"""An instrumented config that supplies stubs for template methods."""
576
588
my_config = self._get_sample_config()
577
589
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]])
579
599
def test_get_no_alias(self):
580
600
my_config = self._get_sample_config()
581
601
self.assertEqual(None, my_config.get_alias('foo'))
585
605
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'))
588
630
class TestLocationConfig(tests.TestCaseInTempDir):
590
632
def test_constructs(self):
909
951
self.assertIs(self.my_config.get_user_option('foo'), None)
910
952
self.my_config.set_user_option('foo', 'bar')
911
953
self.assertEqual(
912
self.my_config.branch.control_files.files['branch.conf'],
954
self.my_config.branch.control_files.files['branch.conf'].strip(),
914
956
self.assertEqual(self.my_config.get_user_option('foo'), 'bar')
915
957
self.my_config.set_user_option('foo', 'baz',
916
958
store=config.STORE_LOCATION)
960
1002
my_config = config.BranchConfig(branch)
961
1003
self.assertEqual("Robert Collins <robertc@example.net>",
962
1004
my_config.username())
963
branch.control_files.email = "John"
1005
my_config.branch.control_files.files['email'] = "John"
964
1006
my_config.set_user_option('email',
965
1007
"Robert Collins <robertc@example.org>")
966
1008
self.assertEqual("John", my_config.username())
967
branch.control_files.email = None
1009
del my_config.branch.control_files.files['email']
968
1010
self.assertEqual("Robert Collins <robertc@example.org>",
969
1011
my_config.username())
971
1013
def test_not_set_in_branch(self):
972
1014
my_config = self.get_branch_config(sample_config_text)
973
my_config.branch.control_files.email = None
974
1015
self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
975
1016
my_config._get_user_id())
976
my_config.branch.control_files.email = "John"
1017
my_config.branch.control_files.files['email'] = "John"
977
1018
self.assertEqual("John", my_config._get_user_id())
979
1020
def test_BZR_EMAIL_OVERRIDES(self):
1136
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())
1139
1220
class TestAuthenticationConfigFile(tests.TestCase):
1140
1221
"""Test the authentication.conf file matching"""
1156
1237
self.assertEquals({}, conf._get_config())
1157
1238
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1159
def test_broken_config(self):
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):
1160
1245
conf = config.AuthenticationConfig(_file=StringIO('[DEF'))
1161
1246
self.assertRaises(errors.ParseConfigError, conf._get_config)
1248
def test_auth_value_not_boolean(self):
1163
1249
conf = config.AuthenticationConfig(_file=StringIO(
1291
1379
self._got_user_passwd(None, None,
1292
1380
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')
1294
1394
def test_verify_certificates(self):
1295
1395
conf = config.AuthenticationConfig(_file=StringIO(
1354
1454
'SMTP %(user)s@%(host)s:%(port)d password: ',
1355
1455
'smtp', port=10025)
1457
def test_ssh_password_emits_warning(self):
1458
conf = config.AuthenticationConfig(_file=StringIO(
1466
entered_password = 'typed-by-hand'
1467
stdout = tests.StringIOWrapper()
1468
ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1471
# Since the password defined in the authentication config is ignored,
1472
# the user is prompted
1473
self.assertEquals(entered_password,
1474
conf.get_password('ssh', 'bar.org', user='jim'))
1475
self.assertContainsRe(
1476
self._get_log(keep_log_file=True),
1477
'password ignored in section \[ssh with password\]')
1479
def test_ssh_without_password_doesnt_emit_warning(self):
1480
conf = config.AuthenticationConfig(_file=StringIO(
1487
entered_password = 'typed-by-hand'
1488
stdout = tests.StringIOWrapper()
1489
ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1492
# Since the password defined in the authentication config is ignored,
1493
# the user is prompted
1494
self.assertEquals(entered_password,
1495
conf.get_password('ssh', 'bar.org', user='jim'))
1496
# No warning shoud be emitted since there is no password. We are only
1498
self.assertNotContainsRe(
1499
self._get_log(keep_log_file=True),
1500
'password ignored in section \[ssh with password\]')
1358
1503
# FIXME: Once we have a way to declare authentication to all test servers, we
1359
1504
# can implement generic tests.