Skip to content

Commit

Permalink
tests: pass config to constructor instead of configuring after
Browse files Browse the repository at this point in the history
  • Loading branch information
consideRatio committed Sep 23, 2024
1 parent 5ba6aa1 commit 4cf07bc
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 43 deletions.
32 changes: 18 additions & 14 deletions ldapauthenticator/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
import os

import pytest

from ..ldapauthenticator import LDAPAuthenticator
from traitlets.config import Config


@pytest.fixture()
def authenticator():
authenticator = LDAPAuthenticator()
authenticator.server_address = os.environ.get("LDAP_HOST", "localhost")
authenticator.lookup_dn = True
authenticator.bind_dn_template = "cn={username},ou=people,dc=planetexpress,dc=com"
authenticator.user_search_base = "ou=people,dc=planetexpress,dc=com"
authenticator.user_attribute = "uid"
authenticator.lookup_dn_user_dn_attribute = "cn"
authenticator.attributes = ["uid", "cn", "mail", "ou"]
authenticator.use_lookup_dn_username = False
def c():
"""
A base configuration for LDAPAuthenticator that individual tests can adjust.
"""
c = Config()
c.LDAPAuthenticator.server_address = os.environ.get("LDAP_HOST", "localhost")
c.LDAPAuthenticator.lookup_dn = True
c.LDAPAuthenticator.bind_dn_template = (
"cn={username},ou=people,dc=planetexpress,dc=com"
)
c.LDAPAuthenticator.user_search_base = "ou=people,dc=planetexpress,dc=com"
c.LDAPAuthenticator.user_attribute = "uid"
c.LDAPAuthenticator.lookup_dn_user_dn_attribute = "cn"
c.LDAPAuthenticator.attributes = ["uid", "cn", "mail", "ou"]
c.LDAPAuthenticator.use_lookup_dn_username = False

