14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
18
"""Tests for finding and reading the bzr config file[s]."""
19
19
# import system imports here
428
425
locations = config.locations_config_filename()
429
426
config.ensure_config_dir_exists()
430
427
local_url = urlutils.local_path_to_url('branch')
431
open(locations, 'wb').write('[%s]\nnickname = foobar'
428
open(locations, 'wb').write('[%s]\nnickname = foobar'
433
430
self.assertEqual('foobar', branch.nick)
440
437
locations = config.locations_config_filename()
441
438
config.ensure_config_dir_exists()
442
open(locations, 'wb').write('[%s/branch]\nnickname = barry'
439
open(locations, 'wb').write('[%s/branch]\nnickname = barry'
443
440
% (osutils.getcwd().encode('utf8'),))
444
441
self.assertEqual('barry', branch.nick)
1267
1264
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1269
def test_unknown_password_encoding(self):
1270
conf = config.AuthenticationConfig(_file=StringIO(
1274
password_encoding=unknown
1276
self.assertRaises(ValueError, conf.get_password,
1277
'ftp', 'foo.net', 'joe')
1279
1266
def test_credentials_for_scheme_host(self):
1280
1267
conf = config.AuthenticationConfig(_file=StringIO(
1281
1268
"""# Identity on foo.net
1430
1417
def test_set_credentials(self):
1431
1418
conf = config.AuthenticationConfig()
1432
1419
conf.set_credentials('name', 'host', 'user', 'scheme', 'password',
1433
99, path='/foo', verify_certificates=False, realm='realm')
1420
99, path='/foo', verify_certificates=False)
1434
1421
credentials = conf.get_credentials(host='host', scheme='scheme',
1435
port=99, path='/foo',
1422
port=99, path='/foo')
1437
1423
CREDENTIALS = {'name': 'name', 'user': 'user', 'password': 'password',
1438
'verify_certificates': False, 'scheme': 'scheme',
1439
'host': 'host', 'port': 99, 'path': '/foo',
1424
'verify_certificates': False,}
1441
1425
self.assertEqual(CREDENTIALS, credentials)
1442
1426
credentials_from_disk = config.AuthenticationConfig().get_credentials(
1443
host='host', scheme='scheme', port=99, path='/foo', realm='realm')
1427
host='host', scheme='scheme', port=99, path='/foo')
1444
1428
self.assertEqual(CREDENTIALS, credentials_from_disk)
1446
1430
def test_reset_credentials_different_name(self):
1450
1434
self.assertIs(None, conf._get_config().get('name'))
1451
1435
credentials = conf.get_credentials(host='host', scheme='scheme')
1452
1436
CREDENTIALS = {'name': 'name2', 'user': 'user2', 'password':
1453
'password', 'verify_certificates': True,
1454
'scheme': 'scheme', 'host': 'host', 'port': None,
1455
'path': None, 'realm': None}
1437
'password', 'verify_certificates': True}
1456
1438
self.assertEqual(CREDENTIALS, credentials)
1544
1526
'password ignored in section \[ssh with password\]')
1547
class TestCredentialStoreRegistry(tests.TestCase):
1549
def _get_cs_registry(self):
1550
return config.credential_store_registry
1552
def test_default_credential_store(self):
1553
r = self._get_cs_registry()
1554
default = r.get_credential_store(None)
1555
self.assertIsInstance(default, config.PlainTextCredentialStore)
1557
def test_unknown_credential_store(self):
1558
r = self._get_cs_registry()
1559
# It's hard to imagine someone creating a credential store named
1560
# 'unknown' so we use that as an never registered key.
1561
self.assertRaises(KeyError, r.get_credential_store, 'unknown')
1564
class TestPlainTextCredentialStore(tests.TestCase):
1566
def test_decode_password(self):
1567
r = config.credential_store_registry
1568
plain_text = r.get_credential_store()
1569
decoded = plain_text.decode_password(dict(password='secret'))
1570
self.assertEquals('secret', decoded)
1573
1529
# FIXME: Once we have a way to declare authentication to all test servers, we
1574
1530
# can implement generic tests.
1575
1531
# test_user_password_in_url