Skip to content

Commit

Permalink
convert from unittest to pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
cognifloyd committed Oct 22, 2024
1 parent 96a515e commit e1c1a99
Showing 1 changed file with 39 additions and 37 deletions.
76 changes: 39 additions & 37 deletions tests/unit/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,21 @@
]


class LDAPBackendTest(unittest.TestCase):
class LDAPBackendTest:

def test_instantaite_no_group_dns_provided(self):
# User is member of two of the groups, but none of them are required
required_group_dns = []
expected_msg = 'One or more user groups must be specified'
self.assertRaisesRegexp(ValueError, expected_msg, ldap_backend.LDAPAuthenticationBackend,
LDAP_BIND_DN,
LDAP_BIND_PASSWORD,
LDAP_BASE_OU,
required_group_dns,
LDAP_HOST,
id_attr=LDAP_ID_ATTR)
with pytest.raises(ValueError, match=expected_msg):
ldap_backend.LDAPAuthenticationBackend(
LDAP_BIND_DN,
LDAP_BIND_PASSWORD,
LDAP_BASE_OU,
required_group_dns,
LDAP_HOST,
id_attr=LDAP_ID_ATTR,
)

@pytest.mark.parametrize("required_group_dns", LDAP_GROUP_DNS_CASES)
@mock.patch.object(
Expand All @@ -119,7 +121,7 @@ def test_authenticate(self, required_group_dns):
)

authenticated = backend.authenticate(LDAP_USER_UID, LDAP_USER_PASSWD)
self.assertTrue(authenticated)
assert authenticated

@mock.patch.object(
ldap.ldapobject.SimpleLDAPObject, 'simple_bind_s',
Expand All @@ -139,7 +141,7 @@ def test_authenticate_with_multiple_ldap_hosts(self, required_group_dns):
)

authenticated = backend.authenticate(LDAP_USER_UID, LDAP_USER_PASSWD)
self.assertTrue(authenticated)
assert authenticated

@mock.patch.object(
ldap.ldapobject.SimpleLDAPObject, 'simple_bind_s',
Expand All @@ -158,7 +160,7 @@ def test_authenticate_without_password(self, required_group_dns):
id_attr=LDAP_ID_ATTR
)

with self.assertRaises(ValueError):
with pytest.raises(ValueError):
backend.authenticate(LDAP_USER_UID, '')

@mock.patch.object(
Expand All @@ -176,7 +178,7 @@ def test_authenticate_failure_bad_bind_cred(self, required_group_dns):
)

authenticated = backend.authenticate(LDAP_USER_UID, LDAP_USER_BAD_PASSWD)
self.assertFalse(authenticated)
assert not authenticated

@mock.patch.object(
ldap.ldapobject.SimpleLDAPObject, 'simple_bind_s',
Expand All @@ -196,7 +198,7 @@ def test_authenticate_failure_bad_user_password(self, required_group_dns):
)

authenticated = backend.authenticate(LDAP_USER_UID, LDAP_USER_BAD_PASSWD)
self.assertFalse(authenticated)
assert not authenticated

