~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/HTTPTestUtil.py

  • Committer: Vincent Ladeuil
  • Date: 2007-04-15 15:57:08 UTC
  • mto: (2420.1.1 bzr.http.auth)
  • mto: This revision was merged to the branch mainline in revision 2463.
  • Revision ID: v.ladeuil+lp@free.fr-20070415155708-frrm29cd9vvvd8do
Take jam's review comments into account. Fix typos, give better
explanations, add a test, complete another.

* bzrlib/transport/http/_urllib2_wrappers.py:
(HTTPBasicAuthHandler.http_error_401): Better explanation.

* bzrlib/transport/http/_urllib.py:
(HttpTransport_urllib.__init__): _auth renamed to _auth_scheme.

* bzrlib/tests/test_http.py:
(TestHTTPBasicAuth.test_unknown_user): New test.

* bzrlib/tests/HTTPTestUtil.py:
(BasicAuthHTTPServer): New class. Be explicit about use
requirements: basic authentication is mandatory.

Show diffs side-by-side

added added

removed removed

Lines of Context:
310
310
 
311
311
 
312
312
class BasicAuthRequestHandler(TestingHTTPRequestHandler):
313
 
    """Requires a basic authentification to process requests."""
 
313
    """Requires a basic authentication to process requests.
 
314
 
 
315
    This is intended to be used with a server that always and
 
316
    only use basic authentication.
 
317
    """
314
318
 
315
319
    def do_GET(self):
316
320
        tcs = self.server.test_case_server
317
 
        if tcs.auth == 'basic':
 
321
        if tcs.auth_scheme == 'basic':
318
322
            auth_header = self.headers.get('Authorization')
319
323
            authorized = False
320
 
            if auth_header:
 
324
            if auth_header and auth_header.lower().startswith('basic '):
321
325
                coded_auth = auth_header[len('Basic '):]
322
326
                user, password = coded_auth.decode('base64').split(':')
323
327
                authorized = tcs.authorized(user, password)
330
334
 
331
335
        TestingHTTPRequestHandler.do_GET(self)
332
336
 
 
337
 
333
338
class AuthHTTPServer(HttpServer):
334
 
    """AuthHTTPServer extends HttpServer with a dictionary of passwords"""
335
 
 
336
 
    def __init__(self, request_handler, auth):
 
339
    """AuthHTTPServer extends HttpServer with a dictionary of passwords.
 
340
 
 
341
    This is used as a base class for various schemes.
 
342
 
 
343
    Note that no users are defined by default, so add_user should
 
344
    be called before issuing the first request.
 
345
    """
 
346
 
 
347
    def __init__(self, request_handler, auth_scheme):
337
348
        HttpServer.__init__(self, request_handler)
338
 
        # No authentification is done by default
339
 
        self.auth = auth
 
349
        self.auth_scheme = auth_scheme
340
350
        self.password_of = {}
341
351
 
342
352
    def add_user(self, user, password):
 
353
        """Declare a user with an associated password.
 
354
 
 
355
        password can be empty, use an empty string ('') in that
 
356
        case, not None.
 
357
        """
343
358
        self.password_of[user] = password
344
359
 
345
360
    def authorized(self, user, password):
346
 
        return self.password_of[user] == password
 
361
        expected_password = self.password_of.get(user, None)
 
362
        return expected_password is not None and password == expected_password
 
363
 
 
364
 
 
365
class BasicAuthHTTPServer(AuthHTTPServer):
 
366
    """An HTTP server requiring basic authentication"""
 
367
 
 
368
    def __init__(self):
 
369
        AuthHTTPServer.__init__(self, BasicAuthRequestHandler, 'basic')