~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

  • Committer: Alexander Belchenko
  • Date: 2007-08-10 09:04:38 UTC
  • mto: This revision was merged to the branch mainline in revision 2694.
  • Revision ID: bialix@ukr.net-20070810090438-0835xdz0rl8825qv
fixes after Ian's review

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
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
20
21
from cStringIO import StringIO
21
22
import os
22
23
import sys
23
24
 
24
25
#import bzrlib specific imports here
25
26
from bzrlib import (
26
 
    branch,
27
 
    bzrdir,
28
27
    config,
29
28
    errors,
30
29
    osutils,
31
 
    mail_client,
32
 
    ui,
33
30
    urlutils,
34
 
    tests,
35
31
    trace,
36
32
    )
37
 
from bzrlib.util.configobj import configobj
 
33
from bzrlib.branch import Branch
 
34
from bzrlib.bzrdir import BzrDir
 
35
from bzrlib.tests import TestCase, TestCaseInTempDir, TestCaseWithTransport
38
36
 
39
37
 
40
38
sample_long_alias="log -r-15..-1 --line"
180
178
 
181
179
class InstrumentedConfig(config.Config):
182
180
    """An instrumented config that supplies stubs for template methods."""
183
 
 
 
181
    
184
182
    def __init__(self):
185
183
        super(InstrumentedConfig, self).__init__()
186
184
        self._calls = []
202
200
active = True
203
201
nonactive = False
204
202
"""
205
 
 
206
 
 
207
 
class TestConfigObj(tests.TestCase):
208
 
 
 
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)
215
211
 
216
 
    def test_hash_sign_in_value(self):
217
 
        """
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)
220
 
        """
221
 
        co = config.ConfigObj()
222
 
        co['test'] = 'foo#bar'
223
 
        lines = co.write()
224
 
        self.assertEqual(lines, ['test = "foo#bar"'])
225
 
        co2 = config.ConfigObj(lines)
226
 
        self.assertEqual(co2['test'], 'foo#bar')
227
 
 
228
 
 
229
 
erroneous_config = """[section] # line 1
230
 
good=good # line 2
231
 
[section] # line 3
232
 
whocares=notme # line 4
233
 
