authentik.sources.ldap.tests.test_auth
LDAP Source tests
1"""LDAP Source tests""" 2 3from unittest.mock import MagicMock, Mock, patch 4 5from django.db.models import Q 6from django.test import TestCase 7 8from authentik.blueprints.tests import apply_blueprint 9from authentik.core.models import User 10from authentik.lib.generators import generate_key 11from authentik.sources.ldap.auth import LDAPBackend 12from authentik.sources.ldap.models import ( 13 LDAPSource, 14 LDAPSourceBindMethod, 15 LDAPSourcePropertyMapping, 16) 17from authentik.sources.ldap.sync.users import UserLDAPSynchronizer 18from authentik.sources.ldap.tests.mock_ad import mock_ad_connection 19from authentik.sources.ldap.tests.mock_slapd import mock_slapd_connection 20from authentik.tasks.models import Task 21 22LDAP_PASSWORD = generate_key() 23 24 25class LDAPSyncTests(TestCase): 26 """LDAP Sync tests""" 27 28 @apply_blueprint("system/sources-ldap.yaml") 29 def setUp(self): 30 self.source = LDAPSource.objects.create( 31 name="ldap", 32 slug="ldap", 33 base_dn="dc=goauthentik,dc=io", 34 additional_user_dn="ou=users", 35 additional_group_dn="ou=groups", 36 ) 37 38 def test_auth_direct_user_ad(self): 39 """Test direct auth""" 40 self.source.base_dn = "dc=t,dc=goauthentik,dc=io" 41 self.source.additional_user_dn = "" 42 self.source.additional_group_dn = "" 43 self.source.save() 44 self.source.user_property_mappings.set( 45 LDAPSourcePropertyMapping.objects.filter( 46 Q(managed__startswith="goauthentik.io/sources/ldap/default-") 47 | Q(managed__startswith="goauthentik.io/sources/ldap/ms-") 48 ) 49 ) 50 raw_conn = mock_ad_connection() 51 bind_mock = Mock(wraps=raw_conn.bind) 52 raw_conn.bind = bind_mock 53 connection = MagicMock(return_value=raw_conn) 54 user_connection = MagicMock(return_value=raw_conn) 55 with ( 56 patch("authentik.sources.ldap.models.LDAPSource.connection", connection), 57 patch( 58 "authentik.sources.ldap.models.LDAPSource.connection_as_user", 59 user_connection, 60 ), 61 ): 62 user_sync = UserLDAPSynchronizer(self.source, Task()) 63 user_sync.sync_full() 64 65 user = User.objects.get(username="erin.h") 66 self.source.service_bind_method = LDAPSourceBindMethod.SASL_EXTERNAL 67 self.source.save() 68 backend = LDAPBackend() 69 self.assertEqual( 70 backend.authenticate(None, username="erin.h", password=LDAP_PASSWORD), 71 user, 72 ) 73 user_connection.assert_called_once_with( 74 "CN=Erin M. Hagens,OU=ak-test,DC=t,DC=goauthentik,DC=io", 75 LDAP_PASSWORD, 76 ) 77 bind_mock.assert_not_called() 78 79 def test_auth_synced_user_ad(self): 80 """Test Cached auth""" 81 self.source.base_dn = "dc=t,dc=goauthentik,dc=io" 82 self.source.additional_user_dn = "" 83 self.source.additional_group_dn = "" 84 self.source.save() 85 self.source.user_property_mappings.set( 86 LDAPSourcePropertyMapping.objects.filter( 87 Q(managed__startswith="goauthentik.io/sources/ldap/default-") 88 | Q(managed__startswith="goauthentik.io/sources/ldap/ms-") 89 ) 90 ) 91 connection = MagicMock(return_value=mock_ad_connection()) 92 with patch("authentik.sources.ldap.models.LDAPSource.connection", connection): 93 user_sync = UserLDAPSynchronizer(self.source, Task()) 94 user_sync.sync_full() 95 96 user = User.objects.get(username="erin.h") 97 auth_user_by_bind = Mock(return_value=user) 98 with patch( 99 "authentik.sources.ldap.auth.LDAPBackend.auth_user_by_bind", 100 auth_user_by_bind, 101 ): 102 backend = LDAPBackend() 103 self.assertEqual( 104 backend.authenticate(None, username="erin.h", password=LDAP_PASSWORD), 105 user, 106 ) 107 108 def test_auth_synced_user_openldap(self): 109 """Test Cached auth""" 110 self.source.object_uniqueness_field = "uid" 111 self.source.user_property_mappings.set( 112 LDAPSourcePropertyMapping.objects.filter( 113 Q(name__startswith="authentik default LDAP Mapping") 114 | Q(name__startswith="authentik default OpenLDAP Mapping") 115 ) 116 ) 117 self.source.save() 118 connection = MagicMock(return_value=mock_slapd_connection(LDAP_PASSWORD)) 119 with patch("authentik.sources.ldap.models.LDAPSource.connection", connection): 120 user_sync = UserLDAPSynchronizer(self.source, Task()) 121 user_sync.sync_full() 122 123 user = User.objects.get(username="user0_sn") 124 auth_user_by_bind = Mock(return_value=user) 125 with patch( 126 "authentik.sources.ldap.auth.LDAPBackend.auth_user_by_bind", 127 auth_user_by_bind, 128 ): 129 backend = LDAPBackend() 130 self.assertEqual( 131 backend.authenticate(None, username="user0_sn", password=LDAP_PASSWORD), 132 user, 133 )
LDAP_PASSWORD =
"ta>j]$v.;KCoE=J:hkE~$uN0~OU+?blo|Q4pRKgfrX.{&=C^:Dku6UnoC9RHG51I&{M:Htb?-|TKiE1x)yiIHbs5B[$<0DDeNeOTOq6U'cf,3-Sh?F~B,K=-zYgr9WE0"
class
LDAPSyncTests(django.test.testcases.TestCase):
26class LDAPSyncTests(TestCase): 27 """LDAP Sync tests""" 28 29 @apply_blueprint("system/sources-ldap.yaml") 30 def setUp(self): 31 self.source = LDAPSource.objects.create( 32 name="ldap", 33 slug="ldap", 34 base_dn="dc=goauthentik,dc=io", 35 additional_user_dn="ou=users", 36 additional_group_dn="ou=groups", 37 ) 38 39 def test_auth_direct_user_ad(self): 40 """Test direct auth""" 41 self.source.base_dn = "dc=t,dc=goauthentik,dc=io" 42 self.source.additional_user_dn = "" 43 self.source.additional_group_dn = "" 44 self.source.save() 45 self.source.user_property_mappings.set( 46 LDAPSourcePropertyMapping.objects.filter( 47 Q(managed__startswith="goauthentik.io/sources/ldap/default-") 48 | Q(managed__startswith="goauthentik.io/sources/ldap/ms-") 49 ) 50 ) 51 raw_conn = mock_ad_connection() 52 bind_mock = Mock(wraps=raw_conn.bind) 53 raw_conn.bind = bind_mock 54 connection = MagicMock(return_value=raw_conn) 55 user_connection = MagicMock(return_value=raw_conn) 56 with ( 57 patch("authentik.sources.ldap.models.LDAPSource.connection", connection), 58 patch( 59 "authentik.sources.ldap.models.LDAPSource.connection_as_user", 60 user_connection, 61 ), 62 ): 63 user_sync = UserLDAPSynchronizer(self.source, Task()) 64 user_sync.sync_full() 65 66 user = User.objects.get(username="erin.h") 67 self.source.service_bind_method = LDAPSourceBindMethod.SASL_EXTERNAL 68 self.source.save() 69 backend = LDAPBackend() 70 self.assertEqual( 71 backend.authenticate(None, username="erin.h", password=LDAP_PASSWORD), 72 user, 73 ) 74 user_connection.assert_called_once_with( 75 "CN=Erin M. Hagens,OU=ak-test,DC=t,DC=goauthentik,DC=io", 76 LDAP_PASSWORD, 77 ) 78 bind_mock.assert_not_called() 79 80 def test_auth_synced_user_ad(self): 81 """Test Cached auth""" 82 self.source.base_dn = "dc=t,dc=goauthentik,dc=io" 83 self.source.additional_user_dn = "" 84 self.source.additional_group_dn = "" 85 self.source.save() 86 self.source.user_property_mappings.set( 87 LDAPSourcePropertyMapping.objects.filter( 88 Q(managed__startswith="goauthentik.io/sources/ldap/default-") 89 | Q(managed__startswith="goauthentik.io/sources/ldap/ms-") 90 ) 91 ) 92 connection = MagicMock(return_value=mock_ad_connection()) 93 with patch("authentik.sources.ldap.models.LDAPSource.connection", connection): 94 user_sync = UserLDAPSynchronizer(self.source, Task()) 95 user_sync.sync_full() 96 97 user = User.objects.get(username="erin.h") 98 auth_user_by_bind = Mock(return_value=user) 99 with patch( 100 "authentik.sources.ldap.auth.LDAPBackend.auth_user_by_bind", 101 auth_user_by_bind, 102 ): 103 backend = LDAPBackend() 104 self.assertEqual( 105 backend.authenticate(None, username="erin.h", password=LDAP_PASSWORD), 106 user, 107 ) 108 109 def test_auth_synced_user_openldap(self): 110 """Test Cached auth""" 111 self.source.object_uniqueness_field = "uid" 112 self.source.user_property_mappings.set( 113 LDAPSourcePropertyMapping.objects.filter( 114 Q(name__startswith="authentik default LDAP Mapping") 115 | Q(name__startswith="authentik default OpenLDAP Mapping") 116 ) 117 ) 118 self.source.save() 119 connection = MagicMock(return_value=mock_slapd_connection(LDAP_PASSWORD)) 120 with patch("authentik.sources.ldap.models.LDAPSource.connection", connection): 121 user_sync = UserLDAPSynchronizer(self.source, Task()) 122 user_sync.sync_full() 123 124 user = User.objects.get(username="user0_sn") 125 auth_user_by_bind = Mock(return_value=user) 126 with patch( 127 "authentik.sources.ldap.auth.LDAPBackend.auth_user_by_bind", 128 auth_user_by_bind, 129 ): 130 backend = LDAPBackend() 131 self.assertEqual( 132 backend.authenticate(None, username="user0_sn", password=LDAP_PASSWORD), 133 user, 134 )
LDAP Sync tests
@apply_blueprint('system/sources-ldap.yaml')
def
setUp(self):
29 @apply_blueprint("system/sources-ldap.yaml") 30 def setUp(self): 31 self.source = LDAPSource.objects.create( 32 name="ldap", 33 slug="ldap", 34 base_dn="dc=goauthentik,dc=io", 35 additional_user_dn="ou=users", 36 additional_group_dn="ou=groups", 37 )
Hook method for setting up the test fixture before exercising it.
def
test_auth_direct_user_ad(self):
39 def test_auth_direct_user_ad(self): 40 """Test direct auth""" 41 self.source.base_dn = "dc=t,dc=goauthentik,dc=io" 42 self.source.additional_user_dn = "" 43 self.source.additional_group_dn = "" 44 self.source.save() 45 self.source.user_property_mappings.set( 46 LDAPSourcePropertyMapping.objects.filter( 47 Q(managed__startswith="goauthentik.io/sources/ldap/default-") 48 | Q(managed__startswith="goauthentik.io/sources/ldap/ms-") 49 ) 50 ) 51 raw_conn = mock_ad_connection() 52 bind_mock = Mock(wraps=raw_conn.bind) 53 raw_conn.bind = bind_mock 54 connection = MagicMock(return_value=raw_conn) 55 user_connection = MagicMock(return_value=raw_conn) 56 with ( 57 patch("authentik.sources.ldap.models.LDAPSource.connection", connection), 58 patch( 59 "authentik.sources.ldap.models.LDAPSource.connection_as_user", 60 user_connection, 61 ), 62 ): 63 user_sync = UserLDAPSynchronizer(self.source, Task()) 64 user_sync.sync_full() 65 66 user = User.objects.get(username="erin.h") 67 self.source.service_bind_method = LDAPSourceBindMethod.SASL_EXTERNAL 68 self.source.save() 69 backend = LDAPBackend() 70 self.assertEqual( 71 backend.authenticate(None, username="erin.h", password=LDAP_PASSWORD), 72 user, 73 ) 74 user_connection.assert_called_once_with( 75 "CN=Erin M. Hagens,OU=ak-test,DC=t,DC=goauthentik,DC=io", 76 LDAP_PASSWORD, 77 ) 78 bind_mock.assert_not_called()
Test direct auth
def
test_auth_synced_user_ad(self):
80 def test_auth_synced_user_ad(self): 81 """Test Cached auth""" 82 self.source.base_dn = "dc=t,dc=goauthentik,dc=io" 83 self.source.additional_user_dn = "" 84 self.source.additional_group_dn = "" 85 self.source.save() 86 self.source.user_property_mappings.set( 87 LDAPSourcePropertyMapping.objects.filter( 88 Q(managed__startswith="goauthentik.io/sources/ldap/default-") 89 | Q(managed__startswith="goauthentik.io/sources/ldap/ms-") 90 ) 91 ) 92 connection = MagicMock(return_value=mock_ad_connection()) 93 with patch("authentik.sources.ldap.models.LDAPSource.connection", connection): 94 user_sync = UserLDAPSynchronizer(self.source, Task()) 95 user_sync.sync_full() 96 97 user = User.objects.get(username="erin.h") 98 auth_user_by_bind = Mock(return_value=user) 99 with patch( 100 "authentik.sources.ldap.auth.LDAPBackend.auth_user_by_bind", 101 auth_user_by_bind, 102 ): 103 backend = LDAPBackend() 104 self.assertEqual( 105 backend.authenticate(None, username="erin.h", password=LDAP_PASSWORD), 106 user, 107 )
Test Cached auth
def
test_auth_synced_user_openldap(self):
109 def test_auth_synced_user_openldap(self): 110 """Test Cached auth""" 111 self.source.object_uniqueness_field = "uid" 112 self.source.user_property_mappings.set( 113 LDAPSourcePropertyMapping.objects.filter( 114 Q(name__startswith="authentik default LDAP Mapping") 115 | Q(name__startswith="authentik default OpenLDAP Mapping") 116 ) 117 ) 118 self.source.save() 119 connection = MagicMock(return_value=mock_slapd_connection(LDAP_PASSWORD)) 120 with patch("authentik.sources.ldap.models.LDAPSource.connection", connection): 121 user_sync = UserLDAPSynchronizer(self.source, Task()) 122 user_sync.sync_full() 123 124 user = User.objects.get(username="user0_sn") 125 auth_user_by_bind = Mock(return_value=user) 126 with patch( 127 "authentik.sources.ldap.auth.LDAPBackend.auth_user_by_bind", 128 auth_user_by_bind, 129 ): 130 backend = LDAPBackend() 131 self.assertEqual( 132 backend.authenticate(None, username="user0_sn", password=LDAP_PASSWORD), 133 user, 134 )
Test Cached auth