42
from breezy.symbol_versioning import (
42
from ..sixish import (
45
from ..symbol_versioning import (
45
from breezy.transport import remote as transport_remote
46
from breezy.tests import (
48
from ..transport import remote as transport_remote
51
from breezy.util.configobj import configobj
54
56
def lockable_config_scenarios():
406
408
class TestConfigObj(tests.TestCase):
408
410
def test_get_bool(self):
409
co = config.ConfigObj(StringIO(bool_config))
411
co = config.ConfigObj(BytesIO(bool_config))
410
412
self.assertIs(co.get_bool('DEFAULT', 'active'), True)
411
413
self.assertIs(co.get_bool('DEFAULT', 'inactive'), False)
412
414
self.assertIs(co.get_bool('UPPERCASE', 'active'), True)
443
445
# This issue only affects test, but it's better to avoid
444
446
# `co.write()` construct at all.
445
447
# [bialix 20110222] bug report sent to ConfigObj's author
447
449
co.write(outfile=outfile)
448
450
output = outfile.getvalue()
449
451
# now we're trying to read it back
450
co2 = config.ConfigObj(StringIO(output))
452
co2 = config.ConfigObj(BytesIO(output))
451
453
self.assertEqual(triple_quotes_value, co2['test'])
463
465
def test_duplicate_section_name_error_line(self):
465
co = configobj.ConfigObj(StringIO(erroneous_config),
467
co = configobj.ConfigObj(BytesIO(erroneous_config),
466
468
raise_errors=True)
467
except config.configobj.DuplicateError, e:
469
except config.configobj.DuplicateError as e:
468
470
self.assertEqual(3, e.line_number)
470
472
self.fail('Error in config file not detected')
662
664
self.assertEqual('ini.conf', conf.file_name)
664
666
def test_get_parser_file_parameter_is_deprecated_(self):
665
config_file = StringIO(sample_config_text.encode('utf-8'))
667
config_file = BytesIO(sample_config_text.encode('utf-8'))
666
668
conf = config.IniBasedConfig.from_string(sample_config_text)
667
669
conf = self.callDeprecated([
668
670
'IniBasedConfig._get_parser(file=xxx) was deprecated in 2.3.'
2681
2683
def test_id(self):
2682
2684
store = self.get_store(self)
2683
if type(store) == config.TransportIniFileStore:
2685
if isinstance(store, config.TransportIniFileStore):
2684
2686
raise tests.TestNotApplicable(
2685
2687
"%s is not a concrete Store implementation"
2686
2688
" so it doesn't need an id" % (store.__class__.__name__,))
2690
2692
class TestStore(tests.TestCaseWithTransport):
2692
def assertSectionContent(self, expected, (store, section)):
2694
def assertSectionContent(self, expected, store_and_section):
2693
2695
"""Assert that some options have the proper values in a section."""
2696
_, section = store_and_section
2694
2697
expected_name, expected_options = expected
2695
2698
self.assertEqual(expected_name, section.id)
2696
2699
self.assertEqual(
4446
4449
self.assertEqual(expected_password, password)
4448
4451
def test_empty_config(self):
4449
conf = config.AuthenticationConfig(_file=StringIO())
4452
conf = config.AuthenticationConfig(_file=BytesIO())
4450
4453
self.assertEqual({}, conf._get_config())
4451
4454
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
4453
4456
def test_non_utf8_config(self):
4454
conf = config.AuthenticationConfig(_file=StringIO(
4457
conf = config.AuthenticationConfig(_file=BytesIO(b'foo = bar\xff'))
4456
4458
self.assertRaises(errors.ConfigContentError, conf._get_config)
4458
4460
def test_missing_auth_section_header(self):
4459
conf = config.AuthenticationConfig(_file=StringIO('foo = bar'))
4461
conf = config.AuthenticationConfig(_file=BytesIO(b'foo = bar'))
4460
4462
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
4462
4464
def test_auth_section_header_not_closed(self):
4463
conf = config.AuthenticationConfig(_file=StringIO('[DEF'))
4465
conf = config.AuthenticationConfig(_file=BytesIO(b'[DEF'))
4464
4466
self.assertRaises(errors.ParseConfigError, conf._get_config)
4466
4468
def test_auth_value_not_boolean(self):
4467
conf = config.AuthenticationConfig(_file=StringIO(
4469
conf = config.AuthenticationConfig(_file=BytesIO(b"""\
4471
4473
verify_certificates=askme # Error: Not a boolean
4473
4475
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
4475
4477
def test_auth_value_not_int(self):
4476
conf = config.AuthenticationConfig(_file=StringIO(
4478
conf = config.AuthenticationConfig(_file=BytesIO(b"""\
4480
4482
port=port # Error: Not an int
4482
4484
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
4484
4486
def test_unknown_password_encoding(self):
4485
conf = config.AuthenticationConfig(_file=StringIO(
4487
conf = config.AuthenticationConfig(_file=BytesIO(b"""\
4489
4491
password_encoding=unknown
4508
4510
self._got_user_passwd(None, None, conf, 'ftp', 'bar.net')
4510
4512
def test_credentials_for_host_port(self):
4511
conf = config.AuthenticationConfig(_file=StringIO(
4512
"""# Identity on foo.net
4513
conf = config.AuthenticationConfig(_file=BytesIO(b"""\
4514
# Identity on foo.net
4513
4515
[ftp definition]
4524
4526
self._got_user_passwd(None, None, conf, 'ftp', 'foo.net')
4526
4528
def test_for_matching_host(self):
4527
conf = config.AuthenticationConfig(_file=StringIO(
4528
"""# Identity on foo.net
4529
conf = config.AuthenticationConfig(_file=BytesIO(b"""\
4530
# Identity on foo.net
4531
4533
host=bzr.sf.net
4545
4547
conf, 'bzr', 'bbzr.sf.net')
4547
4549
def test_for_matching_host_None(self):
4548
conf = config.AuthenticationConfig(_file=StringIO(
4549
"""# Identity on foo.net
4550
conf = config.AuthenticationConfig(_file=BytesIO(b"""\
4551
# Identity on foo.net
4589
4590
conf, 'http', host='bar.org',path='/dir1/subdir')
4591
4592
def test_credentials_for_user(self):
4592
conf = config.AuthenticationConfig(_file=StringIO(
4593
conf = config.AuthenticationConfig(_file=BytesIO(b"""
4608
4608
conf, 'http', 'bar.org', user='georges')
4610
4610
def test_credentials_for_user_without_password(self):
4611
conf = config.AuthenticationConfig(_file=StringIO(
4611
conf = config.AuthenticationConfig(_file=BytesIO(b"""
4613
4612
[without password]
4684
4682
'scheme': scheme, 'host': host, 'port': port,
4685
4683
'user': user, 'realm': realm}
4687
stdout = tests.StringIOWrapper()
4688
stderr = tests.StringIOWrapper()
4689
ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
4690
stdout=stdout, stderr=stderr)
4685
ui.ui_factory = tests.TestUIFactory(stdin=password + '\n')
4691
4686
# We use an empty conf so that the user is always prompted
4692
4687
conf = config.AuthenticationConfig()
4693
4688
self.assertEqual(password,
4694
4689
conf.get_password(scheme, host, user, port=port,
4695
4690
realm=realm, path=path))
4696
self.assertEqual(expected_prompt, stderr.getvalue())
4697
self.assertEqual('', stdout.getvalue())
4691
self.assertEqual(expected_prompt, ui.ui_factory.stderr.getvalue())
4692
self.assertEqual('', ui.ui_factory.stdout.getvalue())
4699
4694
def _check_default_username_prompt(self, expected_prompt_format, scheme,
4700
4695
host=None, port=None, realm=None,
4705
4700
expected_prompt = expected_prompt_format % {
4706
4701
'scheme': scheme, 'host': host, 'port': port,
4707
4702
'realm': realm}
4708
stdout = tests.StringIOWrapper()
4709
stderr = tests.StringIOWrapper()
4710
ui.ui_factory = tests.TestUIFactory(stdin=username+ '\n',
4711
stdout=stdout, stderr=stderr)
4703
ui.ui_factory = tests.TestUIFactory(stdin=username+ '\n')
4712
4704
# We use an empty conf so that the user is always prompted
4713
4705
conf = config.AuthenticationConfig()
4714
4706
self.assertEqual(username, conf.get_user(scheme, host, port=port,
4715
4707
realm=realm, path=path, ask=True))
4716
self.assertEqual(expected_prompt, stderr.getvalue())
4717
self.assertEqual('', stdout.getvalue())
4708
self.assertEqual(expected_prompt, ui.ui_factory.stderr.getvalue())
4709
self.assertEqual('', ui.ui_factory.stdout.getvalue())
4719
4711
def test_username_defaults_prompts(self):
4720
4712
# HTTP prompts can't be tested here, see test_http.py
4751
4743
u'SMTP %(user)s@%(host)s:%(port)d password: ', 'smtp', port=10025)
4753
4745
def test_ssh_password_emits_warning(self):
4754
conf = config.AuthenticationConfig(_file=StringIO(
4746
conf = config.AuthenticationConfig(_file=BytesIO(b"""
4756
4747
[ssh with password]
4760
4751
password=jimpass
4762
4753
entered_password = 'typed-by-hand'
4763
stdout = tests.StringIOWrapper()
4764
stderr = tests.StringIOWrapper()
4765
ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
4766
stdout=stdout, stderr=stderr)
4754
ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n')
4768
4756
# Since the password defined in the authentication config is ignored,
4769
4757
# the user is prompted
4774
4762
'password ignored in section \[ssh with password\]')
4776
4764
def test_ssh_without_password_doesnt_emit_warning(self):
4777
conf = config.AuthenticationConfig(_file=StringIO(
4765
conf = config.AuthenticationConfig(_file=BytesIO(b"""
4779
4766
[ssh with password]
4784
4771
entered_password = 'typed-by-hand'
4785
stdout = tests.StringIOWrapper()
4786
stderr = tests.StringIOWrapper()
4787
ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
4772
ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n')
4791
4774
# Since the password defined in the authentication config is ignored,
4792
4775
# the user is prompted
4804
4787
store = StubCredentialStore()
4805
4788
store.add_credentials("http", "example.com", "joe", "secret")
4806
4789
config.credential_store_registry.register("stub", store, fallback=True)
4807
conf = config.AuthenticationConfig(_file=StringIO())
4790
conf = config.AuthenticationConfig(_file=BytesIO())
4808
4791
creds = conf.get_credentials("http", "example.com")
4809
4792
self.assertEqual("joe", creds["user"])
4810
4793
self.assertEqual("secret", creds["password"])