"""
234
 
 
235
 
 
236
 
class TestConfigObjErrors(tests.TestCase):
237
 
 
238
 
    def test_duplicate_section_name_error_line(self):
239
 
        try:
240
 
            co = configobj.ConfigObj(StringIO(erroneous_config),
241
 
                                     raise_errors=True)
242
 
        except config.configobj.DuplicateError, e:
243
 
            self.assertEqual(3, e.line_number)
244
 
        else:
245
 
            self.fail('Error in config file not detected')
246
 
 
247
 
 
248
 
class TestConfig(tests.TestCase):
 
212
 
 
213
class TestConfig(TestCase):
249
214
 
250
215
    def test_constructs(self):
251
216
        config.Config()
252
 
 
 
217
 
253
218
    def test_no_default_editor(self):
254
219
        self.assertRaises(NotImplementedError, config.Config().get_editor)
255
220
 
301
266
        self.assertEqual('long', my_config.log_format())
302
267
 
303
268
 
304
 
class TestConfigPath(tests.TestCase):
 
269
class TestConfigPath(TestCase):
305
270
 
306
271
    def setUp(self):
307
272
        super(TestConfigPath, self).setUp()
309
274
        if sys.platform == 'win32':
310
275
            os.environ['BZR_HOME'] = \
311
276
                r'C:\Documents and Settings\bogus\Application Data'
312
 
            self.bzr_home = \
313
 
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0'
314
 
        else:
315
 
            self.bzr_home = '/home/bogus/.bazaar'
316
277
 
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')
 
282
        else:
 
283
            self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
319
284
 
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')
 
289
        else:
 
290
            self.assertEqual(config.config_filename(),
 
291
                             '/home/bogus/.bazaar/bazaar.conf')
323
292
 
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')
 
297
        else:
 
298
            self.assertEqual(config.branches_config_filename(),
 
299
                             '/home/bogus/.bazaar/branches.conf')
327
300
 
328
301
    def test_locations_config_filename(self):
329
 
        self.assertEqual(config.locations_config_filename(),
330
 
                         self.bzr_home + '/locations.conf')
331
 
 
332
 
    def test_authentication_config_filename(self):
333
 
        self.assertEqual(config.authentication_config_filename(),
334
 
                         self.bzr_home + '/authentication.conf')
335
 
 
336
 
 
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')
 
305
        else:
 
306
            self.assertEqual(config.locations_config_filename(),
 
307
                             '/home/bogus/.bazaar/locations.conf')
 
308
 
 
309
class TestIniConfig(TestCase):
338
310
 
339
311
    def test_contructs(self):
340
312
        my_config = config.IniBasedConfig("nothing")
344
316
        my_config = config.IniBasedConfig(None)
345
317
        self.failUnless(
346
318
            isinstance(my_config._get_parser(file=config_file),
347
 
                        configobj.ConfigObj))
 
319
                        ConfigObj))
348
320
 
349
321
    def test_cached(self):
350
322
        config_file = StringIO(sample_config_text.encode('utf-8'))
353
325
        self.failUnless(my_config._get_parser() is parser)
354
326
 
355
327
 
356
 
class TestGetConfig(tests.TestCase):
 
328
class TestGetConfig(TestCase):
357
329
 
358
330
    def test_constructs(self):
359
331
        my_config = config.GlobalConfig()
360
332
 
361
333
    def test_calls_read_filenames(self):
362
 
        # replace the class that is constructed, to check its parameters
 
334
        # replace the class that is constructured, to check its parameters
363
335
        oldparserclass = config.ConfigObj
364
336
        config.ConfigObj = InstrumentedConfigObj
365
337
        my_config = config.GlobalConfig()
372
344
                                          'utf-8')])
373
345
 
374
346
 
375
 
class TestBranchConfig(tests.TestCaseWithTransport):
 
347
class TestBranchConfig(TestCaseWithTransport):
376
348
 
377
349
    def test_constructs(self):
378
350
        branch = FakeBranch()
388
360
 
389
361
    def test_get_config(self):
390
362
        """The Branch.get_config method works properly"""
391
 
        b = bzrdir.BzrDir.create_standalone_workingtree('.').branch
 
363
        b = BzrDir.create_standalone_workingtree('.').branch
392
364
        my_config = b.get_config()
393
365
        self.assertIs(my_config.get_user_option('wacky'), None)
394
366
        my_config.set_user_option('wacky', 'unlikely')
395
367
        self.assertEqual(my_config.get_user_option('wacky'), 'unlikely')
396
368
 
397
369
        # Ensure we get the same thing if we start again
398
 
        b2 = branch.Branch.open('.')
 
370
        b2 = Branch.open('.')
399
371
        my_config2 = b2.get_config()
400
372
        self.assertEqual(my_config2.get_user_option('wacky'), 'unlikely')
401
373
 
436
408
        local_path = osutils.getcwd().encode('utf8')
437
409
        # Surprisingly ConfigObj doesn't create a trailing newline
438
410
        self.check_file_contents(locations,
439
 
                                 '[%s/branch]\n'
440
 
                                 'push_location = http://foobar\n'
441
 
                                 'push_location:policy = norecurse\n'
442
 
                                 % (local_path,))
 
411
            '[%s/branch]\npush_location = http://foobar\npush_location:policy = norecurse' % (local_path,))
443
412
 
444
413
    def test_autonick_urlencoded(self):
445
414
        b = self.make_branch('!repo')
483
452
            trace.warning = _warning
484
453
 
485
454
 
486
 
class TestGlobalConfigItems(tests.TestCase):
 
455
class TestGlobalConfigItems(TestCase):
487
456
 
488
457
    def test_user_id(self):
489
458
        config_file = StringIO(sample_config_text.encode('utf-8'))
563
532
        my_config = self._get_sample_config()
564
533
        self.assertEqual("something",
565
534
                         my_config.get_user_option('user_global_option'))
566
 
 
 
535
        
567
536
    def test_post_commit_default(self):
568
537
        my_config = self._get_sample_config()
569
538
        self.assertEqual(None, my_config.post_commit())
585
554
        self.assertEqual(sample_long_alias, my_config.get_alias('ll'))
586
555
 
587
556
 
588
 
class TestLocationConfig(tests.TestCaseInTempDir):
 
557
class TestLocationConfig(TestCaseInTempDir):
589
558
 
590
559
    def test_constructs(self):
591
560
        my_config = config.LocationConfig('http://example.com')
595
564
        # This is testing the correct file names are provided.
596
565
        # TODO: consolidate with the test for GlobalConfigs filename checks.
597
566
        #
598
 
        # replace the class that is constructed, to check its parameters
 
567
        # replace the class that is constructured, to check its parameters
599
568
        oldparserclass = config.ConfigObj
600
569
        config.ConfigObj = InstrumentedConfigObj
601
570
        try:
629
598
    def test__get_matching_sections_no_match(self):
630
599
        self.get_branch_config('/')
631
600
        self.assertEqual([], self.my_location_config._get_matching_sections())
632
 
 
 
601
        
633
602
    def test__get_matching_sections_exact(self):
634
603
        self.get_branch_config('http://www.example.com')
635
604
        self.assertEqual([('http://www.example.com', '')],
636
605
                         self.my_location_config._get_matching_sections())
637
 
 
 
606
   
638
607
    def test__get_matching_sections_suffix_does_not(self):
639
608
        self.get_branch_config('http://www.example.com-com')
640
609
        self.assertEqual([], self.my_location_config._get_matching_sections())
652
621
    def test__get_matching_sections_ignoreparent_subdir(self):
653
622
        self.get_branch_config(
654
623
            'http://www.example.com/ignoreparent/childbranch')
655
 
        self.assertEqual([('http://www.example.com/ignoreparent',
656
 
                           'childbranch')],
 
624
        self.assertEqual([('http://www.example.com/ignoreparent', 'childbranch')],
657
625
                         self.my_location_config._get_matching_sections())
658
626
 
659
627
    def test__get_matching_sections_subdir_trailing_slash(self):
739
707
        self.get_branch_config('/a/c')
740
708
        self.assertEqual(config.CHECK_NEVER,
741
709
                         self.my_config.signature_checking())
742
 
 
 
710
        
743
711
    def test_signatures_when_available(self):
744
712
        self.get_branch_config('/a/', global_config=sample_ignore_signatures)
745
713
        self.assertEqual(config.CHECK_IF_POSSIBLE,
746
714
                         self.my_config.signature_checking())
747
 
 
 
715
        
748
716
    def test_signatures_always(self):
749
717
        self.get_branch_config('/b')
750
718
        self.assertEqual(config.CHECK_ALWAYS,
751
719
                         self.my_config.signature_checking())
752
 
 
 
720
        
753
721
    def test_gpg_signing_command(self):
754
722
        self.get_branch_config('/b')
755
723
        self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
909
877
        self.assertIs(self.my_config.get_user_option('foo'), None)
910
878
        self.my_config.set_user_option('foo', 'bar')
911
879
        self.assertEqual(
912
 
            self.my_config.branch.control_files.files['branch.conf'],
913
 
            'foo = bar\n')
 
880
            self.my_config.branch.control_files.files['branch.conf'], 
 
881
            'foo = bar')
914
882
        self.assertEqual(self.my_config.get_user_option('foo'), 'bar')
915
883
        self.my_config.set_user_option('foo', 'baz',
916
884
                                       store=config.STORE_LOCATION)
917
885
        self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
918
886
        self.my_config.set_user_option('foo', 'qux')
919
887
        self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
920
 
 
921
 
    def test_get_bzr_remote_path(self):
922
 
        my_config = config.LocationConfig('/a/c')
923
 
        self.assertEqual('bzr', my_config.get_bzr_remote_path())
924
 
        my_config.set_user_option('bzr_remote_path', '/path-bzr')
925
 
        self.assertEqual('/path-bzr', my_config.get_bzr_remote_path())
926
 
        os.environ['BZR_REMOTE_PATH'] = '/environ-bzr'
927
 
        self.assertEqual('/environ-bzr', my_config.get_bzr_remote_path())
928
 
 
 
888
        
929
889
 
930
890
precedence_global = 'option = global'
931
891
precedence_branch = 'option = branch'
938
898
"""
939
899
 
