mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-23 08:11:00 +00:00
improve proxyauth tests
This commit is contained in:
parent
f55df034e6
commit
0c5b56f7ce
@ -10,81 +10,70 @@ from mitmproxy.test import tflow
|
|||||||
from mitmproxy.test import tutils
|
from mitmproxy.test import tutils
|
||||||
|
|
||||||
|
|
||||||
def test_parse_http_basic_auth():
|
class TestMkauth:
|
||||||
assert proxyauth.parse_http_basic_auth(
|
def test_mkauth_scheme(self):
|
||||||
proxyauth.mkauth("test", "test")
|
assert proxyauth.mkauth('username', 'password') == 'basic dXNlcm5hbWU6cGFzc3dvcmQ=\n'
|
||||||
) == ("basic", "test", "test")
|
|
||||||
with pytest.raises(ValueError):
|
@pytest.mark.parametrize('scheme, expected', [
|
||||||
proxyauth.parse_http_basic_auth("")
|
('', ' dXNlcm5hbWU6cGFzc3dvcmQ=\n'),
|
||||||
with pytest.raises(ValueError):
|
('basic', 'basic dXNlcm5hbWU6cGFzc3dvcmQ=\n'),
|
||||||
proxyauth.parse_http_basic_auth("foo bar")
|
('foobar', 'foobar dXNlcm5hbWU6cGFzc3dvcmQ=\n'),
|
||||||
with pytest.raises(ValueError):
|
])
|
||||||
proxyauth.parse_http_basic_auth("basic abc")
|
def test_mkauth(self, scheme, expected):
|
||||||
with pytest.raises(ValueError):
|
assert proxyauth.mkauth('username', 'password', scheme) == expected
|
||||||
v = "basic " + binascii.b2a_base64(b"foo").decode("ascii")
|
|
||||||
proxyauth.parse_http_basic_auth(v)
|
|
||||||
|
|
||||||
|
|
||||||
def test_configure():
|
class TestParseHttpBasicAuth:
|
||||||
|
@pytest.mark.parametrize('input', [
|
||||||
|
'',
|
||||||
|
'foo bar',
|
||||||
|
'basic abc',
|
||||||
|
'basic ' + binascii.b2a_base64(b"foo").decode("ascii"),
|
||||||
|
])
|
||||||
|
def test_parse_http_basic_auth_error(self, input):
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
proxyauth.parse_http_basic_auth(input)
|
||||||
|
|
||||||
|
def test_parse_http_basic_auth(self):
|
||||||
|
input = proxyauth.mkauth("test", "test")
|
||||||
|
assert proxyauth.parse_http_basic_auth(input) == ("basic", "test", "test")
|
||||||
|
|
||||||
|
|
||||||
|
class TestProxyAuth:
|
||||||
|
@pytest.mark.parametrize('mode, expected', [
|
||||||
|
('', False),
|
||||||
|
('foobar', False),
|
||||||
|
('regular', True),
|
||||||
|
('upstream:', True),
|
||||||
|
('upstream:foobar', True),
|
||||||
|
])
|
||||||
|
def test_is_proxy_auth(self, mode, expected):
|
||||||
up = proxyauth.ProxyAuth()
|
up = proxyauth.ProxyAuth()
|
||||||
with taddons.context() as ctx:
|
with taddons.context() as ctx:
|
||||||
with pytest.raises(exceptions.OptionsError):
|
ctx.options.mode = mode
|
||||||
ctx.configure(up, proxyauth="foo")
|
assert up.is_proxy_auth() is expected
|
||||||
|
|
||||||
ctx.configure(up, proxyauth="foo:bar")
|
@pytest.mark.parametrize('is_proxy_auth, expected', [
|
||||||
assert up.singleuser == ["foo", "bar"]
|
(True, 'Proxy-Authorization'),
|
||||||
|
(False, 'Authorization'),
|
||||||
|
])
|
||||||
|
def test_which_auth_header(self, is_proxy_auth, expected):
|
||||||
|
up = proxyauth.ProxyAuth()
|
||||||
|
with mock.patch('mitmproxy.addons.proxyauth.ProxyAuth.is_proxy_auth', return_value=is_proxy_auth):
|
||||||
|
assert up.which_auth_header() == expected
|
||||||
|
|
||||||
ctx.configure(up, proxyauth=None)
|
@pytest.mark.parametrize('is_proxy_auth, expected_status_code, expected_header', [
|
||||||
assert up.singleuser is None
|
(True, 407, 'Proxy-Authenticate'),
|
||||||
|
(False, 401, 'WWW-Authenticate'),
|
||||||
|
])
|
||||||
|
def test_auth_required_response(self, is_proxy_auth, expected_status_code, expected_header):
|
||||||
|
up = proxyauth.ProxyAuth()
|
||||||
|
with mock.patch('mitmproxy.addons.proxyauth.ProxyAuth.is_proxy_auth', return_value=is_proxy_auth):
|
||||||
|
resp = up.auth_required_response()
|
||||||
|
assert resp.status_code == expected_status_code
|
||||||
|
assert expected_header in resp.headers.keys()
|
||||||
|
|
||||||
ctx.configure(up, proxyauth="any")
|
def test_check(self):
|
||||||
assert up.nonanonymous
|
|
||||||
ctx.configure(up, proxyauth=None)
|
|
||||||
assert not up.nonanonymous
|
|
||||||
|
|
||||||
with mock.patch('ldap3.Server', return_value="ldap://fake_server:389 - cleartext"):
|
|
||||||
with mock.patch('ldap3.Connection', return_value="test"):
|
|
||||||
ctx.configure(up, proxyauth="ldap:localhost:cn=default,dc=cdhdt,dc=com:password:ou=application,dc=cdhdt,dc=com")
|
|
||||||
assert up.ldapserver
|
|
||||||
ctx.configure(up, proxyauth="ldaps:localhost:cn=default,dc=cdhdt,dc=com:password:ou=application,dc=cdhdt,dc=com")
|
|
||||||
assert up.ldapserver
|
|
||||||
|
|
||||||
with pytest.raises(exceptions.OptionsError):
|
|
||||||
ctx.configure(up, proxyauth="ldap:test:test:test")
|
|
||||||
|
|
||||||
with pytest.raises(IndexError):
|
|
||||||
ctx.configure(up, proxyauth="ldap:fake_serveruid=?dc=example,dc=com:person")
|
|
||||||
|
|
||||||
with pytest.raises(exceptions.OptionsError):
|
|
||||||
ctx.configure(up, proxyauth="ldapssssssss:fake_server:dn:password:tree")
|
|
||||||
|
|
||||||
with pytest.raises(exceptions.OptionsError):
|
|
||||||
ctx.configure(
|
|
||||||
up,
|
|
||||||
proxyauth= "@" + tutils.test_data.path("mitmproxy/net/data/server.crt")
|
|
||||||
)
|
|
||||||
with pytest.raises(exceptions.OptionsError):
|
|
||||||
ctx.configure(up, proxyauth="@nonexistent")
|
|
||||||
|
|
||||||
ctx.configure(
|
|
||||||
up,
|
|
||||||
proxyauth= "@" + tutils.test_data.path(
|
|
||||||
"mitmproxy/net/data/htpasswd"
|
|
||||||
)
|
|
||||||
)
|
|
||||||
assert up.htpasswd
|
|
||||||
assert up.htpasswd.check_password("test", "test")
|
|
||||||
assert not up.htpasswd.check_password("test", "foo")
|
|
||||||
ctx.configure(up, proxyauth=None)
|
|
||||||
assert not up.htpasswd
|
|
||||||
|
|
||||||
with pytest.raises(exceptions.OptionsError):
|
|
||||||
ctx.configure(up, proxyauth="any", mode="transparent")
|
|
||||||
with pytest.raises(exceptions.OptionsError):
|
|
||||||
ctx.configure(up, proxyauth="any", mode="socks5")
|
|
||||||
|
|
||||||
|
|
||||||
def test_check(monkeypatch):
|
|
||||||
up = proxyauth.ProxyAuth()
|
up = proxyauth.ProxyAuth()
|
||||||
with taddons.context() as ctx:
|
with taddons.context() as ctx:
|
||||||
ctx.configure(up, proxyauth="any", mode="regular")
|
ctx.configure(up, proxyauth="any", mode="regular")
|
||||||
@ -142,8 +131,7 @@ def test_check(monkeypatch):
|
|||||||
)
|
)
|
||||||
assert not up.check(f)
|
assert not up.check(f)
|
||||||
|
|
||||||
|
def test_authenticate(self):
|
||||||
def test_authenticate():
|
|
||||||
up = proxyauth.ProxyAuth()
|
up = proxyauth.ProxyAuth()
|
||||||
with taddons.context() as ctx:
|
with taddons.context() as ctx:
|
||||||
ctx.configure(up, proxyauth="any", mode="regular")
|
ctx.configure(up, proxyauth="any", mode="regular")
|
||||||
@ -175,8 +163,65 @@ def test_authenticate():
|
|||||||
assert not f.response
|
assert not f.response
|
||||||
assert not f.request.headers.get("Authorization")
|
assert not f.request.headers.get("Authorization")
|
||||||
|
|
||||||
|
def test_configure(self):
|
||||||
|
up = proxyauth.ProxyAuth()
|
||||||
|
with taddons.context() as ctx:
|
||||||
|
with pytest.raises(exceptions.OptionsError):
|
||||||
|
ctx.configure(up, proxyauth="foo")
|
||||||
|
|
||||||
def test_handlers():
|
ctx.configure(up, proxyauth="foo:bar")
|
||||||
|
assert up.singleuser == ["foo", "bar"]
|
||||||
|
|
||||||
|
ctx.configure(up, proxyauth=None)
|
||||||
|
assert up.singleuser is None
|
||||||
|
|
||||||
|
ctx.configure(up, proxyauth="any")
|
||||||
|
assert up.nonanonymous
|
||||||
|
ctx.configure(up, proxyauth=None)
|
||||||
|
assert not up.nonanonymous
|
||||||
|
|
||||||
|
with mock.patch('ldap3.Server', return_value="ldap://fake_server:389 - cleartext"):
|
||||||
|
with mock.patch('ldap3.Connection', return_value="test"):
|
||||||
|
ctx.configure(up, proxyauth="ldap:localhost:cn=default,dc=cdhdt,dc=com:password:ou=application,dc=cdhdt,dc=com")
|
||||||
|
assert up.ldapserver
|
||||||
|
ctx.configure(up, proxyauth="ldaps:localhost:cn=default,dc=cdhdt,dc=com:password:ou=application,dc=cdhdt,dc=com")
|
||||||
|
assert up.ldapserver
|
||||||
|
|
||||||
|
with pytest.raises(exceptions.OptionsError):
|
||||||
|
ctx.configure(up, proxyauth="ldap:test:test:test")
|
||||||
|
|
||||||
|
with pytest.raises(IndexError):
|
||||||
|
ctx.configure(up, proxyauth="ldap:fake_serveruid=?dc=example,dc=com:person")
|
||||||
|
|
||||||
|
with pytest.raises(exceptions.OptionsError):
|
||||||
|
ctx.configure(up, proxyauth="ldapssssssss:fake_server:dn:password:tree")
|
||||||
|
|
||||||
|
with pytest.raises(exceptions.OptionsError):
|
||||||
|
ctx.configure(
|
||||||
|
up,
|
||||||
|
proxyauth= "@" + tutils.test_data.path("mitmproxy/net/data/server.crt")
|
||||||
|
)
|
||||||
|
with pytest.raises(exceptions.OptionsError):
|
||||||
|
ctx.configure(up, proxyauth="@nonexistent")
|
||||||
|
|
||||||
|
ctx.configure(
|
||||||
|
up,
|
||||||
|
proxyauth= "@" + tutils.test_data.path(
|
||||||
|
"mitmproxy/net/data/htpasswd"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
assert up.htpasswd
|
||||||
|
assert up.htpasswd.check_password("test", "test")
|
||||||
|
assert not up.htpasswd.check_password("test", "foo")
|
||||||
|
ctx.configure(up, proxyauth=None)
|
||||||
|
assert not up.htpasswd
|
||||||
|
|
||||||
|
with pytest.raises(exceptions.OptionsError):
|
||||||
|
ctx.configure(up, proxyauth="any", mode="transparent")
|
||||||
|
with pytest.raises(exceptions.OptionsError):
|
||||||
|
ctx.configure(up, proxyauth="any", mode="socks5")
|
||||||
|
|
||||||
|
def test_handlers(self):
|
||||||
up = proxyauth.ProxyAuth()
|
up = proxyauth.ProxyAuth()
|
||||||
with taddons.context() as ctx:
|
with taddons.context() as ctx:
|
||||||
ctx.configure(up, proxyauth="any", mode="regular")
|
ctx.configure(up, proxyauth="any", mode="regular")
|
||||||
|
Loading…
Reference in New Issue
Block a user