1116
1107
self.assertEqual(value, 'value3-top')
1117
1108
value = tree_config.get_option('key3', 'SECTION')
1118
1109
self.assertEqual(value, 'value3-section')
1121
class TestAuthenticationConfigFile(tests.TestCase):
1122
"""Test the authentication.conf file matching"""
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:
1131
user = credentials['user']
1132
password = credentials['password']
1133
self.assertEquals(expected_user, user)
1134
self.assertEquals(expected_password, password)
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')
1141
def test_broken_config(self):
1142
conf = config.AuthenticationConfig(_file=StringIO('[DEF'))
1143
self.assertRaises(errors.ParseConfigError, conf._get_config)
1145
conf = config.AuthenticationConfig(_file=StringIO(
1149
verify_certificates=askme # Error: Not a boolean
1151
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1152
conf = config.AuthenticationConfig(_file=StringIO(
1156
port=port # Error: Not an int
1158
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1160
def test_credentials_for_scheme_host(self):
1161
conf = config.AuthenticationConfig(_file=StringIO(
1162
"""# Identity on foo.net
1167
password=secret-pass
1170
self._got_user_passwd('joe', 'secret-pass', conf, 'ftp', 'foo.net')
1172
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1174
self._got_user_passwd(None, None, conf, 'ftp', 'bar.net')
1176
def test_credentials_for_host_port(self):
1177
conf = config.AuthenticationConfig(_file=StringIO(
1178
"""# Identity on foo.net
1184
password=secret-pass
1187
self._got_user_passwd('joe', 'secret-pass',
1188
conf, 'ftp', 'foo.net', port=10021)
1190
self._got_user_passwd(None, None, conf, 'ftp', 'foo.net')
1192
def test_for_matching_host(self):
1193
conf = config.AuthenticationConfig(_file=StringIO(
1194
"""# Identity on foo.net
1200
[sourceforge domain]
1207
self._got_user_passwd('georges', 'bendover',
1208
conf, 'bzr', 'foo.bzr.sf.net')
1210
self._got_user_passwd(None, None,
1211
conf, 'bzr', 'bbzr.sf.net')
1213
def test_for_matching_host_None(self):
1214
conf = config.AuthenticationConfig(_file=StringIO(
1215
"""# Identity on foo.net
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')
1231
def test_credentials_for_path(self):
1232
conf = config.AuthenticationConfig(_file=StringIO(
1248
self._got_user_passwd(None, None,
1249
conf, 'http', host='bar.org', path='/dir3')
1251
self._got_user_passwd('georges', 'bendover',
1252
conf, 'http', host='bar.org', path='/dir2')
1254
self._got_user_passwd('jim', 'jimpass',
1255
conf, 'http', host='bar.org',path='/dir1/subdir')
1257
def test_credentials_for_user(self):
1258
conf = config.AuthenticationConfig(_file=StringIO(
1267
self._got_user_passwd('jim', 'jimpass',
1268
conf, 'http', 'bar.org')
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')
1276
def test_verify_certificates(self):
1277
conf = config.AuthenticationConfig(_file=StringIO(
1284
verify_certificates=False
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'))
1297
class TestAuthenticationConfig(tests.TestCase):
1298
"""Test AuthenticationConfig behaviour"""
1300
def _check_default_prompt(self, expected_prompt_format, scheme,
1301
host=None, port=None, realm=None, path=None):
1304
user, password = 'jim', 'precious'
1305
expected_prompt = expected_prompt_format % {
1306
'scheme': scheme, 'host': host, 'port': port,
1307
'user': user, 'realm': realm}
1309
stdout = tests.StringIOWrapper()
1310
ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
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)
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: ',
1325
self._check_default_prompt('SSH %(user)s@%(host)s:%(port)d password: ',
1327
# SMTP port handling is a bit special (it's handled if embedded in the
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: ',
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: ',
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):