diff --git a/zerver/views/auth.py b/zerver/views/auth.py index e7e0f7a27d..b75fe277c0 100644 --- a/zerver/views/auth.py +++ b/zerver/views/auth.py @@ -1,4 +1,3 @@ -from mock import patch from django.forms import Form from django.conf import settings from django.core.exceptions import ValidationError @@ -591,6 +590,13 @@ class TwoFactorLoginView(BaseTwoFactorLoginView): realm.uri instead of '/'. """ realm_uri = self.get_user().realm.uri + # This mock.patch business is an unpleasant hack that we'd + # ideally like to remove by instead patching the upstream + # module to support better configurability of the + # LOGIN_REDIRECT_URL setting. But until then, it works. We + # import mock.patch here because mock has an expensive import + # process involving pbr -> pkgresources (which is really slow). + from mock import patch with patch.object(settings, 'LOGIN_REDIRECT_URL', realm_uri): return super().done(form_list, **kwargs) diff --git a/zproject/backends.py b/zproject/backends.py index 99760f1203..555d87ea24 100644 --- a/zproject/backends.py +++ b/zproject/backends.py @@ -1,5 +1,4 @@ import logging -import mock from typing import Any, Dict, List, Set, Tuple, Optional, Sequence from django_auth_ldap.backend import LDAPBackend, _LDAPUser @@ -319,7 +318,13 @@ class ZulipLDAPAuthBackend(ZulipLDAPAuthBackendBase): REALM_IS_NONE_ERROR = 1 def __init__(self) -> None: - if settings.DEVELOPMENT and settings.FAKE_LDAP_MODE: # nocoverage # We only use this in development + if settings.DEVELOPMENT and settings.FAKE_LDAP_MODE: # nocoverage + # We only use this in development. Importing mock inside + # this function is an import time optimization, which + # avoids the expensive import of the mock module (slow + # because its dependency pbr uses pkgresources, which is + # really slow to import.) + import mock from fakeldap import MockLDAP ldap_patcher = mock.patch('django_auth_ldap.config.ldap.initialize')