535
756
self.failUnless(isinstance(global_config, config.GlobalConfig))
536
757
self.failUnless(global_config is my_config._get_global_config())
538
def test__get_section_no_match(self):
759
def test__get_matching_sections_no_match(self):
539
760
self.get_branch_config('/')
540
self.assertEqual(None, self.my_location_config._get_section())
542
def test__get_section_exact(self):
761
self.assertEqual([], self.my_location_config._get_matching_sections())
763
def test__get_matching_sections_exact(self):
543
764
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):
765
self.assertEqual([('http://www.example.com', '')],
766
self.my_location_config._get_matching_sections())
768
def test__get_matching_sections_suffix_does_not(self):
548
769
self.get_branch_config('http://www.example.com-com')
549
self.assertEqual(None, self.my_location_config._get_section())
770
self.assertEqual([], self.my_location_config._get_matching_sections())
551
def test__get_section_subdir_recursive(self):
772
def test__get_matching_sections_subdir_recursive(self):
552
773
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):
774
self.assertEqual([('http://www.example.com', 'com')],
775
self.my_location_config._get_matching_sections())
777
def test__get_matching_sections_ignoreparent(self):
778
self.get_branch_config('http://www.example.com/ignoreparent')
779
self.assertEqual([('http://www.example.com/ignoreparent', '')],
780
self.my_location_config._get_matching_sections())
782
def test__get_matching_sections_ignoreparent_subdir(self):
562
783
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())
784
'http://www.example.com/ignoreparent/childbranch')
785
self.assertEqual([('http://www.example.com/ignoreparent',
787
self.my_location_config._get_matching_sections())
567
def test__get_section_subdir_trailing_slash(self):
789
def test__get_matching_sections_subdir_trailing_slash(self):
568
790
self.get_branch_config('/b')
569
self.assertEqual('/b/', self.my_location_config._get_section())
791
self.assertEqual([('/b/', '')],
792
self.my_location_config._get_matching_sections())
571
def test__get_section_subdir_child(self):
794
def test__get_matching_sections_subdir_child(self):
572
795
self.get_branch_config('/a/foo')
573
self.assertEqual('/a/*', self.my_location_config._get_section())
796
self.assertEqual([('/a/*', ''), ('/a/', 'foo')],
797
self.my_location_config._get_matching_sections())
575
def test__get_section_subdir_child_child(self):
799
def test__get_matching_sections_subdir_child_child(self):
576
800
self.get_branch_config('/a/foo/bar')
577
self.assertEqual('/a/', self.my_location_config._get_section())
801
self.assertEqual([('/a/*', 'bar'), ('/a/', 'foo/bar')],
802
self.my_location_config._get_matching_sections())
579
def test__get_section_trailing_slash_with_children(self):
804
def test__get_matching_sections_trailing_slash_with_children(self):
580
805
self.get_branch_config('/a/')
581
self.assertEqual('/a/', self.my_location_config._get_section())
806
self.assertEqual([('/a/', '')],
807
self.my_location_config._get_matching_sections())
583
def test__get_section_explicit_over_glob(self):
809
def test__get_matching_sections_explicit_over_glob(self):
810
# XXX: 2006-09-08 jamesh
811
# This test only passes because ord('c') > ord('*'). If there
812
# was a config section for '/a/?', it would get precedence
584
814
self.get_branch_config('/a/c')
585
self.assertEqual('/a/c', self.my_location_config._get_section())
815
self.assertEqual([('/a/c', ''), ('/a/*', ''), ('/a/', 'c')],
816
self.my_location_config._get_matching_sections())
818
def test__get_option_policy_normal(self):
819
self.get_branch_config('http://www.example.com')
821
self.my_location_config._get_config_policy(
822
'http://www.example.com', 'normal_option'),
825
def test__get_option_policy_norecurse(self):
826
self.get_branch_config('http://www.example.com')
828
self.my_location_config._get_option_policy(
829
'http://www.example.com', 'norecurse_option'),
830
config.POLICY_NORECURSE)
831
# Test old recurse=False setting:
833
self.my_location_config._get_option_policy(
834
'http://www.example.com/norecurse', 'normal_option'),
835
config.POLICY_NORECURSE)
837
def test__get_option_policy_normal(self):
838
self.get_branch_config('http://www.example.com')
840
self.my_location_config._get_option_policy(
841
'http://www.example.com', 'appendpath_option'),
842
config.POLICY_APPENDPATH)
588
844
def test_location_without_username(self):
589
self.get_branch_config('http://www.example.com/useglobal')
845
self.get_branch_config('http://www.example.com/ignoreparent')
590
846
self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
591
847
self.my_config.username())
799
1154
# post-commit is ignored when bresent in branch data
800
1155
self.assertEqual('bzrlib.tests.test_config.post_commit',
801
1156
my_config.post_commit())
802
my_config.set_user_option('post_commit', 'rmtree_root', local=True)
1157
my_config.set_user_option('post_commit', 'rmtree_root',
1158
store=config.STORE_LOCATION)
803
1159
self.assertEqual('rmtree_root', my_config.post_commit())
805
1161
def test_config_precedence(self):
806
1162
my_config = self.get_branch_config(global_config=precedence_global)
807
1163
self.assertEqual(my_config.get_user_option('option'), 'global')
808
my_config = self.get_branch_config(global_config=precedence_global,
1164
my_config = self.get_branch_config(global_config=precedence_global,
809
1165
branch_data_config=precedence_branch)
810
1166
self.assertEqual(my_config.get_user_option('option'), 'branch')
811
my_config = self.get_branch_config(global_config=precedence_global,
1167
my_config = self.get_branch_config(global_config=precedence_global,
812
1168
branch_data_config=precedence_branch,
813
1169
location_config=precedence_location)
814
1170
self.assertEqual(my_config.get_user_option('option'), 'recurse')
815
my_config = self.get_branch_config(global_config=precedence_global,
1171
my_config = self.get_branch_config(global_config=precedence_global,
816
1172
branch_data_config=precedence_branch,
817
1173
location_config=precedence_location,
818
1174
location='http://example.com/specific')
819
1175
self.assertEqual(my_config.get_user_option('option'), 'exact')
822
class TestMailAddressExtraction(TestCase):
1177
def test_get_mail_client(self):
1178
config = self.get_branch_config()
1179
client = config.get_mail_client()
1180
self.assertIsInstance(client, mail_client.DefaultMail)
1183
config.set_user_option('mail_client', 'evolution')
1184
client = config.get_mail_client()
1185
self.assertIsInstance(client, mail_client.Evolution)
1187
config.set_user_option('mail_client', 'kmail')
1188
client = config.get_mail_client()
1189
self.assertIsInstance(client, mail_client.KMail)
1191
config.set_user_option('mail_client', 'mutt')
1192
client = config.get_mail_client()
1193
self.assertIsInstance(client, mail_client.Mutt)
1195
config.set_user_option('mail_client', 'thunderbird')
1196
client = config.get_mail_client()
1197
self.assertIsInstance(client, mail_client.Thunderbird)
1200
config.set_user_option('mail_client', 'default')
1201
client = config.get_mail_client()
1202
self.assertIsInstance(client, mail_client.DefaultMail)
1204
config.set_user_option('mail_client', 'editor')
1205
client = config.get_mail_client()
1206
self.assertIsInstance(client, mail_client.Editor)
1208
config.set_user_option('mail_client', 'mapi')
1209
client = config.get_mail_client()
1210
self.assertIsInstance(client, mail_client.MAPIClient)
1212
config.set_user_option('mail_client', 'xdg-email')
1213
client = config.get_mail_client()
1214
self.assertIsInstance(client, mail_client.XDGEmail)
1216
config.set_user_option('mail_client', 'firebird')
1217
self.assertRaises(errors.UnknownMailClient, config.get_mail_client)
1220
class TestMailAddressExtraction(tests.TestCase):
824
1222
def test_extract_email_address(self):
825
1223
self.assertEqual('jane@test.com',
826
1224
config.extract_email_address('Jane <jane@test.com>'))
827
self.assertRaises(errors.BzrError,
1225
self.assertRaises(errors.NoEmailInUsername,
828
1226
config.extract_email_address, 'Jane Tester')
1228
def test_parse_username(self):
1229
self.assertEqual(('', 'jdoe@example.com'),
1230
config.parse_username('jdoe@example.com'))
1231
self.assertEqual(('', 'jdoe@example.com'),
1232
config.parse_username('<jdoe@example.com>'))
1233
self.assertEqual(('John Doe', 'jdoe@example.com'),
1234
config.parse_username('John Doe <jdoe@example.com>'))
1235
self.assertEqual(('John Doe', ''),
1236
config.parse_username('John Doe'))
1237
self.assertEqual(('John Doe', 'jdoe@example.com'),
1238
config.parse_username('John Doe jdoe@example.com'))
1240
class TestTreeConfig(tests.TestCaseWithTransport):
1242
def test_get_value(self):
1243
"""Test that retreiving a value from a section is possible"""
1244
branch = self.make_branch('.')
1245
tree_config = config.TreeConfig(branch)
1246
tree_config.set_option('value', 'key', 'SECTION')
1247
tree_config.set_option('value2', 'key2')
1248
tree_config.set_option('value3-top', 'key3')
1249
tree_config.set_option('value3-section', 'key3', 'SECTION')
1250
value = tree_config.get_option('key', 'SECTION')
1251
self.assertEqual(value, 'value')
1252
value = tree_config.get_option('key2')
1253
self.assertEqual(value, 'value2')
1254
self.assertEqual(tree_config.get_option('non-existant'), None)
1255
value = tree_config.get_option('non-existant', 'SECTION')
1256
self.assertEqual(value, None)
1257
value = tree_config.get_option('non-existant', default='default')
1258
self.assertEqual(value, 'default')
1259
self.assertEqual(tree_config.get_option('key2', 'NOSECTION'), None)
1260
value = tree_config.get_option('key2', 'NOSECTION', default='default')
1261
self.assertEqual(value, 'default')
1262
value = tree_config.get_option('key3')
1263
self.assertEqual(value, 'value3-top')
1264
value = tree_config.get_option('key3', 'SECTION')
1265
self.assertEqual(value, 'value3-section')
1268
class TestTransportConfig(tests.TestCaseWithTransport):
1270
def test_get_value(self):
1271
"""Test that retreiving a value from a section is possible"""
1272
bzrdir_config = config.TransportConfig(transport.get_transport('.'),
1274
bzrdir_config.set_option('value', 'key', 'SECTION')
1275
bzrdir_config.set_option('value2', 'key2')
1276
bzrdir_config.set_option('value3-top', 'key3')
1277
bzrdir_config.set_option('value3-section', 'key3', 'SECTION')
1278
value = bzrdir_config.get_option('key', 'SECTION')
1279
self.assertEqual(value, 'value')
1280
value = bzrdir_config.get_option('key2')
1281
self.assertEqual(value, 'value2')
1282
self.assertEqual(bzrdir_config.get_option('non-existant'), None)
1283
value = bzrdir_config.get_option('non-existant', 'SECTION')
1284
self.assertEqual(value, None)
1285
value = bzrdir_config.get_option('non-existant', default='default')
1286
self.assertEqual(value, 'default')
1287
self.assertEqual(bzrdir_config.get_option('key2', 'NOSECTION'), None)
1288
value = bzrdir_config.get_option('key2', 'NOSECTION',
1290
self.assertEqual(value, 'default')
1291
value = bzrdir_config.get_option('key3')
1292
self.assertEqual(value, 'value3-top')
1293
value = bzrdir_config.get_option('key3', 'SECTION')
1294
self.assertEqual(value, 'value3-section')
1296
def test_set_unset_default_stack_on(self):
1297
my_dir = self.make_bzrdir('.')
1298
bzrdir_config = config.BzrDirConfig(my_dir)
1299
self.assertIs(None, bzrdir_config.get_default_stack_on())
1300
bzrdir_config.set_default_stack_on('Foo')
1301
self.assertEqual('Foo', bzrdir_config._config.get_option(
1302
'default_stack_on'))
1303
self.assertEqual('Foo', bzrdir_config.get_default_stack_on())
1304
bzrdir_config.set_default_stack_on(None)
1305
self.assertIs(None, bzrdir_config.get_default_stack_on())
1308
class TestAuthenticationConfigFile(tests.TestCase):
1309
"""Test the authentication.conf file matching"""
1311
def _got_user_passwd(self, expected_user, expected_password,
1312
config, *args, **kwargs):
1313
credentials = config.get_credentials(*args, **kwargs)
1314
if credentials is None:
1318
user = credentials['user']
1319
password = credentials['password']
1320
self.assertEquals(expected_user, user)
1321
self.assertEquals(expected_password, password)
1323
def test_empty_config(self):
1324
conf = config.AuthenticationConfig(_file=StringIO())
1325
self.assertEquals({}, conf._get_config())
1326
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1328
def test_missing_auth_section_header(self):
1329
conf = config.AuthenticationConfig(_file=StringIO('foo = bar'))
1330
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1332
def test_auth_section_header_not_closed(self):
1333
conf = config.AuthenticationConfig(_file=StringIO('[DEF'))
1334
self.assertRaises(errors.ParseConfigError, conf._get_config)
1336
def test_auth_value_not_boolean(self):
1337
conf = config.AuthenticationConfig(_file=StringIO(
1341
verify_certificates=askme # Error: Not a boolean
1343
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1345
def test_auth_value_not_int(self):
1346
conf = config.AuthenticationConfig(_file=StringIO(
1350
port=port # Error: Not an int
1352
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1354
def test_unknown_password_encoding(self):
1355
conf = config.AuthenticationConfig(_file=StringIO(
1359
password_encoding=unknown
1361
self.assertRaises(ValueError, conf.get_password,
1362
'ftp', 'foo.net', 'joe')
1364
def test_credentials_for_scheme_host(self):
1365
conf = config.AuthenticationConfig(_file=StringIO(
1366
"""# Identity on foo.net
1371
password=secret-pass
1374
self._got_user_passwd('joe', 'secret-pass', conf, 'ftp', 'foo.net')
1376
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1378
self._got_user_passwd(None, None, conf, 'ftp', 'bar.net')
1380
def test_credentials_for_host_port(self):
1381
conf = config.AuthenticationConfig(_file=StringIO(
1382
"""# Identity on foo.net
1388
password=secret-pass
1391
self._got_user_passwd('joe', 'secret-pass',
1392
conf, 'ftp', 'foo.net', port=10021)
1394
self._got_user_passwd(None, None, conf, 'ftp', 'foo.net')
1396
def test_for_matching_host(self):
1397
conf = config.AuthenticationConfig(_file=StringIO(
1398
"""# Identity on foo.net
1404
[sourceforge domain]
1411
self._got_user_passwd('georges', 'bendover',
1412
conf, 'bzr', 'foo.bzr.sf.net')
1414
self._got_user_passwd(None, None,
1415
conf, 'bzr', 'bbzr.sf.net')
1417
def test_for_matching_host_None(self):
1418
conf = config.AuthenticationConfig(_file=StringIO(
1419
"""# Identity on foo.net
1429
self._got_user_passwd('joe', 'joepass',
1430
conf, 'bzr', 'quux.net')
1431
# no host but different scheme
1432
self._got_user_passwd('georges', 'bendover',
1433
conf, 'ftp', 'quux.net')
1435
def test_credentials_for_path(self):
1436
conf = config.AuthenticationConfig(_file=StringIO(
1452
self._got_user_passwd(None, None,
1453
conf, 'http', host='bar.org', path='/dir3')
1455
self._got_user_passwd('georges', 'bendover',
1456
conf, 'http', host='bar.org', path='/dir2')
1458
self._got_user_passwd('jim', 'jimpass',
1459
conf, 'http', host='bar.org',path='/dir1/subdir')
1461
def test_credentials_for_user(self):
1462
conf = config.AuthenticationConfig(_file=StringIO(
1471
self._got_user_passwd('jim', 'jimpass',
1472
conf, 'http', 'bar.org')
1474
self._got_user_passwd('jim', 'jimpass',
1475
conf, 'http', 'bar.org', user='jim')
1476
# Don't get a different user if one is specified
1477
self._got_user_passwd(None, None,
1478
conf, 'http', 'bar.org', user='georges')
1480
def test_credentials_for_user_without_password(self):
1481
conf = config.AuthenticationConfig(_file=StringIO(
1488
# Get user but no password
1489
self._got_user_passwd('jim', None,
1490
conf, 'http', 'bar.org')
1492
def test_verify_certificates(self):
1493
conf = config.AuthenticationConfig(_file=StringIO(
1500
verify_certificates=False
1507
credentials = conf.get_credentials('https', 'bar.org')
1508
self.assertEquals(False, credentials.get('verify_certificates'))
1509
credentials = conf.get_credentials('https', 'foo.net')
1510
self.assertEquals(True, credentials.get('verify_certificates'))
1513
class TestAuthenticationStorage(tests.TestCaseInTempDir):
1515
def test_set_credentials(self):
1516
conf = config.AuthenticationConfig()
1517
conf.set_credentials('name', 'host', 'user', 'scheme', 'password',
1518
99, path='/foo', verify_certificates=False, realm='realm')
1519
credentials = conf.get_credentials(host='host', scheme='scheme',
1520
port=99, path='/foo',
1522
CREDENTIALS = {'name': 'name', 'user': 'user', 'password': 'password',
1523
'verify_certificates': False, 'scheme': 'scheme',
1524
'host': 'host', 'port': 99, 'path': '/foo',
1526
self.assertEqual(CREDENTIALS, credentials)
1527
credentials_from_disk = config.AuthenticationConfig().get_credentials(
1528
host='host', scheme='scheme', port=99, path='/foo', realm='realm')
1529
self.assertEqual(CREDENTIALS, credentials_from_disk)
1531
def test_reset_credentials_different_name(self):
1532
conf = config.AuthenticationConfig()
1533
conf.set_credentials('name', 'host', 'user', 'scheme', 'password'),
1534
conf.set_credentials('name2', 'host', 'user2', 'scheme', 'password'),
1535
self.assertIs(None, conf._get_config().get('name'))
1536
credentials = conf.get_credentials(host='host', scheme='scheme')
1537
CREDENTIALS = {'name': 'name2', 'user': 'user2', 'password':
1538
'password', 'verify_certificates': True,
1539
'scheme': 'scheme', 'host': 'host', 'port': None,
1540
'path': None, 'realm': None}
1541
self.assertEqual(CREDENTIALS, credentials)
1544
class TestAuthenticationConfig(tests.TestCase):
1545
"""Test AuthenticationConfig behaviour"""
1547
def _check_default_password_prompt(self, expected_prompt_format, scheme,
1548
host=None, port=None, realm=None,
1552
user, password = 'jim', 'precious'
1553
expected_prompt = expected_prompt_format % {
1554
'scheme': scheme, 'host': host, 'port': port,
1555
'user': user, 'realm': realm}
1557
stdout = tests.StringIOWrapper()
1558
stderr = tests.StringIOWrapper()
1559
ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
1560
stdout=stdout, stderr=stderr)
1561
# We use an empty conf so that the user is always prompted
1562
conf = config.AuthenticationConfig()
1563
self.assertEquals(password,
1564
conf.get_password(scheme, host, user, port=port,
1565
realm=realm, path=path))
1566
self.assertEquals(expected_prompt, stderr.getvalue())
1567
self.assertEquals('', stdout.getvalue())
1569
def _check_default_username_prompt(self, expected_prompt_format, scheme,
1570
host=None, port=None, realm=None,
1575
expected_prompt = expected_prompt_format % {
1576
'scheme': scheme, 'host': host, 'port': port,
1578
stdout = tests.StringIOWrapper()
1579
stderr = tests.StringIOWrapper()
1580
ui.ui_factory = tests.TestUIFactory(stdin=username+ '\n',
1581
stdout=stdout, stderr=stderr)
1582
# We use an empty conf so that the user is always prompted
1583
conf = config.AuthenticationConfig()
1584
self.assertEquals(username, conf.get_user(scheme, host, port=port,
1585
realm=realm, path=path, ask=True))
1586
self.assertEquals(expected_prompt, stderr.getvalue())
1587
self.assertEquals('', stdout.getvalue())
1589
def test_username_defaults_prompts(self):
1590
# HTTP prompts can't be tested here, see test_http.py
1591
self._check_default_username_prompt('FTP %(host)s username: ', 'ftp')
1592
self._check_default_username_prompt(
1593
'FTP %(host)s:%(port)d username: ', 'ftp', port=10020)
1594
self._check_default_username_prompt(
1595
'SSH %(host)s:%(port)d username: ', 'ssh', port=12345)
1597
def test_username_default_no_prompt(self):
1598
conf = config.AuthenticationConfig()
1599
self.assertEquals(None,
1600
conf.get_user('ftp', 'example.com'))
1601
self.assertEquals("explicitdefault",
1602
conf.get_user('ftp', 'example.com', default="explicitdefault"))
1604
def test_password_default_prompts(self):
1605
# HTTP prompts can't be tested here, see test_http.py
1606
self._check_default_password_prompt(
1607
'FTP %(user)s@%(host)s password: ', 'ftp')
1608
self._check_default_password_prompt(
1609
'FTP %(user)s@%(host)s:%(port)d password: ', 'ftp', port=10020)
1610
self._check_default_password_prompt(
1611
'SSH %(user)s@%(host)s:%(port)d password: ', 'ssh', port=12345)
1612
# SMTP port handling is a bit special (it's handled if embedded in the
1614
# FIXME: should we: forbid that, extend it to other schemes, leave
1615
# things as they are that's fine thank you ?
1616
self._check_default_password_prompt('SMTP %(user)s@%(host)s password: ',
1618
self._check_default_password_prompt('SMTP %(user)s@%(host)s password: ',
1619
'smtp', host='bar.org:10025')
1620
self._check_default_password_prompt(
1621
'SMTP %(user)s@%(host)s:%(port)d password: ',
1624
def test_ssh_password_emits_warning(self):
1625
conf = config.AuthenticationConfig(_file=StringIO(
1633
entered_password = 'typed-by-hand'
1634
stdout = tests.StringIOWrapper()
1635
stderr = tests.StringIOWrapper()
1636
ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1637
stdout=stdout, stderr=stderr)
1639
# Since the password defined in the authentication config is ignored,
1640
# the user is prompted
1641
self.assertEquals(entered_password,
1642
conf.get_password('ssh', 'bar.org', user='jim'))
1643
self.assertContainsRe(
1645
'password ignored in section \[ssh with password\]')
1647
def test_ssh_without_password_doesnt_emit_warning(self):
1648
conf = config.AuthenticationConfig(_file=StringIO(
1655
entered_password = 'typed-by-hand'
1656
stdout = tests.StringIOWrapper()
1657
stderr = tests.StringIOWrapper()
1658
ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1662
# Since the password defined in the authentication config is ignored,
1663
# the user is prompted
1664
self.assertEquals(entered_password,
1665
conf.get_password('ssh', 'bar.org', user='jim'))
1666
# No warning shoud be emitted since there is no password. We are only
1668
self.assertNotContainsRe(
1670
'password ignored in section \[ssh with password\]')
1672
def test_uses_fallback_stores(self):
1673
self.overrideAttr(config, 'credential_store_registry',
1674
config.CredentialStoreRegistry())
1675
store = StubCredentialStore()
1676
store.add_credentials("http", "example.com", "joe", "secret")
1677
config.credential_store_registry.register("stub", store, fallback=True)
1678
conf = config.AuthenticationConfig(_file=StringIO())
1679
creds = conf.get_credentials("http", "example.com")
1680
self.assertEquals("joe", creds["user"])
1681
self.assertEquals("secret", creds["password"])
1684
class StubCredentialStore(config.CredentialStore):
1690
def add_credentials(self, scheme, host, user, password=None):
1691
self._username[(scheme, host)] = user
1692
self._password[(scheme, host)] = password
1694
def get_credentials(self, scheme, host, port=None, user=None,
1695
path=None, realm=None):
1696
key = (scheme, host)
1697
if not key in self._username:
1699
return { "scheme": scheme, "host": host, "port": port,
1700
"user": self._username[key], "password": self._password[key]}
1703
class CountingCredentialStore(config.CredentialStore):
1708
def get_credentials(self, scheme, host, port=None, user=None,
1709
path=None, realm=None):
1714
class TestCredentialStoreRegistry(tests.TestCase):
1716
def _get_cs_registry(self):
1717
return config.credential_store_registry
1719
def test_default_credential_store(self):
1720
r = self._get_cs_registry()
1721
default = r.get_credential_store(None)
1722
self.assertIsInstance(default, config.PlainTextCredentialStore)
1724
def test_unknown_credential_store(self):
1725
r = self._get_cs_registry()
1726
# It's hard to imagine someone creating a credential store named
1727
# 'unknown' so we use that as an never registered key.
1728
self.assertRaises(KeyError, r.get_credential_store, 'unknown')
1730
def test_fallback_none_registered(self):
1731
r = config.CredentialStoreRegistry()
1732
self.assertEquals(None,
1733
r.get_fallback_credentials("http", "example.com"))
1735
def test_register(self):
1736
r = config.CredentialStoreRegistry()
1737
r.register("stub", StubCredentialStore(), fallback=False)
1738
r.register("another", StubCredentialStore(), fallback=True)
1739
self.assertEquals(["another", "stub"], r.keys())
1741
def test_register_lazy(self):
1742
r = config.CredentialStoreRegistry()
1743
r.register_lazy("stub", "bzrlib.tests.test_config",
1744
"StubCredentialStore", fallback=False)
1745
self.assertEquals(["stub"], r.keys())
1746
self.assertIsInstance(r.get_credential_store("stub"),
1747
StubCredentialStore)
1749
def test_is_fallback(self):
1750
r = config.CredentialStoreRegistry()
1751
r.register("stub1", None, fallback=False)
1752
r.register("stub2", None, fallback=True)
1753
self.assertEquals(False, r.is_fallback("stub1"))
1754
self.assertEquals(True, r.is_fallback("stub2"))
1756
def test_no_fallback(self):
1757
r = config.CredentialStoreRegistry()
1758
store = CountingCredentialStore()
1759
r.register("count", store, fallback=False)
1760
self.assertEquals(None,
1761
r.get_fallback_credentials("http", "example.com"))
1762
self.assertEquals(0, store._calls)
1764
def test_fallback_credentials(self):
1765
r = config.CredentialStoreRegistry()
1766
store = StubCredentialStore()
1767
store.add_credentials("http", "example.com",
1768
"somebody", "geheim")
1769
r.register("stub", store, fallback=True)
1770
creds = r.get_fallback_credentials("http", "example.com")
1771
self.assertEquals("somebody", creds["user"])
1772
self.assertEquals("geheim", creds["password"])
1774
def test_fallback_first_wins(self):
1775
r = config.CredentialStoreRegistry()
1776
stub1 = StubCredentialStore()
1777
stub1.add_credentials("http", "example.com",
1778
"somebody", "stub1")
1779
r.register("stub1", stub1, fallback=True)
1780
stub2 = StubCredentialStore()
1781
stub2.add_credentials("http", "example.com",
1782
"somebody", "stub2")
1783
r.register("stub2", stub1, fallback=True)
1784
creds = r.get_fallback_credentials("http", "example.com")
1785
self.assertEquals("somebody", creds["user"])
1786
self.assertEquals("stub1", creds["password"])
1789
class TestPlainTextCredentialStore(tests.TestCase):
1791
def test_decode_password(self):
1792
r = config.credential_store_registry
1793
plain_text = r.get_credential_store()
1794
decoded = plain_text.decode_password(dict(password='secret'))
1795
self.assertEquals('secret', decoded)
1798
# FIXME: Once we have a way to declare authentication to all test servers, we
1799
# can implement generic tests.
1800
# test_user_password_in_url
1801
# test_user_in_url_password_from_config
1802
# test_user_in_url_password_prompted
1803
# test_user_in_config
1804
# test_user_getpass.getuser
1805
# test_user_prompted ?
1806
class TestAuthenticationRing(tests.TestCaseWithTransport):