535
672
self.failUnless(isinstance(global_config, config.GlobalConfig))
536
673
self.failUnless(global_config is my_config._get_global_config())
538
def test__get_section_no_match(self):
675
def test__get_matching_sections_no_match(self):
539
676
self.get_branch_config('/')
540
self.assertEqual(None, self.my_location_config._get_section())
542
def test__get_section_exact(self):
677
self.assertEqual([], self.my_location_config._get_matching_sections())
679
def test__get_matching_sections_exact(self):
543
680
self.get_branch_config('http://www.example.com')
544
self.assertEqual('http://www.example.com',
545
self.my_location_config._get_section())
547
def test__get_section_suffix_does_not(self):
681
self.assertEqual([('http://www.example.com', '')],
682
self.my_location_config._get_matching_sections())
684
def test__get_matching_sections_suffix_does_not(self):
548
685
self.get_branch_config('http://www.example.com-com')
549
self.assertEqual(None, self.my_location_config._get_section())
686
self.assertEqual([], self.my_location_config._get_matching_sections())
551
def test__get_section_subdir_recursive(self):
688
def test__get_matching_sections_subdir_recursive(self):
552
689
self.get_branch_config('http://www.example.com/com')
553
self.assertEqual('http://www.example.com',
554
self.my_location_config._get_section())
556
def test__get_section_subdir_matches(self):
557
self.get_branch_config('http://www.example.com/useglobal')
558
self.assertEqual('http://www.example.com/useglobal',
559
self.my_location_config._get_section())
561
def test__get_section_subdir_nonrecursive(self):
690
self.assertEqual([('http://www.example.com', 'com')],
691
self.my_location_config._get_matching_sections())
693
def test__get_matching_sections_ignoreparent(self):
694
self.get_branch_config('http://www.example.com/ignoreparent')
695
self.assertEqual([('http://www.example.com/ignoreparent', '')],
696
self.my_location_config._get_matching_sections())
698
def test__get_matching_sections_ignoreparent_subdir(self):
562
699
self.get_branch_config(
563
'http://www.example.com/useglobal/childbranch')
564
self.assertEqual('http://www.example.com',
565
self.my_location_config._get_section())
700
'http://www.example.com/ignoreparent/childbranch')
701
self.assertEqual([('http://www.example.com/ignoreparent',
703
self.my_location_config._get_matching_sections())
567
def test__get_section_subdir_trailing_slash(self):
705
def test__get_matching_sections_subdir_trailing_slash(self):
568
706
self.get_branch_config('/b')
569
self.assertEqual('/b/', self.my_location_config._get_section())
707
self.assertEqual([('/b/', '')],
708
self.my_location_config._get_matching_sections())
571
def test__get_section_subdir_child(self):
710
def test__get_matching_sections_subdir_child(self):
572
711
self.get_branch_config('/a/foo')
573
self.assertEqual('/a/*', self.my_location_config._get_section())
712
self.assertEqual([('/a/*', ''), ('/a/', 'foo')],
713
self.my_location_config._get_matching_sections())
575
def test__get_section_subdir_child_child(self):
715
def test__get_matching_sections_subdir_child_child(self):
576
716
self.get_branch_config('/a/foo/bar')
577
self.assertEqual('/a/', self.my_location_config._get_section())
717
self.assertEqual([('/a/*', 'bar'), ('/a/', 'foo/bar')],
718
self.my_location_config._get_matching_sections())
579
def test__get_section_trailing_slash_with_children(self):
720
def test__get_matching_sections_trailing_slash_with_children(self):
580
721
self.get_branch_config('/a/')
581
self.assertEqual('/a/', self.my_location_config._get_section())
722
self.assertEqual([('/a/', '')],
723
self.my_location_config._get_matching_sections())
583
def test__get_section_explicit_over_glob(self):
725
def test__get_matching_sections_explicit_over_glob(self):
726
# XXX: 2006-09-08 jamesh
727
# This test only passes because ord('c') > ord('*'). If there
728
# was a config section for '/a/?', it would get precedence
584
730
self.get_branch_config('/a/c')
585
self.assertEqual('/a/c', self.my_location_config._get_section())
731
self.assertEqual([('/a/c', ''), ('/a/*', ''), ('/a/', 'c')],
732
self.my_location_config._get_matching_sections())
734
def test__get_option_policy_normal(self):
735
self.get_branch_config('http://www.example.com')
737
self.my_location_config._get_config_policy(
738
'http://www.example.com', 'normal_option'),
741
def test__get_option_policy_norecurse(self):
742
self.get_branch_config('http://www.example.com')
744
self.my_location_config._get_option_policy(
745
'http://www.example.com', 'norecurse_option'),
746
config.POLICY_NORECURSE)
747
# Test old recurse=False setting:
749
self.my_location_config._get_option_policy(
750
'http://www.example.com/norecurse', 'normal_option'),
751
config.POLICY_NORECURSE)
753
def test__get_option_policy_normal(self):
754
self.get_branch_config('http://www.example.com')
756
self.my_location_config._get_option_policy(
757
'http://www.example.com', 'appendpath_option'),
758
config.POLICY_APPENDPATH)
588
760
def test_location_without_username(self):
589
self.get_branch_config('http://www.example.com/useglobal')
761
self.get_branch_config('http://www.example.com/ignoreparent')
590
762
self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
591
763
self.my_config.username())
799
1070
# post-commit is ignored when bresent in branch data
800
1071
self.assertEqual('bzrlib.tests.test_config.post_commit',
801
1072
my_config.post_commit())
802
my_config.set_user_option('post_commit', 'rmtree_root', local=True)
1073
my_config.set_user_option('post_commit', 'rmtree_root',
1074
store=config.STORE_LOCATION)
803
1075
self.assertEqual('rmtree_root', my_config.post_commit())
805
1077
def test_config_precedence(self):
806
1078
my_config = self.get_branch_config(global_config=precedence_global)
807
1079
self.assertEqual(my_config.get_user_option('option'), 'global')
808
my_config = self.get_branch_config(global_config=precedence_global,
1080
my_config = self.get_branch_config(global_config=precedence_global,
809
1081
branch_data_config=precedence_branch)
810
1082
self.assertEqual(my_config.get_user_option('option'), 'branch')
811
my_config = self.get_branch_config(global_config=precedence_global,
1083
my_config = self.get_branch_config(global_config=precedence_global,
812
1084
branch_data_config=precedence_branch,
813
1085
location_config=precedence_location)
814
1086
self.assertEqual(my_config.get_user_option('option'), 'recurse')
815
my_config = self.get_branch_config(global_config=precedence_global,
1087
my_config = self.get_branch_config(global_config=precedence_global,
816
1088
branch_data_config=precedence_branch,
817
1089
location_config=precedence_location,
818
1090
location='http://example.com/specific')
819
1091
self.assertEqual(my_config.get_user_option('option'), 'exact')
822
class TestMailAddressExtraction(TestCase):
1093
def test_get_mail_client(self):
1094
config = self.get_branch_config()
1095
client = config.get_mail_client()
1096
self.assertIsInstance(client, mail_client.DefaultMail)
1099
config.set_user_option('mail_client', 'evolution')
1100
client = config.get_mail_client()
1101
self.assertIsInstance(client, mail_client.Evolution)
1103
config.set_user_option('mail_client', 'kmail')
1104
client = config.get_mail_client()
1105
self.assertIsInstance(client, mail_client.KMail)
1107
config.set_user_option('mail_client', 'mutt')
1108
client = config.get_mail_client()
1109
self.assertIsInstance(client, mail_client.Mutt)
1111
config.set_user_option('mail_client', 'thunderbird')
1112
client = config.get_mail_client()
1113
self.assertIsInstance(client, mail_client.Thunderbird)
1116
config.set_user_option('mail_client', 'default')
1117
client = config.get_mail_client()
1118
self.assertIsInstance(client, mail_client.DefaultMail)
1120
config.set_user_option('mail_client', 'editor')
1121
client = config.get_mail_client()
1122
self.assertIsInstance(client, mail_client.Editor)
1124
config.set_user_option('mail_client', 'mapi')
1125
client = config.get_mail_client()
1126
self.assertIsInstance(client, mail_client.MAPIClient)
1128
config.set_user_option('mail_client', 'xdg-email')
1129
client = config.get_mail_client()
1130
self.assertIsInstance(client, mail_client.XDGEmail)
1132
config.set_user_option('mail_client', 'firebird')
1133
self.assertRaises(errors.UnknownMailClient, config.get_mail_client)
1136
class TestMailAddressExtraction(tests.TestCase):
824
1138
def test_extract_email_address(self):
825
1139
self.assertEqual('jane@test.com',
826
1140
config.extract_email_address('Jane <jane@test.com>'))
827
self.assertRaises(errors.BzrError,
1141
self.assertRaises(errors.NoEmailInUsername,
828
1142
config.extract_email_address, 'Jane Tester')
1144
def test_parse_username(self):
1145
self.assertEqual(('', 'jdoe@example.com'),
1146
config.parse_username('jdoe@example.com'))
1147
self.assertEqual(('', 'jdoe@example.com'),
1148
config.parse_username('<jdoe@example.com>'))
1149
self.assertEqual(('John Doe', 'jdoe@example.com'),
1150
config.parse_username('John Doe <jdoe@example.com>'))
1151
self.assertEqual(('John Doe', ''),
1152
config.parse_username('John Doe'))
1153
self.assertEqual(('John Doe', 'jdoe@example.com'),
1154
config.parse_username('John Doe jdoe@example.com'))
1156
class TestTreeConfig(tests.TestCaseWithTransport):
1158
def test_get_value(self):
1159
"""Test that retreiving a value from a section is possible"""
1160
branch = self.make_branch('.')
1161
tree_config = config.TreeConfig(branch)
1162
tree_config.set_option('value', 'key', 'SECTION')
1163
tree_config.set_option('value2', 'key2')
1164
tree_config.set_option('value3-top', 'key3')
1165
tree_config.set_option('value3-section', 'key3', 'SECTION')
1166
value = tree_config.get_option('key', 'SECTION')
1167
self.assertEqual(value, 'value')
1168
value = tree_config.get_option('key2')
1169
self.assertEqual(value, 'value2')
1170
self.assertEqual(tree_config.get_option('non-existant'), None)
1171
value = tree_config.get_option('non-existant', 'SECTION')
1172
self.assertEqual(value, None)
1173
value = tree_config.get_option('non-existant', default='default')
1174
self.assertEqual(value, 'default')
1175
self.assertEqual(tree_config.get_option('key2', 'NOSECTION'), None)
1176
value = tree_config.get_option('key2', 'NOSECTION', default='default')
1177
self.assertEqual(value, 'default')
1178
value = tree_config.get_option('key3')
1179
self.assertEqual(value, 'value3-top')
1180
value = tree_config.get_option('key3', 'SECTION')
1181
self.assertEqual(value, 'value3-section')
1184
class TestTransportConfig(tests.TestCaseWithTransport):
1186
def test_get_value(self):
1187
"""Test that retreiving a value from a section is possible"""
1188
bzrdir_config = config.TransportConfig(transport.get_transport('.'),
1190
bzrdir_config.set_option('value', 'key', 'SECTION')
1191
bzrdir_config.set_option('value2', 'key2')
1192
bzrdir_config.set_option('value3-top', 'key3')
1193
bzrdir_config.set_option('value3-section', 'key3', 'SECTION')
1194
value = bzrdir_config.get_option('key', 'SECTION')
1195
self.assertEqual(value, 'value')
1196
value = bzrdir_config.get_option('key2')
1197
self.assertEqual(value, 'value2')
1198
self.assertEqual(bzrdir_config.get_option('non-existant'), None)
1199
value = bzrdir_config.get_option('non-existant', 'SECTION')
1200
self.assertEqual(value, None)
1201
value = bzrdir_config.get_option('non-existant', default='default')
1202
self.assertEqual(value, 'default')
1203
self.assertEqual(bzrdir_config.get_option('key2', 'NOSECTION'), None)
1204
value = bzrdir_config.get_option('key2', 'NOSECTION',
1206
self.assertEqual(value, 'default')
1207
value = bzrdir_config.get_option('key3')
1208
self.assertEqual(value, 'value3-top')
1209
value = bzrdir_config.get_option('key3', 'SECTION')
1210
self.assertEqual(value, 'value3-section')
1212
def test_set_unset_default_stack_on(self):
1213
my_dir = self.make_bzrdir('.')
1214
bzrdir_config = config.BzrDirConfig(my_dir)
1215
self.assertIs(None, bzrdir_config.get_default_stack_on())
1216
bzrdir_config.set_default_stack_on('Foo')
1217
self.assertEqual('Foo', bzrdir_config._config.get_option(
1218
'default_stack_on'))
1219
self.assertEqual('Foo', bzrdir_config.get_default_stack_on())
1220
bzrdir_config.set_default_stack_on(None)
1221
self.assertIs(None, bzrdir_config.get_default_stack_on())
1224
class TestAuthenticationConfigFile(tests.TestCase):
1225
"""Test the authentication.conf file matching"""
1227
def _got_user_passwd(self, expected_user, expected_password,
1228
config, *args, **kwargs):
1229
credentials = config.get_credentials(*args, **kwargs)
1230
if credentials is None:
1234
user = credentials['user']
1235
password = credentials['password']
1236
self.assertEquals(expected_user, user)
1237
self.assertEquals(expected_password, password)
1239
def test_empty_config(self):
1240
conf = config.AuthenticationConfig(_file=StringIO())
1241
self.assertEquals({}, conf._get_config())
1242
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1244
def test_missing_auth_section_header(self):
1245
conf = config.AuthenticationConfig(_file=StringIO('foo = bar'))
1246
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1248
def test_auth_section_header_not_closed(self):
1249
conf = config.AuthenticationConfig(_file=StringIO('[DEF'))
1250
self.assertRaises(errors.ParseConfigError, conf._get_config)
1252
def test_auth_value_not_boolean(self):
1253
conf = config.AuthenticationConfig(_file=StringIO(
1257
verify_certificates=askme # Error: Not a boolean
1259
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1261
def test_auth_value_not_int(self):
1262
conf = config.AuthenticationConfig(_file=StringIO(
1266
port=port # Error: Not an int
1268
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1270
def test_unknown_password_encoding(self):
1271
conf = config.AuthenticationConfig(_file=StringIO(
1275
password_encoding=unknown
1277
self.assertRaises(ValueError, conf.get_password,
1278
'ftp', 'foo.net', 'joe')
1280
def test_credentials_for_scheme_host(self):
1281
conf = config.AuthenticationConfig(_file=StringIO(
1282
"""# Identity on foo.net
1287
password=secret-pass
1290
self._got_user_passwd('joe', 'secret-pass', conf, 'ftp', 'foo.net')
1292
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1294
self._got_user_passwd(None, None, conf, 'ftp', 'bar.net')
1296
def test_credentials_for_host_port(self):
1297
conf = config.AuthenticationConfig(_file=StringIO(
1298
"""# Identity on foo.net
1304
password=secret-pass
1307
self._got_user_passwd('joe', 'secret-pass',
1308
conf, 'ftp', 'foo.net', port=10021)
1310
self._got_user_passwd(None, None, conf, 'ftp', 'foo.net')
1312
def test_for_matching_host(self):
1313
conf = config.AuthenticationConfig(_file=StringIO(
1314
"""# Identity on foo.net
1320
[sourceforge domain]
1327
self._got_user_passwd('georges', 'bendover',
1328
conf, 'bzr', 'foo.bzr.sf.net')
1330
self._got_user_passwd(None, None,
1331
conf, 'bzr', 'bbzr.sf.net')
1333
def test_for_matching_host_None(self):
1334
conf = config.AuthenticationConfig(_file=StringIO(
1335
"""# Identity on foo.net
1345
self._got_user_passwd('joe', 'joepass',
1346
conf, 'bzr', 'quux.net')
1347
# no host but different scheme
1348
self._got_user_passwd('georges', 'bendover',
1349
conf, 'ftp', 'quux.net')
1351
def test_credentials_for_path(self):
1352
conf = config.AuthenticationConfig(_file=StringIO(
1368
self._got_user_passwd(None, None,
1369
conf, 'http', host='bar.org', path='/dir3')
1371
self._got_user_passwd('georges', 'bendover',
1372
conf, 'http', host='bar.org', path='/dir2')
1374
self._got_user_passwd('jim', 'jimpass',
1375
conf, 'http', host='bar.org',path='/dir1/subdir')
1377
def test_credentials_for_user(self):
1378
conf = config.AuthenticationConfig(_file=StringIO(
1387
self._got_user_passwd('jim', 'jimpass',
1388
conf, 'http', 'bar.org')
1390
self._got_user_passwd('jim', 'jimpass',
1391
conf, 'http', 'bar.org', user='jim')
1392
# Don't get a different user if one is specified
1393
self._got_user_passwd(None, None,
1394
conf, 'http', 'bar.org', user='georges')
1396
def test_credentials_for_user_without_password(self):
1397
conf = config.AuthenticationConfig(_file=StringIO(
1404
# Get user but no password
1405
self._got_user_passwd('jim', None,
1406
conf, 'http', 'bar.org')
1408
def test_verify_certificates(self):
1409
conf = config.AuthenticationConfig(_file=StringIO(
1416
verify_certificates=False
1423
credentials = conf.get_credentials('https', 'bar.org')
1424
self.assertEquals(False, credentials.get('verify_certificates'))
1425
credentials = conf.get_credentials('https', 'foo.net')
1426
self.assertEquals(True, credentials.get('verify_certificates'))
1429
class TestAuthenticationStorage(tests.TestCaseInTempDir):
1431
def test_set_credentials(self):
1432
conf = config.AuthenticationConfig()
1433
conf.set_credentials('name', 'host', 'user', 'scheme', 'password',
1434
99, path='/foo', verify_certificates=False, realm='realm')
1435
credentials = conf.get_credentials(host='host', scheme='scheme',
1436
port=99, path='/foo',
1438
CREDENTIALS = {'name': 'name', 'user': 'user', 'password': 'password',
1439
'verify_certificates': False, 'scheme': 'scheme',
1440
'host': 'host', 'port': 99, 'path': '/foo',
1442
self.assertEqual(CREDENTIALS, credentials)
1443
credentials_from_disk = config.AuthenticationConfig().get_credentials(
1444
host='host', scheme='scheme', port=99, path='/foo', realm='realm')
1445
self.assertEqual(CREDENTIALS, credentials_from_disk)
1447
def test_reset_credentials_different_name(self):
1448
conf = config.AuthenticationConfig()
1449
conf.set_credentials('name', 'host', 'user', 'scheme', 'password'),
1450
conf.set_credentials('name2', 'host', 'user2', 'scheme', 'password'),
1451
self.assertIs(None, conf._get_config().get('name'))
1452
credentials = conf.get_credentials(host='host', scheme='scheme')
1453
CREDENTIALS = {'name': 'name2', 'user': 'user2', 'password':
1454
'password', 'verify_certificates': True,
1455
'scheme': 'scheme', 'host': 'host', 'port': None,
1456
'path': None, 'realm': None}
1457
self.assertEqual(CREDENTIALS, credentials)
1460
class TestAuthenticationConfig(tests.TestCase):
1461
"""Test AuthenticationConfig behaviour"""
1463
def _check_default_password_prompt(self, expected_prompt_format, scheme,
1464
host=None, port=None, realm=None,
1468
user, password = 'jim', 'precious'
1469
expected_prompt = expected_prompt_format % {
1470
'scheme': scheme, 'host': host, 'port': port,
1471
'user': user, 'realm': realm}
1473
stdout = tests.StringIOWrapper()
1474
stderr = tests.StringIOWrapper()
1475
ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
1476
stdout=stdout, stderr=stderr)
1477
# We use an empty conf so that the user is always prompted
1478
conf = config.AuthenticationConfig()
1479
self.assertEquals(password,
1480
conf.get_password(scheme, host, user, port=port,
1481
realm=realm, path=path))
1482
self.assertEquals(expected_prompt, stderr.getvalue())
1483
self.assertEquals('', stdout.getvalue())
1485
def _check_default_username_prompt(self, expected_prompt_format, scheme,
1486
host=None, port=None, realm=None,
1491
expected_prompt = expected_prompt_format % {
1492
'scheme': scheme, 'host': host, 'port': port,
1494
stdout = tests.StringIOWrapper()
1495
stderr = tests.StringIOWrapper()
1496
ui.ui_factory = tests.TestUIFactory(stdin=username+ '\n',
1497
stdout=stdout, stderr=stderr)
1498
# We use an empty conf so that the user is always prompted
1499
conf = config.AuthenticationConfig()
1500
self.assertEquals(username, conf.get_user(scheme, host, port=port,
1501
realm=realm, path=path, ask=True))
1502
self.assertEquals(expected_prompt, stderr.getvalue())
1503
self.assertEquals('', stdout.getvalue())
1505
def test_username_defaults_prompts(self):
1506
# HTTP prompts can't be tested here, see test_http.py
1507
self._check_default_username_prompt('FTP %(host)s username: ', 'ftp')
1508
self._check_default_username_prompt(
1509
'FTP %(host)s:%(port)d username: ', 'ftp', port=10020)
1510
self._check_default_username_prompt(
1511
'SSH %(host)s:%(port)d username: ', 'ssh', port=12345)
1513
def test_username_default_no_prompt(self):
1514
conf = config.AuthenticationConfig()
1515
self.assertEquals(None,
1516
conf.get_user('ftp', 'example.com'))
1517
self.assertEquals("explicitdefault",
1518
conf.get_user('ftp', 'example.com', default="explicitdefault"))
1520
def test_password_default_prompts(self):
1521
# HTTP prompts can't be tested here, see test_http.py
1522
self._check_default_password_prompt(
1523
'FTP %(user)s@%(host)s password: ', 'ftp')
1524
self._check_default_password_prompt(
1525
'FTP %(user)s@%(host)s:%(port)d password: ', 'ftp', port=10020)
1526
self._check_default_password_prompt(
1527
'SSH %(user)s@%(host)s:%(port)d password: ', 'ssh', port=12345)
1528
# SMTP port handling is a bit special (it's handled if embedded in the
1530
# FIXME: should we: forbid that, extend it to other schemes, leave
1531
# things as they are that's fine thank you ?
1532
self._check_default_password_prompt('SMTP %(user)s@%(host)s password: ',
1534
self._check_default_password_prompt('SMTP %(user)s@%(host)s password: ',
1535
'smtp', host='bar.org:10025')
1536
self._check_default_password_prompt(
1537
'SMTP %(user)s@%(host)s:%(port)d password: ',
1540
def test_ssh_password_emits_warning(self):
1541
conf = config.AuthenticationConfig(_file=StringIO(
1549
entered_password = 'typed-by-hand'
1550
stdout = tests.StringIOWrapper()
1551
ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1554
# Since the password defined in the authentication config is ignored,
1555
# the user is prompted
1556
self.assertEquals(entered_password,
1557
conf.get_password('ssh', 'bar.org', user='jim'))
1558
self.assertContainsRe(
1559
self._get_log(keep_log_file=True),
1560
'password ignored in section \[ssh with password\]')
1562
def test_ssh_without_password_doesnt_emit_warning(self):
1563
conf = config.AuthenticationConfig(_file=StringIO(
1570
entered_password = 'typed-by-hand'
1571
stdout = tests.StringIOWrapper()
1572
ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1575
# Since the password defined in the authentication config is ignored,
1576
# the user is prompted
1577
self.assertEquals(entered_password,
1578
conf.get_password('ssh', 'bar.org', user='jim'))
1579
# No warning shoud be emitted since there is no password. We are only
1581
self.assertNotContainsRe(
1582
self._get_log(keep_log_file=True),
1583
'password ignored in section \[ssh with password\]')
1585
def test_uses_fallback_stores(self):
1586
self._old_cs_registry = config.credential_store_registry
1588
config.credential_store_registry = self._old_cs_registry
1589
self.addCleanup(restore)
1590
config.credential_store_registry = config.CredentialStoreRegistry()
1591
store = StubCredentialStore()
1592
store.add_credentials("http", "example.com", "joe", "secret")
1593
config.credential_store_registry.register("stub", store, fallback=True)
1594
conf = config.AuthenticationConfig(_file=StringIO())
1595
creds = conf.get_credentials("http", "example.com")
1596
self.assertEquals("joe", creds["user"])
1597
self.assertEquals("secret", creds["password"])
1600
class StubCredentialStore(config.CredentialStore):
1606
def add_credentials(self, scheme, host, user, password=None):
1607
self._username[(scheme, host)] = user
1608
self._password[(scheme, host)] = password
1610
def get_credentials(self, scheme, host, port=None, user=None,
1611
path=None, realm=None):
1612
key = (scheme, host)
1613
if not key in self._username:
1615
return { "scheme": scheme, "host": host, "port": port,
1616
"user": self._username[key], "password": self._password[key]}
1619
class CountingCredentialStore(config.CredentialStore):
1624
def get_credentials(self, scheme, host, port=None, user=None,
1625
path=None, realm=None):
1630
class TestCredentialStoreRegistry(tests.TestCase):
1632
def _get_cs_registry(self):
1633
return config.credential_store_registry
1635
def test_default_credential_store(self):
1636
r = self._get_cs_registry()
1637
default = r.get_credential_store(None)
1638
self.assertIsInstance(default, config.PlainTextCredentialStore)
1640
def test_unknown_credential_store(self):
1641
r = self._get_cs_registry()
1642
# It's hard to imagine someone creating a credential store named
1643
# 'unknown' so we use that as an never registered key.
1644
self.assertRaises(KeyError, r.get_credential_store, 'unknown')
1646
def test_fallback_none_registered(self):
1647
r = config.CredentialStoreRegistry()
1648
self.assertEquals(None,
1649
r.get_fallback_credentials("http", "example.com"))
1651
def test_register(self):
1652
r = config.CredentialStoreRegistry()
1653
r.register("stub", StubCredentialStore(), fallback=False)
1654
r.register("another", StubCredentialStore(), fallback=True)
1655
self.assertEquals(["another", "stub"], r.keys())
1657
def test_register_lazy(self):
1658
r = config.CredentialStoreRegistry()
1659
r.register_lazy("stub", "bzrlib.tests.test_config",
1660
"StubCredentialStore", fallback=False)
1661
self.assertEquals(["stub"], r.keys())
1662
self.assertIsInstance(r.get_credential_store("stub"),
1663
StubCredentialStore)
1665
def test_is_fallback(self):
1666
r = config.CredentialStoreRegistry()
1667
r.register("stub1", None, fallback=False)
1668
r.register("stub2", None, fallback=True)
1669
self.assertEquals(False, r.is_fallback("stub1"))
1670
self.assertEquals(True, r.is_fallback("stub2"))
1672
def test_no_fallback(self):
1673
r = config.CredentialStoreRegistry()
1674
store = CountingCredentialStore()
1675
r.register("count", store, fallback=False)
1676
self.assertEquals(None,
1677
r.get_fallback_credentials("http", "example.com"))
1678
self.assertEquals(0, store._calls)
1680
def test_fallback_credentials(self):
1681
r = config.CredentialStoreRegistry()
1682
store = StubCredentialStore()
1683
store.add_credentials("http", "example.com",
1684
"somebody", "geheim")
1685
r.register("stub", store, fallback=True)
1686
creds = r.get_fallback_credentials("http", "example.com")
1687
self.assertEquals("somebody", creds["user"])
1688
self.assertEquals("geheim", creds["password"])
1690
def test_fallback_first_wins(self):
1691
r = config.CredentialStoreRegistry()
1692
stub1 = StubCredentialStore()
1693
stub1.add_credentials("http", "example.com",
1694
"somebody", "stub1")
1695
r.register("stub1", stub1, fallback=True)
1696
stub2 = StubCredentialStore()
1697
stub2.add_credentials("http", "example.com",
1698
"somebody", "stub2")
1699
r.register("stub2", stub1, fallback=True)
1700
creds = r.get_fallback_credentials("http", "example.com")
1701
self.assertEquals("somebody", creds["user"])
1702
self.assertEquals("stub1", creds["password"])
1705
class TestPlainTextCredentialStore(tests.TestCase):
1707
def test_decode_password(self):
1708
r = config.credential_store_registry
1709
plain_text = r.get_credential_store()
1710
decoded = plain_text.decode_password(dict(password='secret'))
1711
self.assertEquals('secret', decoded)
1714
# FIXME: Once we have a way to declare authentication to all test servers, we
1715
# can implement generic tests.
1716
# test_user_password_in_url
1717
# test_user_in_url_password_from_config
1718
# test_user_in_url_password_prompted
1719
# test_user_in_config
1720
# test_user_getpass.getuser
1721
# test_user_prompted ?
1722
class TestAuthenticationRing(tests.TestCaseWithTransport):