~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

  • Committer: John Arbash Meinel
  • Date: 2007-11-16 02:11:34 UTC
  • mto: This revision was merged to the branch mainline in revision 3002.
  • Revision ID: john@arbash-meinel.com-20071116021134-e0fjjpjllimkhj3v
Re-introduce the None check in case someone asks to uncommit *to* the last revision

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
#import bzrlib specific imports here
26
26
from bzrlib import (
27
 
    branch,
28
 
    bzrdir,
29
27
    config,
30
28
    errors,
31
29
    osutils,
32
30
    mail_client,
33
 
    ui,
34
31
    urlutils,
35
 
    tests,
36
32
    trace,
37
33
    )
 
34
from bzrlib.branch import Branch
 
35
from bzrlib.bzrdir import BzrDir
 
36
from bzrlib.tests import TestCase, TestCaseInTempDir, TestCaseWithTransport
38
37
 
39
38
 
40
39
sample_long_alias="log -r-15..-1 --line"
202
201
active = True
203
202
nonactive = False
204
203
"""
205
 
class TestConfigObj(tests.TestCase):
 
204
class TestConfigObj(TestCase):
206
205
    def test_get_bool(self):
207
206
        from bzrlib.config import ConfigObj
208
207
        co = ConfigObj(StringIO(bool_config))
217
216
[section] # line 3
218
217
whocares=notme # line 4
219
218
"""
220
 
class TestConfigObjErrors(tests.TestCase):
 
219
class TestConfigObjErrors(TestCase):
221
220
 
222
221
    def test_duplicate_section_name_error_line(self):
223
222
        try:
227
226
        else:
228
227
            self.fail('Error in config file not detected')
229
228
 
230
 
class TestConfig(tests.TestCase):
 
229
class TestConfig(TestCase):
231
230
 
232
231
    def test_constructs(self):
233
232
        config.Config()
283
282
        self.assertEqual('long', my_config.log_format())
284
283
 
285
284
 
286
 
class TestConfigPath(tests.TestCase):
 
285
class TestConfigPath(TestCase):
287
286
 
288
287
    def setUp(self):
289
288
        super(TestConfigPath, self).setUp()
323
322
            self.assertEqual(config.locations_config_filename(),
324
323
                             '/home/bogus/.bazaar/locations.conf')
325
324
 
326
 
    def test_authentication_config_filename(self):
327
 
        if sys.platform == 'win32':
328
 
            self.assertEqual(config.authentication_config_filename(), 
329
 
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/authentication.conf')
330
 
        else:
331
 
            self.assertEqual(config.authentication_config_filename(),
332
 
                             '/home/bogus/.bazaar/authentication.conf')
333
 
 
334
 
class TestIniConfig(tests.TestCase):
 
325
class TestIniConfig(TestCase):
335
326
 
336
327
    def test_contructs(self):
337
328
        my_config = config.IniBasedConfig("nothing")
350
341
        self.failUnless(my_config._get_parser() is parser)
351
342
 
352
343
 
353
 
class TestGetConfig(tests.TestCase):
 
344
class TestGetConfig(TestCase):
354
345
 
355
346
    def test_constructs(self):
356
347
        my_config = config.GlobalConfig()
369
360
                                          'utf-8')])
370
361
 
371
362
 
372
 
class TestBranchConfig(tests.TestCaseWithTransport):
 
363
class TestBranchConfig(TestCaseWithTransport):
373
364
 
374
365
    def test_constructs(self):
375
366
        branch = FakeBranch()
385
376
 
386
377
    def test_get_config(self):
387
378
        """The Branch.get_config method works properly"""
388
 
        b = bzrdir.BzrDir.create_standalone_workingtree('.').branch
 
379
        b = BzrDir.create_standalone_workingtree('.').branch
389
380
        my_config = b.get_config()
390
381
        self.assertIs(my_config.get_user_option('wacky'), None)
391
382
        my_config.set_user_option('wacky', 'unlikely')
392
383
        self.assertEqual(my_config.get_user_option('wacky'), 'unlikely')
393
384
 
394
385
        # Ensure we get the same thing if we start again
