[PATCH 3 of 3] test-http: test http authentication

Patrick Mezard pmezard at gmail.com
Fri Aug 5 14:24:59 CDT 2011


# HG changeset patch
# User Mads Kiilerich <mads at kiilerich.com>
# Date 1312497594 -7200
# Branch stable
# Node ID 7ef0a3f63bbdbd159ab194c983edf14d85a5ad5e
# Parent  0a9f8def5e25da6450f9ab0554cfcc69d112d24a
test-http: test http authentication

The extension hack is based on Henrik Stuart's hg-textauth.

diff --git a/tests/test-http.t b/tests/test-http.t
--- a/tests/test-http.t
+++ b/tests/test-http.t
@@ -110,6 +110,55 @@
   abort: HTTP Error 404: Not Found
   [255]
 
+test http authentication
+
+  $ cd test
+  $ cat << EOT > userpass.py
+  > import base64
+  > from mercurial.hgweb import common
+  > def perform_authentication(hgweb, req, op):
+  >     auth = req.env.get('HTTP_AUTHORIZATION')
+  >     if not auth:
+  >         raise common.ErrorResponse(common.HTTP_UNAUTHORIZED, 'who',
+  >                 [('WWW-Authenticate', 'Basic Realm="mercurial"')])
+  >     if base64.b64decode(auth.split()[1]).split(':', 1) != ['user', 'pass']: 
+  >         raise common.ErrorResponse(common.HTTP_FORBIDDEN, 'no')
+  > def extsetup():
+  >     common.permhooks.insert(0, perform_authentication)
+  > EOT
+  $ hg --config extensions.x=userpass.py serve -p $HGPORT2 -d --pid-file=pid
+  $ cat pid >> $DAEMON_PIDS
+
+  $ hg id http://localhost:$HGPORT2/  
+  abort: http authorization required
+  [255]
+  $ hg id http://user@localhost:$HGPORT2/ 
+  abort: http authorization required
+  [255]
+  $ hg id http://user:pass@localhost:$HGPORT2/
+  5fed3813f7f5
+  $ echo '[auth]' >> .hg/hgrc 
+  $ echo 'l.schemes=http' >> .hg/hgrc
+  $ echo 'l.prefix=lo' >> .hg/hgrc
+  $ echo 'l.username=user' >> .hg/hgrc
+  $ echo 'l.password=pass' >> .hg/hgrc
+  $ hg id http://localhost:$HGPORT2/ 
+  5fed3813f7f5
+  $ hg id http://localhost:$HGPORT2/  
+  5fed3813f7f5
+  $ hg id http://user@localhost:$HGPORT2/ 
+  5fed3813f7f5
+  $ hg id http://user:pass@localhost:$HGPORT2/
+  5fed3813f7f5
+  $ hg id http://user2@localhost:$HGPORT2/ 
+  abort: http authorization required
+  [255]
+  $ hg id http://user:pass2@localhost:$HGPORT2/
+  abort: HTTP Error 403: no
+  [255]
+
+  $ cd ..
+
 check error log
 
   $ cat error.log


More information about the Mercurial-devel mailing list