406
607
# This is testing the correct file names are provided.
407
608
# TODO: consolidate with the test for GlobalConfigs filename checks.
409
# replace the class that is constructured, to check its parameters
610
# replace the class that is constructed, to check its parameters
410
611
oldparserclass = config.ConfigObj
411
612
config.ConfigObj = InstrumentedConfigObj
412
my_config = config.LocationConfig('http://www.example.com')
614
my_config = config.LocationConfig('http://www.example.com')
414
615
parser = my_config._get_parser()
416
617
config.ConfigObj = oldparserclass
417
618
self.failUnless(isinstance(parser, InstrumentedConfigObj))
418
619
self.assertEqual(parser._calls,
419
[('__init__', config.branches_config_filename())])
620
[('__init__', config.locations_config_filename(),
622
config.ensure_config_dir_exists()
623
#os.mkdir(config.config_dir())
624
f = file(config.branches_config_filename(), 'wb')
627
oldparserclass = config.ConfigObj
628
config.ConfigObj = InstrumentedConfigObj
630
my_config = config.LocationConfig('http://www.example.com')
631
parser = my_config._get_parser()
633
config.ConfigObj = oldparserclass
421
635
def test_get_global_config(self):
422
my_config = config.LocationConfig('http://example.com')
636
my_config = config.BranchConfig(FakeBranch('http://example.com'))
423
637
global_config = my_config._get_global_config()
424
638
self.failUnless(isinstance(global_config, config.GlobalConfig))
425
639
self.failUnless(global_config is my_config._get_global_config())
427
def test__get_section_no_match(self):
428
self.get_location_config('/')
429
self.assertEqual(None, self.my_config._get_section())
431
def test__get_section_exact(self):
432
self.get_location_config('http://www.example.com')
433
self.assertEqual('http://www.example.com',
434
self.my_config._get_section())
436
def test__get_section_suffix_does_not(self):
437
self.get_location_config('http://www.example.com-com')
438
self.assertEqual(None, self.my_config._get_section())
440
def test__get_section_subdir_recursive(self):
441
self.get_location_config('http://www.example.com/com')
442
self.assertEqual('http://www.example.com',
443
self.my_config._get_section())
445
def test__get_section_subdir_matches(self):
446
self.get_location_config('http://www.example.com/useglobal')
447
self.assertEqual('http://www.example.com/useglobal',
448
self.my_config._get_section())
450
def test__get_section_subdir_nonrecursive(self):
451
self.get_location_config(
452
'http://www.example.com/useglobal/childbranch')
453
self.assertEqual('http://www.example.com',
454
self.my_config._get_section())
456
def test__get_section_subdir_trailing_slash(self):
457
self.get_location_config('/b')
458
self.assertEqual('/b/', self.my_config._get_section())
460
def test__get_section_subdir_child(self):
461
self.get_location_config('/a/foo')
462
self.assertEqual('/a/*', self.my_config._get_section())
464
def test__get_section_subdir_child_child(self):
465
self.get_location_config('/a/foo/bar')
466
self.assertEqual('/a/', self.my_config._get_section())
468
def test__get_section_trailing_slash_with_children(self):
469
self.get_location_config('/a/')
470
self.assertEqual('/a/', self.my_config._get_section())
472
def test__get_section_explicit_over_glob(self):
473
self.get_location_config('/a/c')
474
self.assertEqual('/a/c', self.my_config._get_section())
476
def get_location_config(self, location, global_config=None):
477
if global_config is None:
478
global_file = StringIO(sample_config_text)
480
global_file = StringIO(global_config)
481
branches_file = StringIO(sample_branches_text)
482
self.my_config = config.LocationConfig(location)
483
self.my_config._get_parser(branches_file)
484
self.my_config._get_global_config()._get_parser(global_file)
641
def test__get_matching_sections_no_match(self):
642
self.get_branch_config('/')
643
self.assertEqual([], self.my_location_config._get_matching_sections())
645
def test__get_matching_sections_exact(self):
646
self.get_branch_config('http://www.example.com')
647
self.assertEqual([('http://www.example.com', '')],
648
self.my_location_config._get_matching_sections())
650
def test__get_matching_sections_suffix_does_not(self):
651
self.get_branch_config('http://www.example.com-com')
652
self.assertEqual([], self.my_location_config._get_matching_sections())
654
def test__get_matching_sections_subdir_recursive(self):
655
self.get_branch_config('http://www.example.com/com')
656
self.assertEqual([('http://www.example.com', 'com')],
657
self.my_location_config._get_matching_sections())
659
def test__get_matching_sections_ignoreparent(self):
660
self.get_branch_config('http://www.example.com/ignoreparent')
661
self.assertEqual([('http://www.example.com/ignoreparent', '')],
662
self.my_location_config._get_matching_sections())
664
def test__get_matching_sections_ignoreparent_subdir(self):
665
self.get_branch_config(
666
'http://www.example.com/ignoreparent/childbranch')
667
self.assertEqual([('http://www.example.com/ignoreparent',
669
self.my_location_config._get_matching_sections())
671
def test__get_matching_sections_subdir_trailing_slash(self):
672
self.get_branch_config('/b')
673
self.assertEqual([('/b/', '')],
674
self.my_location_config._get_matching_sections())
676
def test__get_matching_sections_subdir_child(self):
677
self.get_branch_config('/a/foo')
678
self.assertEqual([('/a/*', ''), ('/a/', 'foo')],
679
self.my_location_config._get_matching_sections())
681
def test__get_matching_sections_subdir_child_child(self):
682
self.get_branch_config('/a/foo/bar')
683
self.assertEqual([('/a/*', 'bar'), ('/a/', 'foo/bar')],
684
self.my_location_config._get_matching_sections())
686
def test__get_matching_sections_trailing_slash_with_children(self):
687
self.get_branch_config('/a/')
688
self.assertEqual([('/a/', '')],
689
self.my_location_config._get_matching_sections())
691
def test__get_matching_sections_explicit_over_glob(self):
692
# XXX: 2006-09-08 jamesh
693
# This test only passes because ord('c') > ord('*'). If there
694
# was a config section for '/a/?', it would get precedence
696
self.get_branch_config('/a/c')
697
self.assertEqual([('/a/c', ''), ('/a/*', ''), ('/a/', 'c')],
698
self.my_location_config._get_matching_sections())
700
def test__get_option_policy_normal(self):
701
self.get_branch_config('http://www.example.com')
703
self.my_location_config._get_config_policy(
704
'http://www.example.com', 'normal_option'),
707
def test__get_option_policy_norecurse(self):
708
self.get_branch_config('http://www.example.com')
710
self.my_location_config._get_option_policy(
711
'http://www.example.com', 'norecurse_option'),
712
config.POLICY_NORECURSE)
713
# Test old recurse=False setting:
715
self.my_location_config._get_option_policy(
716
'http://www.example.com/norecurse', 'normal_option'),
717
config.POLICY_NORECURSE)
719
def test__get_option_policy_normal(self):
720
self.get_branch_config('http://www.example.com')
722
self.my_location_config._get_option_policy(
723
'http://www.example.com', 'appendpath_option'),
724
config.POLICY_APPENDPATH)
486
726
def test_location_without_username(self):
487
self.get_location_config('http://www.example.com/useglobal')
488
self.assertEqual('Robert Collins <robertc@example.com>',
727
self.get_branch_config('http://www.example.com/ignoreparent')
728
self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
489
729
self.my_config.username())
491
731
def test_location_not_listed(self):
492
self.get_location_config('/home/robertc/sources')
493
self.assertEqual('Robert Collins <robertc@example.com>',
732
"""Test that the global username is used when no location matches"""
733
self.get_branch_config('/home/robertc/sources')
734
self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
494
735
self.my_config.username())
496
737
def test_overriding_location(self):
497
self.get_location_config('http://www.example.com/foo')
738
self.get_branch_config('http://www.example.com/foo')
498
739
self.assertEqual('Robert Collins <robertc@example.org>',
499
740
self.my_config.username())
501
742
def test_signatures_not_set(self):
502
self.get_location_config('http://www.example.com',
743
self.get_branch_config('http://www.example.com',
503
744
global_config=sample_ignore_signatures)
504
self.assertEqual(config.CHECK_NEVER,
745
self.assertEqual(config.CHECK_ALWAYS,
505
746
self.my_config.signature_checking())
747
self.assertEqual(config.SIGN_NEVER,
748
self.my_config.signing_policy())
507
750
def test_signatures_never(self):
508
self.get_location_config('/a/c')
751
self.get_branch_config('/a/c')
509
752
self.assertEqual(config.CHECK_NEVER,
510
753
self.my_config.signature_checking())
512
755
def test_signatures_when_available(self):
513
self.get_location_config('/a/', global_config=sample_ignore_signatures)
756
self.get_branch_config('/a/', global_config=sample_ignore_signatures)
514
757
self.assertEqual(config.CHECK_IF_POSSIBLE,
515
758
self.my_config.signature_checking())
517
760
def test_signatures_always(self):
518
self.get_location_config('/b')
761
self.get_branch_config('/b')
519
762
self.assertEqual(config.CHECK_ALWAYS,
520
763
self.my_config.signature_checking())
522
765
def test_gpg_signing_command(self):
523
self.get_location_config('/b')
766
self.get_branch_config('/b')
524
767
self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
526
769
def test_gpg_signing_command_missing(self):
527
self.get_location_config('/a')
770
self.get_branch_config('/a')
528
771
self.assertEqual("false", self.my_config.gpg_signing_command())
530
773
def test_get_user_option_global(self):
531
self.get_location_config('/a')
774
self.get_branch_config('/a')
532
775
self.assertEqual('something',
533
776
self.my_config.get_user_option('user_global_option'))
535
778
def test_get_user_option_local(self):
536
self.get_location_config('/a')
779
self.get_branch_config('/a')
537
780
self.assertEqual('local',
538
781
self.my_config.get_user_option('user_local_option'))
783
def test_get_user_option_appendpath(self):
784
# returned as is for the base path:
785
self.get_branch_config('http://www.example.com')
786
self.assertEqual('append',
787
self.my_config.get_user_option('appendpath_option'))
788
# Extra path components get appended:
789
self.get_branch_config('http://www.example.com/a/b/c')
790
self.assertEqual('append/a/b/c',
791
self.my_config.get_user_option('appendpath_option'))
792
# Overriden for http://www.example.com/dir, where it is a
794
self.get_branch_config('http://www.example.com/dir/a/b/c')
795
self.assertEqual('normal',
796
self.my_config.get_user_option('appendpath_option'))
798
def test_get_user_option_norecurse(self):
799
self.get_branch_config('http://www.example.com')
800
self.assertEqual('norecurse',
801
self.my_config.get_user_option('norecurse_option'))
802
self.get_branch_config('http://www.example.com/dir')
803
self.assertEqual(None,
804
self.my_config.get_user_option('norecurse_option'))
805
# http://www.example.com/norecurse is a recurse=False section
806
# that redefines normal_option. Subdirectories do not pick up
808
self.get_branch_config('http://www.example.com/norecurse')
809
self.assertEqual('norecurse',
810
self.my_config.get_user_option('normal_option'))
811
self.get_branch_config('http://www.example.com/norecurse/subdir')
812
self.assertEqual('normal',
813
self.my_config.get_user_option('normal_option'))
815
def test_set_user_option_norecurse(self):
816
self.get_branch_config('http://www.example.com')
817
self.my_config.set_user_option('foo', 'bar',
818
store=config.STORE_LOCATION_NORECURSE)
820
self.my_location_config._get_option_policy(
821
'http://www.example.com', 'foo'),
822
config.POLICY_NORECURSE)
824
def test_set_user_option_appendpath(self):
825
self.get_branch_config('http://www.example.com')
826
self.my_config.set_user_option('foo', 'bar',
827
store=config.STORE_LOCATION_APPENDPATH)
829
self.my_location_config._get_option_policy(
830
'http://www.example.com', 'foo'),
831
config.POLICY_APPENDPATH)
833
def test_set_user_option_change_policy(self):
834
self.get_branch_config('http://www.example.com')
835
self.my_config.set_user_option('norecurse_option', 'normal',
836
store=config.STORE_LOCATION)
838
self.my_location_config._get_option_policy(
839
'http://www.example.com', 'norecurse_option'),
842
def test_set_user_option_recurse_false_section(self):
843
# The following section has recurse=False set. The test is to
844
# make sure that a normal option can be added to the section,
845
# converting recurse=False to the norecurse policy.
846
self.get_branch_config('http://www.example.com/norecurse')
847
self.callDeprecated(['The recurse option is deprecated as of 0.14. '
848
'The section "http://www.example.com/norecurse" '
849
'has been converted to use policies.'],
850
self.my_config.set_user_option,
851
'foo', 'bar', store=config.STORE_LOCATION)
853
self.my_location_config._get_option_policy(
854
'http://www.example.com/norecurse', 'foo'),
856
# The previously existing option is still norecurse:
858
self.my_location_config._get_option_policy(
859
'http://www.example.com/norecurse', 'normal_option'),
860
config.POLICY_NORECURSE)
540
862
def test_post_commit_default(self):
541
self.get_location_config('/a/c')
863
self.get_branch_config('/a/c')
542
864
self.assertEqual('bzrlib.tests.test_config.post_commit',
543
865
self.my_config.post_commit())
546
class TestLocationConfig(TestCaseInTempDir):
548
def get_location_config(self, location, global_config=None):
867
def get_branch_config(self, location, global_config=None):
549
868
if global_config is None:
550
869
global_file = StringIO(sample_config_text.encode('utf-8'))
552
871
global_file = StringIO(global_config.encode('utf-8'))
553
872
branches_file = StringIO(sample_branches_text.encode('utf-8'))
554
self.my_config = config.LocationConfig(location)
555
self.my_config._get_parser(branches_file)
873
self.my_config = config.BranchConfig(FakeBranch(location))
874
# Force location config to use specified file
875
self.my_location_config = self.my_config._get_location_config()
876
self.my_location_config._get_parser(branches_file)
877
# Force global config to use specified file
556
878
self.my_config._get_global_config()._get_parser(global_file)
558
880
def test_set_user_setting_sets_and_saves(self):
559
self.get_location_config('/a/c')
881
self.get_branch_config('/a/c')
560
882
record = InstrumentedConfigObj("foo")
561
self.my_config._parser = record
883
self.my_location_config._parser = record
563
885
real_mkdir = os.mkdir
564
886
self.created = False
579
905
('__setitem__', '/a/c', {}),
580
906
('__getitem__', '/a/c'),
581
907
('__setitem__', 'foo', 'bar'),
908
('__getitem__', '/a/c'),
909
('as_bool', 'recurse'),
910
('__getitem__', '/a/c'),
911
('__delitem__', 'recurse'),
912
('__getitem__', '/a/c'),
914
('__getitem__', '/a/c'),
915
('__contains__', 'foo:policy'),
583
917
record._calls[1:])
586
class TestBranchConfigItems(TestCase):
919
def test_set_user_setting_sets_and_saves2(self):
920
self.get_branch_config('/a/c')
921
self.assertIs(self.my_config.get_user_option('foo'), None)
922
self.my_config.set_user_option('foo', 'bar')
924
self.my_config.branch.control_files.files['branch.conf'],
926
self.assertEqual(self.my_config.get_user_option('foo'), 'bar')
927
self.my_config.set_user_option('foo', 'baz',
928
store=config.STORE_LOCATION)
929
self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
930
self.my_config.set_user_option('foo', 'qux')
931
self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
933
def test_get_bzr_remote_path(self):
934
my_config = config.LocationConfig('/a/c')
935
self.assertEqual('bzr', my_config.get_bzr_remote_path())
936
my_config.set_user_option('bzr_remote_path', '/path-bzr')
937
self.assertEqual('/path-bzr', my_config.get_bzr_remote_path())
938
os.environ['BZR_REMOTE_PATH'] = '/environ-bzr'
939
self.assertEqual('/environ-bzr', my_config.get_bzr_remote_path())
942
precedence_global = 'option = global'
943
precedence_branch = 'option = branch'
944
precedence_location = """
948
[http://example.com/specific]
953
class TestBranchConfigItems(tests.TestCaseInTempDir):
955
def get_branch_config(self, global_config=None, location=None,
956
location_config=None, branch_data_config=None):
957
my_config = config.BranchConfig(FakeBranch(location))
958
if global_config is not None:
959
global_file = StringIO(global_config.encode('utf-8'))
960
my_config._get_global_config()._get_parser(global_file)
961
self.my_location_config = my_config._get_location_config()
962
if location_config is not None:
963
location_file = StringIO(location_config.encode('utf-8'))
964
self.my_location_config._get_parser(location_file)
965
if branch_data_config is not None:
966
my_config.branch.control_files.files['branch.conf'] = \
588
970
def test_user_id(self):
589
branch = FakeBranch()
971
branch = FakeBranch(user_id='Robert Collins <robertc@example.net>')
590
972
my_config = config.BranchConfig(branch)
591
973
self.assertEqual("Robert Collins <robertc@example.net>",
592
my_config._get_user_id())
593
branch.control_files.email = "John"
594
self.assertEqual("John", my_config._get_user_id())
974
my_config.username())
975
my_config.branch.control_files.files['email'] = "John"
976
my_config.set_user_option('email',
977
"Robert Collins <robertc@example.org>")
978
self.assertEqual("John", my_config.username())
979
del my_config.branch.control_files.files['email']
980
self.assertEqual("Robert Collins <robertc@example.org>",
981
my_config.username())
596
983
def test_not_set_in_branch(self):
597
branch = FakeBranch()
598
my_config = config.BranchConfig(branch)
599
branch.control_files.email = None
600
config_file = StringIO(sample_config_text.encode('utf-8'))
601
(my_config._get_location_config().
602
_get_global_config()._get_parser(config_file))
984
my_config = self.get_branch_config(sample_config_text)
603
985
self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
604
986
my_config._get_user_id())
605
branch.control_files.email = "John"
987
my_config.branch.control_files.files['email'] = "John"
606
988
self.assertEqual("John", my_config._get_user_id())
608
def test_BZREMAIL_OVERRIDES(self):
609
os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
990
def test_BZR_EMAIL_OVERRIDES(self):
991
os.environ['BZR_EMAIL'] = "Robert Collins <robertc@example.org>"
610
992
branch = FakeBranch()
611
993
my_config = config.BranchConfig(branch)
612
994
self.assertEqual("Robert Collins <robertc@example.org>",
613
995
my_config.username())
615
997
def test_signatures_forced(self):
616
branch = FakeBranch()
617
my_config = config.BranchConfig(branch)
618
config_file = StringIO(sample_always_signatures)
619
(my_config._get_location_config().
620
_get_global_config()._get_parser(config_file))
621
self.assertEqual(config.CHECK_ALWAYS, my_config.signature_checking())
998
my_config = self.get_branch_config(
999
global_config=sample_always_signatures)
1000
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
1001
self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
1002
self.assertTrue(my_config.signature_needed())
1004
def test_signatures_forced_branch(self):
1005
my_config = self.get_branch_config(
1006
global_config=sample_ignore_signatures,
1007
branch_data_config=sample_always_signatures)
1008
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
1009
self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
1010
self.assertTrue(my_config.signature_needed())
623
1012
def test_gpg_signing_command(self):
624
branch = FakeBranch()
625
my_config = config.BranchConfig(branch)
1013
my_config = self.get_branch_config(
1014
# branch data cannot set gpg_signing_command
1015
branch_data_config="gpg_signing_command=pgp")
626
1016
config_file = StringIO(sample_config_text.encode('utf-8'))
627
(my_config._get_location_config().
628
_get_global_config()._get_parser(config_file))
1017
my_config._get_global_config()._get_parser(config_file)
629
1018
self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
631
1020
def test_get_user_option_global(self):
632
1021
branch = FakeBranch()
633
1022
my_config = config.BranchConfig(branch)
634
1023
config_file = StringIO(sample_config_text.encode('utf-8'))
635
(my_config._get_location_config().
636
_get_global_config()._get_parser(config_file))
1024
(my_config._get_global_config()._get_parser(config_file))
637
1025
self.assertEqual('something',
638
1026
my_config.get_user_option('user_global_option'))
640
1028
def test_post_commit_default(self):
641
1029
branch = FakeBranch()
643
my_config = config.BranchConfig(branch)
644
config_file = StringIO(sample_config_text.encode('utf-8'))
645
(my_config._get_location_config().
646
_get_global_config()._get_parser(config_file))
647
branch_file = StringIO(sample_branches_text)
648
my_config._get_location_config()._get_parser(branch_file)
649
self.assertEqual('bzrlib.tests.test_config.post_commit',
650
my_config.post_commit())
653
class TestMailAddressExtraction(TestCase):
1030
my_config = self.get_branch_config(sample_config_text, '/a/c',
1031
sample_branches_text)
1032
self.assertEqual(my_config.branch.base, '/a/c')
1033
self.assertEqual('bzrlib.tests.test_config.post_commit',
1034
my_config.post_commit())
1035
my_config.set_user_option('post_commit', 'rmtree_root')
1036
# post-commit is ignored when bresent in branch data
1037
self.assertEqual('bzrlib.tests.test_config.post_commit',
1038
my_config.post_commit())
1039
my_config.set_user_option('post_commit', 'rmtree_root',
1040
store=config.STORE_LOCATION)
1041
self.assertEqual('rmtree_root', my_config.post_commit())
1043
def test_config_precedence(self):
1044
my_config = self.get_branch_config(global_config=precedence_global)
1045
self.assertEqual(my_config.get_user_option('option'), 'global')
1046
my_config = self.get_branch_config(global_config=precedence_global,
1047
branch_data_config=precedence_branch)
1048
self.assertEqual(my_config.get_user_option('option'), 'branch')
1049
my_config = self.get_branch_config(global_config=precedence_global,
1050
branch_data_config=precedence_branch,
1051
location_config=precedence_location)
1052
self.assertEqual(my_config.get_user_option('option'), 'recurse')
1053
my_config = self.get_branch_config(global_config=precedence_global,
1054
branch_data_config=precedence_branch,
1055
location_config=precedence_location,
1056
location='http://example.com/specific')
1057
self.assertEqual(my_config.get_user_option('option'), 'exact')
1059
def test_get_mail_client(self):
1060
config = self.get_branch_config()
1061
client = config.get_mail_client()
1062
self.assertIsInstance(client, mail_client.DefaultMail)
1065
config.set_user_option('mail_client', 'evolution')
1066
client = config.get_mail_client()
1067
self.assertIsInstance(client, mail_client.Evolution)
1069
config.set_user_option('mail_client', 'kmail')
1070
client = config.get_mail_client()
1071
self.assertIsInstance(client, mail_client.KMail)
1073
config.set_user_option('mail_client', 'mutt')
1074
client = config.get_mail_client()
1075
self.assertIsInstance(client, mail_client.Mutt)
1077
config.set_user_option('mail_client', 'thunderbird')
1078
client = config.get_mail_client()
1079
self.assertIsInstance(client, mail_client.Thunderbird)
1082
config.set_user_option('mail_client', 'default')
1083
client = config.get_mail_client()
1084
self.assertIsInstance(client, mail_client.DefaultMail)
1086
config.set_user_option('mail_client', 'editor')
1087
client = config.get_mail_client()
1088
self.assertIsInstance(client, mail_client.Editor)
1090
config.set_user_option('mail_client', 'mapi')
1091
client = config.get_mail_client()
1092
self.assertIsInstance(client, mail_client.MAPIClient)
1094
config.set_user_option('mail_client', 'xdg-email')
1095
client = config.get_mail_client()
1096
self.assertIsInstance(client, mail_client.XDGEmail)
1098
config.set_user_option('mail_client', 'firebird')
1099
self.assertRaises(errors.UnknownMailClient, config.get_mail_client)
1102
class TestMailAddressExtraction(tests.TestCase):
655
1104
def test_extract_email_address(self):
656
1105
self.assertEqual('jane@test.com',
657
1106
config.extract_email_address('Jane <jane@test.com>'))
658
self.assertRaises(errors.BzrError,
1107
self.assertRaises(errors.NoEmailInUsername,
659
1108
config.extract_email_address, 'Jane Tester')
1110
def test_parse_username(self):
1111
self.assertEqual(('', 'jdoe@example.com'),
1112
config.parse_username('jdoe@example.com'))
1113
self.assertEqual(('', 'jdoe@example.com'),
1114
config.parse_username('<jdoe@example.com>'))
1115
self.assertEqual(('John Doe', 'jdoe@example.com'),
1116
config.parse_username('John Doe <jdoe@example.com>'))
1117
self.assertEqual(('John Doe', ''),
1118
config.parse_username('John Doe'))
1119
self.assertEqual(('John Doe', 'jdoe@example.com'),
1120
config.parse_username('John Doe jdoe@example.com'))
1122
class TestTreeConfig(tests.TestCaseWithTransport):
1124
def test_get_value(self):
1125
"""Test that retreiving a value from a section is possible"""
1126
branch = self.make_branch('.')
1127
tree_config = config.TreeConfig(branch)
1128
tree_config.set_option('value', 'key', 'SECTION')
1129
tree_config.set_option('value2', 'key2')
1130
tree_config.set_option('value3-top', 'key3')
1131
tree_config.set_option('value3-section', 'key3', 'SECTION')
1132
value = tree_config.get_option('key', 'SECTION')
1133
self.assertEqual(value, 'value')
1134
value = tree_config.get_option('key2')
1135
self.assertEqual(value, 'value2')
1136
self.assertEqual(tree_config.get_option('non-existant'), None)
1137
value = tree_config.get_option('non-existant', 'SECTION')
1138
self.assertEqual(value, None)
1139
value = tree_config.get_option('non-existant', default='default')
1140
self.assertEqual(value, 'default')
1141
self.assertEqual(tree_config.get_option('key2', 'NOSECTION'), None)
1142
value = tree_config.get_option('key2', 'NOSECTION', default='default')
1143
self.assertEqual(value, 'default')
1144
value = tree_config.get_option('key3')
1145
self.assertEqual(value, 'value3-top')
1146
value = tree_config.get_option('key3', 'SECTION')
1147
self.assertEqual(value, 'value3-section')
1150
class TestTransportConfig(tests.TestCaseWithTransport):
1152
def test_get_value(self):
1153
"""Test that retreiving a value from a section is possible"""
1154
bzrdir_config = config.TransportConfig(transport.get_transport('.'),
1156
bzrdir_config.set_option('value', 'key', 'SECTION')
1157
bzrdir_config.set_option('value2', 'key2')
1158
bzrdir_config.set_option('value3-top', 'key3')
1159
bzrdir_config.set_option('value3-section', 'key3', 'SECTION')
1160
value = bzrdir_config.get_option('key', 'SECTION')
1161
self.assertEqual(value, 'value')
1162
value = bzrdir_config.get_option('key2')
1163
self.assertEqual(value, 'value2')
1164
self.assertEqual(bzrdir_config.get_option('non-existant'), None)
1165
value = bzrdir_config.get_option('non-existant', 'SECTION')
1166
self.assertEqual(value, None)
1167
value = bzrdir_config.get_option('non-existant', default='default')
1168
self.assertEqual(value, 'default')
1169
self.assertEqual(bzrdir_config.get_option('key2', 'NOSECTION'), None)
1170
value = bzrdir_config.get_option('key2', 'NOSECTION',
1172
self.assertEqual(value, 'default')
1173
value = bzrdir_config.get_option('key3')
1174
self.assertEqual(value, 'value3-top')
1175
value = bzrdir_config.get_option('key3', 'SECTION')
1176
self.assertEqual(value, 'value3-section')
1179
class TestAuthenticationConfigFile(tests.TestCase):
1180
"""Test the authentication.conf file matching"""
1182
def _got_user_passwd(self, expected_user, expected_password,
1183
config, *args, **kwargs):
1184
credentials = config.get_credentials(*args, **kwargs)
1185
if credentials is None:
1189
user = credentials['user']
1190
password = credentials['password']
1191
self.assertEquals(expected_user, user)
1192
self.assertEquals(expected_password, password)
1194
def test_empty_config(self):
1195
conf = config.AuthenticationConfig(_file=StringIO())
1196
self.assertEquals({}, conf._get_config())
1197
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1199
def test_missing_auth_section_header(self):
1200
conf = config.AuthenticationConfig(_file=StringIO('foo = bar'))
1201
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1203
def test_auth_section_header_not_closed(self):
1204
conf = config.AuthenticationConfig(_file=StringIO('[DEF'))
1205
self.assertRaises(errors.ParseConfigError, conf._get_config)
1207
def test_auth_value_not_boolean(self):
1208
conf = config.AuthenticationConfig(_file=StringIO(
1212
verify_certificates=askme # Error: Not a boolean
1214
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1216
def test_auth_value_not_int(self):
1217
conf = config.AuthenticationConfig(_file=StringIO(
1221
port=port # Error: Not an int
1223
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1225
def test_credentials_for_scheme_host(self):
1226
conf = config.AuthenticationConfig(_file=StringIO(
1227
"""# Identity on foo.net
1232
password=secret-pass
1235
self._got_user_passwd('joe', 'secret-pass', conf, 'ftp', 'foo.net')
1237
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1239
self._got_user_passwd(None, None, conf, 'ftp', 'bar.net')
1241
def test_credentials_for_host_port(self):
1242
conf = config.AuthenticationConfig(_file=StringIO(
1243
"""# Identity on foo.net
1249
password=secret-pass
1252
self._got_user_passwd('joe', 'secret-pass',
1253
conf, 'ftp', 'foo.net', port=10021)
1255
self._got_user_passwd(None, None, conf, 'ftp', 'foo.net')
1257
def test_for_matching_host(self):
1258
conf = config.AuthenticationConfig(_file=StringIO(
1259
"""# Identity on foo.net
1265
[sourceforge domain]
1272
self._got_user_passwd('georges', 'bendover',
1273
conf, 'bzr', 'foo.bzr.sf.net')
1275
self._got_user_passwd(None, None,
1276
conf, 'bzr', 'bbzr.sf.net')
1278
def test_for_matching_host_None(self):
1279
conf = config.AuthenticationConfig(_file=StringIO(
1280
"""# Identity on foo.net
1290
self._got_user_passwd('joe', 'joepass',
1291
conf, 'bzr', 'quux.net')
1292
# no host but different scheme
1293
self._got_user_passwd('georges', 'bendover',
1294
conf, 'ftp', 'quux.net')
1296
def test_credentials_for_path(self):
1297
conf = config.AuthenticationConfig(_file=StringIO(
1313
self._got_user_passwd(None, None,
1314
conf, 'http', host='bar.org', path='/dir3')
1316
self._got_user_passwd('georges', 'bendover',
1317
conf, 'http', host='bar.org', path='/dir2')
1319
self._got_user_passwd('jim', 'jimpass',
1320
conf, 'http', host='bar.org',path='/dir1/subdir')
1322
def test_credentials_for_user(self):
1323
conf = config.AuthenticationConfig(_file=StringIO(
1332
self._got_user_passwd('jim', 'jimpass',
1333
conf, 'http', 'bar.org')
1335
self._got_user_passwd('jim', 'jimpass',
1336
conf, 'http', 'bar.org', user='jim')
1337
# Don't get a different user if one is specified
1338
self._got_user_passwd(None, None,
1339
conf, 'http', 'bar.org', user='georges')
1341
def test_credentials_for_user_without_password(self):
1342
conf = config.AuthenticationConfig(_file=StringIO(
1349
# Get user but no password
1350
self._got_user_passwd('jim', None,
1351
conf, 'http', 'bar.org')
1353
def test_verify_certificates(self):
1354
conf = config.AuthenticationConfig(_file=StringIO(
1361
verify_certificates=False
1368
credentials = conf.get_credentials('https', 'bar.org')
1369
self.assertEquals(False, credentials.get('verify_certificates'))
1370
credentials = conf.get_credentials('https', 'foo.net')
1371
self.assertEquals(True, credentials.get('verify_certificates'))
1374
class TestAuthenticationConfig(tests.TestCase):
1375
"""Test AuthenticationConfig behaviour"""
1377
def _check_default_prompt(self, expected_prompt_format, scheme,
1378
host=None, port=None, realm=None, path=None):
1381
user, password = 'jim', 'precious'
1382
expected_prompt = expected_prompt_format % {
1383
'scheme': scheme, 'host': host, 'port': port,
1384
'user': user, 'realm': realm}
1386
stdout = tests.StringIOWrapper()
1387
ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
1389
# We use an empty conf so that the user is always prompted
1390
conf = config.AuthenticationConfig()
1391
self.assertEquals(password,
1392
conf.get_password(scheme, host, user, port=port,
1393
realm=realm, path=path))
1394
self.assertEquals(stdout.getvalue(), expected_prompt)
1396
def test_default_prompts(self):
1397
# HTTP prompts can't be tested here, see test_http.py
1398
self._check_default_prompt('FTP %(user)s@%(host)s password: ', 'ftp')
1399
self._check_default_prompt('FTP %(user)s@%(host)s:%(port)d password: ',
1402
self._check_default_prompt('SSH %(user)s@%(host)s:%(port)d password: ',
1404
# SMTP port handling is a bit special (it's handled if embedded in the
1406
# FIXME: should we: forbid that, extend it to other schemes, leave
1407
# things as they are that's fine thank you ?
1408
self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1410
self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1411
'smtp', host='bar.org:10025')
1412
self._check_default_prompt(
1413
'SMTP %(user)s@%(host)s:%(port)d password: ',
1416
def test_ssh_password_emits_warning(self):
1417
conf = config.AuthenticationConfig(_file=StringIO(
1425
entered_password = 'typed-by-hand'
1426
stdout = tests.StringIOWrapper()
1427
ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1430
# Since the password defined in the authentication config is ignored,
1431
# the user is prompted
1432
self.assertEquals(entered_password,
1433
conf.get_password('ssh', 'bar.org', user='jim'))
1434
self.assertContainsRe(
1435
self._get_log(keep_log_file=True),
1436
'password ignored in section \[ssh with password\]')
1438
def test_ssh_without_password_doesnt_emit_warning(self):
1439
conf = config.AuthenticationConfig(_file=StringIO(
1446
entered_password = 'typed-by-hand'
1447
stdout = tests.StringIOWrapper()
1448
ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1451
# Since the password defined in the authentication config is ignored,
1452
# the user is prompted
1453
self.assertEquals(entered_password,
1454
conf.get_password('ssh', 'bar.org', user='jim'))
1455
# No warning shoud be emitted since there is no password. We are only
1457
self.assertNotContainsRe(
1458
self._get_log(keep_log_file=True),
1459
'password ignored in section \[ssh with password\]')
1462
# FIXME: Once we have a way to declare authentication to all test servers, we
1463
# can implement generic tests.
1464
# test_user_password_in_url
1465
# test_user_in_url_password_from_config
1466
# test_user_in_url_password_prompted
1467
# test_user_in_config
1468
# test_user_getpass.getuser
1469
# test_user_prompted ?
1470
class TestAuthenticationRing(tests.TestCaseWithTransport):