~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

  • Committer: Vincent Ladeuil
  • Date: 2007-10-12 16:19:31 UTC
  • mto: (2961.1.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 2962.
  • Revision ID: v.ladeuil+lp@free.fr-20071012161931-2an2ctxh3n5ibw4k
Credentials matching implementation.

* bzrlib/tests/test_config.py:
(TestAuthenticationConfig): Test the matching mechanism of
the authentication.conf file

* bzrlib/config.py:
(AuthenticationConfig): New class.

* doc/developers/authentication-ring.txt: 
Clarify some details.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1107
1107
        self.assertEqual(value, 'value3-top')
1108
1108
        value = tree_config.get_option('key3', 'SECTION')
1109
1109
        self.assertEqual(value, 'value3-section')
 
1110
 
 
1111
 
 
1112
class TestAuthenticationConfig(TestCase):
 
1113
    """Test the authentication.conf file matching."""
 
1114
 
 
1115
    # XXX: test definitions without users.
 
1116
 
 
1117
    def _got_user_passwd(self, expected_user, expected_password,
 
1118
                         config, *args, **kwargs):
 
1119
        credentials = config.get_credentials(*args, **kwargs)
 
1120
        if credentials is None:
 
1121
            user = None
 
1122
            password = None
 
1123
        else:
 
1124
            user = credentials['user']
 
1125
            password = credentials['password']
 
1126
        self.assertEquals(expected_user, user)
 
1127
        self.assertEquals(expected_password, password)
 
1128
 
 
1129
    def  test_empty_config(self):
 
1130
        conf = config.AuthenticationConfig(_file=StringIO())
 
1131
        self.assertEquals({}, conf._get_config())
 
1132
        self._got_user_passwd(None, None, conf, 'http', 'foo.net')
 
1133
 
 
1134
    def test_broken_config(self):
 
1135
        conf = config.AuthenticationConfig(_file=StringIO('[DEF'))
 
1136
        self.assertRaises(errors.ParseConfigError, conf._get_config)
 
1137
 
 
1138
    def test_credentials_for_scheme_host(self):
 
1139
        conf = config.AuthenticationConfig(_file=StringIO(
 
1140
                """# Identity on foo.net
 
1141
[ftp definition]
 
1142
scheme=ftp
 
1143
host=foo.net
 
1144
user=joe
 
1145
password=secret-pass
 
1146
"""))
 
1147
        # Basic matching
 
1148
        self._got_user_passwd('joe', 'secret-pass', conf, 'ftp', 'foo.net')
 
1149
        # different scheme
 
1150
        self._got_user_passwd(None, None, conf, 'http', 'foo.net')
 
1151
        # different host
 
1152
        self._got_user_passwd(None, None, conf, 'ftp', 'bar.net')
 
1153
 
 
1154
    def test_credentials_for_host_port(self):
 
1155
        conf = config.AuthenticationConfig(_file=StringIO(
 
1156
                """# Identity on foo.net
 
1157
[ftp definition]
 
1158
scheme=ftp
 
1159
port=10021
 
1160
host=foo.net
 
1161
user=joe
 
1162
password=secret-pass
 
1163
"""))
 
1164
        # No port
 
1165
        self._got_user_passwd('joe', 'secret-pass',
 
1166
                              conf, 'ftp', 'foo.net', port=10021)
 
1167
        # different port
 
1168
        self._got_user_passwd(None, None, conf, 'ftp', 'foo.net')
 
1169
 
 
1170
    def test_for_matching_host(self):
 
1171
        conf = config.AuthenticationConfig(_file=StringIO(
 
1172
                """# Identity on foo.net
 
1173
[sourceforge]
 
1174
scheme=bzr
 
1175
host=bzr.sf.net
 
1176
user=joe
 
1177
password=joepass
 
1178
[sourceforge domain]
 
1179
scheme=bzr
 
1180
host=.bzr.sf.net
 
1181
user=georges
 
1182
password=bendover
 
1183
"""))
 
1184
        # matching domain
 
1185
        self._got_user_passwd('georges', 'bendover',
 
1186
                              conf, 'bzr', 'foo.bzr.sf.net')
 
1187
        # phishing attempt
 
1188
        self._got_user_passwd(None, None,
 
1189
                              conf, 'bzr', 'bbzr.sf.net')
 
1190
 
 
1191
    def test_for_matching_host_None(self):
 
1192
        conf = config.AuthenticationConfig(_file=StringIO(
 
1193
                """# Identity on foo.net
 
1194
[catchup bzr]
 
1195
scheme=bzr
 
1196
user=joe
 
1197
password=joepass
 
1198
[DEFAULT]
 
1199
user=georges
 
1200
password=bendover
 
1201
"""))
 
1202
        # match no host
 
1203
        self._got_user_passwd('joe', 'joepass',
 
1204
                              conf, 'bzr', 'quux.net')
 
1205
        # no host but different scheme
 
1206
        self._got_user_passwd('georges', 'bendover',
 
1207
                              conf, 'ftp', 'quux.net')
 
1208
 
 
1209
    def test_credentials_for_path(self):
 
1210
        conf = config.AuthenticationConfig(_file=StringIO(
 
1211
                """
 
1212
[http dir1]
 
1213
scheme=http
 
1214
host=bar.org
 
1215
path=/dir1
 
1216
user=jim
 
1217
password=jimpass
 
1218
[http dir2]
 
1219
scheme=http
 
1220
host=bar.org
 
1221
path=/dir2
 
1222
user=georges
 
1223
password=bendover
 
1224
"""))
 
1225
        # no path no dice
 
1226
        self._got_user_passwd(None, None,
 
1227
                              conf, 'http', host='bar.org', path='/dir3')
 
1228
        # matching path
 
1229
        self._got_user_passwd('georges', 'bendover',
 
1230
                              conf, 'http', host='bar.org', path='/dir2')
 
1231
        # matching subdir
 
1232
        self._got_user_passwd('jim', 'jimpass',
 
1233
                              conf, 'http', host='bar.org',path='/dir1/subdir')
 
1234
 
 
1235
    def test_credentials_for_user(self):
 
1236
        conf = config.AuthenticationConfig(_file=StringIO(
 
1237
                """
 
1238
[with user]
 
1239
scheme=http
 
1240
host=bar.org
 
1241
user=jim
 
1242
password=jimpass
 
1243
"""))
 
1244
        # Get user
 
1245
        self._got_user_passwd('jim', 'jimpass',
 
1246
                              conf, 'http', 'bar.org')
 
1247
        # Get same user
 
1248
        self._got_user_passwd('jim', 'jimpass',
 
1249
                              conf, 'http', 'bar.org', user='jim')
 
1250
        # Don't get a different user if one is specified
 
1251
        self._got_user_passwd(None, None,
 
1252
                              conf, 'http', 'bar.org', user='georges')
 
1253
 
 
1254
    def test_verify_certificates(self):
 
1255
        conf = config.AuthenticationConfig(_file=StringIO(
 
1256
                """
 
1257
[self-signed]
 
1258
scheme=https
 
1259
host=bar.org
 
1260
user=jim
 
1261
password=jimpass
 
1262
verify_certificates=False
 
1263
[normal]
 
1264
scheme=https
 
1265
host=foo.net
 
1266
user=georges
 
1267
password=bendover
 
1268
"""))
 
1269
        credentials = conf.get_credentials('https', 'bar.org')
 
1270
        self.assertEquals(False, credentials.get('verify_certificates'))
 
1271
        credentials = conf.get_credentials('https', 'foo.net')
 
1272
        self.assertEquals(True, credentials.get('verify_certificates'))