| 
									
										
										
										
											2021-06-13 13:09:44 -08:00
										 |  |  | from pathlib import Path | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 20:41:07 -09:00
										 |  |  | from pytest import MonkeyPatch | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 13:09:44 -08:00
										 |  |  | from mealie.core import security | 
					
						
							| 
									
										
										
										
											2021-11-23 20:41:07 -09:00
										 |  |  | from mealie.core.config import get_app_settings | 
					
						
							| 
									
										
										
										
											2021-08-28 15:36:46 -08:00
										 |  |  | from mealie.core.dependencies import validate_file_token | 
					
						
							| 
									
										
										
										
											2021-11-23 20:41:07 -09:00
										 |  |  | from mealie.db.db_setup import create_session | 
					
						
							|  |  |  | from tests.utils.factories import random_string | 
					
						
							| 
									
										
										
										
											2021-06-13 13:09:44 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_create_file_token(): | 
					
						
							|  |  |  |     file_path = Path(__file__).parent | 
					
						
							|  |  |  |     file_token = security.create_file_token(file_path) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert file_path == validate_file_token(file_token) | 
					
						
							| 
									
										
										
										
											2021-11-23 20:41:07 -09:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_ldap_authentication_mocked(monkeypatch: MonkeyPatch): | 
					
						
							|  |  |  |     import ldap | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     user = random_string(10) | 
					
						
							|  |  |  |     password = random_string(10) | 
					
						
							|  |  |  |     bind_template = "cn={},dc=example,dc=com" | 
					
						
							| 
									
										
										
										
											2022-09-16 12:33:36 +09:00
										 |  |  |     base_dn = "(dc=example,dc=com)" | 
					
						
							| 
									
										
										
										
											2021-11-23 20:41:07 -09:00
										 |  |  |     monkeypatch.setenv("LDAP_AUTH_ENABLED", "true") | 
					
						
							|  |  |  |     monkeypatch.setenv("LDAP_SERVER_URL", "")  # Not needed due to mocking | 
					
						
							|  |  |  |     monkeypatch.setenv("LDAP_BIND_TEMPLATE", bind_template) | 
					
						
							| 
									
										
										
										
											2022-09-16 12:33:36 +09:00
										 |  |  |     monkeypatch.setenv("LDAP_BASE_DN", base_dn) | 
					
						
							| 
									
										
										
										
											2021-11-23 20:41:07 -09:00
										 |  |  | 
 | 
					
						
							|  |  |  |     class LdapConnMock: | 
					
						
							|  |  |  |         def simple_bind_s(self, dn, bind_pw): | 
					
						
							|  |  |  |             assert dn == bind_template.format(user) | 
					
						
							|  |  |  |             return bind_pw == password | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def search_s(self, dn, scope, filter, attrlist): | 
					
						
							| 
									
										
										
										
											2022-09-16 12:33:36 +09:00
										 |  |  |             assert attrlist == ["name", "mail"] | 
					
						
							|  |  |  |             assert filter == f"(&(objectClass=user)(|(cn={user})(sAMAccountName={user})(mail={user})))" | 
					
						
							|  |  |  |             assert dn == base_dn | 
					
						
							|  |  |  |             assert scope == ldap.SCOPE_SUBTREE | 
					
						
							| 
									
										
										
										
											2021-11-23 20:41:07 -09:00
										 |  |  |             return [()] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def ldap_initialize_mock(url): | 
					
						
							|  |  |  |         assert url == "" | 
					
						
							|  |  |  |         return LdapConnMock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     monkeypatch.setattr(ldap, "initialize", ldap_initialize_mock) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     get_app_settings.cache_clear() | 
					
						
							|  |  |  |     result = security.authenticate_user(create_session(), user, password) | 
					
						
							| 
									
										
										
										
											2022-09-16 12:33:36 +09:00
										 |  |  |     assert result is False |