419
606
# This is testing the correct file names are provided.
420
607
# TODO: consolidate with the test for GlobalConfigs filename checks.
422
# replace the class that is constructured, to check its parameters
609
# replace the class that is constructed, to check its parameters
423
610
oldparserclass = config.ConfigObj
424
611
config.ConfigObj = InstrumentedConfigObj
425
my_config = config.LocationConfig('http://www.example.com')
613
my_config = config.LocationConfig('http://www.example.com')
427
614
parser = my_config._get_parser()
429
616
config.ConfigObj = oldparserclass
430
617
self.failUnless(isinstance(parser, InstrumentedConfigObj))
431
618
self.assertEqual(parser._calls,
432
[('__init__', config.branches_config_filename(),
619
[('__init__', config.locations_config_filename(),
621
config.ensure_config_dir_exists()
622
#os.mkdir(config.config_dir())
623
f = file(config.branches_config_filename(), 'wb')
626
oldparserclass = config.ConfigObj
627
config.ConfigObj = InstrumentedConfigObj
629
my_config = config.LocationConfig('http://www.example.com')
630
parser = my_config._get_parser()
632
config.ConfigObj = oldparserclass
435
634
def test_get_global_config(self):
436
my_config = config.LocationConfig('http://example.com')
635
my_config = config.BranchConfig(FakeBranch('http://example.com'))
437
636
global_config = my_config._get_global_config()
438
637
self.failUnless(isinstance(global_config, config.GlobalConfig))
439
638
self.failUnless(global_config is my_config._get_global_config())
441
def test__get_section_no_match(self):
442
self.get_location_config('/')
443
self.assertEqual(None, self.my_config._get_section())
445
def test__get_section_exact(self):
446
self.get_location_config('http://www.example.com')
447
self.assertEqual('http://www.example.com',
448
self.my_config._get_section())
450
def test__get_section_suffix_does_not(self):
451
self.get_location_config('http://www.example.com-com')
452
self.assertEqual(None, self.my_config._get_section())
454
def test__get_section_subdir_recursive(self):
455
self.get_location_config('http://www.example.com/com')
456
self.assertEqual('http://www.example.com',
457
self.my_config._get_section())
459
def test__get_section_subdir_matches(self):
460
self.get_location_config('http://www.example.com/useglobal')
461
self.assertEqual('http://www.example.com/useglobal',
462
self.my_config._get_section())
464
def test__get_section_subdir_nonrecursive(self):
465
self.get_location_config(
466
'http://www.example.com/useglobal/childbranch')
467
self.assertEqual('http://www.example.com',
468
self.my_config._get_section())
470
def test__get_section_subdir_trailing_slash(self):
471
self.get_location_config('/b')
472
self.assertEqual('/b/', self.my_config._get_section())
474
def test__get_section_subdir_child(self):
475
self.get_location_config('/a/foo')
476
self.assertEqual('/a/*', self.my_config._get_section())
478
def test__get_section_subdir_child_child(self):
479
self.get_location_config('/a/foo/bar')
480
self.assertEqual('/a/', self.my_config._get_section())
482
def test__get_section_trailing_slash_with_children(self):
483
self.get_location_config('/a/')
484
self.assertEqual('/a/', self.my_config._get_section())
486
def test__get_section_explicit_over_glob(self):
487
self.get_location_config('/a/c')
488
self.assertEqual('/a/c', self.my_config._get_section())
640
def test__get_matching_sections_no_match(self):
641
self.get_branch_config('/')
642
self.assertEqual([], self.my_location_config._get_matching_sections())
644
def test__get_matching_sections_exact(self):
645
self.get_branch_config('http://www.example.com')
646
self.assertEqual([('http://www.example.com', '')],
647
self.my_location_config._get_matching_sections())
649
def test__get_matching_sections_suffix_does_not(self):
650
self.get_branch_config('http://www.example.com-com')
651
self.assertEqual([], self.my_location_config._get_matching_sections())
653
def test__get_matching_sections_subdir_recursive(self):
654
self.get_branch_config('http://www.example.com/com')
655
self.assertEqual([('http://www.example.com', 'com')],
656
self.my_location_config._get_matching_sections())
658
def test__get_matching_sections_ignoreparent(self):
659
self.get_branch_config('http://www.example.com/ignoreparent')
660
self.assertEqual([('http://www.example.com/ignoreparent', '')],
661
self.my_location_config._get_matching_sections())
663
def test__get_matching_sections_ignoreparent_subdir(self):
664
self.get_branch_config(
665
'http://www.example.com/ignoreparent/childbranch')
666
self.assertEqual([('http://www.example.com/ignoreparent',
668
self.my_location_config._get_matching_sections())
670
def test__get_matching_sections_subdir_trailing_slash(self):
671
self.get_branch_config('/b')
672
self.assertEqual([('/b/', '')],
673
self.my_location_config._get_matching_sections())
675
def test__get_matching_sections_subdir_child(self):
676
self.get_branch_config('/a/foo')
677
self.assertEqual([('/a/*', ''), ('/a/', 'foo')],
678
self.my_location_config._get_matching_sections())
680
def test__get_matching_sections_subdir_child_child(self):
681
self.get_branch_config('/a/foo/bar')
682
self.assertEqual([('/a/*', 'bar'), ('/a/', 'foo/bar')],
683
self.my_location_config._get_matching_sections())
685
def test__get_matching_sections_trailing_slash_with_children(self):
686
self.get_branch_config('/a/')
687
self.assertEqual([('/a/', '')],
688
self.my_location_config._get_matching_sections())
690
def test__get_matching_sections_explicit_over_glob(self):
691
# XXX: 2006-09-08 jamesh
692
# This test only passes because ord('c') > ord('*'). If there
693
# was a config section for '/a/?', it would get precedence
695
self.get_branch_config('/a/c')
696
self.assertEqual([('/a/c', ''), ('/a/*', ''), ('/a/', 'c')],
697
self.my_location_config._get_matching_sections())
699
def test__get_option_policy_normal(self):
700
self.get_branch_config('http://www.example.com')
702
self.my_location_config._get_config_policy(
703
'http://www.example.com', 'normal_option'),
706
def test__get_option_policy_norecurse(self):
707
self.get_branch_config('http://www.example.com')
709
self.my_location_config._get_option_policy(
710
'http://www.example.com', 'norecurse_option'),
711
config.POLICY_NORECURSE)
712
# Test old recurse=False setting:
714
self.my_location_config._get_option_policy(
715
'http://www.example.com/norecurse', 'normal_option'),
716
config.POLICY_NORECURSE)
718
def test__get_option_policy_normal(self):
719
self.get_branch_config('http://www.example.com')
721
self.my_location_config._get_option_policy(
722
'http://www.example.com', 'appendpath_option'),
723
config.POLICY_APPENDPATH)
491
725
def test_location_without_username(self):
492
self.get_location_config('http://www.example.com/useglobal')
726
self.get_branch_config('http://www.example.com/ignoreparent')
493
727
self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
494
728
self.my_config.username())
496
730
def test_location_not_listed(self):
497
731
"""Test that the global username is used when no location matches"""
498
self.get_location_config('/home/robertc/sources')
732
self.get_branch_config('/home/robertc/sources')
499
733
self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
500
734
self.my_config.username())
502
736
def test_overriding_location(self):
503
self.get_location_config('http://www.example.com/foo')
737
self.get_branch_config('http://www.example.com/foo')
504
738
self.assertEqual('Robert Collins <robertc@example.org>',
505
739
self.my_config.username())
507
741
def test_signatures_not_set(self):
508
self.get_location_config('http://www.example.com',
742
self.get_branch_config('http://www.example.com',
509
743
global_config=sample_ignore_signatures)
510
744
self.assertEqual(config.CHECK_ALWAYS,
511
745
self.my_config.signature_checking())
513
747
self.my_config.signing_policy())
515
749
def test_signatures_never(self):
516
self.get_location_config('/a/c')
750
self.get_branch_config('/a/c')
517
751
self.assertEqual(config.CHECK_NEVER,
518
752
self.my_config.signature_checking())
520
754
def test_signatures_when_available(self):
521
self.get_location_config('/a/', global_config=sample_ignore_signatures)
755
self.get_branch_config('/a/', global_config=sample_ignore_signatures)
522
756
self.assertEqual(config.CHECK_IF_POSSIBLE,
523
757
self.my_config.signature_checking())
525
759
def test_signatures_always(self):
526
self.get_location_config('/b')
760
self.get_branch_config('/b')
527
761
self.assertEqual(config.CHECK_ALWAYS,
528
762
self.my_config.signature_checking())
530
764
def test_gpg_signing_command(self):
531
self.get_location_config('/b')
765
self.get_branch_config('/b')
532
766
self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
534
768
def test_gpg_signing_command_missing(self):
535
self.get_location_config('/a')
769
self.get_branch_config('/a')
536
770
self.assertEqual("false", self.my_config.gpg_signing_command())
538
772
def test_get_user_option_global(self):
539
self.get_location_config('/a')
773
self.get_branch_config('/a')
540
774
self.assertEqual('something',
541
775
self.my_config.get_user_option('user_global_option'))
543
777
def test_get_user_option_local(self):
544
self.get_location_config('/a')
778
self.get_branch_config('/a')
545
779
self.assertEqual('local',
546
780
self.my_config.get_user_option('user_local_option'))
782
def test_get_user_option_appendpath(self):
783
# returned as is for the base path:
784
self.get_branch_config('http://www.example.com')
785
self.assertEqual('append',
786
self.my_config.get_user_option('appendpath_option'))
787
# Extra path components get appended:
788
self.get_branch_config('http://www.example.com/a/b/c')
789
self.assertEqual('append/a/b/c',
790
self.my_config.get_user_option('appendpath_option'))
791
# Overriden for http://www.example.com/dir, where it is a
793
self.get_branch_config('http://www.example.com/dir/a/b/c')
794
self.assertEqual('normal',
795
self.my_config.get_user_option('appendpath_option'))
797
def test_get_user_option_norecurse(self):
798
self.get_branch_config('http://www.example.com')
799
self.assertEqual('norecurse',
800
self.my_config.get_user_option('norecurse_option'))
801
self.get_branch_config('http://www.example.com/dir')
802
self.assertEqual(None,
803
self.my_config.get_user_option('norecurse_option'))
804
# http://www.example.com/norecurse is a recurse=False section
805
# that redefines normal_option. Subdirectories do not pick up
807
self.get_branch_config('http://www.example.com/norecurse')
808
self.assertEqual('norecurse',
809
self.my_config.get_user_option('normal_option'))
810
self.get_branch_config('http://www.example.com/norecurse/subdir')
811
self.assertEqual('normal',
812
self.my_config.get_user_option('normal_option'))
814
def test_set_user_option_norecurse(self):
815
self.get_branch_config('http://www.example.com')
816
self.my_config.set_user_option('foo', 'bar',
817
store=config.STORE_LOCATION_NORECURSE)
819
self.my_location_config._get_option_policy(
820
'http://www.example.com', 'foo'),
821
config.POLICY_NORECURSE)
823
def test_set_user_option_appendpath(self):
824
self.get_branch_config('http://www.example.com')
825
self.my_config.set_user_option('foo', 'bar',
826
store=config.STORE_LOCATION_APPENDPATH)
828
self.my_location_config._get_option_policy(
829
'http://www.example.com', 'foo'),
830
config.POLICY_APPENDPATH)
832
def test_set_user_option_change_policy(self):
833
self.get_branch_config('http://www.example.com')
834
self.my_config.set_user_option('norecurse_option', 'normal',
835
store=config.STORE_LOCATION)
837
self.my_location_config._get_option_policy(
838
'http://www.example.com', 'norecurse_option'),
841
def test_set_user_option_recurse_false_section(self):
842
# The following section has recurse=False set. The test is to
843
# make sure that a normal option can be added to the section,
844
# converting recurse=False to the norecurse policy.
845
self.get_branch_config('http://www.example.com/norecurse')
846
self.callDeprecated(['The recurse option is deprecated as of 0.14. '
847
'The section "http://www.example.com/norecurse" '
848
'has been converted to use policies.'],
849
self.my_config.set_user_option,
850
'foo', 'bar', store=config.STORE_LOCATION)
852
self.my_location_config._get_option_policy(
853
'http://www.example.com/norecurse', 'foo'),
855
# The previously existing option is still norecurse:
857
self.my_location_config._get_option_policy(
858
'http://www.example.com/norecurse', 'normal_option'),
859
config.POLICY_NORECURSE)
548
861
def test_post_commit_default(self):
549
self.get_location_config('/a/c')
862
self.get_branch_config('/a/c')
550
863
self.assertEqual('bzrlib.tests.test_config.post_commit',
551
864
self.my_config.post_commit())
553
def get_location_config(self, location, global_config=None):
866
def get_branch_config(self, location, global_config=None):
554
867
if global_config is None:
555
868
global_file = StringIO(sample_config_text.encode('utf-8'))
557
870
global_file = StringIO(global_config.encode('utf-8'))
558
871
branches_file = StringIO(sample_branches_text.encode('utf-8'))
559
self.my_config = config.LocationConfig(location)
560
self.my_config._get_parser(branches_file)
872
self.my_config = config.BranchConfig(FakeBranch(location))
873
# Force location config to use specified file
874
self.my_location_config = self.my_config._get_location_config()
875
self.my_location_config._get_parser(branches_file)
876
# Force global config to use specified file
561
877
self.my_config._get_global_config()._get_parser(global_file)
563
879
def test_set_user_setting_sets_and_saves(self):
564
self.get_location_config('/a/c')
880
self.get_branch_config('/a/c')
565
881
record = InstrumentedConfigObj("foo")
566
self.my_config._parser = record
882
self.my_location_config._parser = record
568
884
real_mkdir = os.mkdir
569
885
self.created = False
584
904
('__setitem__', '/a/c', {}),
585
905
('__getitem__', '/a/c'),
586
906
('__setitem__', 'foo', 'bar'),
907
('__getitem__', '/a/c'),
908
('as_bool', 'recurse'),
909
('__getitem__', '/a/c'),
910
('__delitem__', 'recurse'),
911
('__getitem__', '/a/c'),
913
('__getitem__', '/a/c'),
914
('__contains__', 'foo:policy'),
588
916
record._calls[1:])
591
class TestBranchConfigItems(TestCase):
918
def test_set_user_setting_sets_and_saves2(self):
919
self.get_branch_config('/a/c')
920
self.assertIs(self.my_config.get_user_option('foo'), None)
921
self.my_config.set_user_option('foo', 'bar')
923
self.my_config.branch.control_files.files['branch.conf'],
925
self.assertEqual(self.my_config.get_user_option('foo'), 'bar')
926
self.my_config.set_user_option('foo', 'baz',
927
store=config.STORE_LOCATION)
928
self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
929
self.my_config.set_user_option('foo', 'qux')
930
self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
932
def test_get_bzr_remote_path(self):
933
my_config = config.LocationConfig('/a/c')
934
self.assertEqual('bzr', my_config.get_bzr_remote_path())
935
my_config.set_user_option('bzr_remote_path', '/path-bzr')
936
self.assertEqual('/path-bzr', my_config.get_bzr_remote_path())
937
os.environ['BZR_REMOTE_PATH'] = '/environ-bzr'
938
self.assertEqual('/environ-bzr', my_config.get_bzr_remote_path())
941
precedence_global = 'option = global'
942
precedence_branch = 'option = branch'
943
precedence_location = """
947
[http://example.com/specific]
952
class TestBranchConfigItems(tests.TestCaseInTempDir):
954
def get_branch_config(self, global_config=None, location=None,
955
location_config=None, branch_data_config=None):
956
my_config = config.BranchConfig(FakeBranch(location))
957
if global_config is not None:
958
global_file = StringIO(global_config.encode('utf-8'))
959
my_config._get_global_config()._get_parser(global_file)
960
self.my_location_config = my_config._get_location_config()
961
if location_config is not None:
962
location_file = StringIO(location_config.encode('utf-8'))
963
self.my_location_config._get_parser(location_file)
964
if branch_data_config is not None:
965
my_config.branch.control_files.files['branch.conf'] = \
593
969
def test_user_id(self):
594
branch = FakeBranch()
970
branch = FakeBranch(user_id='Robert Collins <robertc@example.net>')
595
971
my_config = config.BranchConfig(branch)
596
972
self.assertEqual("Robert Collins <robertc@example.net>",
597
my_config._get_user_id())
598
branch.control_files.email = "John"
599
self.assertEqual("John", my_config._get_user_id())
973
my_config.username())
974
my_config.branch.control_files.files['email'] = "John"
975
my_config.set_user_option('email',
976
"Robert Collins <robertc@example.org>")
977
self.assertEqual("John", my_config.username())
978
del my_config.branch.control_files.files['email']
979
self.assertEqual("Robert Collins <robertc@example.org>",
980
my_config.username())
601
982
def test_not_set_in_branch(self):
602
branch = FakeBranch()
603
my_config = config.BranchConfig(branch)
604
branch.control_files.email = None
605
config_file = StringIO(sample_config_text.encode('utf-8'))
606
(my_config._get_location_config().
607
_get_global_config()._get_parser(config_file))
983
my_config = self.get_branch_config(sample_config_text)
608
984
self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
609
985
my_config._get_user_id())
610
branch.control_files.email = "John"
986
my_config.branch.control_files.files['email'] = "John"
611
987
self.assertEqual("John", my_config._get_user_id())
613
def test_BZREMAIL_OVERRIDES(self):
614
os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
989
def test_BZR_EMAIL_OVERRIDES(self):
990
os.environ['BZR_EMAIL'] = "Robert Collins <robertc@example.org>"
615
991
branch = FakeBranch()
616
992
my_config = config.BranchConfig(branch)
617
993
self.assertEqual("Robert Collins <robertc@example.org>",
618
994
my_config.username())
620
996
def test_signatures_forced(self):
621
branch = FakeBranch()
622
my_config = config.BranchConfig(branch)
623
config_file = StringIO(sample_always_signatures)
624
(my_config._get_location_config().
625
_get_global_config()._get_parser(config_file))
997
my_config = self.get_branch_config(
998
global_config=sample_always_signatures)
999
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
1000
self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
1001
self.assertTrue(my_config.signature_needed())
1003
def test_signatures_forced_branch(self):
1004
my_config = self.get_branch_config(
1005
global_config=sample_ignore_signatures,
1006
branch_data_config=sample_always_signatures)
626
1007
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
627
1008
self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
628
1009
self.assertTrue(my_config.signature_needed())
630
1011
def test_gpg_signing_command(self):
631
branch = FakeBranch()
632
my_config = config.BranchConfig(branch)
1012
my_config = self.get_branch_config(
1013
# branch data cannot set gpg_signing_command
1014
branch_data_config="gpg_signing_command=pgp")
633
1015
config_file = StringIO(sample_config_text.encode('utf-8'))
634
(my_config._get_location_config().
635
_get_global_config()._get_parser(config_file))
1016
my_config._get_global_config()._get_parser(config_file)
636
1017
self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
638
1019
def test_get_user_option_global(self):
639
1020
branch = FakeBranch()
640
1021
my_config = config.BranchConfig(branch)
641
1022
config_file = StringIO(sample_config_text.encode('utf-8'))
642
(my_config._get_location_config().
643
_get_global_config()._get_parser(config_file))
1023
(my_config._get_global_config()._get_parser(config_file))
644
1024
self.assertEqual('something',
645
1025
my_config.get_user_option('user_global_option'))
647
1027
def test_post_commit_default(self):
648
1028
branch = FakeBranch()
650
my_config = config.BranchConfig(branch)
651
config_file = StringIO(sample_config_text.encode('utf-8'))
652
(my_config._get_location_config().
653
_get_global_config()._get_parser(config_file))
654
branch_file = StringIO(sample_branches_text)
655
my_config._get_location_config()._get_parser(branch_file)
656
self.assertEqual('bzrlib.tests.test_config.post_commit',
657
my_config.post_commit())
660
class TestMailAddressExtraction(TestCase):
1029
my_config = self.get_branch_config(sample_config_text, '/a/c',
1030
sample_branches_text)
1031
self.assertEqual(my_config.branch.base, '/a/c')
1032
self.assertEqual('bzrlib.tests.test_config.post_commit',
1033
my_config.post_commit())
1034
my_config.set_user_option('post_commit', 'rmtree_root')
1035
# post-commit is ignored when bresent in branch data
1036
self.assertEqual('bzrlib.tests.test_config.post_commit',
1037
my_config.post_commit())
1038
my_config.set_user_option('post_commit', 'rmtree_root',
1039
store=config.STORE_LOCATION)
1040
self.assertEqual('rmtree_root', my_config.post_commit())
1042
def test_config_precedence(self):
1043
my_config = self.get_branch_config(global_config=precedence_global)
1044
self.assertEqual(my_config.get_user_option('option'), 'global')
1045
my_config = self.get_branch_config(global_config=precedence_global,
1046
branch_data_config=precedence_branch)
1047
self.assertEqual(my_config.get_user_option('option'), 'branch')
1048
my_config = self.get_branch_config(global_config=precedence_global,
1049
branch_data_config=precedence_branch,
1050
location_config=precedence_location)
1051
self.assertEqual(my_config.get_user_option('option'), 'recurse')
1052
my_config = self.get_branch_config(global_config=precedence_global,
1053
branch_data_config=precedence_branch,
1054
location_config=precedence_location,
1055
location='http://example.com/specific')
1056
self.assertEqual(my_config.get_user_option('option'), 'exact')
1058
def test_get_mail_client(self):
1059
config = self.get_branch_config()
1060
client = config.get_mail_client()
1061
self.assertIsInstance(client, mail_client.DefaultMail)
1064
config.set_user_option('mail_client', 'evolution')
1065
client = config.get_mail_client()
1066
self.assertIsInstance(client, mail_client.Evolution)
1068
config.set_user_option('mail_client', 'kmail')
1069
client = config.get_mail_client()
1070
self.assertIsInstance(client, mail_client.KMail)
1072
config.set_user_option('mail_client', 'mutt')
1073
client = config.get_mail_client()
1074
self.assertIsInstance(client, mail_client.Mutt)
1076
config.set_user_option('mail_client', 'thunderbird')
1077
client = config.get_mail_client()
1078
self.assertIsInstance(client, mail_client.Thunderbird)
1081
config.set_user_option('mail_client', 'default')
1082
client = config.get_mail_client()
1083
self.assertIsInstance(client, mail_client.DefaultMail)
1085
config.set_user_option('mail_client', 'editor')
1086
client = config.get_mail_client()
1087
self.assertIsInstance(client, mail_client.Editor)
1089
config.set_user_option('mail_client', 'mapi')
1090
client = config.get_mail_client()
1091
self.assertIsInstance(client, mail_client.MAPIClient)
1093
config.set_user_option('mail_client', 'xdg-email')
1094
client = config.get_mail_client()
1095
self.assertIsInstance(client, mail_client.XDGEmail)
1097
config.set_user_option('mail_client', 'firebird')
1098
self.assertRaises(errors.UnknownMailClient, config.get_mail_client)
1101
class TestMailAddressExtraction(tests.TestCase):
662
1103
def test_extract_email_address(self):
663
1104
self.assertEqual('jane@test.com',
664
1105
config.extract_email_address('Jane <jane@test.com>'))
665
self.assertRaises(errors.BzrError,
1106
self.assertRaises(errors.NoEmailInUsername,
666
1107
config.extract_email_address, 'Jane Tester')
1109
def test_parse_username(self):
1110
self.assertEqual(('', 'jdoe@example.com'),
1111
config.parse_username('jdoe@example.com'))
1112
self.assertEqual(('', 'jdoe@example.com'),
1113
config.parse_username('<jdoe@example.com>'))
1114
self.assertEqual(('John Doe', 'jdoe@example.com'),
1115
config.parse_username('John Doe <jdoe@example.com>'))
1116
self.assertEqual(('John Doe', ''),
1117
config.parse_username('John Doe'))
1118
self.assertEqual(('John Doe', 'jdoe@example.com'),
1119
config.parse_username('John Doe jdoe@example.com'))
1121
class TestTreeConfig(tests.TestCaseWithTransport):
1123
def test_get_value(self):
1124
"""Test that retreiving a value from a section is possible"""
1125
branch = self.make_branch('.')
1126
tree_config = config.TreeConfig(branch)
1127
tree_config.set_option('value', 'key', 'SECTION')
1128
tree_config.set_option('value2', 'key2')
1129
tree_config.set_option('value3-top', 'key3')
1130
tree_config.set_option('value3-section', 'key3', 'SECTION')
1131
value = tree_config.get_option('key', 'SECTION')
1132
self.assertEqual(value, 'value')
1133
value = tree_config.get_option('key2')
1134
self.assertEqual(value, 'value2')
1135
self.assertEqual(tree_config.get_option('non-existant'), None)
1136
value = tree_config.get_option('non-existant', 'SECTION')
1137
self.assertEqual(value, None)
1138
value = tree_config.get_option('non-existant', default='default')
1139
self.assertEqual(value, 'default')
1140
self.assertEqual(tree_config.get_option('key2', 'NOSECTION'), None)
1141
value = tree_config.get_option('key2', 'NOSECTION', default='default')
1142
self.assertEqual(value, 'default')
1143
value = tree_config.get_option('key3')
1144
self.assertEqual(value, 'value3-top')
1145
value = tree_config.get_option('key3', 'SECTION')
1146
self.assertEqual(value, 'value3-section')
1149
class TestTransportConfig(tests.TestCaseWithTransport):
1151
def test_get_value(self):
1152
"""Test that retreiving a value from a section is possible"""
1153
bzrdir_config = config.TransportConfig(transport.get_transport('.'),
1155
bzrdir_config.set_option('value', 'key', 'SECTION')
1156
bzrdir_config.set_option('value2', 'key2')
1157
bzrdir_config.set_option('value3-top', 'key3')
1158
bzrdir_config.set_option('value3-section', 'key3', 'SECTION')
1159
value = bzrdir_config.get_option('key', 'SECTION')
1160
self.assertEqual(value, 'value')
1161
value = bzrdir_config.get_option('key2')
1162
self.assertEqual(value, 'value2')
1163
self.assertEqual(bzrdir_config.get_option('non-existant'), None)
1164
value = bzrdir_config.get_option('non-existant', 'SECTION')
1165
self.assertEqual(value, None)
1166
value = bzrdir_config.get_option('non-existant', default='default')
1167
self.assertEqual(value, 'default')
1168
self.assertEqual(bzrdir_config.get_option('key2', 'NOSECTION'), None)
1169
value = bzrdir_config.get_option('key2', 'NOSECTION',
1171
self.assertEqual(value, 'default')
1172
value = bzrdir_config.get_option('key3')
1173
self.assertEqual(value, 'value3-top')
1174
value = bzrdir_config.get_option('key3', 'SECTION')
1175
self.assertEqual(value, 'value3-section')
1178
class TestAuthenticationConfigFile(tests.TestCase):
1179
"""Test the authentication.conf file matching"""
1181
def _got_user_passwd(self, expected_user, expected_password,
1182
config, *args, **kwargs):
1183
credentials = config.get_credentials(*args, **kwargs)
1184
if credentials is None:
1188
user = credentials['user']
1189
password = credentials['password']
1190
self.assertEquals(expected_user, user)
1191
self.assertEquals(expected_password, password)
1193
def test_empty_config(self):
1194
conf = config.AuthenticationConfig(_file=StringIO())
1195
self.assertEquals({}, conf._get_config())
1196
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1198
def test_broken_config(self):
1199
conf = config.AuthenticationConfig(_file=StringIO('[DEF'))
1200
self.assertRaises(errors.ParseConfigError, conf._get_config)
1202
conf = config.AuthenticationConfig(_file=StringIO(
1206
verify_certificates=askme # Error: Not a boolean
1208
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1209
conf = config.AuthenticationConfig(_file=StringIO(
1213
port=port # Error: Not an int
1215
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1217
def test_credentials_for_scheme_host(self):
1218
conf = config.AuthenticationConfig(_file=StringIO(
1219
"""# Identity on foo.net
1224
password=secret-pass
1227
self._got_user_passwd('joe', 'secret-pass', conf, 'ftp', 'foo.net')
1229
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1231
self._got_user_passwd(None, None, conf, 'ftp', 'bar.net')
1233
def test_credentials_for_host_port(self):
1234
conf = config.AuthenticationConfig(_file=StringIO(
1235
"""# Identity on foo.net
1241
password=secret-pass
1244
self._got_user_passwd('joe', 'secret-pass',
1245
conf, 'ftp', 'foo.net', port=10021)
1247
self._got_user_passwd(None, None, conf, 'ftp', 'foo.net')
1249
def test_for_matching_host(self):
1250
conf = config.AuthenticationConfig(_file=StringIO(
1251
"""# Identity on foo.net
1257
[sourceforge domain]
1264
self._got_user_passwd('georges', 'bendover',
1265
conf, 'bzr', 'foo.bzr.sf.net')
1267
self._got_user_passwd(None, None,
1268
conf, 'bzr', 'bbzr.sf.net')
1270
def test_for_matching_host_None(self):
1271
conf = config.AuthenticationConfig(_file=StringIO(
1272
"""# Identity on foo.net
1282
self._got_user_passwd('joe', 'joepass',
1283
conf, 'bzr', 'quux.net')
1284
# no host but different scheme
1285
self._got_user_passwd('georges', 'bendover',
1286
conf, 'ftp', 'quux.net')
1288
def test_credentials_for_path(self):
1289
conf = config.AuthenticationConfig(_file=StringIO(
1305
self._got_user_passwd(None, None,
1306
conf, 'http', host='bar.org', path='/dir3')
1308
self._got_user_passwd('georges', 'bendover',
1309
conf, 'http', host='bar.org', path='/dir2')
1311
self._got_user_passwd('jim', 'jimpass',
1312
conf, 'http', host='bar.org',path='/dir1/subdir')
1314
def test_credentials_for_user(self):
1315
conf = config.AuthenticationConfig(_file=StringIO(
1324
self._got_user_passwd('jim', 'jimpass',
1325
conf, 'http', 'bar.org')
1327
self._got_user_passwd('jim', 'jimpass',
1328
conf, 'http', 'bar.org', user='jim')
1329
# Don't get a different user if one is specified
1330
self._got_user_passwd(None, None,
1331
conf, 'http', 'bar.org', user='georges')
1333
def test_verify_certificates(self):
1334
conf = config.AuthenticationConfig(_file=StringIO(
1341
verify_certificates=False
1348
credentials = conf.get_credentials('https', 'bar.org')
1349
self.assertEquals(False, credentials.get('verify_certificates'))
1350
credentials = conf.get_credentials('https', 'foo.net')
1351
self.assertEquals(True, credentials.get('verify_certificates'))
1354
class TestAuthenticationConfig(tests.TestCase):
1355
"""Test AuthenticationConfig behaviour"""
1357
def _check_default_prompt(self, expected_prompt_format, scheme,
1358
host=None, port=None, realm=None, path=None):
1361
user, password = 'jim', 'precious'
1362
expected_prompt = expected_prompt_format % {
1363
'scheme': scheme, 'host': host, 'port': port,
1364
'user': user, 'realm': realm}
1366
stdout = tests.StringIOWrapper()
1367
ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
1369
# We use an empty conf so that the user is always prompted
1370
conf = config.AuthenticationConfig()
1371
self.assertEquals(password,
1372
conf.get_password(scheme, host, user, port=port,
1373
realm=realm, path=path))
1374
self.assertEquals(stdout.getvalue(), expected_prompt)
1376
def test_default_prompts(self):
1377
# HTTP prompts can't be tested here, see test_http.py
1378
self._check_default_prompt('FTP %(user)s@%(host)s password: ', 'ftp')
1379
self._check_default_prompt('FTP %(user)s@%(host)s:%(port)d password: ',
1382
self._check_default_prompt('SSH %(user)s@%(host)s:%(port)d password: ',
1384
# SMTP port handling is a bit special (it's handled if embedded in the
1386
# FIXME: should we: forbid that, extend it to other schemes, leave
1387
# things as they are that's fine thank you ?
1388
self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1390
self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1391
'smtp', host='bar.org:10025')
1392
self._check_default_prompt(
1393
'SMTP %(user)s@%(host)s:%(port)d password: ',
1397
# FIXME: Once we have a way to declare authentication to all test servers, we
1398
# can implement generic tests.
1399
# test_user_password_in_url
1400
# test_user_in_url_password_from_config
1401
# test_user_in_url_password_prompted
1402
# test_user_in_config
1403
# test_user_getpass.getuser
1404
# test_user_prompted ?
1405
class TestAuthenticationRing(tests.TestCaseWithTransport):