Skip to content

Commit

Permalink
massively speed up tests by properly mocking ldap proxyauth
Browse files Browse the repository at this point in the history
  • Loading branch information
mhils committed Dec 19, 2020
1 parent b05c13d commit 2f725e5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 44 deletions.
4 changes: 3 additions & 1 deletion mitmproxy/addons/proxyauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,15 @@ def configure(self, updated):
auto_bind=True)
self.ldapconn = conn
self.ldapserver = server
else:
elif ":" in ctx.options.proxyauth:
parts = ctx.options.proxyauth.split(':')
if len(parts) != 2:
raise exceptions.OptionsError(
"Invalid single-user auth specification."
)
self.singleuser = parts
else:
raise exceptions.OptionsError("Invalid proxyauth specification.")
if self.enabled():
if ctx.options.mode == "transparent":
raise exceptions.OptionsError(
Expand Down
98 changes: 55 additions & 43 deletions test/mitmproxy/addons/test_proxyauth.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import binascii
from unittest import mock

import ldap3
import pytest

from unittest import mock
from mitmproxy import exceptions
from mitmproxy.addons import proxyauth
from mitmproxy.test import taddons
Expand Down Expand Up @@ -162,63 +163,74 @@ def test_authenticate(self):
assert not f.response
assert not f.request.headers.get("Authorization")

def test_configure(self, tdata):
up = proxyauth.ProxyAuth()
with taddons.context(up) as ctx:
with pytest.raises(exceptions.OptionsError):
ctx.configure(up, proxyauth="foo")
def test_configure(self, monkeypatch, tdata):
monkeypatch.setattr(ldap3, "Server", lambda *_, **__: True)
monkeypatch.setattr(ldap3, "Connection", lambda *_, **__: True)

ctx.configure(up, proxyauth="foo:bar")
assert up.singleuser == ["foo", "bar"]
pa = proxyauth.ProxyAuth()
with taddons.context(pa) as ctx:
with pytest.raises(exceptions.OptionsError, match="Invalid proxyauth specification"):
ctx.configure(pa, proxyauth="foo")

ctx.configure(up, proxyauth=None)
assert up.singleuser is None
with pytest.raises(exceptions.OptionsError, match="Invalid single-user auth specification."):
ctx.configure(pa, proxyauth="foo:bar:baz")

ctx.configure(up, proxyauth="any")
assert up.nonanonymous
ctx.configure(up, proxyauth=None)
assert not up.nonanonymous
ctx.configure(pa, proxyauth="foo:bar")
assert pa.singleuser == ["foo", "bar"]

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
ctx.configure(pa, proxyauth=None)
assert pa.singleuser is None

with pytest.raises(exceptions.OptionsError):
ctx.configure(up, proxyauth="ldap:test:test:test")
ctx.configure(pa, proxyauth="any")
assert pa.nonanonymous
ctx.configure(pa, proxyauth=None)
assert not pa.nonanonymous

with pytest.raises(exceptions.OptionsError):
ctx.configure(up, proxyauth="ldap:fake_serveruid=?dc=example,dc=com:person")
ctx.configure(
pa,
proxyauth="ldap:localhost:cn=default,dc=cdhdt,dc=com:password:ou=application,dc=cdhdt,dc=com"
)
assert pa.ldapserver
ctx.configure(
pa,
proxyauth="ldaps:localhost:cn=default,dc=cdhdt,dc=com:password:ou=application,dc=cdhdt,dc=com"
)
assert pa.ldapserver

with pytest.raises(exceptions.OptionsError, match="Invalid ldap specification"):
ctx.configure(pa, proxyauth="ldap:test:test:test")

with pytest.raises(exceptions.OptionsError):
ctx.configure(up, proxyauth="ldapssssssss:fake_server:dn:password:tree")
with pytest.raises(exceptions.OptionsError, match="Invalid ldap specification"):
ctx.configure(pa, proxyauth="ldap:fake_serveruid=?dc=example,dc=com:person")

with pytest.raises(exceptions.OptionsError):
with pytest.raises(exceptions.OptionsError, match="Invalid ldap specification"):
ctx.configure(pa, proxyauth="ldapssssssss:fake_server:dn:password:tree")

with pytest.raises(exceptions.OptionsError, match="Could not open htpasswd file"):
ctx.configure(
up,
proxyauth= "@" + tdata.path("mitmproxy/net/data/server.crt")
pa,
proxyauth="@" + tdata.path("mitmproxy/net/data/server.crt")
)
with pytest.raises(exceptions.OptionsError):
ctx.configure(up, proxyauth="@nonexistent")
with pytest.raises(exceptions.OptionsError, match="Could not open htpasswd file"):
ctx.configure(pa, proxyauth="@nonexistent")

ctx.configure(
up,
proxyauth= "@" + tdata.path(
pa,
proxyauth="@" + tdata.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")
assert pa.htpasswd
assert pa.htpasswd.check_password("test", "test")
assert not pa.htpasswd.check_password("test", "foo")
ctx.configure(pa, proxyauth=None)
assert not pa.htpasswd

with pytest.raises(exceptions.OptionsError,
match="Proxy Authentication not supported in transparent mode."):
ctx.configure(pa, proxyauth="any", mode="transparent")
with pytest.raises(exceptions.OptionsError, match="Proxy Authentication not supported in SOCKS mode."):
ctx.configure(pa, proxyauth="any", mode="socks5")

def test_handlers(self):
up = proxyauth.ProxyAuth()
Expand Down

0 comments on commit 2f725e5

Please sign in to comment.