Unit test auth actions.

This commit is contained in:
Aldo Cortesi 2013-12-08 13:35:42 +13:00
parent d05c20d8fa
commit 7213f86d49
2 changed files with 30 additions and 10 deletions

View File

@ -125,23 +125,19 @@ class AuthAction(Action):
"""
def __call__(self, parser, namespace, values, option_string=None):
passman = self.getPasswordManager(values)
if passman:
authenticator = BasicProxyAuth(passman, "mitmproxy")
else:
authenticator = NullProxyAuth(None)
setattr(namespace, "authenticator", authenticator)
def getPasswordManager(self, s):
"""
returns the password manager
"""
def getPasswordManager(self, s): # pragma: nocover
raise NotImplementedError()
class SingleuserAuthAction(AuthAction):
def getPasswordManager(self, s):
if len(s.split(':')) != 2:
raise ArgumentTypeError("Invalid single-user specification. Please use the format username:password")
raise ArgumentTypeError(
"Invalid single-user specification. Please use the format username:password"
)
username, password = s.split(':')
return PassManSingleUser(username, password)
@ -155,3 +151,4 @@ class HtpasswdAuthAction(AuthAction):
def getPasswordManager(self, s):
with open(s, "r") as f:
return PassManHtpasswd(f)

View File

@ -1,5 +1,6 @@
import binascii, cStringIO
from netlib import odict, http_auth, http
import mock
import tutils
class TestPassManNonAnon:
@ -79,3 +80,25 @@ class TestBasicProxyAuth:
hdrs[ba.AUTH_HEADER] = [http.assemble_http_basic_auth(*vals)]
assert not ba.authenticate(hdrs)
class Bunch: pass
class TestAuthAction:
def test_nonanonymous(self):
m = Bunch()
aa = http_auth.NonanonymousAuthAction(None, None)
aa(None, m, None, None)
assert m.authenticator
def test_singleuser(self):
m = Bunch()
aa = http_auth.SingleuserAuthAction(None, None)
aa(None, m, "foo:bar", None)
assert m.authenticator
tutils.raises("invalid", aa, None, m, "foo", None)
def test_httppasswd(self):
m = Bunch()
aa = http_auth.HtpasswdAuthAction(None, None)
aa(None, m, tutils.test_data.path("data/htpasswd"), None)
assert m.authenticator