aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/data/htpasswd1
-rw-r--r--test/test_http.py11
-rw-r--r--test/test_http_auth.py81
3 files changed, 92 insertions, 1 deletions
diff --git a/test/data/htpasswd b/test/data/htpasswd
new file mode 100644
index 00000000..54c95b8c
--- /dev/null
+++ b/test/data/htpasswd
@@ -0,0 +1 @@
+test:$apr1$/LkYxy3x$WI4.YbiJlu537jLGEW2eu1
diff --git a/test/test_http.py b/test/test_http.py
index 666dfdbb..1c89900c 100644
--- a/test/test_http.py
+++ b/test/test_http.py
@@ -1,4 +1,4 @@
-import cStringIO, textwrap
+import cStringIO, textwrap, binascii
from netlib import http, odict
import tutils
@@ -291,3 +291,12 @@ def test_parse_url():
assert not http.parse_url("https://foo:bar")
assert not http.parse_url("https://foo:")
+
+def test_parse_http_basic_auth():
+ vals = ("basic", "foo", "bar")
+ assert http.parse_http_basic_auth(http.assemble_http_basic_auth(*vals)) == vals
+ assert not http.parse_http_basic_auth("")
+ assert not http.parse_http_basic_auth("foo bar")
+ v = "basic " + binascii.b2a_base64("foo")
+ assert not http.parse_http_basic_auth(v)
+
diff --git a/test/test_http_auth.py b/test/test_http_auth.py
new file mode 100644
index 00000000..cae69f5e
--- /dev/null
+++ b/test/test_http_auth.py
@@ -0,0 +1,81 @@
+import binascii, cStringIO
+from netlib import odict, http_auth, http
+import tutils
+
+class TestPassManNonAnon:
+ def test_simple(self):
+ p = http_auth.PassManNonAnon()
+ assert not p.test("", "")
+ assert p.test("user", "")
+
+
+class TestPassManHtpasswd:
+ def test_file_errors(self):
+ s = cStringIO.StringIO("foo")
+ tutils.raises("invalid htpasswd", http_auth.PassManHtpasswd, s)
+ s = cStringIO.StringIO("foo:bar$foo")
+ tutils.raises("invalid htpasswd", http_auth.PassManHtpasswd, s)
+
+ def test_simple(self):
+ f = open(tutils.test_data.path("data/htpasswd"))
+ pm = http_auth.PassManHtpasswd(f)
+
+ vals = ("basic", "test", "test")
+ p = http.assemble_http_basic_auth(*vals)
+ assert pm.test("test", "test")
+ assert not pm.test("test", "foo")
+ assert not pm.test("foo", "test")
+ assert not pm.test("test", "")
+ assert not pm.test("", "")
+
+
+class TestPassManSingleUser:
+ def test_simple(self):
+ pm = http_auth.PassManSingleUser("test", "test")
+ assert pm.test("test", "test")
+ assert not pm.test("test", "foo")
+ assert not pm.test("foo", "test")
+
+
+class TestNullProxyAuth:
+ def test_simple(self):
+ na = http_auth.NullProxyAuth(http_auth.PassManNonAnon())
+ assert not na.auth_challenge_headers()
+ assert na.authenticate("foo")
+ na.clean({})
+
+
+class TestBasicProxyAuth:
+ def test_simple(self):
+ ba = http_auth.BasicProxyAuth(http_auth.PassManNonAnon(), "test")
+ h = odict.ODictCaseless()
+ assert ba.auth_challenge_headers()
+ assert not ba.authenticate(h)
+
+ def test_authenticate_clean(self):
+ ba = http_auth.BasicProxyAuth(http_auth.PassManNonAnon(), "test")
+
+ hdrs = odict.ODictCaseless()
+ vals = ("basic", "foo", "bar")
+ hdrs[ba.AUTH_HEADER] = [http.assemble_http_basic_auth(*vals)]
+ assert ba.authenticate(hdrs)
+
+ ba.clean(hdrs)
+ assert not ba.AUTH_HEADER in hdrs
+
+
+ hdrs[ba.AUTH_HEADER] = [""]
+ assert not ba.authenticate(hdrs)
+
+ hdrs[ba.AUTH_HEADER] = ["foo"]
+ assert not ba.authenticate(hdrs)
+
+ vals = ("foo", "foo", "bar")
+ hdrs[ba.AUTH_HEADER] = [http.assemble_http_basic_auth(*vals)]
+ assert not ba.authenticate(hdrs)
+
+ ba = http_auth.BasicProxyAuth(http_auth.PassMan(), "test")
+ vals = ("basic", "foo", "bar")
+ hdrs[ba.AUTH_HEADER] = [http.assemble_http_basic_auth(*vals)]
+ assert not ba.authenticate(hdrs)
+