ldap: Make Zulip compatible with django-auth-ldap==1.5.

In version 1.5, get_or_create_user method is not used. It exists just
for the compatibility. The main function to use now is
get_or_build_user.

See the changelog:
https://django-auth-ldap.readthedocs.io/en/latest/changes.html#id1

Fixes #9307
This commit is contained in:
Umair Khan
2018-05-22 11:33:56 +05:00
committed by Tim Abbott
parent 03eb17c02f
commit f38d6ac6fe
5 changed files with 19 additions and 20 deletions

View File

@@ -52,7 +52,7 @@ cryptography==2.2.2
defusedxml==0.5.0
# Needed for LDAP support
django-auth-ldap==1.3.0
django-auth-ldap==1.5.0
# Django extension providing bitfield support
django-bitfield==1.9.3

View File

@@ -45,7 +45,7 @@ cssutils==1.0.2 # via premailer
decorator==4.1.2 # via ipython, traitlets
defusedxml==0.5.0
disposable-email-domains==0.0.26
django-auth-ldap==1.3.0
django-auth-ldap==1.5.0
django-bitfield==1.9.3
django-formtools==2.1 # via django-two-factor-auth
django-otp==0.4.1.1 # via django-two-factor-auth
@@ -121,7 +121,7 @@ pydispatcher==2.0.5 # via scrapy
pyflakes==1.6.0
pygments==2.2.0
pyjwt==1.6.1
pyldap==3.0.0.post1 # via django-auth-ldap, fakeldap
pyldap==3.0.0.post1 # via fakeldap
pylibmc==1.5.2
pyoembed==0.1.2
pyopenssl==17.3.0 # via ndg-httpsclient, scrapy, service-identity
@@ -130,7 +130,7 @@ pysocks==1.6.7 # via twilio
python-dateutil==2.6.1
python-digitalocean==1.13.2
python-gcm==0.4
python-ldap==3.0.0 # via pyldap
python-ldap==3.0.0 # via django-auth-ldap, pyldap
python-twitter==3.4.1
python3-openid==3.1.0 # via social-auth-core
pytz==2018.3

View File

@@ -32,7 +32,7 @@ cssutils==1.0.2 # via premailer
decorator==4.1.2 # via ipython, traitlets
defusedxml==0.5.0
disposable-email-domains==0.0.26
django-auth-ldap==1.3.0
django-auth-ldap==1.5.0
django-bitfield==1.9.3
django-formtools==2.1 # via django-two-factor-auth
django-otp==0.4.1.1 # via django-two-factor-auth
@@ -86,14 +86,13 @@ pycparser==2.18 # via cffi
pycrypto==2.6.1
pygments==2.2.0
pyjwt==1.6.1
pyldap==3.0.0.post1 # via django-auth-ldap
pylibmc==1.5.2
pyoembed==0.1.2
pyopenssl==17.3.0 # via ndg-httpsclient
pysocks==1.6.7 # via twilio
python-dateutil==2.6.1
python-gcm==0.4
python-ldap==3.0.0 # via pyldap
python-ldap==3.0.0 # via django-auth-ldap
python-twitter==3.4.1
python3-openid==3.1.0 # via social-auth-core
pytz==2018.3

View File

