18
18
"""Tests for finding and reading the bzr config file[s]."""
19
19
# import system imports here
20
from bzrlib.util.configobj.configobj import ConfigObj, ConfigObjError
21
20
from cStringIO import StringIO
25
24
#import bzrlib specific imports here
26
import bzrlib.config as config
27
from bzrlib.branch import Branch
28
from bzrlib.bzrdir import BzrDir
29
import bzrlib.errors as errors
30
from bzrlib.tests import TestCase, TestCaseInTempDir, TestCaseWithTransport
38
from bzrlib.util.configobj import configobj
33
41
sample_long_alias="log -r-15..-1 --line"
34
sample_config_text = ("[DEFAULT]\n"
35
u"email=Erik B\u00e5gfors <erik@bagfors.nu>\n"
37
"gpg_signing_command=gnome-gpg\n"
39
"user_global_option=something\n"
42
"ll=" + sample_long_alias + "\n")
45
sample_always_signatures = ("[DEFAULT]\n"
46
"check_signatures=ignore\n"
47
"create_signatures=always")
50
sample_ignore_signatures = ("[DEFAULT]\n"
51
"check_signatures=require\n"
52
"create_signatures=never")
55
sample_maybe_signatures = ("[DEFAULT]\n"
56
"check_signatures=ignore\n"
57
"create_signatures=when-required")
60
sample_branches_text = ("[http://www.example.com]\n"
61
"# Top level policy\n"
62
"email=Robert Collins <robertc@example.org>\n"
63
"[http://www.example.com/useglobal]\n"
64
"# different project, forces global lookup\n"
67
"check_signatures=require\n"
68
"# test trailing / matching with no children\n"
70
"check_signatures=check-available\n"
71
"gpg_signing_command=false\n"
72
"user_local_option=local\n"
73
"# test trailing / matching\n"
75
"#subdirs will match but not the parent\n"
78
"check_signatures=ignore\n"
79
"post_commit=bzrlib.tests.test_config.post_commit\n"
80
"#testing explicit beats globs\n")
42
sample_config_text = u"""
44
email=Erik B\u00e5gfors <erik@bagfors.nu>
46
gpg_signing_command=gnome-gpg
48
user_global_option=something
51
ll=""" + sample_long_alias + "\n"
54
sample_always_signatures = """
56
check_signatures=ignore
57
create_signatures=always
60
sample_ignore_signatures = """
62
check_signatures=require
63
create_signatures=never
66
sample_maybe_signatures = """
68
check_signatures=ignore
69
create_signatures=when-required
72
sample_branches_text = """
73
[http://www.example.com]
75
email=Robert Collins <robertc@example.org>
76
normal_option = normal
77
appendpath_option = append
78
appendpath_option:policy = appendpath
79
norecurse_option = norecurse
80
norecurse_option:policy = norecurse
81
[http://www.example.com/ignoreparent]
82
# different project: ignore parent dir config
84
[http://www.example.com/norecurse]
85
# configuration items that only apply to this dir
87
normal_option = norecurse
88
[http://www.example.com/dir]
89
appendpath_option = normal
91
check_signatures=require
92
# test trailing / matching with no children
94
check_signatures=check-available
95
gpg_signing_command=false
96
user_local_option=local
97
# test trailing / matching
99
#subdirs will match but not the parent
101
check_signatures=ignore
102
post_commit=bzrlib.tests.test_config.post_commit
103
#testing explicit beats globs
84
107
class InstrumentedConfigObj(object):
498
668
self.failUnless(isinstance(global_config, config.GlobalConfig))
499
669
self.failUnless(global_config is my_config._get_global_config())
501
def test__get_section_no_match(self):
671
def test__get_matching_sections_no_match(self):
502
672
self.get_branch_config('/')
503
self.assertEqual(None, self.my_location_config._get_section())
505
def test__get_section_exact(self):
673
self.assertEqual([], self.my_location_config._get_matching_sections())
675
def test__get_matching_sections_exact(self):
506
676
self.get_branch_config('http://www.example.com')
507
self.assertEqual('http://www.example.com',
508
self.my_location_config._get_section())
510
def test__get_section_suffix_does_not(self):
677
self.assertEqual([('http://www.example.com', '')],
678
self.my_location_config._get_matching_sections())
680
def test__get_matching_sections_suffix_does_not(self):
511
681
self.get_branch_config('http://www.example.com-com')
512
self.assertEqual(None, self.my_location_config._get_section())
682
self.assertEqual([], self.my_location_config._get_matching_sections())
514
def test__get_section_subdir_recursive(self):
684
def test__get_matching_sections_subdir_recursive(self):
515
685
self.get_branch_config('http://www.example.com/com')
516
self.assertEqual('http://www.example.com',
517
self.my_location_config._get_section())
519
def test__get_section_subdir_matches(self):
520
self.get_branch_config('http://www.example.com/useglobal')
521
self.assertEqual('http://www.example.com/useglobal',
522
self.my_location_config._get_section())
524
def test__get_section_subdir_nonrecursive(self):
686
self.assertEqual([('http://www.example.com', 'com')],
687
self.my_location_config._get_matching_sections())
689
def test__get_matching_sections_ignoreparent(self):
690
self.get_branch_config('http://www.example.com/ignoreparent')
691
self.assertEqual([('http://www.example.com/ignoreparent', '')],
692
self.my_location_config._get_matching_sections())
694
def test__get_matching_sections_ignoreparent_subdir(self):
525
695
self.get_branch_config(
526
'http://www.example.com/useglobal/childbranch')
527
self.assertEqual('http://www.example.com',
528
self.my_location_config._get_section())
696
'http://www.example.com/ignoreparent/childbranch')
697
self.assertEqual([('http://www.example.com/ignoreparent',
699
self.my_location_config._get_matching_sections())
530
def test__get_section_subdir_trailing_slash(self):
701
def test__get_matching_sections_subdir_trailing_slash(self):
531
702
self.get_branch_config('/b')
532
self.assertEqual('/b/', self.my_location_config._get_section())
703
self.assertEqual([('/b/', '')],
704
self.my_location_config._get_matching_sections())
534
def test__get_section_subdir_child(self):
706
def test__get_matching_sections_subdir_child(self):
535
707
self.get_branch_config('/a/foo')
536
self.assertEqual('/a/*', self.my_location_config._get_section())
708
self.assertEqual([('/a/*', ''), ('/a/', 'foo')],
709
self.my_location_config._get_matching_sections())
538
def test__get_section_subdir_child_child(self):
711
def test__get_matching_sections_subdir_child_child(self):
539
712
self.get_branch_config('/a/foo/bar')
540
self.assertEqual('/a/', self.my_location_config._get_section())
713
self.assertEqual([('/a/*', 'bar'), ('/a/', 'foo/bar')],
714
self.my_location_config._get_matching_sections())
542
def test__get_section_trailing_slash_with_children(self):
716
def test__get_matching_sections_trailing_slash_with_children(self):
543
717
self.get_branch_config('/a/')
544
self.assertEqual('/a/', self.my_location_config._get_section())
718
self.assertEqual([('/a/', '')],
719
self.my_location_config._get_matching_sections())
546
def test__get_section_explicit_over_glob(self):
721
def test__get_matching_sections_explicit_over_glob(self):
722
# XXX: 2006-09-08 jamesh
723
# This test only passes because ord('c') > ord('*'). If there
724
# was a config section for '/a/?', it would get precedence
547
726
self.get_branch_config('/a/c')
548
self.assertEqual('/a/c', self.my_location_config._get_section())
727
self.assertEqual([('/a/c', ''), ('/a/*', ''), ('/a/', 'c')],
728
self.my_location_config._get_matching_sections())
730
def test__get_option_policy_normal(self):
731
self.get_branch_config('http://www.example.com')
733
self.my_location_config._get_config_policy(
734
'http://www.example.com', 'normal_option'),
737
def test__get_option_policy_norecurse(self):
738
self.get_branch_config('http://www.example.com')
740
self.my_location_config._get_option_policy(
741
'http://www.example.com', 'norecurse_option'),
742
config.POLICY_NORECURSE)
743
# Test old recurse=False setting:
745
self.my_location_config._get_option_policy(
746
'http://www.example.com/norecurse', 'normal_option'),
747
config.POLICY_NORECURSE)
749
def test__get_option_policy_normal(self):
750
self.get_branch_config('http://www.example.com')
752
self.my_location_config._get_option_policy(
753
'http://www.example.com', 'appendpath_option'),
754
config.POLICY_APPENDPATH)
551
756
def test_location_without_username(self):
552
self.get_branch_config('http://www.example.com/useglobal')
757
self.get_branch_config('http://www.example.com/ignoreparent')
553
758
self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
554
759
self.my_config.username())
604
809
self.get_branch_config('/a')
605
810
self.assertEqual('local',
606
811
self.my_config.get_user_option('user_local_option'))
813
def test_get_user_option_appendpath(self):
814
# returned as is for the base path:
815
self.get_branch_config('http://www.example.com')
816
self.assertEqual('append',
817
self.my_config.get_user_option('appendpath_option'))
818
# Extra path components get appended:
819
self.get_branch_config('http://www.example.com/a/b/c')
820
self.assertEqual('append/a/b/c',
821
self.my_config.get_user_option('appendpath_option'))
822
# Overriden for http://www.example.com/dir, where it is a
824
self.get_branch_config('http://www.example.com/dir/a/b/c')
825
self.assertEqual('normal',
826
self.my_config.get_user_option('appendpath_option'))
828
def test_get_user_option_norecurse(self):
829
self.get_branch_config('http://www.example.com')
830
self.assertEqual('norecurse',
831
self.my_config.get_user_option('norecurse_option'))
832
self.get_branch_config('http://www.example.com/dir')
833
self.assertEqual(None,
834
self.my_config.get_user_option('norecurse_option'))
835
# http://www.example.com/norecurse is a recurse=False section
836
# that redefines normal_option. Subdirectories do not pick up
838
self.get_branch_config('http://www.example.com/norecurse')
839
self.assertEqual('norecurse',
840
self.my_config.get_user_option('normal_option'))
841
self.get_branch_config('http://www.example.com/norecurse/subdir')
842
self.assertEqual('normal',
843
self.my_config.get_user_option('normal_option'))
845
def test_set_user_option_norecurse(self):
846
self.get_branch_config('http://www.example.com')
847
self.my_config.set_user_option('foo', 'bar',
848
store=config.STORE_LOCATION_NORECURSE)
850
self.my_location_config._get_option_policy(
851
'http://www.example.com', 'foo'),
852
config.POLICY_NORECURSE)
854
def test_set_user_option_appendpath(self):
855
self.get_branch_config('http://www.example.com')
856
self.my_config.set_user_option('foo', 'bar',
857
store=config.STORE_LOCATION_APPENDPATH)
859
self.my_location_config._get_option_policy(
860
'http://www.example.com', 'foo'),
861
config.POLICY_APPENDPATH)
863
def test_set_user_option_change_policy(self):
864
self.get_branch_config('http://www.example.com')
865
self.my_config.set_user_option('norecurse_option', 'normal',
866
store=config.STORE_LOCATION)
868
self.my_location_config._get_option_policy(
869
'http://www.example.com', 'norecurse_option'),
872
def test_set_user_option_recurse_false_section(self):
873
# The following section has recurse=False set. The test is to
874
# make sure that a normal option can be added to the section,
875
# converting recurse=False to the norecurse policy.
876
self.get_branch_config('http://www.example.com/norecurse')
877
self.callDeprecated(['The recurse option is deprecated as of 0.14. '
878
'The section "http://www.example.com/norecurse" '
879
'has been converted to use policies.'],
880
self.my_config.set_user_option,
881
'foo', 'bar', store=config.STORE_LOCATION)
883
self.my_location_config._get_option_policy(
884
'http://www.example.com/norecurse', 'foo'),
886
# The previously existing option is still norecurse:
888
self.my_location_config._get_option_policy(
889
'http://www.example.com/norecurse', 'normal_option'),
890
config.POLICY_NORECURSE)
608
892
def test_post_commit_default(self):
609
893
self.get_branch_config('/a/c')
610
894
self.assertEqual('bzrlib.tests.test_config.post_commit',
762
1066
# post-commit is ignored when bresent in branch data
763
1067
self.assertEqual('bzrlib.tests.test_config.post_commit',
764
1068
my_config.post_commit())
765
my_config.set_user_option('post_commit', 'rmtree_root', local=True)
1069
my_config.set_user_option('post_commit', 'rmtree_root',
1070
store=config.STORE_LOCATION)
766
1071
self.assertEqual('rmtree_root', my_config.post_commit())
768
1073
def test_config_precedence(self):
769
1074
my_config = self.get_branch_config(global_config=precedence_global)
770
1075
self.assertEqual(my_config.get_user_option('option'), 'global')
771
my_config = self.get_branch_config(global_config=precedence_global,
1076
my_config = self.get_branch_config(global_config=precedence_global,
772
1077
branch_data_config=precedence_branch)
773
1078
self.assertEqual(my_config.get_user_option('option'), 'branch')
774
my_config = self.get_branch_config(global_config=precedence_global,
1079
my_config = self.get_branch_config(global_config=precedence_global,
775
1080
branch_data_config=precedence_branch,
776
1081
location_config=precedence_location)
777
1082
self.assertEqual(my_config.get_user_option('option'), 'recurse')
778
my_config = self.get_branch_config(global_config=precedence_global,
1083
my_config = self.get_branch_config(global_config=precedence_global,
779
1084
branch_data_config=precedence_branch,
780
1085
location_config=precedence_location,
781
1086
location='http://example.com/specific')
782
1087
self.assertEqual(my_config.get_user_option('option'), 'exact')
785
class TestMailAddressExtraction(TestCase):
1089
def test_get_mail_client(self):
1090
config = self.get_branch_config()
1091
client = config.get_mail_client()
1092
self.assertIsInstance(client, mail_client.DefaultMail)
1095
config.set_user_option('mail_client', 'evolution')
1096
client = config.get_mail_client()
1097
self.assertIsInstance(client, mail_client.Evolution)
1099
config.set_user_option('mail_client', 'kmail')
1100
client = config.get_mail_client()
1101
self.assertIsInstance(client, mail_client.KMail)
1103
config.set_user_option('mail_client', 'mutt')
1104
client = config.get_mail_client()
1105
self.assertIsInstance(client, mail_client.Mutt)
1107
config.set_user_option('mail_client', 'thunderbird')
1108
client = config.get_mail_client()
1109
self.assertIsInstance(client, mail_client.Thunderbird)
1112
config.set_user_option('mail_client', 'default')
1113
client = config.get_mail_client()
1114
self.assertIsInstance(client, mail_client.DefaultMail)
1116
config.set_user_option('mail_client', 'editor')
1117
client = config.get_mail_client()
1118
self.assertIsInstance(client, mail_client.Editor)
1120
config.set_user_option('mail_client', 'mapi')
1121
client = config.get_mail_client()
1122
self.assertIsInstance(client, mail_client.MAPIClient)
1124
config.set_user_option('mail_client', 'xdg-email')
1125
client = config.get_mail_client()
1126
self.assertIsInstance(client, mail_client.XDGEmail)
1128
config.set_user_option('mail_client', 'firebird')
1129
self.assertRaises(errors.UnknownMailClient, config.get_mail_client)
1132
class TestMailAddressExtraction(tests.TestCase):
787
1134
def test_extract_email_address(self):
788
1135
self.assertEqual('jane@test.com',
789
1136
config.extract_email_address('Jane <jane@test.com>'))
790
self.assertRaises(errors.BzrError,
1137
self.assertRaises(errors.NoEmailInUsername,
791
1138
config.extract_email_address, 'Jane Tester')
1140
def test_parse_username(self):
1141
self.assertEqual(('', 'jdoe@example.com'),
1142
config.parse_username('jdoe@example.com'))
1143
self.assertEqual(('', 'jdoe@example.com'),
1144
config.parse_username('<jdoe@example.com>'))
1145
self.assertEqual(('John Doe', 'jdoe@example.com'),
1146
config.parse_username('John Doe <jdoe@example.com>'))
1147
self.assertEqual(('John Doe', ''),
1148
config.parse_username('John Doe'))
1149
self.assertEqual(('John Doe', 'jdoe@example.com'),
1150
config.parse_username('John Doe jdoe@example.com'))
1152
class TestTreeConfig(tests.TestCaseWithTransport):
1154
def test_get_value(self):
1155
"""Test that retreiving a value from a section is possible"""
1156
branch = self.make_branch('.')
1157
tree_config = config.TreeConfig(branch)
1158
tree_config.set_option('value', 'key', 'SECTION')
1159
tree_config.set_option('value2', 'key2')
1160
tree_config.set_option('value3-top', 'key3')
1161
tree_config.set_option('value3-section', 'key3', 'SECTION')
1162
value = tree_config.get_option('key', 'SECTION')
1163
self.assertEqual(value, 'value')
1164
value = tree_config.get_option('key2')
1165
self.assertEqual(value, 'value2')
1166
self.assertEqual(tree_config.get_option('non-existant'), None)
1167
value = tree_config.get_option('non-existant', 'SECTION')
1168
self.assertEqual(value, None)
1169
value = tree_config.get_option('non-existant', default='default')
1170
self.assertEqual(value, 'default')
1171
self.assertEqual(tree_config.get_option('key2', 'NOSECTION'), None)
1172
value = tree_config.get_option('key2', 'NOSECTION', default='default')
1173
self.assertEqual(value, 'default')
1174
value = tree_config.get_option('key3')
1175
self.assertEqual(value, 'value3-top')
1176
value = tree_config.get_option('key3', 'SECTION')
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())
1220
class TestAuthenticationConfigFile(tests.TestCase):
1221
"""Test the authentication.conf file matching"""
1223
def _got_user_passwd(self, expected_user, expected_password,
1224
config, *args, **kwargs):
1225
credentials = config.get_credentials(*args, **kwargs)
1226
if credentials is None:
1230
user = credentials['user']
1231
password = credentials['password']
1232
self.assertEquals(expected_user, user)
1233
self.assertEquals(expected_password, password)
1235
def test_empty_config(self):
1236
conf = config.AuthenticationConfig(_file=StringIO())
1237
self.assertEquals({}, conf._get_config())
1238
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):
1245
conf = config.AuthenticationConfig(_file=StringIO('[DEF'))
1246
self.assertRaises(errors.ParseConfigError, conf._get_config)
1248
def test_auth_value_not_boolean(self):
1249
conf = config.AuthenticationConfig(_file=StringIO(
1253
verify_certificates=askme # Error: Not a boolean
1255
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1257
def test_auth_value_not_int(self):
1258
conf = config.AuthenticationConfig(_file=StringIO(
1262
port=port # Error: Not an int
1264
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1266
def test_credentials_for_scheme_host(self):
1267
conf = config.AuthenticationConfig(_file=StringIO(
1268
"""# Identity on foo.net
1273
password=secret-pass
1276
self._got_user_passwd('joe', 'secret-pass', conf, 'ftp', 'foo.net')
1278
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1280
self._got_user_passwd(None, None, conf, 'ftp', 'bar.net')
1282
def test_credentials_for_host_port(self):
1283
conf = config.AuthenticationConfig(_file=StringIO(
1284
"""# Identity on foo.net
1290
password=secret-pass
1293
self._got_user_passwd('joe', 'secret-pass',
1294
conf, 'ftp', 'foo.net', port=10021)
1296
self._got_user_passwd(None, None, conf, 'ftp', 'foo.net')
1298
def test_for_matching_host(self):
1299
conf = config.AuthenticationConfig(_file=StringIO(
1300
"""# Identity on foo.net
1306
[sourceforge domain]
1313
self._got_user_passwd('georges', 'bendover',
1314
conf, 'bzr', 'foo.bzr.sf.net')
1316
self._got_user_passwd(None, None,
1317
conf, 'bzr', 'bbzr.sf.net')
1319
def test_for_matching_host_None(self):
1320
conf = config.AuthenticationConfig(_file=StringIO(
1321
"""# Identity on foo.net
1331
self._got_user_passwd('joe', 'joepass',
1332
conf, 'bzr', 'quux.net')
1333
# no host but different scheme
1334
self._got_user_passwd('georges', 'bendover',
1335
conf, 'ftp', 'quux.net')
1337
def test_credentials_for_path(self):
1338
conf = config.AuthenticationConfig(_file=StringIO(
1354
self._got_user_passwd(None, None,
1355
conf, 'http', host='bar.org', path='/dir3')
1357
self._got_user_passwd('georges', 'bendover',
1358
conf, 'http', host='bar.org', path='/dir2')
1360
self._got_user_passwd('jim', 'jimpass',
1361
conf, 'http', host='bar.org',path='/dir1/subdir')
1363
def test_credentials_for_user(self):
1364
conf = config.AuthenticationConfig(_file=StringIO(
1373
self._got_user_passwd('jim', 'jimpass',
1374
conf, 'http', 'bar.org')
1376
self._got_user_passwd('jim', 'jimpass',
1377
conf, 'http', 'bar.org', user='jim')
1378
# Don't get a different user if one is specified
1379
self._got_user_passwd(None, None,
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')
1394
def test_verify_certificates(self):
1395
conf = config.AuthenticationConfig(_file=StringIO(
1402
verify_certificates=False
1409
credentials = conf.get_credentials('https', 'bar.org')
1410
self.assertEquals(False, credentials.get('verify_certificates'))
1411
credentials = conf.get_credentials('https', 'foo.net')
1412
self.assertEquals(True, credentials.get('verify_certificates'))
1415
class TestAuthenticationConfig(tests.TestCase):
1416
"""Test AuthenticationConfig behaviour"""
1418
def _check_default_prompt(self, expected_prompt_format, scheme,
1419
host=None, port=None, realm=None, path=None):
1422
user, password = 'jim', 'precious'
1423
expected_prompt = expected_prompt_format % {
1424
'scheme': scheme, 'host': host, 'port': port,
1425
'user': user, 'realm': realm}
1427
stdout = tests.StringIOWrapper()
1428
ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
1430
# We use an empty conf so that the user is always prompted
1431
conf = config.AuthenticationConfig()
1432
self.assertEquals(password,
1433
conf.get_password(scheme, host, user, port=port,
1434
realm=realm, path=path))
1435
self.assertEquals(stdout.getvalue(), expected_prompt)
1437
def test_default_prompts(self):
1438
# HTTP prompts can't be tested here, see test_http.py
1439
self._check_default_prompt('FTP %(user)s@%(host)s password: ', 'ftp')
1440
self._check_default_prompt('FTP %(user)s@%(host)s:%(port)d password: ',
1443
self._check_default_prompt('SSH %(user)s@%(host)s:%(port)d password: ',
1445
# SMTP port handling is a bit special (it's handled if embedded in the
1447
# FIXME: should we: forbid that, extend it to other schemes, leave
1448
# things as they are that's fine thank you ?
1449
self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1451
self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1452
'smtp', host='bar.org:10025')
1453
self._check_default_prompt(
1454
'SMTP %(user)s@%(host)s:%(port)d password: ',
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\]')
1503
# FIXME: Once we have a way to declare authentication to all test servers, we
1504
# can implement generic tests.
1505
# test_user_password_in_url
1506
# test_user_in_url_password_from_config
1507
# test_user_in_url_password_prompted
1508
# test_user_in_config
1509
# test_user_getpass.getuser
1510
# test_user_prompted ?
1511
class TestAuthenticationRing(tests.TestCaseWithTransport):