~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-03-16 14:01:20 UTC
  • mfrom: (3280.2.5 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20080316140120-i3yq8yr1l66m11h7
Start 1.4 development

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2008 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
2
2
#   Authors: Robert Collins <robert.collins@canonical.com>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
33
33
    urlutils,
34
34
    tests,
35
35
    trace,
36
 
    transport,
37
36
    )
38
37
from bzrlib.util.configobj import configobj
39
38
 
147
146
            self.base = "http://example.com/branches/demo"
148
147
        else:
149
148
            self.base = base
150
 
        self._transport = self.control_files = \
151
 
            FakeControlFilesAndTransport(user_id=user_id)
 
149
        self.control_files = FakeControlFiles(user_id=user_id)
152
150
 
153
151
    def lock_write(self):
154
152
        pass
157
155
        pass
158
156
 
159
157
 
160
 
class FakeControlFilesAndTransport(object):
 
158
class FakeControlFiles(object):
161
159
 
162
160
    def __init__(self, user_id=None):
 
161
        self.email = user_id
163
162
        self.files = {}
164
 
        if user_id:
165
 
            self.files['email'] = user_id
166
 
        self._transport = self
167
163
 
168
164
    def get_utf8(self, filename):
169
 
        # from LockableFiles
170
 
        raise AssertionError("get_utf8 should no longer be used")
 
165
        if filename != 'email':
 
166
            raise NotImplementedError
 
167
        if self.email is not None:
 
168
            return StringIO(self.email)
 
169
        raise errors.NoSuchFile(filename)
171
170
 
172
171
    def get(self, filename):
173
 
        # from Transport
174
172
        try:
175
173
            return StringIO(self.files[filename])
176
174
        except KeyError:
177
175
            raise errors.NoSuchFile(filename)
178
176
 
179
 
    def get_bytes(self, filename):
180
 
        # from Transport
181
 
        try:
182
 
            return self.files[filename]
183
 
        except KeyError:
184
 
            raise errors.NoSuchFile(filename)
185
 
 
186
177
    def put(self, filename, fileobj):
187
178
        self.files[filename] = fileobj.read()
188
179
 
189
 
    def put_file(self, filename, fileobj):
190
 
        return self.put(filename, fileobj)
191
 
 
192
180
 
193
181
class InstrumentedConfig(config.Config):
194
182
    """An instrumented config that supplies stubs for template methods."""
588
576
        my_config = self._get_sample_config()
589
577
        self.assertEqual('help', my_config.get_alias('h'))
590
578
 
591
 
    def test_get_aliases(self):
592
 
        my_config = self._get_sample_config()
593
 
        aliases = my_config.get_aliases()
594
 
        self.assertEqual(2, len(aliases))
595
 
        sorted_keys = sorted(aliases)
596
 
        self.assertEqual('help', aliases[sorted_keys[0]])
597
 
        self.assertEqual(sample_long_alias, aliases[sorted_keys[1]])
598
 
 
599
579
    def test_get_no_alias(self):
600
580
        my_config = self._get_sample_config()
601
581
        self.assertEqual(None, my_config.get_alias('foo'))
605
585
        self.assertEqual(sample_long_alias, my_config.get_alias('ll'))
606
586
 
607
587
 
608
 
class TestGlobalConfigSavingOptions(tests.TestCaseInTempDir):
609
 
 
610
 
    def test_empty(self):
611
 
        my_config = config.GlobalConfig()
612
 
        self.assertEqual(0, len(my_config.get_aliases()))
613
 
 
614
 
    def test_set_alias(self):
615
 
        my_config = config.GlobalConfig()
616
 
        alias_value = 'commit --strict'
617
 
        my_config.set_alias('commit', alias_value)
618
 
        new_config = config.GlobalConfig()
619
 
        self.assertEqual(alias_value, new_config.get_alias('commit'))
620
 
 
621
 
    def test_remove_alias(self):
622
 
        my_config = config.GlobalConfig()
623
 
        my_config.set_alias('commit', 'commit --strict')
624
 
        # Now remove the alias again.
625
 
        my_config.unset_alias('commit')
626
 
        new_config = config.GlobalConfig()
627
 
        self.assertIs(None, new_config.get_alias('commit'))
628
 
 
629
 
 
630
588
class TestLocationConfig(tests.TestCaseInTempDir):
631
589
 
632
590
    def test_constructs(self):
951
909
        self.assertIs(self.my_config.get_user_option('foo'), None)
952
910
        self.my_config.set_user_option('foo', 'bar')
953
911
        self.assertEqual(
954
 
            self.my_config.branch.control_files.files['branch.conf'].strip(),
955
 
            'foo = bar')
 
912
            self.my_config.branch.control_files.files['branch.conf'],
 
913
            'foo = bar\n')
956
914
        self.assertEqual(self.my_config.get_user_option('foo'), 'bar')
957
915
        self.my_config.set_user_option('foo', 'baz',
958
916
                                       store=config.STORE_LOCATION)
1002
960
        my_config = config.BranchConfig(branch)