395
 
        b2 = branch.Branch.open('.')
 
386
        b2 = Branch.open('.')
396
387
        my_config2 = b2.get_config()
397
388
        self.assertEqual(my_config2.get_user_option('wacky'), 'unlikely')
398
389
 
477
468
            trace.warning = _warning
478
469
 
479
470
 
480
 
class TestGlobalConfigItems(tests.TestCase):
 
471
class TestGlobalConfigItems(TestCase):
481
472
 
482
473
    def test_user_id(self):
483
474
        config_file = StringIO(sample_config_text.encode('utf-8'))
579
570
        self.assertEqual(sample_long_alias, my_config.get_alias('ll'))
580
571
 
581
572
 
582
 
class TestLocationConfig(tests.TestCaseInTempDir):
 
573
class TestLocationConfig(TestCaseInTempDir):
583
574
 
584
575
    def test_constructs(self):
585
576
        my_config = config.LocationConfig('http://example.com')
931
922
"""
932
923
 
933
924
 
934
 
class TestBranchConfigItems(tests.TestCaseInTempDir):
 
925
class TestBranchConfigItems(TestCaseInTempDir):
935
926
 
936
927
    def get_branch_config(self, global_config=None, location=None, 
937
928
                          location_config=None, branch_data_config=None):
1081
1072
        self.assertRaises(errors.UnknownMailClient, config.get_mail_client)
1082
1073
 
1083
1074
 
1084
 
class TestMailAddressExtraction(tests.TestCase):
 
1075
class TestMailAddressExtraction(TestCase):
1085
1076
 
1086
1077
    def test_extract_email_address(self):
1087
1078
        self.assertEqual('jane@test.com',
1090
1081
                          config.extract_email_address, 'Jane Tester')
1091
1082
 
1092
1083
 
1093
 
class TestTreeConfig(tests.TestCaseWithTransport):
 
1084
class TestTreeConfig(TestCaseWithTransport):
1094
1085
 
1095
1086
    def test_get_value(self):
1096
1087
        """Test that retreiving a value from a section is possible"""
1116
1107
        self.assertEqual(value, 'value3-top')
1117
1108
        value = tree_config.get_option('key3', 'SECTION')
1118
1109
        self.assertEqual(value, 'value3-section')
1119
 
 
1120
 
 
1121
 
class TestAuthenticationConfigFile(tests.TestCase):
1122
 
    """Test the authentication.conf file matching"""
1123
 
 
1124
 
    def _got_user_passwd(self, expected_user, expected_password,
1125
 
                         config, *args, **kwargs):
1126
 
        credentials = config.get_credentials(*args, **kwargs)
1127
 
        if credentials is None:
1128
 
            user = None
1129
 
            password = None
1130
 
        else:
1131
 
            user = credentials['user']
1132
 
            password = credentials['password']
1133
 
        self.assertEquals(expected_user, user)
1134
 
        self.assertEquals(expected_password, password)
1135
 
 
1136
 
    def test_empty_config(self):
1137
 
        conf = config.AuthenticationConfig(_file=StringIO())
1138
 
        self.assertEquals({}, conf._get_config())
1139
 
        self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1140
 
 
1141
 
    def test_broken_config(self):
1142
 
        conf = config.AuthenticationConfig(_file=StringIO('[DEF'))
1143
 
        self.assertRaises(errors.ParseConfigError, conf._get_config)
1144
 
 
1145
 
        conf = config.AuthenticationConfig(_file=StringIO(
1146
 
                """[broken]
1147
 
scheme=ftp
1148
 
user=joe
1149
 
verify_certificates=askme # Error: Not a boolean
1150
 
"""))
1151
 
        self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1152
 
        conf = config.AuthenticationConfig(_file=StringIO(
1153
 
                """[broken]
1154
 
scheme=ftp
1155
 
user=joe
1156
 
port=port # Error: Not an int
1157
 
"""))
1158
 
        self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1159
 
 
1160
 
    def test_credentials_for_scheme_host(self):
1161
 
        conf = config.AuthenticationConfig(_file=StringIO(
1162
 
                """# Identity on foo.net