@@ -2182,7 +2182,7 @@ class TestLDAP(ZulipTestCase):
self.mock_initialize.return_value = self.mock_ldap
self.backend = ZulipLDAPAuthBackend()
# Internally `_realm` attribute is automatically set by the
# `authenticate()` method. But for testing the `get_or_create_user()`
# `authenticate()` method. But for testing the `get_or_build_user()`
# method separately, we need to set it manually.
self.backend._realm = get_realm('zulip')
@@ -2280,18 +2280,18 @@ class TestLDAP(ZulipTestCase):
self.assertEqual(username, '"hamlet@test"@zulip.com')
@override_settings(AUTHENTICATION_BACKENDS=('zproject.backends.ZulipLDAPAuthBackend',))
def test_get_or_create_user_when_user_exists(self) -> None:
def test_get_or_build_user_when_user_exists(self) -> None:
class _LDAPUser:
attrs = {'fn': ['Full Name'], 'sn': ['Short Name']}
backend = self.backend
email = self.example_email("hamlet")
user_profile, created = backend.get_or_create_user(str(email), _LDAPUser())
user_profile, created = backend.get_or_build_user(str(email), _LDAPUser())
self.assertFalse(created)
self.assertEqual(user_profile.email, email)
@override_settings(AUTHENTICATION_BACKENDS=('zproject.backends.ZulipLDAPAuthBackend',))
def test_get_or_create_user_when_user_does_not_exist(self) -> None:
def test_get_or_build_user_when_user_does_not_exist(self) -> None:
class _LDAPUser:
attrs = {'fn': ['Full Name'], 'sn': ['Short Name']}
@@ -2300,13 +2300,13 @@ class TestLDAP(ZulipTestCase):
with self.settings(AUTH_LDAP_USER_ATTR_MAP=ldap_user_attr_map):
backend = self.backend
email = 'nonexisting@zulip.com'
user_profile, created = backend.get_or_create_user(email, _LDAPUser())
user_profile, created = backend.get_or_build_user(email, _LDAPUser())
self.assertTrue(created)
self.assertEqual(user_profile.email, email)
self.assertEqual(user_profile.full_name, 'Full Name')
@override_settings(AUTHENTICATION_BACKENDS=('zproject.backends.ZulipLDAPAuthBackend',))
def test_get_or_create_user_when_user_has_invalid_name(self) -> None:
def test_get_or_build_user_when_user_has_invalid_name(self) -> None:
class _LDAPUser:
attrs = {'fn': ['<invalid name>'], 'sn': ['Short Name']}
@@ -2316,10 +2316,10 @@ class TestLDAP(ZulipTestCase):
backend = self.backend
email = 'nonexisting@zulip.com'
with self.assertRaisesRegex(Exception, "Invalid characters in name!"):
backend.get_or_create_user(email, _LDAPUser())
backend.get_or_build_user(email, _LDAPUser())
@override_settings(AUTHENTICATION_BACKENDS=('zproject.backends.ZulipLDAPAuthBackend',))
def test_get_or_create_user_when_realm_is_deactivated(self) -> None:
def test_get_or_build_user_when_realm_is_deactivated(self) -> None:
class _LDAPUser:
attrs = {'fn': ['Full Name'], 'sn': ['Short Name']}
@@ -2330,10 +2330,10 @@ class TestLDAP(ZulipTestCase):
email = 'nonexisting@zulip.com'
do_deactivate_realm(backend._realm)
with self.assertRaisesRegex(Exception, 'Realm has been deactivated'):
backend.get_or_create_user(email, _LDAPUser())
backend.get_or_build_user(email, _LDAPUser())
@override_settings(AUTHENTICATION_BACKENDS=('zproject.backends.ZulipLDAPAuthBackend',))
def test_get_or_create_user_when_ldap_has_no_email_attr(self) -> None:
def test_get_or_build_user_when_ldap_has_no_email_attr(self) -> None:
class _LDAPUser:
attrs = {'fn': ['Full Name'], 'sn': ['Short Name']}
@@ -2342,7 +2342,7 @@ class TestLDAP(ZulipTestCase):
backend = self.backend
email = 'nonexisting@zulip.com'
with self.assertRaisesRegex(Exception, 'LDAP user doesn\'t have the needed email attribute'):
backend.get_or_create_user(email, _LDAPUser())
backend.get_or_build_user(email, _LDAPUser())
@override_settings(AUTHENTICATION_BACKENDS=('zproject.backends.ZulipLDAPAuthBackend',))
def test_django_to_ldap_username_when_domain_does_not_match(self) -> None:
@@ -2602,7 +2602,7 @@ class LDAPBackendTest(ZulipTestCase):
data = {'username': email, 'password': initial_password(email)}
error_type = ZulipLDAPAuthBackend.REALM_IS_NONE_ERROR
error = ZulipLDAPConfigurationError('Realm is None', error_type)
with mock.patch('zproject.backends.ZulipLDAPAuthBackend.get_or_create_user',
with mock.patch('zproject.backends.ZulipLDAPAuthBackend.get_or_build_user',
side_effect=error), \
mock.patch('django_auth_ldap.backend._LDAPUser._authenticate_user_dn'):
response = self.client_post('/login/', data)

View File

@@ -423,7 +423,7 @@ class ZulipLDAPAuthBackend(ZulipLDAPAuthBackendBase):
except ZulipLDAPException:
return None # nocoverage # TODO: this may no longer be possible
def get_or_create_user(self, username: str, ldap_user: _LDAPUser) -> Tuple[UserProfile, bool]:
def get_or_build_user(self, username: str, ldap_user: _LDAPUser) -> Tuple[UserProfile, bool]:
if settings.LDAP_EMAIL_ATTR is not None:
# Get email from ldap attributes.