[PATCH 1 of 6] tests: extract the http server authentication extension to a single module

Matt Harbison mharbison72 at gmail.com
Wed Feb 6 02:35:44 UTC 2019


# HG changeset patch
# User Matt Harbison <matt_harbison at yahoo.com>
# Date 1549377443 18000
#      Tue Feb 05 09:37:23 2019 -0500
# Node ID e68578660449146a4b77089e8419542a61a3dc1c
# Parent  13f7a6a4f0dba217789055f6dc240d41be8dd65b
tests: extract the http server authentication extension to a single module

We had 4 copy/pastes of this, and no coverage for http digests (which are
currently broken on py3).

diff --git a/tests/httpserverauth.py b/tests/httpserverauth.py
new file mode 100644
--- /dev/null
+++ b/tests/httpserverauth.py
@@ -0,0 +1,17 @@
+from __future__ import absolute_import
+
+import base64
+
+from mercurial.hgweb import common
+
+def perform_authentication(hgweb, req, op):
+    auth = req.headers.get(b'Authorization')
+    if not auth:
+        raise common.ErrorResponse(common.HTTP_UNAUTHORIZED, b'who',
+                [(b'WWW-Authenticate', b'Basic Realm="mercurial"')])
+
+    if base64.b64decode(auth.split()[1]).split(b':', 1) != [b'user', b'pass']:
+        raise common.ErrorResponse(common.HTTP_FORBIDDEN, b'no')
+
+def extsetup(ui):
+    common.permhooks.insert(0, perform_authentication)
diff --git a/tests/test-http-bundle1.t b/tests/test-http-bundle1.t
--- a/tests/test-http-bundle1.t
+++ b/tests/test-http-bundle1.t
@@ -175,22 +175,9 @@ test http authentication
 + use the same server to test server side streaming preference
 
   $ cd test
-  $ cat << EOT > userpass.py
-  > import base64
-  > from mercurial.hgweb import common
-  > def perform_authentication(hgweb, req, op):
-  >     auth = req.headers.get(b'Authorization')
-  >     if not auth:
-  >         raise common.ErrorResponse(common.HTTP_UNAUTHORIZED, b'who',
-  >                 [(b'WWW-Authenticate', b'Basic Realm="mercurial"')])
-  >     if base64.b64decode(auth.split()[1]).split(b':', 1) != [b'user',
-  >                                                             b'pass']:
-  >         raise common.ErrorResponse(common.HTTP_FORBIDDEN, b'no')
-  > def extsetup(ui):
-  >     common.permhooks.insert(0, perform_authentication)
-  > EOT
-  $ hg serve --config extensions.x=userpass.py -p $HGPORT2 -d --pid-file=pid \
-  >    --config server.preferuncompressed=True \
+
+  $ hg serve --config extensions.x=$TESTDIR/httpserverauth.py -p $HGPORT2 -d \
+  >    --pid-file=pid --config server.preferuncompressed=True \
   >    --config web.push_ssl=False --config web.allow_push=* -A ../access.log
   $ cat pid >> $DAEMON_PIDS
 
diff --git a/tests/test-http.t b/tests/test-http.t
--- a/tests/test-http.t
+++ b/tests/test-http.t
@@ -171,21 +171,9 @@ test http authentication
 + use the same server to test server side streaming preference
 
   $ cd test
-  $ cat << EOT > userpass.py
-  > import base64
-  > from mercurial.hgweb import common
-  > def perform_authentication(hgweb, req, op):
-  >     auth = req.headers.get(b'Authorization')
-  >     if not auth:
-  >         raise common.ErrorResponse(common.HTTP_UNAUTHORIZED, b'who',
-  >                 [(b'WWW-Authenticate', b'Basic Realm="mercurial"')])
-  >     if base64.b64decode(auth.split()[1]).split(b':', 1) != [b'user', b'pass']:
-  >         raise common.ErrorResponse(common.HTTP_FORBIDDEN, b'no')
-  > def extsetup(ui):
-  >     common.permhooks.insert(0, perform_authentication)
-  > EOT
-  $ hg serve --config extensions.x=userpass.py -p $HGPORT2 -d --pid-file=pid \
-  >    --config server.preferuncompressed=True \
+
+  $ hg serve --config extensions.x=$TESTDIR/httpserverauth.py -p $HGPORT2 -d \
+  >    --pid-file=pid --config server.preferuncompressed=True \
   >    --config web.push_ssl=False --config web.allow_push=* -A ../access.log
   $ cat pid >> $DAEMON_PIDS
 
diff --git a/tests/test-largefiles-wireproto.t b/tests/test-largefiles-wireproto.t
--- a/tests/test-largefiles-wireproto.t
+++ b/tests/test-largefiles-wireproto.t
@@ -420,20 +420,8 @@ a large file from the server rather than
   $ rm "${USERCACHE}"/*
 
   $ cd ..
-  $ cat << EOT > userpass.py
-  > import base64
-  > from mercurial.hgweb import common
-  > def perform_authentication(hgweb, req, op):
-  >     auth = req.headers.get(b'Authorization')
-  >     if not auth:
-  >         raise common.ErrorResponse(common.HTTP_UNAUTHORIZED, b'who',
-  >                 [(b'WWW-Authenticate', b'Basic Realm="mercurial"')])
-  >     if base64.b64decode(auth.split()[1]).split(b':', 1) != [b'user', b'pass']:
-  >         raise common.ErrorResponse(common.HTTP_FORBIDDEN, b'no')
-  > def extsetup(ui):
-  >     common.permhooks.insert(0, perform_authentication)
-  > EOT
-  $ hg serve --config extensions.x=userpass.py -R credentialmain \
+
+  $ hg serve --config extensions.x=$TESTDIR/httpserverauth.py -R credentialmain \
   >          -d -p $HGPORT --pid-file hg.pid -A access.log
   $ cat hg.pid >> $DAEMON_PIDS
   $ cat << EOF > get_pass.py
diff --git a/tests/test-lfs-serve-access.t b/tests/test-lfs-serve-access.t
--- a/tests/test-lfs-serve-access.t
+++ b/tests/test-lfs-serve-access.t
@@ -393,22 +393,7 @@ the GET/PUT request.
   > l.password=pass
   > EOF
 
-  $ cat << EOF > userpass.py
-  > import base64
-  > from mercurial.hgweb import common
-  > def perform_authentication(hgweb, req, op):
-  >     auth = req.headers.get(b'Authorization')
-  >     if not auth:
-  >         raise common.ErrorResponse(common.HTTP_UNAUTHORIZED, b'who',
-  >                 [(b'WWW-Authenticate', b'Basic Realm="mercurial"')])
-  >     if base64.b64decode(auth.split()[1]).split(b':', 1) != [b'user',
-  >                                                             b'pass']:
-  >         raise common.ErrorResponse(common.HTTP_FORBIDDEN, b'no')
-  > def extsetup(ui):
-  >     common.permhooks.insert(0, perform_authentication)
-  > EOF
-
-  $ hg --config extensions.x=$TESTTMP/userpass.py \
+  $ hg --config extensions.x=$TESTDIR/httpserverauth.py \
   >    -R server serve -d -p $HGPORT1 --pid-file=hg.pid \
   >    -A $TESTTMP/access.log -E $TESTTMP/errors.log
   $ mv hg.pid $DAEMON_PIDS


More information about the Mercurial-devel mailing list