535
689
self.failUnless(isinstance(global_config, config.GlobalConfig))
536
690
self.failUnless(global_config is my_config._get_global_config())
538
def test__get_section_no_match(self):
692
def test__get_matching_sections_no_match(self):
539
693
self.get_branch_config('/')
540
self.assertEqual(None, self.my_location_config._get_section())
542
def test__get_section_exact(self):
694
self.assertEqual([], self.my_location_config._get_matching_sections())
696
def test__get_matching_sections_exact(self):
543
697
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):
698
self.assertEqual([('http://www.example.com', '')],
699
self.my_location_config._get_matching_sections())
701
def test__get_matching_sections_suffix_does_not(self):
548
702
self.get_branch_config('http://www.example.com-com')
549
self.assertEqual(None, self.my_location_config._get_section())
703
self.assertEqual([], self.my_location_config._get_matching_sections())
551
def test__get_section_subdir_recursive(self):
705
def test__get_matching_sections_subdir_recursive(self):
552
706
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):
707
self.assertEqual([('http://www.example.com', 'com')],
708
self.my_location_config._get_matching_sections())
710
def test__get_matching_sections_ignoreparent(self):
711
self.get_branch_config('http://www.example.com/ignoreparent')
712
self.assertEqual([('http://www.example.com/ignoreparent', '')],
713
self.my_location_config._get_matching_sections())
715
def test__get_matching_sections_ignoreparent_subdir(self):
562
716
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())
717
'http://www.example.com/ignoreparent/childbranch')
718
self.assertEqual([('http://www.example.com/ignoreparent',
720
self.my_location_config._get_matching_sections())
567
def test__get_section_subdir_trailing_slash(self):
722
def test__get_matching_sections_subdir_trailing_slash(self):
568
723
self.get_branch_config('/b')
569
self.assertEqual('/b/', self.my_location_config._get_section())
724
self.assertEqual([('/b/', '')],
725
self.my_location_config._get_matching_sections())
571
def test__get_section_subdir_child(self):
727
def test__get_matching_sections_subdir_child(self):
572
728
self.get_branch_config('/a/foo')
573
self.assertEqual('/a/*', self.my_location_config._get_section())
729
self.assertEqual([('/a/*', ''), ('/a/', 'foo')],
730
self.my_location_config._get_matching_sections())
575
def test__get_section_subdir_child_child(self):
732
def test__get_matching_sections_subdir_child_child(self):
576
733
self.get_branch_config('/a/foo/bar')
577
self.assertEqual('/a/', self.my_location_config._get_section())
734
self.assertEqual([('/a/*', 'bar'), ('/a/', 'foo/bar')],
735
self.my_location_config._get_matching_sections())
579
def test__get_section_trailing_slash_with_children(self):
737
def test__get_matching_sections_trailing_slash_with_children(self):
580
738
self.get_branch_config('/a/')
581
self.assertEqual('/a/', self.my_location_config._get_section())
739
self.assertEqual([('/a/', '')],
740
self.my_location_config._get_matching_sections())
583
def test__get_section_explicit_over_glob(self):
742
def test__get_matching_sections_explicit_over_glob(self):
743
# XXX: 2006-09-08 jamesh
744
# This test only passes because ord('c') > ord('*'). If there
745
# was a config section for '/a/?', it would get precedence
584
747
self.get_branch_config('/a/c')
585
self.assertEqual('/a/c', self.my_location_config._get_section())
748
self.assertEqual([('/a/c', ''), ('/a/*', ''), ('/a/', 'c')],
749
self.my_location_config._get_matching_sections())
751
def test__get_option_policy_normal(self):
752
self.get_branch_config('http://www.example.com')
754
self.my_location_config._get_config_policy(
755
'http://www.example.com', 'normal_option'),
758
def test__get_option_policy_norecurse(self):
759
self.get_branch_config('http://www.example.com')
761
self.my_location_config._get_option_policy(
762
'http://www.example.com', 'norecurse_option'),
763
config.POLICY_NORECURSE)
764
# Test old recurse=False setting:
766
self.my_location_config._get_option_policy(
767
'http://www.example.com/norecurse', 'normal_option'),
768
config.POLICY_NORECURSE)
770
def test__get_option_policy_normal(self):
771
self.get_branch_config('http://www.example.com')
773
self.my_location_config._get_option_policy(
774
'http://www.example.com', 'appendpath_option'),
775
config.POLICY_APPENDPATH)
588
777
def test_location_without_username(self):
589
self.get_branch_config('http://www.example.com/useglobal')
778
self.get_branch_config('http://www.example.com/ignoreparent')
590
779
self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
591
780
self.my_config.username())
799
1087
# post-commit is ignored when bresent in branch data
800
1088
self.assertEqual('bzrlib.tests.test_config.post_commit',
801
1089
my_config.post_commit())
802
my_config.set_user_option('post_commit', 'rmtree_root', local=True)
1090
my_config.set_user_option('post_commit', 'rmtree_root',
1091
store=config.STORE_LOCATION)
803
1092
self.assertEqual('rmtree_root', my_config.post_commit())
805
1094
def test_config_precedence(self):
806
1095
my_config = self.get_branch_config(global_config=precedence_global)
807
1096
self.assertEqual(my_config.get_user_option('option'), 'global')
808
my_config = self.get_branch_config(global_config=precedence_global,
1097
my_config = self.get_branch_config(global_config=precedence_global,
809
1098
branch_data_config=precedence_branch)
810
1099
self.assertEqual(my_config.get_user_option('option'), 'branch')
811
my_config = self.get_branch_config(global_config=precedence_global,
1100
my_config = self.get_branch_config(global_config=precedence_global,
812
1101
branch_data_config=precedence_branch,
813
1102
location_config=precedence_location)
814
1103
self.assertEqual(my_config.get_user_option('option'), 'recurse')
815
my_config = self.get_branch_config(global_config=precedence_global,
1104
my_config = self.get_branch_config(global_config=precedence_global,
816
1105
branch_data_config=precedence_branch,
817
1106
location_config=precedence_location,
818
1107
location='http://example.com/specific')
819
1108
self.assertEqual(my_config.get_user_option('option'), 'exact')
822
class TestMailAddressExtraction(TestCase):
1110
def test_get_mail_client(self):
1111
config = self.get_branch_config()
1112
client = config.get_mail_client()
1113
self.assertIsInstance(client, mail_client.DefaultMail)
1116
config.set_user_option('mail_client', 'evolution')
1117
client = config.get_mail_client()
1118
self.assertIsInstance(client, mail_client.Evolution)
1120
config.set_user_option('mail_client', 'kmail')
1121
client = config.get_mail_client()
1122
self.assertIsInstance(client, mail_client.KMail)
1124
config.set_user_option('mail_client', 'mutt')
1125
client = config.get_mail_client()
1126
self.assertIsInstance(client, mail_client.Mutt)
1128
config.set_user_option('mail_client', 'thunderbird')
1129
client = config.get_mail_client()
1130
self.assertIsInstance(client, mail_client.Thunderbird)
1133
config.set_user_option('mail_client', 'default')
1134
client = config.get_mail_client()
1135
self.assertIsInstance(client, mail_client.DefaultMail)
1137
config.set_user_option('mail_client', 'editor')
1138
client = config.get_mail_client()
1139
self.assertIsInstance(client, mail_client.Editor)
1141
config.set_user_option('mail_client', 'mapi')
1142
client = config.get_mail_client()
1143
self.assertIsInstance(client, mail_client.MAPIClient)
1145
config.set_user_option('mail_client', 'xdg-email')
1146
client = config.get_mail_client()
1147
self.assertIsInstance(client, mail_client.XDGEmail)
1149
config.set_user_option('mail_client', 'firebird')
1150
self.assertRaises(errors.UnknownMailClient, config.get_mail_client)
1153
class TestMailAddressExtraction(tests.TestCase):
824
1155
def test_extract_email_address(self):
825
1156
self.assertEqual('jane@test.com',
826
1157
config.extract_email_address('Jane <jane@test.com>'))
827
self.assertRaises(errors.BzrError,
1158
self.assertRaises(errors.NoEmailInUsername,
828
1159
config.extract_email_address, 'Jane Tester')
1161
def test_parse_username(self):
1162
self.assertEqual(('', 'jdoe@example.com'),
1163
config.parse_username('jdoe@example.com'))
1164
self.assertEqual(('', 'jdoe@example.com'),
1165
config.parse_username('<jdoe@example.com>'))
1166
self.assertEqual(('John Doe', 'jdoe@example.com'),
1167
config.parse_username('John Doe <jdoe@example.com>'))
1168
self.assertEqual(('John Doe', ''),
1169
config.parse_username('John Doe'))
1170
self.assertEqual(('John Doe', 'jdoe@example.com'),
1171
config.parse_username('John Doe jdoe@example.com'))
1173
class TestTreeConfig(tests.TestCaseWithTransport):
1175
def test_get_value(self):
1176
"""Test that retreiving a value from a section is possible"""
1177
branch = self.make_branch('.')
1178
tree_config = config.TreeConfig(branch)
1179
tree_config.set_option('value', 'key', 'SECTION')
1180
tree_config.set_option('value2', 'key2')
1181
tree_config.set_option('value3-top', 'key3')
1182
tree_config.set_option('value3-section', 'key3', 'SECTION')
1183
value = tree_config.get_option('key', 'SECTION')
1184
self.assertEqual(value, 'value')
1185
value = tree_config.get_option('key2')
1186
self.assertEqual(value, 'value2')
1187
self.assertEqual(tree_config.get_option('non-existant'), None)
1188
value = tree_config.get_option('non-existant', 'SECTION')
1189
self.assertEqual(value, None)
1190
value = tree_config.get_option('non-existant', default='default')
1191
self.assertEqual(value, 'default')
1192
self.assertEqual(tree_config.get_option('key2', 'NOSECTION'), None)
1193
value = tree_config.get_option('key2', 'NOSECTION', default='default')
1194
self.assertEqual(value, 'default')
1195
value = tree_config.get_option('key3')
1196
self.assertEqual(value, 'value3-top')
1197
value = tree_config.get_option('key3', 'SECTION')
1198
self.assertEqual(value, 'value3-section')
1201
class TestTransportConfig(tests.TestCaseWithTransport):
1203
def test_get_value(self):
1204
"""Test that retreiving a value from a section is possible"""
1205
bzrdir_config = config.TransportConfig(transport.get_transport('.'),
1207
bzrdir_config.set_option('value', 'key', 'SECTION')
1208
bzrdir_config.set_option('value2', 'key2')
1209
bzrdir_config.set_option('value3-top', 'key3')
1210
bzrdir_config.set_option('value3-section', 'key3', 'SECTION')
1211
value = bzrdir_config.get_option('key', 'SECTION')
1212
self.assertEqual(value, 'value')
1213
value = bzrdir_config.get_option('key2')
1214
self.assertEqual(value, 'value2')
1215
self.assertEqual(bzrdir_config.get_option('non-existant'), None)
1216
value = bzrdir_config.get_option('non-existant', 'SECTION')
1217
self.assertEqual(value, None)
1218
value = bzrdir_config.get_option('non-existant', default='default')
1219
self.assertEqual(value, 'default')
1220
self.assertEqual(bzrdir_config.get_option('key2', 'NOSECTION'), None)
1221
value = bzrdir_config.get_option('key2', 'NOSECTION',
1223
self.assertEqual(value, 'default')
1224
value = bzrdir_config.get_option('key3')
1225
self.assertEqual(value, 'value3-top')
1226
value = bzrdir_config.get_option('key3', 'SECTION')
1227
self.assertEqual(value, 'value3-section')
1229
def test_set_unset_default_stack_on(self):
1230
my_dir = self.make_bzrdir('.')
1231
bzrdir_config = config.BzrDirConfig(my_dir)
1232
self.assertIs(None, bzrdir_config.get_default_stack_on())
1233
bzrdir_config.set_default_stack_on('Foo')
1234
self.assertEqual('Foo', bzrdir_config._config.get_option(
1235
'default_stack_on'))
1236
self.assertEqual('Foo', bzrdir_config.get_default_stack_on())
1237
bzrdir_config.set_default_stack_on(None)
1238
self.assertIs(None, bzrdir_config.get_default_stack_on())
1241
class TestAuthenticationConfigFile(tests.TestCase):
1242
"""Test the authentication.conf file matching"""
1244
def _got_user_passwd(self, expected_user, expected_password,
1245
config, *args, **kwargs):
1246
credentials = config.get_credentials(*args, **kwargs)
1247
if credentials is None:
1251
user = credentials['user']
1252
password = credentials['password']
1253
self.assertEquals(expected_user, user)
1254
self.assertEquals(expected_password, password)
1256
def test_empty_config(self):
1257
conf = config.AuthenticationConfig(_file=StringIO())
1258
self.assertEquals({}, conf._get_config())
1259
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1261
def test_missing_auth_section_header(self):
1262
conf = config.AuthenticationConfig(_file=StringIO('foo = bar'))
1263
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1265
def test_auth_section_header_not_closed(self):
1266
conf = config.AuthenticationConfig(_file=StringIO('[DEF'))
1267
self.assertRaises(errors.ParseConfigError, conf._get_config)
1269
def test_auth_value_not_boolean(self):
1270
conf = config.AuthenticationConfig(_file=StringIO(
1274
verify_certificates=askme # Error: Not a boolean
1276
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1278
def test_auth_value_not_int(self):
1279
conf = config.AuthenticationConfig(_file=StringIO(
1283
port=port # Error: Not an int
1285
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1287
def test_unknown_password_encoding(self):
1288
conf = config.AuthenticationConfig(_file=StringIO(
1292
password_encoding=unknown
1294
self.assertRaises(ValueError, conf.get_password,
1295
'ftp', 'foo.net', 'joe')
1297
def test_credentials_for_scheme_host(self):
1298
conf = config.AuthenticationConfig(_file=StringIO(
1299
"""# Identity on foo.net
1304
password=secret-pass
1307
self._got_user_passwd('joe', 'secret-pass', conf, 'ftp', 'foo.net')
1309
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1311
self._got_user_passwd(None, None, conf, 'ftp', 'bar.net')
1313
def test_credentials_for_host_port(self):
1314
conf = config.AuthenticationConfig(_file=StringIO(
1315
"""# Identity on foo.net
1321
password=secret-pass
1324
self._got_user_passwd('joe', 'secret-pass',
1325
conf, 'ftp', 'foo.net', port=10021)
1327
self._got_user_passwd(None, None, conf, 'ftp', 'foo.net')
1329
def test_for_matching_host(self):
1330
conf = config.AuthenticationConfig(_file=StringIO(
1331
"""# Identity on foo.net
1337
[sourceforge domain]
1344
self._got_user_passwd('georges', 'bendover',
1345
conf, 'bzr', 'foo.bzr.sf.net')
1347
self._got_user_passwd(None, None,
1348
conf, 'bzr', 'bbzr.sf.net')
1350
def test_for_matching_host_None(self):
1351
conf = config.AuthenticationConfig(_file=StringIO(
1352
"""# Identity on foo.net
1362
self._got_user_passwd('joe', 'joepass',
1363
conf, 'bzr', 'quux.net')
1364
# no host but different scheme
1365
self._got_user_passwd('georges', 'bendover',
1366
conf, 'ftp', 'quux.net')
1368
def test_credentials_for_path(self):
1369
conf = config.AuthenticationConfig(_file=StringIO(
1385
self._got_user_passwd(None, None,
1386
conf, 'http', host='bar.org', path='/dir3')
1388
self._got_user_passwd('georges', 'bendover',
1389
conf, 'http', host='bar.org', path='/dir2')
1391
self._got_user_passwd('jim', 'jimpass',
1392
conf, 'http', host='bar.org',path='/dir1/subdir')
1394
def test_credentials_for_user(self):
1395
conf = config.AuthenticationConfig(_file=StringIO(
1404
self._got_user_passwd('jim', 'jimpass',
1405
conf, 'http', 'bar.org')
1407
self._got_user_passwd('jim', 'jimpass',
1408
conf, 'http', 'bar.org', user='jim')
1409
# Don't get a different user if one is specified
1410
self._got_user_passwd(None, None,
1411
conf, 'http', 'bar.org', user='georges')
1413
def test_credentials_for_user_without_password(self):
1414
conf = config.AuthenticationConfig(_file=StringIO(
1421
# Get user but no password
1422
self._got_user_passwd('jim', None,
1423
conf, 'http', 'bar.org')
1425
def test_verify_certificates(self):
1426
conf = config.AuthenticationConfig(_file=StringIO(
1433
verify_certificates=False
1440
credentials = conf.get_credentials('https', 'bar.org')
1441
self.assertEquals(False, credentials.get('verify_certificates'))
1442
credentials = conf.get_credentials('https', 'foo.net')
1443
self.assertEquals(True, credentials.get('verify_certificates'))
1446
class TestAuthenticationStorage(tests.TestCaseInTempDir):
1448
def test_set_credentials(self):
1449
conf = config.AuthenticationConfig()
1450
conf.set_credentials('name', 'host', 'user', 'scheme', 'password',
1451
99, path='/foo', verify_certificates=False, realm='realm')
1452
credentials = conf.get_credentials(host='host', scheme='scheme',
1453
port=99, path='/foo',
1455
CREDENTIALS = {'name': 'name', 'user': 'user', 'password': 'password',
1456
'verify_certificates': False, 'scheme': 'scheme',
1457
'host': 'host', 'port': 99, 'path': '/foo',
1459
self.assertEqual(CREDENTIALS, credentials)
1460
credentials_from_disk = config.AuthenticationConfig().get_credentials(
1461
host='host', scheme='scheme', port=99, path='/foo', realm='realm')
1462
self.assertEqual(CREDENTIALS, credentials_from_disk)
1464
def test_reset_credentials_different_name(self):
1465
conf = config.AuthenticationConfig()
1466
conf.set_credentials('name', 'host', 'user', 'scheme', 'password'),
1467
conf.set_credentials('name2', 'host', 'user2', 'scheme', 'password'),
1468
self.assertIs(None, conf._get_config().get('name'))
1469
credentials = conf.get_credentials(host='host', scheme='scheme')
1470
CREDENTIALS = {'name': 'name2', 'user': 'user2', 'password':
1471
'password', 'verify_certificates': True,
1472
'scheme': 'scheme', 'host': 'host', 'port': None,
1473
'path': None, 'realm': None}
1474
self.assertEqual(CREDENTIALS, credentials)
1477
class TestAuthenticationConfig(tests.TestCase):
1478
"""Test AuthenticationConfig behaviour"""
1480
def _check_default_password_prompt(self, expected_prompt_format, scheme,
1481
host=None, port=None, realm=None,
1485
user, password = 'jim', 'precious'
1486
expected_prompt = expected_prompt_format % {
1487
'scheme': scheme, 'host': host, 'port': port,
1488
'user': user, 'realm': realm}
1490
stdout = tests.StringIOWrapper()
1491
stderr = tests.StringIOWrapper()
1492
ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
1493
stdout=stdout, stderr=stderr)
1494
# We use an empty conf so that the user is always prompted
1495
conf = config.AuthenticationConfig()
1496
self.assertEquals(password,
1497
conf.get_password(scheme, host, user, port=port,
1498
realm=realm, path=path))
1499
self.assertEquals(expected_prompt, stderr.getvalue())
1500
self.assertEquals('', stdout.getvalue())
1502
def _check_default_username_prompt(self, expected_prompt_format, scheme,
1503
host=None, port=None, realm=None,
1508
expected_prompt = expected_prompt_format % {
1509
'scheme': scheme, 'host': host, 'port': port,
1511
stdout = tests.StringIOWrapper()
1512
stderr = tests.StringIOWrapper()
1513
ui.ui_factory = tests.TestUIFactory(stdin=username+ '\n',
1514
stdout=stdout, stderr=stderr)
1515
# We use an empty conf so that the user is always prompted
1516
conf = config.AuthenticationConfig()
1517
self.assertEquals(username, conf.get_user(scheme, host, port=port,
1518
realm=realm, path=path, ask=True))
1519
self.assertEquals(expected_prompt, stderr.getvalue())
1520
self.assertEquals('', stdout.getvalue())
1522
def test_username_defaults_prompts(self):
1523
# HTTP prompts can't be tested here, see test_http.py
1524
self._check_default_username_prompt('FTP %(host)s username: ', 'ftp')
1525
self._check_default_username_prompt(
1526
'FTP %(host)s:%(port)d username: ', 'ftp', port=10020)
1527
self._check_default_username_prompt(
1528
'SSH %(host)s:%(port)d username: ', 'ssh', port=12345)
1530
def test_username_default_no_prompt(self):
1531
conf = config.AuthenticationConfig()
1532
self.assertEquals(None,
1533
conf.get_user('ftp', 'example.com'))
1534
self.assertEquals("explicitdefault",
1535
conf.get_user('ftp', 'example.com', default="explicitdefault"))
1537
def test_password_default_prompts(self):
1538
# HTTP prompts can't be tested here, see test_http.py
1539
self._check_default_password_prompt(
1540
'FTP %(user)s@%(host)s password: ', 'ftp')
1541
self._check_default_password_prompt(
1542
'FTP %(user)s@%(host)s:%(port)d password: ', 'ftp', port=10020)
1543
self._check_default_password_prompt(
1544
'SSH %(user)s@%(host)s:%(port)d password: ', 'ssh', port=12345)
1545
# SMTP port handling is a bit special (it's handled if embedded in the
1547
# FIXME: should we: forbid that, extend it to other schemes, leave
1548
# things as they are that's fine thank you ?
1549
self._check_default_password_prompt('SMTP %(user)s@%(host)s password: ',
1551
self._check_default_password_prompt('SMTP %(user)s@%(host)s password: ',
1552
'smtp', host='bar.org:10025')
1553
self._check_default_password_prompt(
1554
'SMTP %(user)s@%(host)s:%(port)d password: ',
1557
def test_ssh_password_emits_warning(self):
1558
conf = config.AuthenticationConfig(_file=StringIO(
1566
entered_password = 'typed-by-hand'
1567
stdout = tests.StringIOWrapper()
1568
stderr = tests.StringIOWrapper()
1569
ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1570
stdout=stdout, stderr=stderr)
1572
# Since the password defined in the authentication config is ignored,
1573
# the user is prompted
1574
self.assertEquals(entered_password,
1575
conf.get_password('ssh', 'bar.org', user='jim'))
1576
self.assertContainsRe(
1577
self._get_log(keep_log_file=True),
1578
'password ignored in section \[ssh with password\]')
1580
def test_ssh_without_password_doesnt_emit_warning(self):
1581
conf = config.AuthenticationConfig(_file=StringIO(
1588
entered_password = 'typed-by-hand'
1589
stdout = tests.StringIOWrapper()
1590
stderr = tests.StringIOWrapper()
1591
ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1595
# Since the password defined in the authentication config is ignored,
1596
# the user is prompted
1597
self.assertEquals(entered_password,
1598
conf.get_password('ssh', 'bar.org', user='jim'))
1599
# No warning shoud be emitted since there is no password. We are only
1601
self.assertNotContainsRe(
1602
self._get_log(keep_log_file=True),
1603
'password ignored in section \[ssh with password\]')
1605
def test_uses_fallback_stores(self):
1606
self._old_cs_registry = config.credential_store_registry
1608
config.credential_store_registry = self._old_cs_registry
1609
self.addCleanup(restore)
1610
config.credential_store_registry = config.CredentialStoreRegistry()
1611
store = StubCredentialStore()
1612
store.add_credentials("http", "example.com", "joe", "secret")
1613
config.credential_store_registry.register("stub", store, fallback=True)
1614
conf = config.AuthenticationConfig(_file=StringIO())
1615
creds = conf.get_credentials("http", "example.com")
1616
self.assertEquals("joe", creds["user"])
1617
self.assertEquals("secret", creds["password"])
1620
class StubCredentialStore(config.CredentialStore):
1626
def add_credentials(self, scheme, host, user, password=None):
1627
self._username[(scheme, host)] = user
1628
self._password[(scheme, host)] = password
1630
def get_credentials(self, scheme, host, port=None, user=None,
1631
path=None, realm=None):
1632
key = (scheme, host)
1633
if not key in self._username:
1635
return { "scheme": scheme, "host": host, "port": port,
1636
"user": self._username[key], "password": self._password[key]}
1639
class CountingCredentialStore(config.CredentialStore):
1644
def get_credentials(self, scheme, host, port=None, user=None,
1645
path=None, realm=None):
1650
class TestCredentialStoreRegistry(tests.TestCase):
1652
def _get_cs_registry(self):
1653
return config.credential_store_registry
1655
def test_default_credential_store(self):
1656
r = self._get_cs_registry()
1657
default = r.get_credential_store(None)
1658
self.assertIsInstance(default, config.PlainTextCredentialStore)
1660
def test_unknown_credential_store(self):
1661
r = self._get_cs_registry()
1662
# It's hard to imagine someone creating a credential store named
1663
# 'unknown' so we use that as an never registered key.
1664
self.assertRaises(KeyError, r.get_credential_store, 'unknown')
1666
def test_fallback_none_registered(self):
1667
r = config.CredentialStoreRegistry()
1668
self.assertEquals(None,
1669
r.get_fallback_credentials("http", "example.com"))
1671
def test_register(self):
1672
r = config.CredentialStoreRegistry()
1673
r.register("stub", StubCredentialStore(), fallback=False)
1674
r.register("another", StubCredentialStore(), fallback=True)
1675
self.assertEquals(["another", "stub"], r.keys())
1677
def test_register_lazy(self):
1678
r = config.CredentialStoreRegistry()
1679
r.register_lazy("stub", "bzrlib.tests.test_config",
1680
"StubCredentialStore", fallback=False)
1681
self.assertEquals(["stub"], r.keys())
1682
self.assertIsInstance(r.get_credential_store("stub"),
1683
StubCredentialStore)
1685
def test_is_fallback(self):
1686
r = config.CredentialStoreRegistry()
1687
r.register("stub1", None, fallback=False)
1688
r.register("stub2", None, fallback=True)
1689
self.assertEquals(False, r.is_fallback("stub1"))
1690
self.assertEquals(True, r.is_fallback("stub2"))
1692
def test_no_fallback(self):
1693
r = config.CredentialStoreRegistry()
1694
store = CountingCredentialStore()
1695
r.register("count", store, fallback=False)
1696
self.assertEquals(None,
1697
r.get_fallback_credentials("http", "example.com"))
1698
self.assertEquals(0, store._calls)
1700
def test_fallback_credentials(self):
1701
r = config.CredentialStoreRegistry()
1702
store = StubCredentialStore()
1703
store.add_credentials("http", "example.com",
1704
"somebody", "geheim")
1705
r.register("stub", store, fallback=True)
1706
creds = r.get_fallback_credentials("http", "example.com")
1707
self.assertEquals("somebody", creds["user"])
1708
self.assertEquals("geheim", creds["password"])
1710
def test_fallback_first_wins(self):
1711
r = config.CredentialStoreRegistry()
1712
stub1 = StubCredentialStore()
1713
stub1.add_credentials("http", "example.com",
1714
"somebody", "stub1")
1715
r.register("stub1", stub1, fallback=True)
1716
stub2 = StubCredentialStore()
1717
stub2.add_credentials("http", "example.com",
1718
"somebody", "stub2")
1719
r.register("stub2", stub1, fallback=True)
1720
creds = r.get_fallback_credentials("http", "example.com")
1721
self.assertEquals("somebody", creds["user"])
1722
self.assertEquals("stub1", creds["password"])
1725
class TestPlainTextCredentialStore(tests.TestCase):
1727
def test_decode_password(self):
1728
r = config.credential_store_registry
1729
plain_text = r.get_credential_store()
1730
decoded = plain_text.decode_password(dict(password='secret'))
1731
self.assertEquals('secret', decoded)
1734
# FIXME: Once we have a way to declare authentication to all test servers, we
1735
# can implement generic tests.
1736
# test_user_password_in_url
1737
# test_user_in_url_password_from_config
1738
# test_user_in_url_password_prompted
1739
# test_user_in_config
1740
# test_user_getpass.getuser
1741
# test_user_prompted ?
1742
class TestAuthenticationRing(tests.TestCaseWithTransport):