@mock.patch.object(
ldap.ldapobject.SimpleLDAPObject, 'simple_bind_s',
Expand All @@ -218,7 +220,7 @@ def test_authenticate_failure_non_group_member_no_groups(self, required_group_dn
)

authenticated = backend.authenticate(LDAP_USER_UID, LDAP_USER_BAD_PASSWD)
self.assertFalse(authenticated)
assert not authenticated

backend = ldap_backend.LDAPAuthenticationBackend(
LDAP_BIND_DN,
Expand All @@ -231,7 +233,7 @@ def test_authenticate_failure_non_group_member_no_groups(self, required_group_dn
)

authenticated = backend.authenticate(LDAP_USER_UID, LDAP_USER_BAD_PASSWD)
self.assertFalse(authenticated)
assert not authenticated

@mock.patch.object(
ldap.ldapobject.SimpleLDAPObject, 'simple_bind_s',
Expand All @@ -254,7 +256,7 @@ def test_authenticatefailure_non_group_member_non_required_group(self, required_
)

authenticated = backend.authenticate(LDAP_USER_UID, LDAP_USER_BAD_PASSWD)
self.assertFalse(authenticated)
assert not authenticated

backend = ldap_backend.LDAPAuthenticationBackend(
LDAP_BIND_DN,
Expand All @@ -267,7 +269,7 @@ def test_authenticatefailure_non_group_member_non_required_group(self, required_
)

authenticated = backend.authenticate(LDAP_USER_UID, LDAP_USER_BAD_PASSWD)
self.assertFalse(authenticated)
assert not authenticated

@mock.patch.object(
ldap.ldapobject.SimpleLDAPObject, 'simple_bind_s',
Expand Down Expand Up @@ -296,7 +298,7 @@ def test_authenticate_and_behavior_failure_non_group_member_of_all_required_grou
)

authenticated = backend.authenticate(LDAP_USER_UID, LDAP_USER_BAD_PASSWD)
self.assertFalse(authenticated)
assert not authenticated

@mock.patch.object(
ldap.ldapobject.SimpleLDAPObject, 'simple_bind_s',
Expand All @@ -323,7 +325,7 @@ def test_authenticate_and_behavior_failure_non_group_member_of_all_required_grou
)

authenticated = backend.authenticate(LDAP_USER_UID, LDAP_USER_BAD_PASSWD)
self.assertFalse(authenticated)
assert not authenticated

@mock.patch.object(
ldap.ldapobject.SimpleLDAPObject, 'simple_bind_s',
Expand Down Expand Up @@ -355,7 +357,7 @@ def test_authenticate_and_behavior_failure_non_group_member_of_all_required_grou
)

authenticated = backend.authenticate(LDAP_USER_UID, LDAP_USER_BAD_PASSWD)
self.assertFalse(authenticated)
assert not authenticated

@mock.patch.object(
ldap.ldapobject.SimpleLDAPObject, 'simple_bind_s',
Expand Down Expand Up @@ -387,7 +389,7 @@ def test_authenticate_and_is_default_behavior_non_group_member_of_all_required_g
)

authenticated = backend.authenticate(LDAP_USER_UID, LDAP_USER_BAD_PASSWD)
self.assertFalse(authenticated)
assert not authenticated

@mock.patch.object(
ldap.ldapobject.SimpleLDAPObject, 'simple_bind_s',
Expand All @@ -412,7 +414,7 @@ def test_authenticate_or_behavior_success_member_of_single_group_1(self):
)

authenticated = backend.authenticate(LDAP_USER_UID, LDAP_USER_BAD_PASSWD)
self.assertTrue(authenticated)
assert authenticated

@mock.patch.object(
ldap.ldapobject.SimpleLDAPObject, 'simple_bind_s',
Expand Down Expand Up @@ -440,7 +442,7 @@ def test_authenticate_or_behavior_success_member_of_single_group_2(self):
)

authenticated = backend.authenticate(LDAP_USER_UID, LDAP_USER_BAD_PASSWD)
self.assertTrue(authenticated)
assert authenticated

@mock.patch.object(
ldap.ldapobject.SimpleLDAPObject, 'simple_bind_s',
Expand Down Expand Up @@ -468,7 +470,7 @@ def test_authenticate_or_behavior_success_member_of_single_group_2b(self):
)

authenticated = backend.authenticate(LDAP_USER_UID, LDAP_USER_BAD_PASSWD)
self.assertTrue(authenticated)
assert authenticated

@mock.patch.object(
ldap.ldapobject.SimpleLDAPObject, 'simple_bind_s',
Expand Down Expand Up @@ -498,7 +500,7 @@ def test_authenticate_or_behavior_success_member_of_multiple_groups_1(self):
)

authenticated = backend.authenticate(LDAP_USER_UID, LDAP_USER_BAD_PASSWD)
self.assertTrue(authenticated)
assert authenticated

@mock.patch.object(
ldap.ldapobject.SimpleLDAPObject, 'simple_bind_s',
Expand All @@ -525,7 +527,7 @@ def test_authenticate_or_behavior_success_member_of_multiple_groups_2(self):
)

authenticated = backend.authenticate(LDAP_USER_UID, LDAP_USER_BAD_PASSWD)
self.assertTrue(authenticated)
assert authenticated

@mock.patch.object(
ldap.ldapobject.SimpleLDAPObject, 'simple_bind_s',
Expand All @@ -552,7 +554,7 @@ def test_authenticate_or_behavior_success_member_of_multiple_groups_3(self):
)

authenticated = backend.authenticate(LDAP_USER_UID, LDAP_USER_BAD_PASSWD)
self.assertTrue(authenticated)
assert authenticated

@mock.patch.object(
ldap.ldapobject.SimpleLDAPObject, 'simple_bind_s',
Expand Down Expand Up @@ -580,7 +582,7 @@ def test_authenticate_or_behavior_success_member_of_multiple_groups_3b(self):
)

authenticated = backend.authenticate(LDAP_USER_UID, LDAP_USER_BAD_PASSWD)
self.assertTrue(authenticated)
assert authenticated

@mock.patch.object(
ldap.ldapobject.SimpleLDAPObject, 'simple_bind_s',
Expand All @@ -602,7 +604,7 @@ def test_ssl_authenticate(self, required_group_dns):
)

authenticated = backend.authenticate(LDAP_USER_UID, LDAP_USER_PASSWD)
self.assertTrue(authenticated)
assert authenticated

@mock.patch.object(
ldap.ldapobject.SimpleLDAPObject, 'simple_bind_s',
Expand All @@ -624,7 +626,7 @@ def test_ssl_authenticate_failure(self, required_group_dns):
)

authenticated = backend.authenticate(LDAP_USER_UID, LDAP_USER_BAD_PASSWD)
self.assertFalse(authenticated)
assert not authenticated

@mock.patch.object(
ldap.ldapobject.SimpleLDAPObject, 'simple_bind_s',
Expand All @@ -647,7 +649,7 @@ def test_ssl_authenticate_validate_cert(self, required_group_dns):
)

authenticated = backend.authenticate(LDAP_USER_UID, LDAP_USER_PASSWD)
self.assertTrue(authenticated)
assert authenticated

@mock.patch.object(
ldap.ldapobject.SimpleLDAPObject, 'start_tls_s',
Expand All @@ -671,7 +673,7 @@ def test_tls_authenticate(self, required_group_dns):
)

authenticated = backend.authenticate(LDAP_USER_UID, LDAP_USER_PASSWD)
self.assertTrue(authenticated)
assert authenticated

@mock.patch.object(
ldap.ldapobject.SimpleLDAPObject, 'start_tls_s',
Expand All @@ -695,7 +697,7 @@ def test_tls_authenticate_failure(self, required_group_dns):
)

authenticated = backend.authenticate(LDAP_USER_UID, LDAP_USER_BAD_PASSWD)
self.assertFalse(authenticated)
assert not authenticated

@mock.patch.object(
ldap.ldapobject.SimpleLDAPObject, 'start_tls_s',
Expand All @@ -720,7 +722,7 @@ def test_tls_authenticate_validate_cert(self, required_group_dns):
)

authenticated = backend.authenticate(LDAP_USER_UID, LDAP_USER_PASSWD)
self.assertTrue(authenticated)
assert authenticated

@mock.patch.object(
ldap.ldapobject.SimpleLDAPObject, 'simple_bind_s',
Expand Down Expand Up @@ -863,7 +865,7 @@ def test_authenticate_and_get_user_groups_caching_disabled(self):
self.assertEqual(ldap.ldapobject.SimpleLDAPObject.search_s.call_count, 0)

authenticated = backend.authenticate(LDAP_USER_UID, LDAP_USER_BAD_PASSWD)
self.assertTrue(authenticated)
assert authenticated

# 1 for user dn search, 1 for groups search
self.assertEqual(ldap.ldapobject.SimpleLDAPObject.search_s.call_count, 2)
Expand Down Expand Up @@ -902,7 +904,7 @@ def test_authenticate_and_get_user_groups_caching_enabled(self):
self.assertEqual(ldap.ldapobject.SimpleLDAPObject.search_s.call_count, 0)

authenticated = backend.authenticate(LDAP_USER_UID, LDAP_USER_BAD_PASSWD)
self.assertTrue(authenticated)
assert authenticated
self.assertEqual(ldap.ldapobject.SimpleLDAPObject.search_s.call_count, 2)

user_groups = backend.get_user_groups(username=LDAP_USER_UID)
Expand Down Expand Up @@ -1089,4 +1091,4 @@ def test_get_groups_caching_cache_ttl(self):

# Cache should now be empty
time.sleep(1.5)
self.assertFalse(LDAP_USER_UID in backend._user_groups_cache)
assert LDAP_USER_UID not in backend._user_groups_cache

0 comments on commit e1c1a99

Please sign in to comment.