1163
 
[ftp definition]
1164
 
scheme=ftp
1165
 
host=foo.net
1166
 
user=joe
1167
 
password=secret-pass
1168
 
"""))
1169
 
        # Basic matching
1170
 
        self._got_user_passwd('joe', 'secret-pass', conf, 'ftp', 'foo.net')
1171
 
        # different scheme
1172
 
        self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1173
 
        # different host
1174
 
        self._got_user_passwd(None, None, conf, 'ftp', 'bar.net')
1175
 
 
1176
 
    def test_credentials_for_host_port(self):
1177
 
        conf = config.AuthenticationConfig(_file=StringIO(
1178
 
                """# Identity on foo.net
1179
 
[ftp definition]
1180
 
scheme=ftp
1181
 
port=10021
1182
 
host=foo.net
1183
 
user=joe
1184
 
password=secret-pass
1185
 
"""))
1186
 
        # No port
1187
 
        self._got_user_passwd('joe', 'secret-pass',
1188
 
                              conf, 'ftp', 'foo.net', port=10021)
1189
 
        # different port
1190
 
        self._got_user_passwd(None, None, conf, 'ftp', 'foo.net')
1191
 
 
1192
 
    def test_for_matching_host(self):
1193
 
        conf = config.AuthenticationConfig(_file=StringIO(
1194
 
                """# Identity on foo.net
1195
 
[sourceforge]
1196
 
scheme=bzr
1197
 
host=bzr.sf.net
1198
 
user=joe
1199
 
password=joepass
1200
 
[sourceforge domain]
1201
 
scheme=bzr
1202
 
host=.bzr.sf.net
1203
 
user=georges
1204
 
password=bendover
1205
 
"""))
1206
 
        # matching domain
1207
 
        self._got_user_passwd('georges', 'bendover',
1208
 
                              conf, 'bzr', 'foo.bzr.sf.net')
1209
 
        # phishing attempt
1210
 
        self._got_user_passwd(None, None,
1211
 
                              conf, 'bzr', 'bbzr.sf.net')
1212
 
 
1213
 
    def test_for_matching_host_None(self):
1214
 
        conf = config.AuthenticationConfig(_file=StringIO(
1215
 
                """# Identity on foo.net
1216
 
[catchup bzr]
1217
 
scheme=bzr
1218
 
user=joe
1219
 
password=joepass
1220
 
[DEFAULT]
1221
 
user=georges
1222
 
password=bendover
1223
 
"""))
1224
 
        # match no host
1225
 
        self._got_user_passwd('joe', 'joepass',
1226
 
                              conf, 'bzr', 'quux.net')
1227
 
        # no host but different scheme
1228
 
        self._got_user_passwd('georges', 'bendover',
1229
 
                              conf, 'ftp', 'quux.net')
1230
 
 
1231
 
    def test_credentials_for_path(self):
1232
 
        conf = config.AuthenticationConfig(_file=StringIO(
1233
 
                """
1234
 
[http dir1]
1235
 
scheme=http
1236
 
host=bar.org
1237
 
path=/dir1
1238
 
user=jim
1239
 
password=jimpass
1240
 
[http dir2]
1241
 
scheme=http
1242
 
host=bar.org
1243
 
path=/dir2
1244
 
user=georges
1245
 
password=bendover
1246
 
"""))
1247
 
        # no path no dice
1248
 
        self._got_user_passwd(None, None,
1249
 
                              conf, 'http', host='bar.org', path='/dir3')
1250
 
        # matching path
1251
 
        self._got_user_passwd('georges', 'bendover',
1252
 
                              conf, 'http', host='bar.org', path='/dir2')
1253
 
        # matching subdir
1254
 
        self._got_user_passwd('jim', 'jimpass',
1255
 
                              conf, 'http', host='bar.org',path='/dir1/subdir')
1256
 
 
1257
 
    def test_credentials_for_user(self):
1258
 
        conf = config.AuthenticationConfig(_file=StringIO(
1259
 
                """
1260
 
[with user]
1261
 
scheme=http
1262
 
host=bar.org
1263
 
