|
5 | 5 | from ldap3 import Connection, Server |
6 | 6 | from ldap3.core.exceptions import LDAPException, LDAPSocketOpenError |
7 | 7 |
|
8 | | -from fastapi_ldap.client import LDAPClient |
| 8 | +from fastapi_ldap.client import LDAPClient, _normalize_username |
9 | 9 | from fastapi_ldap.config import LDAPSettings |
10 | 10 | from fastapi_ldap.exceptions import LDAPConnectionError, LDAPError |
11 | 11 |
|
@@ -183,6 +183,68 @@ async def test_authenticate_empty_credentials(self, ldap_settings): |
183 | 183 | result = await client.authenticate("", "pass") |
184 | 184 | assert result is None |
185 | 185 |
|
| 186 | + def test_normalize_username(self): |
| 187 | + assert _normalize_username("jsmith@example.com") == "jsmith" |
| 188 | + assert _normalize_username("EXAMPLE\\jsmith") == "jsmith" |
| 189 | + assert _normalize_username("jsmith") == "jsmith" |
| 190 | + |
| 191 | + @pytest.mark.asyncio |
| 192 | + async def test_authenticate_normalizes_upn_for_search(self, ldap_settings): |
| 193 | + ldap_settings.user_search_filter = "(sAMAccountName={username})" |
| 194 | + client = LDAPClient(ldap_settings) |
| 195 | + mock_conn = Mock(spec=Connection) |
| 196 | + mock_conn.bound = True |
| 197 | + mock_conn.closed = False |
| 198 | + mock_conn.entries = [] |
| 199 | + mock_conn.search = Mock(return_value=True) |
| 200 | + |
| 201 | + with patch.object(client, "connection") as mock_ctx: |
| 202 | + mock_ctx.return_value.__aenter__ = AsyncMock(return_value=mock_conn) |
| 203 | + mock_ctx.return_value.__aexit__ = AsyncMock(return_value=False) |
| 204 | + |
| 205 | + await client.authenticate("jsmith@example.com", "password") |
| 206 | + |
| 207 | + search_filter = mock_conn.search.call_args[0][1] |
| 208 | + assert search_filter == "(sAMAccountName=jsmith)" |
| 209 | + |
| 210 | + @pytest.mark.asyncio |
| 211 | + async def test_authenticate_normalizes_netbios_for_search(self, ldap_settings): |
| 212 | + ldap_settings.user_search_filter = "(sAMAccountName={username})" |
| 213 | + client = LDAPClient(ldap_settings) |
| 214 | + mock_conn = Mock(spec=Connection) |
| 215 | + mock_conn.bound = True |
| 216 | + mock_conn.closed = False |
| 217 | + mock_conn.entries = [] |
| 218 | + mock_conn.search = Mock(return_value=True) |
| 219 | + |
| 220 | + with patch.object(client, "connection") as mock_ctx: |
| 221 | + mock_ctx.return_value.__aenter__ = AsyncMock(return_value=mock_conn) |
| 222 | + mock_ctx.return_value.__aexit__ = AsyncMock(return_value=False) |
| 223 | + |
| 224 | + await client.authenticate("EXAMPLE\\jsmith", "password") |
| 225 | + |
| 226 | + search_filter = mock_conn.search.call_args[0][1] |
| 227 | + assert search_filter == "(sAMAccountName=jsmith)" |
| 228 | + |
| 229 | + @pytest.mark.asyncio |
| 230 | + async def test_authenticate_escapes_filter_special_chars(self, ldap_settings): |
| 231 | + ldap_settings.user_search_filter = "(sAMAccountName={username})" |
| 232 | + client = LDAPClient(ldap_settings) |
| 233 | + mock_conn = Mock(spec=Connection) |
| 234 | + mock_conn.bound = True |
| 235 | + mock_conn.closed = False |
| 236 | + mock_conn.entries = [] |
| 237 | + mock_conn.search = Mock(return_value=True) |
| 238 | + |
| 239 | + with patch.object(client, "connection") as mock_ctx: |
| 240 | + mock_ctx.return_value.__aenter__ = AsyncMock(return_value=mock_conn) |
| 241 | + mock_ctx.return_value.__aexit__ = AsyncMock(return_value=False) |
| 242 | + |
| 243 | + await client.authenticate("user*@example.com", "password") |
| 244 | + |
| 245 | + search_filter = mock_conn.search.call_args[0][1] |
| 246 | + assert search_filter == "(sAMAccountName=user\\2a)" |
| 247 | + |
186 | 248 | @pytest.mark.asyncio |
187 | 249 | async def test_get_user_groups_success(self, ldap_settings): |
188 | 250 | client = LDAPClient(ldap_settings) |
|
0 commit comments