940
900
 
941
 
class TestBranchConfigItems(tests.TestCaseInTempDir):
 
901
class TestBranchConfigItems(TestCaseInTempDir):
942
902
 
943
 
    def get_branch_config(self, global_config=None, location=None,
 
903
    def get_branch_config(self, global_config=None, location=None, 
944
904
                          location_config=None, branch_data_config=None):
945
905
        my_config = config.BranchConfig(FakeBranch(location))
946
906
        if global_config is not None:
961
921
        self.assertEqual("Robert Collins <robertc@example.net>",
962
922
                         my_config.username())
963
923
        branch.control_files.email = "John"
964
 
        my_config.set_user_option('email',
 
924
        my_config.set_user_option('email', 
965
925
                                  "Robert Collins <robertc@example.org>")
966
926
        self.assertEqual("John", my_config.username())
967
927
        branch.control_files.email = None
982
942
        my_config = config.BranchConfig(branch)
983
943
        self.assertEqual("Robert Collins <robertc@example.org>",
984
944
                         my_config.username())
985
 
 
 
945
    
986
946
    def test_signatures_forced(self):
987
947
        my_config = self.get_branch_config(
988
948
            global_config=sample_always_signatures)
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')
1047
1007
 
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)
1052
 
 
1053
 
        # Specific clients
1054
 
        config.set_user_option('mail_client', 'evolution')
1055
 
        client = config.get_mail_client()
1056
 
        self.assertIsInstance(client, mail_client.Evolution)
1057
 
 
1058
 
        config.set_user_option('mail_client', 'kmail')
1059
 
        client = config.get_mail_client()
1060
 
        self.assertIsInstance(client, mail_client.KMail)
1061
 
 
1062
 
        config.set_user_option('mail_client', 'mutt')
1063
 
        client = config.get_mail_client()
1064
 
        self.assertIsInstance(client, mail_client.Mutt)
1065
 
 
1066
 
        config.set_user_option('mail_client', 'thunderbird')
1067
 
        client = config.get_mail_client()
1068
 
        self.assertIsInstance(client, mail_client.Thunderbird)
1069
 
 
1070
 
        # Generic options
1071
 
        config.set_user_option('mail_client', 'default')
1072
 
        client = config.get_mail_client()
1073
 
        self.assertIsInstance(client, mail_client.DefaultMail)
1074
 
 
1075
 
        config.set_user_option('mail_client', 'editor')
1076
 
        client = config.get_mail_client()
1077
 
        self.assertIsInstance(client, mail_client.Editor)
1078
 
 
1079
 
        config.set_user_option('mail_client', 'mapi')
1080
 
        client = config.get_mail_client()
1081
 
        self.assertIsInstance(client, mail_client.MAPIClient)
1082
 
 
1083
 
        config.set_user_option('mail_client', 'xdg-email')
1084
 
        client = config.get_mail_client()
1085
 
        self.assertIsInstance(client, mail_client.XDGEmail)
1086
 
 
1087
 
        config.set_user_option('mail_client', 'firebird')
1088
 
        self.assertRaises(errors.UnknownMailClient, config.get_mail_client)
1089
 
 
1090
 
 
1091
 
class TestMailAddressExtraction(tests.TestCase):
 
1008
 
 
1009
class TestMailAddressExtraction(TestCase):
1092
1010
 
1093
1011
    def test_extract_email_address(self):
1094
1012
        self.assertEqual('jane@test.com',
1096
1014
        self.assertRaises(errors.NoEmailInUsername,
1097
1015
                          config.extract_email_address, 'Jane Tester')
1098
1016
 
1099
 
    def test_parse_username(self):
1100
 
        self.assertEqual(('', 'jdoe@example.com'),
1101
 
                         config.parse_username('jdoe@example.com'))
1102
 
        self.assertEqual(('', 'jdoe@example.com'),
1103
 
                         config.parse_username('<jdoe@example.com>'))
1104
 
        self.assertEqual(('John Doe', 'jdoe@example.com'),
1105
 
                         config.parse_username('John Doe <jdoe@example.com>'))
1106
 
        self.assertEqual(('John Doe', ''),
1107
 
                         config.parse_username('John Doe'))
1108
 
        self.assertEqual(('John Doe', 'jdoe@example.com'),
1109
 
                         config.parse_username('John Doe jdoe@example.com'))
1110
1017
 
1111
 
class TestTreeConfig(tests.TestCaseWithTransport):
 
1018
class TestTreeConfig(TestCaseWithTransport):
1112
1019
 
1113
1020
    def test_get_value(self):
1114
1021
        """Test that retreiving a value from a section is possible"""
1134
1041
        self.assertEqual(value, 'value3-top')
1135
1042
        value = tree_config.get_option('key3', 'SECTION')
1136
1043
        self.assertEqual(value, 'value3-section')
1137
 
 
1138
 
 
1139
 
class TestAuthenticationConfigFile(tests.TestCase):
1140
 
    """Test the authentication.conf file matching"""
1141
 
 
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:
1146
 
            user = None
1147
 
            password = None
1148
 
        else:
1149
 
            user = credentials['user']
1150
 
            password = credentials['password']
1151
 
        self.assertEquals(expected_user, user)
1152
 
        self.assertEquals(expected_password, password)
1153
 
 
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')
1158
 
 
1159
 
    def test_broken_config(self):
1160
 
        conf = config.AuthenticationConfig(_file=StringIO('[DEF'))
1161
 
        self.assertRaises(errors.ParseConfigError, conf._get_config)
1162
 
 
1163
 
        conf = config.AuthenticationConfig(_file=StringIO(
1164
 
                """[broken]
1165
 
scheme=ftp
1166
 
user=joe
1167
 
verify_certificates=askme # Error: Not a boolean
1168
 
"""))
1169
 
        self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1170
 
        conf = config.AuthenticationConfig(_file=StringIO(
1171
 
                """[broken]
1172
 
scheme=ftp
1173
 
user=joe
1174
 
port=port # Error: Not an int
1175
 
"""))
1176
 
        self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1177
 
 
1178
 
    def test_credentials_for_scheme_host(self):
1179
 
        conf = config.AuthenticationConfig(_file=StringIO(
1180
 
                """# Identity on foo.net
1181
 
[ftp definition]
1182
 
scheme=ftp
1183
 
host=foo.net
1184
 
user=joe
1185
 
password=secret-pass
1186
 
"""))
1187
 
        # Basic matching
1188
 
        self._got_user_passwd('joe', 'secret-pass', conf, 'ftp', 'foo.net')
1189
 
        # different scheme
1190
 
        self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1191
 
        # different host
1192
 
        self._got_user_passwd(None, None, conf, 'ftp', 'bar.net')
1193
 
 
1194
 
    def test_credentials_for_host_port(self):
1195
 
        conf = config.AuthenticationConfig(_file=StringIO(
1196
 
                """# Identity on foo.net
1197
 
[ftp definition]
1198
 
scheme=ftp
1199
 
port=10021
1200
 
host=foo.net
1201
 
user=joe
1202
 
password=secret-pass
1203
 
"""))
1204
 
        # No port
1205
 
        self._got_user_passwd('joe', 'secret-pass',
1206
 
                              conf, 'ftp', 'foo.net', port=10021)
1207
 
        # different port
1208
 
        self._got_user_passwd(None, None, conf, 'ftp', 'foo.net')
1209
 
 
1210
 
    def test_for_matching_host(self):
1211
 
        conf = config.AuthenticationConfig(_file=StringIO(
1212
 
                """# Identity on foo.net
1213
 
[sourceforge]
1214
 
scheme=bzr
1215
 
host=bzr.sf.net
1216
 
user=joe
1217
 
password=joepass
1218
 
[sourceforge domain]
1219
 
scheme=bzr
1220
 
host=.bzr.sf.net
1221
 
user=georges
1222
 
password=bendover
1223
 
"""))
1224
 
        # matching domain
1225
 
        self._got_user_passwd('georges', 'bendover',
1226
 
                              conf, 'bzr', 'foo.bzr.sf.net')
1227
 
        # phishing attempt
1228
 
        self._got_user_passwd(None, None,
1229
 
                              conf, 'bzr', 'bbzr.sf.net')
1230
 
 
1231
 
    def test_for_matching_host_None(self):
1232
 
        conf = config.AuthenticationConfig(_file=StringIO(
1233
 
                """# Identity on foo.net
1234
 
[catchup bzr]
1235
 
scheme=bzr
1236
 
user=joe
1237
 
password=joepass
1238
 
[DEFAULT]
1239
 
user=georges
1240
 
password=bendover
1241
 
"""))
1242
 
        # match no host
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')
1248
 
 
1249
 
    def test_credentials_for_path(self):
1250
 
        conf = config.AuthenticationConfig(_file=StringIO(
1251
 
                """
1252
 
[http dir1]
1253
 
scheme=http
1254
 
host=bar.org
1255
 
path=/dir1
1256
 
user=jim
1257
 
password=jimpass
1258
 
[http dir2]
1259
 
scheme=http
1260
 
host=bar.org
1261
 
path=/dir2
1262
 
user=georges
1263
 
password=bendover
1264
 
"""))
1265
 
        # no path no dice
1266
 
        self._got_user_passwd(None, None,
1267
 
                              conf, 'http', host='bar.org', path='/dir3')
1268
 
        # matching path
1269
 
        self._got_user_passwd('georges', 'bendover',
1270
 
                              conf, 'http', host='bar.org', path='/dir2')
1271
 
        # matching subdir
1272
 
        self._got_user_passwd('jim', 'jimpass',
1273
 
                              conf, 'http', host='bar.org',path='/dir1/subdir')
1274
 
 
1275
 
    def test_credentials_for_user(self):
1276
 
        conf = config.AuthenticationConfig(_file=StringIO(
1277
 
                """
1278
 
[with user]
1279
 
scheme=http
1280
 
host=bar.org
1281
 
user=jim
1282
 
password=jimpass
1283
 
"""))
1284
 
        # Get user
1285
 
        self._got_user_passwd('jim', 'jimpass',
1286
 
                              conf, 'http', 'bar.org')
1287
 
        # Get same user
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')
1293
 
 
1294
 
    def test_verify_certificates(self):
1295
 
        conf = config.AuthenticationConfig(_file=StringIO(
1296
 
                """
1297
 
[self-signed]
1298
 
scheme=https
1299
 
host=bar.org
1300
 
user=jim
1301
 
password=jimpass
1302
 
verify_certificates=False
1303
 
[normal]
1304
 
scheme=https
1305
 
host=foo.net
1306
 
user=georges
1307
 
password=bendover
1308
 
"""))
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'))
1313
 
 
1314
 
 
1315
 
class TestAuthenticationConfig(tests.TestCase):
1316
 
    """Test AuthenticationConfig behaviour"""
1317
 
 
1318
 
    def _check_default_prompt(self, expected_prompt_format, scheme,
1319
 
                              host=None, port=None, realm=None, path=None):
1320
 
        if host is None:
1321
 
            host = 'bar.org'
1322
 
        user, password = 'jim', 'precious'
1323
 
        expected_prompt = expected_prompt_format % {
1324
 
            'scheme': scheme, 'host': host, 'port': port,
1325
 
            'user': user, 'realm': realm}
1326
 
 
1327
 
        stdout = tests.StringIOWrapper()
1328
 
        ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
1329
 
                                            stdout=stdout)
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)
1336
 
 
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: ',
1341
 
                                   'ftp', port=10020)
1342
 
 
1343
 
        self._check_default_prompt('SSH %(user)s@%(host)s:%(port)d password: ',
1344
 
                                   'ssh', port=12345)
1345
 
        # SMTP port handling is a bit special (it's handled if embedded in the
1346
 
        # host too)
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: ',
1350
 
                                   'smtp')
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: ',
1355
 
            'smtp', port=10025)
1356
 
 
1357
 
 
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):
1367
 
    pass