user=jim
1264
 
password=jimpass
1265
 
"""))
1266
 
        # Get user
1267
 
        self._got_user_passwd('jim', 'jimpass',
1268
 
                              conf, 'http', 'bar.org')
1269
 
        # Get same user
1270
 
        self._got_user_passwd('jim', 'jimpass',
1271
 
                              conf, 'http', 'bar.org', user='jim')
1272
 
        # Don't get a different user if one is specified
1273
 
        self._got_user_passwd(None, None,
1274
 
                              conf, 'http', 'bar.org', user='georges')
1275
 
 
1276
 
    def test_verify_certificates(self):
1277
 
        conf = config.AuthenticationConfig(_file=StringIO(
1278
 
                """
1279
 
[self-signed]
1280
 
scheme=https
1281
 
host=bar.org
1282
 
user=jim
1283
 
password=jimpass
1284
 
verify_certificates=False
1285
 
[normal]
1286
 
scheme=https
1287
 
host=foo.net
1288
 
user=georges
1289
 
password=bendover
1290
 
"""))
1291
 
        credentials = conf.get_credentials('https', 'bar.org')
1292
 
        self.assertEquals(False, credentials.get('verify_certificates'))
1293
 
        credentials = conf.get_credentials('https', 'foo.net')
1294
 
        self.assertEquals(True, credentials.get('verify_certificates'))
1295
 
 
1296
 
 
1297
 
class TestAuthenticationConfig(tests.TestCase):
1298
 
    """Test AuthenticationConfig behaviour"""
1299
 
 
1300
 
    def _check_default_prompt(self, expected_prompt_format, scheme,
1301
 
                              host=None, port=None, realm=None, path=None):
1302
 
        if host is None:
1303
 
            host = 'bar.org'
1304
 
        user, password = 'jim', 'precious'
1305
 
        expected_prompt = expected_prompt_format % {
1306
 
            'scheme': scheme, 'host': host, 'port': port,
1307
 
            'user': user, 'realm': realm}
1308
 
 
1309
 
        stdout = tests.StringIOWrapper()
1310
 
        ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
1311
 
                                            stdout=stdout)
1312
 
        # We use an empty conf so that the user is always prompted
1313
 
        conf = config.AuthenticationConfig()
1314
 
        self.assertEquals(password,
1315
 
                          conf.get_password(scheme, host, user, port=port,
1316
 
                                            realm=realm, path=path))
1317
 
        self.assertEquals(stdout.getvalue(), expected_prompt)
1318
 
 
1319
 
    def test_default_prompts(self):
1320
 
        # HTTP prompts can't be tested here, see test_http.py
1321
 
        self._check_default_prompt('FTP %(user)s@%(host)s password: ', 'ftp')
1322
 
        self._check_default_prompt('FTP %(user)s@%(host)s:%(port)d password: ',
1323
 
                                   'ftp', port=10020)
1324
 
 
1325
 
        self._check_default_prompt('SSH %(user)s@%(host)s:%(port)d password: ',
1326
 
                                   'ssh', port=12345)
1327
 
        # SMTP port handling is a bit special (it's handled if embedded in the
1328
 
        # host too)
1329
 
        # FIXME: should we: forbid that, extend it to other schemes, leave
1330
 
        # things as they are that's fine thank you ?
1331
 
        self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1332
 
                                   'smtp')
1333
 
        self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1334
 
                                   'smtp', host='bar.org:10025')
1335
 
        self._check_default_prompt(
1336
 
            'SMTP %(user)s@%(host)s:%(port)d password: ',
1337
 
            'smtp', port=10025)
1338
 
 
1339
 
 
1340
 
# FIXME: Once we have a way to declare authentication to all test servers, we
1341
 
# can implement generic tests.
1342
 
# test_user_password_in_url
1343
 
# test_user_in_url_password_from_config
1344
 
# test_user_in_url_password_prompted
1345
 
# test_user_in_config
1346
 
# test_user_getpass.getuser
1347
 
# test_user_prompted ?
1348
 
class TestAuthenticationRing(tests.TestCaseWithTransport):
1349
 
    pass