Skip to content

Commit

Permalink
Add more unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Aurélien Bompard <aurelien@bompard.org>
  • Loading branch information
abompard committed Apr 28, 2020
1 parent 24b9364 commit d76bb64
Show file tree
Hide file tree
Showing 10 changed files with 431 additions and 124 deletions.
17 changes: 17 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[run]
branch = True
source =
fasjson

[paths]
source =
fasjson
.tox/*/site-packages/fasjson

[report]
fail_under = 100
exclude_lines =
pragma: no cover
if __name__ == .__main__.:
omit =
fasjson/tests/*
7 changes: 7 additions & 0 deletions fasjson/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ def client(app_config, gss_env):
yield client


@pytest.fixture
def anon_client(app_config):
app.test_client_class = FlaskClient
with app.test_client() as client:
yield client


@pytest.fixture
def gss_user(gss_env, mocker):
creds = mocker.patch("gssapi.Credentials")
Expand Down
124 changes: 16 additions & 108 deletions fasjson/tests/unit/test_lib_ldap.py
Original file line number Diff line number Diff line change
@@ -1,117 +1,25 @@
import types
import datetime

import pytest
from ldap.controls.pagedresults import SimplePagedResultsControl

from fasjson.lib.ldap.client import LDAP, LDAPResult
from fasjson.lib.ldap import get_client, __ldap


@pytest.fixture
def mock_connection(mocker):
conn_factory = mocker.patch("ldap.ldapobject.ReconnectLDAPObject")
return_value = types.SimpleNamespace(
protocol_version=3,
set_option=lambda a, b: a,
search_ext=lambda *a, **kw: 1,
# sasl_interactive_bind_s=lambda s, n: "",
sasl_gssapi_bind_s=lambda authz_id: "",
)
conn_factory.return_value = return_value
yield return_value


def _single_page_result_factory(result):
def _result(msgid, resp_ctrl_classes=""):
return (
101,
[("", item) for item in result],
1,
[SimplePagedResultsControl(True, size=len(result), cookie="")],
)

return _result


def test_whoami(mock_connection):
r = (
"dn: krbprincipalname=http/fasjson.example.test@example.test,"
"cn=services,cn=accounts,dc=example,dc=test"
)
mock_connection.whoami_s = lambda: r

ldap = LDAP("https://dummy.com", basedn="dc=example,dc=test")

expected = {"dn": r[4:]}
assert expected == ldap.whoami()


def test_get_groups(mock_connection):
mocked = [
{"cn": [b"admins"]},
{"cn": [b"ipausers"]},
{"cn": [b"editors"]},
{"cn": [b"trust admins"]},
]
mock_connection.result3 = _single_page_result_factory(mocked)

ldap = LDAP("https://dummy.com", basedn="dc=example,dc=test")

result = ldap.get_groups(page_number=1, page_size=0)
expected = LDAPResult(
items=[
{"name": "admins"},
{"name": "ipausers"},
{"name": "editors"},
{"name": "trust admins"},
],
total=4,
page_size=0,
page_number=1,
)
assert result == expected


def test_get_group_members(mock_connection):
mocked = [{"uid": [b"admin"]}]
mock_connection.result3 = _single_page_result_factory(mocked)

ldap = LDAP("https://dummy.com", basedn="dc=example,dc=test")

result = ldap.get_group_members("admins", page_number=1, page_size=0)
expected = LDAPResult(
items=[{"username": "admin"}], total=1, page_size=0, page_number=1
)
assert result == expected
def cleanup_cache():
yield
__ldap.clear()


def test_get_user(mock_connection):
mocked = [
{
"uid": [b"admin"],
"sn": [b"Administrator"],
"givenName": [b""],
"mail": [b"admin@example.test"],
"fasIRCNick": [b"admin"],
# "fasLocale": None,
"fasTimeZone": [b"UTC"],
# "fasGPGKeyId": None,
"fasCreationTime": [b"20200309103203Z"], # %Y%m%d%H%M%SZ
"nsAccountLock": [b"false"],
}
]
mock_connection.result3 = _single_page_result_factory(mocked)
def test_reuse(mocker, cleanup_cache):
LDAP = mocker.patch("fasjson.lib.ldap.LDAP")
LDAP.side_effect = ["client1", "client2"]
client_1 = get_client("ldap://", "dc=example,dc=test", "dummy")
client_2 = get_client("ldap://", "dc=example,dc=test", "dummy")
assert client_1 is client_2

ldap = LDAP("https://dummy.com", basedn="dc=example,dc=test")

expected = {
"creation": datetime.datetime(2020, 3, 9, 10, 32, 3),
"givenname": "",
"ircnick": ["admin"],
"locked": False,
"username": "admin",
"emails": ["admin@example.test"],
"surname": "Administrator",
"timezone": "UTC",
}
assert expected == ldap.get_user("admin")
def test_dont_reuse_different_users(mocker, cleanup_cache):
LDAP = mocker.patch("fasjson.lib.ldap.LDAP")
LDAP.side_effect = ["client1", "client2"]
client_1 = get_client("ldap://", "dc=example,dc=test", "dummy-1")
client_2 = get_client("ldap://", "dc=example,dc=test", "dummy-2")
assert client_1 is not client_2
Loading

0 comments on commit d76bb64

Please sign in to comment.