From 0fa3eb8ee326c80c45ed0e7bc88ecea1b322ddd4 Mon Sep 17 00:00:00 2001 From: Ben C Date: Tue, 16 Nov 2021 19:36:08 -0500 Subject: [PATCH] Fixed authentication tests --- Users/ldap_auth.py | 18 ++++++++++++++---- Users/ldap_mock.py | 29 +++++++++++++++++++++-------- tests/test_auth.py | 8 ++++---- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/Users/ldap_auth.py b/Users/ldap_auth.py index 4e02a71..8244836 100644 --- a/Users/ldap_auth.py +++ b/Users/ldap_auth.py @@ -24,10 +24,19 @@ class LDAPAuthentication(BaseBackend): :cvar server: The server we're connecting to with LDAP (Must be an ActiveDirectory Server) """ - server = Server(settings.LDAP_URL, get_info=ALL) + server = None + + @classmethod + def setup_server(cls): + """ + This function gets the server to authenticate with + """ + + if cls.server is None: + cls.server = Server(settings.LDAP_URL, get_info=ALL) @staticmethod - def bind_connection(conn): + def bind_connection(conn: Connection) -> Connection: """ This function is used to bind a connection @@ -47,7 +56,7 @@ class LDAPAuthentication(BaseBackend): raise LDAPConnectionError() @classmethod - def get_connection(cls, username, password): + def get_connection(cls, username: str, password: str) -> Connection: """ This function is used to get a Connection object to the server with the given credentials @@ -57,6 +66,7 @@ class LDAPAuthentication(BaseBackend): :type username: str """ + cls.setup_server() conn = Connection(cls.server, user=username, password=password, authentication=NTLM) return cls.bind_connection(conn) @@ -214,7 +224,7 @@ class LDAPAuthentication(BaseBackend): except User.DoesNotExist: return None - def delete_old_users(self, username, password): + def delete_old_users(self, username: str, password: str): """ This function is used to delete any users that are no longer in the ActiveDirectory database. diff --git a/Users/ldap_mock.py b/Users/ldap_mock.py index 4d1e72e..21aa396 100644 --- a/Users/ldap_mock.py +++ b/Users/ldap_mock.py @@ -1,7 +1,7 @@ """ This file provides a fake ldap server to test with """ - +from typing import Optional from uuid import uuid4 from django.conf import settings @@ -16,12 +16,21 @@ class LDAPMockAuthentication(LDAPAuthentication): *NEVER* **EVER** USE THIS IN PRODUCTION """ - server = Server.from_definition('fake_server', - 'tests/ldap_test_server/test_info.json', 'tests/ldap_test_server/test_schema.json') + @classmethod + def setup_server(cls): + """ + This function constructs a fake server to connect to + """ + + if cls.server is None: + cls.server = Server.from_definition('fake_server', + 'tests/ldap_test_server/test_info.json', + 'tests/ldap_test_server/test_schema.json') + users = {} @classmethod - def construct_dn(cls, username, ou=None): + def construct_dn(cls, username: str, ou: str = None) -> str: """ This function constructs a distinguished name for a username @@ -32,13 +41,14 @@ class LDAPMockAuthentication(LDAPAuthentication): :returns: The distinguished name of the user :rtype: str """ + if ou: return f'cn={username},ou={ou},{settings.LDAP_BASE_CONTEXT}' else: return f'cn={username},{settings.LDAP_BASE_CONTEXT}' @classmethod - def extract_ou(cls, username): + def extract_ou(cls, username: str) -> Optional[str]: """ This function gets the organizational unit a test user is in from their username @@ -52,7 +62,7 @@ class LDAPMockAuthentication(LDAPAuthentication): return user.get('ou') @classmethod - def create_fake_user(cls, conn: Connection, username, password, first, last, ou=None): + def create_fake_user(cls, conn: Connection, username: str, password: str, first: str, last: str, ou: str = None): """ This function creates a fake user to use in testing @@ -82,7 +92,7 @@ class LDAPMockAuthentication(LDAPAuthentication): }) @classmethod - def create_fake_users(cls, conn): + def create_fake_users(cls, conn: Connection): """ This function creates a series of fake users from a predefined list @@ -95,7 +105,7 @@ class LDAPMockAuthentication(LDAPAuthentication): cls.create_fake_user(conn, username, user['password'], user['first'], user['last'], user['ou']) @classmethod - def get_connection(cls, username, password): + def get_connection(cls, username: str, password: str) -> Connection: """ This function overrides the base LDAPAuthentication function to provide a fake connection instead of a real one @@ -104,8 +114,11 @@ class LDAPMockAuthentication(LDAPAuthentication): :type username: str :param password: The password to log in with :type password: str + :returns: The connection to use + :rtype: Connection """ + cls.setup_server() username = username.split('\\')[1] conn = Connection(cls.server, cls.construct_dn(username, ou=cls.extract_ou(username)), password, client_strategy=MOCK_SYNC) diff --git a/tests/test_auth.py b/tests/test_auth.py index 483ba07..20801a8 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -19,7 +19,7 @@ class LogoutTest(TestCase): @override_settings(AUTHENTICATION_BACKENDS=['Users.ldap_mock.LDAPMockAuthentication'], LDAP_DOMAIN="example", - LDAP_BASE_CONTEXT="ou=ITP Users,dc=itp,dc=example") + LDAP_BASE_CONTEXT="ou=ITP Users,dc=itp,dc=example", LDAP_URL="example-server") class LDAPAuthTest(TestCase): url = reverse('login') @@ -115,7 +115,7 @@ class LDAPAuthTest(TestCase): response = self.client.post(self.url, {'username': "invalid_user", 'password': "invalid_password"}) self.assertEqual(len(response.context.get('form').non_field_errors()), 1) - @override_settings(AUTHENTICATION_BACKENDS=['Users.ldap_auth.LDAPAuthentication'], LDAP_URL="bad-server") + @override_settings(AUTHENTICATION_BACKENDS=['Users.ldap_auth.LDAPAuthentication']) def test_cant_connect(self) -> None: response = self.client.post(self.url, {'username': "admin", 'password': "admin_password123"}) self.assertIn("There was an error contacting the auth server, please try again later.", @@ -126,7 +126,7 @@ class LDAPAuthTest(TestCase): @override_settings(AUTHENTICATION_BACKENDS=['Users.ldap_mock.LDAPMockAuthentication'], LDAP_DOMAIN="example", - LDAP_BASE_CONTEXT="ou=ITP Users,dc=itp,dc=example") + LDAP_BASE_CONTEXT="ou=ITP Users,dc=itp,dc=example", LDAP_URL="example-server") class UserCleanupTest(TestCase): url = reverse('user-cleanup') @@ -183,7 +183,7 @@ class UserCleanupTest(TestCase): self.assertTrue(User.objects.filter(username="example\\student").exists()) self.assertMessage(response, "You lack permissions to perform this action.") - @override_settings(AUTHENTICATION_BACKENDS=['Users.ldap_auth.LDAPAuthentication'], LDAP_URL="bad-server") + @override_settings(AUTHENTICATION_BACKENDS=['Users.ldap_auth.LDAPAuthentication']) def test_cant_connect(self) -> None: self.client.force_login(self.admin) response = self.client.post(self.url, {'userPassword': "admin_password123"}) -- knotmirror 0.0.1