/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

Merge bzr.dev and resolve conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
147
147
            self.base = "http://example.com/branches/demo"
148
148
        else:
149
149
            self.base = base
150
 
        self.control_files = FakeControlFiles(user_id=user_id)
 
150
        self._transport = self.control_files = \
 
151
            FakeControlFilesAndTransport(user_id=user_id)
151
152
 
152
153
    def lock_write(self):
153
154
        pass
156
157
        pass
157
158
 
158
159
 
159
 
class FakeControlFiles(object):
 
160
class FakeControlFilesAndTransport(object):
160
161
 
161
162
    def __init__(self, user_id=None):
162
 
        self.email = user_id
163
163
        self.files = {}
 
164
        if user_id:
 
165
            self.files['email'] = user_id
164
166
        self._transport = self
165
167
 
166
168
    def get_utf8(self, filename):
167
 
        if filename != 'email':
168
 
            raise NotImplementedError
169
 
        if self.email is not None:
170
 
            return StringIO(self.email)
171
 
        raise errors.NoSuchFile(filename)
 
169
        # from LockableFiles
 
170
        raise AssertionError("get_utf8 should no longer be used")
172
171
 
173
172
    def get(self, filename):
 
173
        # from Transport
174
174
        try:
175
175
            return StringIO(self.files[filename])
176
176
        except KeyError:
177
177
            raise errors.NoSuchFile(filename)
178
178
 
 
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
 
179
186
    def put(self, filename, fileobj):
180
187
        self.files[filename] = fileobj.read()
181
188
 
995
1002
        my_config = config.BranchConfig(branch)
996
1003
        self.assertEqual("Robert Collins <robertc@example.net>",
997
1004
                         my_config.username())
998
 
        branch.control_files.email = "John"
 
1005
        my_config.branch.control_files.files['email'] = "John"
999
1006
        my_config.set_user_option('email',
1000
1007
                                  "Robert Collins <robertc@example.org>")
1001
1008
        self.assertEqual("John", my_config.username())
1002
 
        branch.control_files.email = None
 
1009
        del my_config.branch.control_files.files['email']
1003
1010
        self.assertEqual("Robert Collins <robertc@example.org>",
1004
1011
                         my_config.username())
1005
1012
 
1006
1013
    def test_not_set_in_branch(self):
1007
1014
        my_config = self.get_branch_config(sample_config_text)
1008
 
        my_config.branch.control_files.email = None
1009
1015
        self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
1010
1016
                         my_config._get_user_id())
1011
 
        my_config.branch.control_files.email = "John"
 
1017
        my_config.branch.control_files.files['email'] = "John"
1012
1018
        self.assertEqual("John", my_config._get_user_id())
1013
1019
 
1014
1020
    def test_BZR_EMAIL_OVERRIDES(self):
1220
1226
        self.assertEquals({}, conf._get_config())
1221
1227
        self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1222
1228
 
1223
 
    def test_broken_config(self):
 
1229
    def test_missing_auth_section_header(self):
 
1230
        conf = config.AuthenticationConfig(_file=StringIO('foo = bar'))
 
1231
        self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
 
1232
 
 
1233
    def test_auth_section_header_not_closed(self):
1224
1234
        conf = config.AuthenticationConfig(_file=StringIO('[DEF'))
1225
1235
        self.assertRaises(errors.ParseConfigError, conf._get_config)
1226
1236
 
 
1237
    def test_auth_value_not_boolean(self):
1227
1238
        conf = config.AuthenticationConfig(_file=StringIO(
1228
1239
                """[broken]
1229
1240
scheme=ftp
1231
1242
verify_certificates=askme # Error: Not a boolean
1232
1243
"""))
1233
1244
        self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
 
1245
 
 
1246
    def test_auth_value_not_int(self):
1234
1247
        conf = config.AuthenticationConfig(_file=StringIO(
1235
1248
                """[broken]
1236
1249
scheme=ftp
1355
1368
        self._got_user_passwd(None, None,
1356
1369
                              conf, 'http', 'bar.org', user='georges')
1357
1370
 
 
1371
    def test_credentials_for_user_without_password(self):
 
1372
        conf = config.AuthenticationConfig(_file=StringIO(
 
1373
                """
 
1374
[without password]
 
1375
scheme=http
 
1376
host=bar.org
 
1377
user=jim
 
1378
"""))
 
1379
        # Get user but no password
 
1380
        self._got_user_passwd('jim', None,
 
1381
                              conf, 'http', 'bar.org')
 
1382
 
1358
1383
    def test_verify_certificates(self):
1359
1384
        conf = config.AuthenticationConfig(_file=StringIO(
1360
1385
                """
1418
1443
            'SMTP %(user)s@%(host)s:%(port)d password: ',
1419
1444
            'smtp', port=10025)
1420
1445
 
 
1446
    def test_ssh_password_emits_warning(self):
 
1447
        conf = config.AuthenticationConfig(_file=StringIO(
 
1448
                """
 
1449
[ssh with password]
 
1450
scheme=ssh
 
1451
host=bar.org
 
1452
user=jim
 
1453
password=jimpass
 
1454
"""))
 
1455
        entered_password = 'typed-by-hand'
 
1456
        stdout = tests.StringIOWrapper()
 
1457
        ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
 
1458
                                            stdout=stdout)
 
1459
 
 
1460
        # Since the password defined in the authentication config is ignored,
 
1461
        # the user is prompted
 
1462
        self.assertEquals(entered_password,
 
1463
                          conf.get_password('ssh', 'bar.org', user='jim'))
 
1464
        self.assertContainsRe(
 
1465
            self._get_log(keep_log_file=True),
 
1466
            'password ignored in section \[ssh with password\]')
 
1467
 
 
1468
    def test_ssh_without_password_doesnt_emit_warning(self):
 
1469
        conf = config.AuthenticationConfig(_file=StringIO(
 
1470
                """
 
1471
[ssh with password]
 
1472
scheme=ssh
 
1473
host=bar.org
 
1474
user=jim
 
1475
"""))
 
1476
        entered_password = 'typed-by-hand'
 
1477
        stdout = tests.StringIOWrapper()
 
1478
        ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
 
1479
                                            stdout=stdout)
 
1480
 
 
1481
        # Since the password defined in the authentication config is ignored,
 
1482
        # the user is prompted
 
1483
        self.assertEquals(entered_password,
 
1484
                          conf.get_password('ssh', 'bar.org', user='jim'))
 
1485
        # No warning shoud be emitted since there is no password. We are only
 
1486
        # providing "user".
 
1487
        self.assertNotContainsRe(
 
1488
            self._get_log(keep_log_file=True),
 
1489
            'password ignored in section \[ssh with password\]')
 
1490
 
1421
1491
 
1422
1492
# FIXME: Once we have a way to declare authentication to all test servers, we
1423
1493
# can implement generic tests.