1003
961
        self.assertEqual("Robert Collins <robertc@example.net>",
1004
962
                         my_config.username())
1005
 
        my_config.branch.control_files.files['email'] = "John"
 
963
        branch.control_files.email = "John"
1006
964
        my_config.set_user_option('email',
1007
965
                                  "Robert Collins <robertc@example.org>")
1008
966
        self.assertEqual("John", my_config.username())
1009
 
        del my_config.branch.control_files.files['email']
 
967
        branch.control_files.email = None
1010
968
        self.assertEqual("Robert Collins <robertc@example.org>",
1011
969
                         my_config.username())
1012
970
 
1013
971
    def test_not_set_in_branch(self):
1014
972
        my_config = self.get_branch_config(sample_config_text)
 
973
        my_config.branch.control_files.email = None
1015
974
        self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
1016
975
                         my_config._get_user_id())
1017
 
        my_config.branch.control_files.files['email'] = "John"
 
976
        my_config.branch.control_files.email = "John"
1018
977
        self.assertEqual("John", my_config._get_user_id())
1019
978
 
1020
979
    def test_BZR_EMAIL_OVERRIDES(self):
1177
1136
        self.assertEqual(value, 'value3-section')
1178
1137
 
1179
1138
 
1180
 
class TestTransportConfig(tests.TestCaseWithTransport):
1181
 
 
1182
 
    def test_get_value(self):
1183
 
        """Test that retreiving a value from a section is possible"""
1184
 
        bzrdir_config = config.TransportConfig(transport.get_transport('.'),
1185
 
                                               'control.conf')
1186
 
        bzrdir_config.set_option('value', 'key', 'SECTION')
1187
 
        bzrdir_config.set_option('value2', 'key2')
1188
 
        bzrdir_config.set_option('value3-top', 'key3')
1189
 
        bzrdir_config.set_option('value3-section', 'key3', 'SECTION')
1190
 
        value = bzrdir_config.get_option('key', 'SECTION')
1191
 
        self.assertEqual(value, 'value')
1192
 
        value = bzrdir_config.get_option('key2')
1193
 
        self.assertEqual(value, 'value2')
1194
 
        self.assertEqual(bzrdir_config.get_option('non-existant'), None)
1195
 
        value = bzrdir_config.get_option('non-existant', 'SECTION')
1196
 
        self.assertEqual(value, None)
1197
 
        value = bzrdir_config.get_option('non-existant', default='default')
1198
 
        self.assertEqual(value, 'default')
1199
 
        self.assertEqual(bzrdir_config.get_option('key2', 'NOSECTION'), None)
1200
 
        value = bzrdir_config.get_option('key2', 'NOSECTION',
1201
 
                                         default='default')
1202
 
        self.assertEqual(value, 'default')
1203
 
        value = bzrdir_config.get_option('key3')
1204
 
        self.assertEqual(value, 'value3-top')
1205
 
        value = bzrdir_config.get_option('key3', 'SECTION')
1206
 
        self.assertEqual(value, 'value3-section')
1207
 
 
1208
 
    def test_set_unset_default_stack_on(self):
1209
 
        my_dir = self.make_bzrdir('.')
1210
 
        bzrdir_config = config.BzrDirConfig(my_dir.transport)
1211
 
        self.assertIs(None, bzrdir_config.get_default_stack_on())
1212
 
        bzrdir_config.set_default_stack_on('Foo')
1213
 
        self.assertEqual('Foo', bzrdir_config._config.get_option(
1214
 
                         'default_stack_on'))
1215
 
        self.assertEqual('Foo', bzrdir_config.get_default_stack_on())
1216
 
        bzrdir_config.set_default_stack_on(None)
1217
 
        self.assertIs(None, bzrdir_config.get_default_stack_on())
1218
 
 
1219
 
 
1220
1139
class TestAuthenticationConfigFile(tests.TestCase):
1221
1140
    """Test the authentication.conf file matching"""
1222
1141
 
1237
1156
        self.assertEquals({}, conf._get_config())
1238
1157
        self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1239
1158
 
1240
 
    def test_missing_auth_section_header(self):
1241
 
        conf = config.AuthenticationConfig(_file=StringIO('foo = bar'))
1242
 
        self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1243
 
 
1244
 
    def test_auth_section_header_not_closed(self):
 
1159
    def test_broken_config(self):
1245
1160
        conf = config.AuthenticationConfig(_file=StringIO('[DEF'))
1246
1161
        self.assertRaises(errors.ParseConfigError, conf._get_config)
1247
1162
 
1248
 
    def test_auth_value_not_boolean(self):
1249
1163
        conf = config.AuthenticationConfig(_file=StringIO(
1250
1164
                """[broken]
1251
1165
scheme=ftp
1253
1167
verify_certificates=askme # Error: Not a boolean
1254
1168
"""))
1255
1169
        self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1256
 
 
1257
 
    def test_auth_value_not_int(self):