authenticator.allowed_groups = [
c.LDAPAuthenticator.allowed_groups = [
"cn=admin_staff,ou=people,dc=planetexpress,dc=com",
"cn=ship_crew,ou=people,dc=planetexpress,dc=com",
]

return authenticator
return c
79 changes: 50 additions & 29 deletions ldapauthenticator/tests/test_ldapauthenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@
import pytest
from ldap3.core.exceptions import LDAPSSLConfigurationError

from ..ldapauthenticator import TlsStrategy
from ..ldapauthenticator import LDAPAuthenticator, TlsStrategy


async def test_ldap_auth_allowed(authenticator):
async def test_ldap_auth_allowed(c):
authenticator = LDAPAuthenticator(config=c)
# proper username and password in allowed group
authorized = await authenticator.get_authenticated_user(
None, {"username": "fry", "password": "fry"}
)
assert authorized["name"] == "fry"


async def test_ldap_auth_disallowed(authenticator):
async def test_ldap_auth_disallowed(c):
authenticator = LDAPAuthenticator(config=c)
# invalid username
authorized = await authenticator.get_authenticated_user(
None, {"username": "3fry/", "password": "raw"}
Expand Down Expand Up @@ -51,8 +53,12 @@ async def test_ldap_auth_disallowed(authenticator):
assert authorized is None


async def test_ldap_auth_blank_template(authenticator):
authenticator.bind_dn_template = [authenticator.bind_dn_template, ""]
async def test_ldap_auth_blank_template(c):
c.LDAPAuthenticator.bind_dn_template = [
"cn={username},ou=people,dc=planetexpress,dc=com",
"",
]
authenticator = LDAPAuthenticator(config=c)

# proper username and password in allowed group
authorized = await authenticator.get_authenticated_user(
Expand All @@ -61,7 +67,8 @@ async def test_ldap_auth_blank_template(authenticator):
assert authorized["name"] == "fry"


async def test_ldap_use_ssl_deprecation(authenticator):
async def test_ldap_use_ssl_deprecation(c):
authenticator = LDAPAuthenticator(config=c)
assert authenticator.tls_strategy == TlsStrategy.before_bind

# setting use_ssl to True should result in tls_strategy being set to
Expand All @@ -70,12 +77,13 @@ async def test_ldap_use_ssl_deprecation(authenticator):
assert authenticator.tls_strategy == TlsStrategy.on_connect


async def test_ldap_auth_tls_strategy_on_connect(authenticator):
async def test_ldap_auth_tls_strategy_on_connect(c):
"""
Verifies basic function of the authenticator with a given tls_strategy
without actually confirming use of that strategy.
"""
authenticator.tls_strategy = "on_connect"
c.LDAPAuthenticator.tls_strategy = "on_connect"
authenticator = LDAPAuthenticator(config=c)

# proper username and password in allowed group
authorized = await authenticator.get_authenticated_user(
Expand All @@ -84,12 +92,13 @@ async def test_ldap_auth_tls_strategy_on_connect(authenticator):
assert authorized["name"] == "fry"


async def test_ldap_auth_tls_strategy_insecure(authenticator):
async def test_ldap_auth_tls_strategy_insecure(c):
"""
Verifies basic function of the authenticator with a given tls_strategy
without actually confirming use of that strategy.
"""
authenticator.tls_strategy = "insecure"
c.LDAPAuthenticator.tls_strategy = "insecure"
authenticator = LDAPAuthenticator(config=c)

# proper username and password in allowed group
authorized = await authenticator.get_authenticated_user(
Expand All @@ -98,8 +107,9 @@ async def test_ldap_auth_tls_strategy_insecure(authenticator):
assert authorized["name"] == "fry"


async def test_ldap_auth_use_lookup_dn(authenticator):
authenticator.use_lookup_dn_username = True
async def test_ldap_auth_use_lookup_dn(c):
c.LDAPAuthenticator.use_lookup_dn_username = True
authenticator = LDAPAuthenticator(config=c)

# proper username and password in allowed group
authorized = await authenticator.get_authenticated_user(
Expand All @@ -108,12 +118,13 @@ async def test_ldap_auth_use_lookup_dn(authenticator):
assert authorized["name"] == "philip j. fry"


async def test_ldap_auth_search_filter(authenticator):
authenticator.allowed_groups = []
authenticator.allow_all = True
authenticator.search_filter = (
async def test_ldap_auth_search_filter(c):
c.LDAPAuthenticator.allowed_groups = []
c.LDAPAuthenticator.allow_all = True
c.LDAPAuthenticator.search_filter = (
"(&(objectClass=inetOrgPerson)(ou= Delivering Crew)(cn={username}))"
)
authenticator = LDAPAuthenticator(config=c)

# proper username and password in allowed group
authorized = await authenticator.get_authenticated_user(
Expand All @@ -129,12 +140,16 @@ async def test_ldap_auth_search_filter(authenticator):
assert authorized is None


async def test_allow_config(authenticator):
# test various sources of allow config

async def test_allow_config(c):
"""
test various sources of allow config
"""
# this group allows fry, leela, bender
authenticator.allowed_groups = ["cn=ship_crew,ou=people,dc=planetexpress,dc=com"]
authenticator.allowed_users = {"zoidberg"}
c.LDAPAuthenticator.allowed_groups = [
"cn=ship_crew,ou=people,dc=planetexpress,dc=com"
]
c.LDAPAuthenticator.allowed_users = {"zoidberg"}
authenticator = LDAPAuthenticator(config=c)

# in allowed_groups
authorized = await authenticator.get_authenticated_user(
Expand Down Expand Up @@ -169,8 +184,10 @@ async def test_allow_config(authenticator):
assert authorized["name"] == "professor"


async def test_ldap_auth_state_attributes(authenticator):
authenticator.auth_state_attributes = ["employeeType"]
async def test_ldap_auth_state_attributes(c):
c.LDAPAuthenticator.auth_state_attributes = ["employeeType"]
authenticator = LDAPAuthenticator(config=c)

# proper username and password in allowed group
authorized = await authenticator.get_authenticated_user(
None, {"username": "fry", "password": "fry"}
Expand All @@ -181,10 +198,12 @@ async def test_ldap_auth_state_attributes(authenticator):
}


async def test_ldap_auth_state_attributes2(authenticator):
authenticator.group_search_filter = "(cn=ship_crew)"
authenticator.group_attributes = ["cn"]
authenticator.auth_state_attributes = ["description"]
async def test_ldap_auth_state_attributes2(c):
c.LDAPAuthenticator.group_search_filter = "(cn=ship_crew)"
c.LDAPAuthenticator.group_attributes = ["cn"]
c.LDAPAuthenticator.auth_state_attributes = ["description"]
authenticator = LDAPAuthenticator(config=c)

# proper username and password in allowed group
authorized = await authenticator.get_authenticated_user(
None, {"username": "leela", "password": "leela"}
Expand All @@ -193,14 +212,16 @@ async def test_ldap_auth_state_attributes2(authenticator):
assert authorized["auth_state"]["user_attributes"] == {"description": ["Mutant"]}


async def test_ldap_tls_kwargs_config_passthrough(authenticator):
async def test_ldap_tls_kwargs_config_passthrough(c):
"""
This test is just meant to verify that tls_kwargs is passed through to the
ldap3 Tls object when its constructed.
"""
authenticator.tls_kwargs = {
c.LDAPAuthenticator.tls_kwargs = {
"ca_certs_file": "does-not-exist-so-error-expected",
}
authenticator = LDAPAuthenticator(config=c)

with pytest.raises(LDAPSSLConfigurationError):
await authenticator.get_authenticated_user(
None, {"username": "leela", "password": "leela"}
Expand Down

0 comments on commit 4cf07bc

Please sign in to comment.