~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: 2007-11-04 18:51:39 UTC
  • mfrom: (2961.1.1 trunk)
  • Revision ID: pqm@pqm.ubuntu.com-20071104185139-kaio3sneodg2kp71
Authentication ring implementation (read-only)

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,
27
29
    config,
28
30
    errors,
29
31
    osutils,
30
32
    mail_client,
 
33
    ui,
31
34
    urlutils,
 
35
    tests,
32
36
    trace,
33
37
    )
34
 
from bzrlib.branch import Branch
35
 
from bzrlib.bzrdir import BzrDir
36
 
from bzrlib.tests import TestCase, TestCaseInTempDir, TestCaseWithTransport
37
38
 
38
39
 
39
40
sample_long_alias="log -r-15..-1 --line"
201
202
active = True
202
203
nonactive = False
203
204
"""
204
 
class TestConfigObj(TestCase):
 
205
class TestConfigObj(tests.TestCase):
205
206
    def test_get_bool(self):
206
207
        from bzrlib.config import ConfigObj
207
208
        co = ConfigObj(StringIO(bool_config))
216
217
[section] # line 3
217
218
whocares=notme # line 4
218
219
"""
219
 
class TestConfigObjErrors(TestCase):
 
220
class TestConfigObjErrors(tests.TestCase):
220
221
 
221
222
    def test_duplicate_section_name_error_line(self):
222
223
        try:
226
227
        else:
227
228
            self.fail('Error in config file not detected')
228
229
 
229
 
class TestConfig(TestCase):
 
230
class TestConfig(tests.TestCase):
230
231
 
231
232
    def test_constructs(self):
232
233
        config.Config()
282
283
        self.assertEqual('long', my_config.log_format())
283
284
 
284
285
 
285
 
class TestConfigPath(TestCase):
 
286
class TestConfigPath(tests.TestCase):
286
287
 
287
288
    def setUp(self):
288
289
        super(TestConfigPath, self).setUp()
322
323
            self.assertEqual(config.locations_config_filename(),
323
324
                             '/home/bogus/.bazaar/locations.conf')
324
325
 
325
 
class TestIniConfig(TestCase):
 
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):
326
335
 
327
336
    def test_contructs(self):
328
337
        my_config = config.IniBasedConfig("nothing")
341
350
        self.failUnless(my_config._get_parser() is parser)
342
351
 
343
352
 
344
 
class TestGetConfig(TestCase):
 
353
class TestGetConfig(tests.TestCase):
345
354
 
346
355
    def test_constructs(self):
347
356
        my_config = config.GlobalConfig()
360
369
                                          'utf-8')])
361
370
 
362
371
 
363
 
class TestBranchConfig(TestCaseWithTransport):
 
372
class TestBranchConfig(tests.TestCaseWithTransport):
364
373
 
365
374
    def test_constructs(self):
366
375
        branch = FakeBranch()
376
385
 
377
386
    def test_get_config(self):
378
387
        """The Branch.get_config method works properly"""
379
 
        b = BzrDir.create_standalone_workingtree('.').branch
 
388
        b = bzrdir.BzrDir.create_standalone_workingtree('.').branch
380
389
        my_config = b.get_config()
381
390
        self.assertIs(my_config.get_user_option('wacky'), None)
382
391
        my_config.set_user_option('wacky', 'unlikely')
383
392
        self.assertEqual(my_config.get_user_option('wacky'), 'unlikely')
384
393
 
385
394
        # Ensure we get the same thing if we start again
386
 
        b2 = Branch.open('.')
 
395
        b2 = branch.Branch.open('.')
387
396
        my_config2 = b2.get_config()
388
397
        self.assertEqual(my_config2.get_user_option('wacky'), 'unlikely')
389
398
 
468
477
            trace.warning = _warning
469
478
 
470
479
 
471
 
class TestGlobalConfigItems(TestCase):
 
480
class TestGlobalConfigItems(tests.TestCase):
472
481
 
473
482
    def test_user_id(self):
474
483
        config_file = StringIO(sample_config_text.encode('utf-8'))
570
579
        self.assertEqual(sample_long_alias, my_config.get_alias('ll'))
571
580
 
572
581
 
573
 
class TestLocationConfig(TestCaseInTempDir):
 
582
class TestLocationConfig(tests.TestCaseInTempDir):
574
583
 
575
584
    def test_constructs(self):
576
585
        my_config = config.LocationConfig('http://example.com')
922
931
"""
923
932
 
924
933
 
925
 
class TestBranchConfigItems(TestCaseInTempDir):
 
934
class TestBranchConfigItems(tests.TestCaseInTempDir):
926
935
 
927
936
    def get_branch_config(self, global_config=None, location=None, 
928
937
                          location_config=None, branch_data_config=None):
1072
1081
        self.assertRaises(errors.UnknownMailClient, config.get_mail_client)
1073
1082
 
1074
1083
 
1075
 
class TestMailAddressExtraction(TestCase):
 
1084
class TestMailAddressExtraction(tests.TestCase):
1076
1085
 
1077
1086
    def test_extract_email_address(self):
1078
1087
        self.assertEqual('jane@test.com',
1081
1090
                          config.extract_email_address, 'Jane Tester')
1082
1091
 
1083
1092
 
1084
 
class TestTreeConfig(TestCaseWithTransport):
 
1093
class TestTreeConfig(tests.TestCaseWithTransport):
1085
1094
 
1086
1095
    def test_get_value(self):
1087
1096
        """Test that retreiving a value from a section is possible"""
1107
1116
        self.assertEqual(value, 'value3-top')
1108
1117
        value = tree_config.get_option('key3', 'SECTION')
1109
1118
        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