1258
1170
        conf = config.AuthenticationConfig(_file=StringIO(
1259
1171
                """[broken]
1260
1172
scheme=ftp
1379
1291
        self._got_user_passwd(None, None,
1380
1292
                              conf, 'http', 'bar.org', user='georges')
1381
1293
 
1382
 
    def test_credentials_for_user_without_password(self):
1383
 
        conf = config.AuthenticationConfig(_file=StringIO(
1384
 
                """
1385
 
[without password]
1386
 
scheme=http
1387
 
host=bar.org
1388
 
user=jim
1389
 
"""))
1390
 
        # Get user but no password
1391
 
        self._got_user_passwd('jim', None,
1392
 
                              conf, 'http', 'bar.org')
1393
 
 
1394
1294
    def test_verify_certificates(self):
1395
1295
        conf = config.AuthenticationConfig(_file=StringIO(
1396
1296
                """
1412
1312
        self.assertEquals(True, credentials.get('verify_certificates'))
1413
1313
 
1414
1314
 
1415
 
class TestAuthenticationStorage(tests.TestCaseInTempDir):
1416
 
 
1417
 
    def test_set_credentials(self):
1418
 
        conf = config.AuthenticationConfig()
1419
 
        conf.set_credentials('name', 'host', 'user', 'scheme', 'password',
1420
 
        99, path='/foo', verify_certificates=False)
1421
 
        credentials = conf.get_credentials(host='host', scheme='scheme',
1422
 
                                           port=99, path='/foo')
1423
 
        CREDENTIALS = {'name': 'name', 'user': 'user', 'password': 'password',
1424
 
                       'verify_certificates': False,}
1425
 
        self.assertEqual(CREDENTIALS, credentials)
1426
 
        credentials_from_disk = config.AuthenticationConfig().get_credentials(
1427
 
            host='host', scheme='scheme', port=99, path='/foo')
1428
 
        self.assertEqual(CREDENTIALS, credentials_from_disk)
1429
 
 
1430
 
    def test_reset_credentials_different_name(self):
1431
 
        conf = config.AuthenticationConfig()
1432
 
        conf.set_credentials('name', 'host', 'user', 'scheme', 'password'),
1433
 
        conf.set_credentials('name2', 'host', 'user2', 'scheme', 'password'),
1434
 
        self.assertIs(None, conf._get_config().get('name'))
1435
 
        credentials = conf.get_credentials(host='host', scheme='scheme')
1436
 
        CREDENTIALS = {'name': 'name2', 'user': 'user2', 'password':
1437
 
                       'password', 'verify_certificates': True}
1438
 
        self.assertEqual(CREDENTIALS, credentials)
1439
 
 
1440
 
 
1441
1315
class TestAuthenticationConfig(tests.TestCase):
1442
1316
    """Test AuthenticationConfig behaviour"""
1443
1317
 
1480
1354
            'SMTP %(user)s@%(host)s:%(port)d password: ',
1481
1355
            'smtp', port=10025)
1482
1356
 
1483
 
    def test_ssh_password_emits_warning(self):
1484
 
        conf = config.AuthenticationConfig(_file=StringIO(
1485
 
                """
1486
 
[ssh with password]
1487
 
scheme=ssh
1488
 
host=bar.org
1489
 
user=jim
1490
 
password=jimpass
1491
 
"""))
1492
 
        entered_password = 'typed-by-hand'
1493
 
        stdout = tests.StringIOWrapper()
1494
 
        ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1495
 
                                            stdout=stdout)
1496
 
 
1497
 
        # Since the password defined in the authentication config is ignored,
1498
 
        # the user is prompted
1499
 
        self.assertEquals(entered_password,
1500
 
                          conf.get_password('ssh', 'bar.org', user='jim'))
1501
 
        self.assertContainsRe(
1502
 
            self._get_log(keep_log_file=True),
1503
 
            'password ignored in section \[ssh with password\]')
1504
 
 
1505
 
    def test_ssh_without_password_doesnt_emit_warning(self):
1506
 
        conf = config.AuthenticationConfig(_file=StringIO(
1507
 
                """
1508
 
[ssh with password]
1509
 
scheme=ssh
1510
 
host=bar.org
1511
 
user=jim
1512
 
"""))
1513
 
        entered_password = 'typed-by-hand'
1514
 
        stdout = tests.StringIOWrapper()
1515
 
        ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1516
 
                                            stdout=stdout)
1517
 
 
1518
 
        # Since the password defined in the authentication config is ignored,
1519
 
        # the user is prompted
1520
 
        self.assertEquals(entered_password,
1521
 
                          conf.get_password('ssh', 'bar.org', user='jim'))
1522
 
        # No warning shoud be emitted since there is no password. We are only
1523
 
        # providing "user".
1524
 
        self.assertNotContainsRe(
1525
 
            self._get_log(keep_log_file=True),
1526
 
            'password ignored in section \[ssh with password\]')
1527
 
 
1528
1357
 
1529
1358
# FIXME: Once we have a way to declare authentication to all test servers, we
1530
1359
# can implement generic tests.