mirror of
https://github.com/zulip/zulip.git
synced 2025-11-03 21:43:21 +00:00
auth: Add function for generating test ldap_dir to backends.py.
Generates ldap_dir based on the mode and the no. of extra users. It supports three modes, 'a', 'b' and 'c', description for which can be found in prod_settings_templates.py.
This commit is contained in:
committed by
Tim Abbott
parent
e5668da879
commit
d409555b2f
178
zerver/tests/fixtures/ldap_dir.json
vendored
Normal file
178
zerver/tests/fixtures/ldap_dir.json
vendored
Normal file
@@ -0,0 +1,178 @@
|
|||||||
|
{
|
||||||
|
"a": {
|
||||||
|
"uid=ldap_zoe@zulip.com,ou=users,dc=zulip,dc=com": {
|
||||||
|
"cn": [
|
||||||
|
"Zoe"
|
||||||
|
],
|
||||||
|
"userPassword": "ldap_zoe"
|
||||||
|
},
|
||||||
|
"uid=ldap_othello@zulip.com,ou=users,dc=zulip,dc=com": {
|
||||||
|
"cn": [
|
||||||
|
"Othello, the Moor of Venice"
|
||||||
|
],
|
||||||
|
"userPassword": "ldap_othello"
|
||||||
|
},
|
||||||
|
"uid=ldap_iago@zulip.com,ou=users,dc=zulip,dc=com": {
|
||||||
|
"cn": [
|
||||||
|
"Iago"
|
||||||
|
],
|
||||||
|
"userPassword": "ldap_iago"
|
||||||
|
},
|
||||||
|
"uid=ldap_prospero@zulip.com,ou=users,dc=zulip,dc=com": {
|
||||||
|
"cn": [
|
||||||
|
"Prospero from The Tempest"
|
||||||
|
],
|
||||||
|
"userPassword": "ldap_prospero"
|
||||||
|
},
|
||||||
|
"uid=ldap_cordelia@zulip.com,ou=users,dc=zulip,dc=com": {
|
||||||
|
"cn": [
|
||||||
|
"Cordelia Lear"
|
||||||
|
],
|
||||||
|
"userPassword": "ldap_cordelia"
|
||||||
|
},
|
||||||
|
"uid=ldap_hamlet@zulip.com,ou=users,dc=zulip,dc=com": {
|
||||||
|
"cn": [
|
||||||
|
"King Hamlet"
|
||||||
|
],
|
||||||
|
"userPassword": "ldap_hamlet"
|
||||||
|
},
|
||||||
|
"uid=ldap_aaron@zulip.com,ou=users,dc=zulip,dc=com": {
|
||||||
|
"cn": [
|
||||||
|
"aaron"
|
||||||
|
],
|
||||||
|
"userPassword": "ldap_aaron"
|
||||||
|
},
|
||||||
|
"uid=ldap_polonius@zulip.com,ou=users,dc=zulip,dc=com": {
|
||||||
|
"cn": [
|
||||||
|
"Polonius"
|
||||||
|
],
|
||||||
|
"userPassword": "ldap_polonius"
|
||||||
|
},
|
||||||
|
"uid=ldap_extrauser0@zulip.com,ou=users,dc=zulip,dc=com": {
|
||||||
|
"cn": [
|
||||||
|
"Extra User 0"
|
||||||
|
],
|
||||||
|
"userPassword": "ldap_extrauser0"
|
||||||
|
},
|
||||||
|
"uid=ldap_extrauser1@zulip.com,ou=users,dc=zulip,dc=com": {
|
||||||
|
"cn": [
|
||||||
|
"Extra User 1"
|
||||||
|
],
|
||||||
|
"userPassword": "ldap_extrauser1"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"b": {
|
||||||
|
"uid=ldap_zoe,ou=users,dc=zulip,dc=com": {
|
||||||
|
"cn": [
|
||||||
|
"Zoe"
|
||||||
|
],
|
||||||
|
"userPassword": "ldap_zoe"
|
||||||
|
},
|
||||||
|
"uid=ldap_othello,ou=users,dc=zulip,dc=com": {
|
||||||
|
"cn": [
|
||||||
|
"Othello, the Moor of Venice"
|
||||||
|
],
|
||||||
|
"userPassword": "ldap_othello"
|
||||||
|
},
|
||||||
|
"uid=ldap_iago,ou=users,dc=zulip,dc=com": {
|
||||||
|
"cn": [
|
||||||
|
"Iago"
|
||||||
|
],
|
||||||
|
"userPassword": "ldap_iago"
|
||||||
|
},
|
||||||
|
"uid=ldap_prospero,ou=users,dc=zulip,dc=com": {
|
||||||
|
"cn": [
|
||||||
|
"Prospero from The Tempest"
|
||||||
|
],
|
||||||
|
"userPassword": "ldap_prospero"
|
||||||
|
},
|
||||||
|
"uid=ldap_cordelia,ou=users,dc=zulip,dc=com": {
|
||||||
|
"cn": [
|
||||||
|
"Cordelia Lear"
|
||||||
|
],
|
||||||
|
"userPassword": "ldap_cordelia"
|
||||||
|
},
|
||||||
|
"uid=ldap_hamlet,ou=users,dc=zulip,dc=com": {
|
||||||
|
"cn": [
|
||||||
|
"King Hamlet"
|
||||||
|
],
|
||||||
|
"userPassword": "ldap_hamlet"
|
||||||
|
},
|
||||||
|
"uid=ldap_aaron,ou=users,dc=zulip,dc=com": {
|
||||||
|
"cn": [
|
||||||
|
"aaron"
|
||||||
|
],
|
||||||
|
"userPassword": "ldap_aaron"
|
||||||
|
},
|
||||||
|
"uid=ldap_polonius,ou=users,dc=zulip,dc=com": {
|
||||||
|
"cn": [
|
||||||
|
"Polonius"
|
||||||
|
],
|
||||||
|
"userPassword": "ldap_polonius"
|
||||||
|
},
|
||||||
|
"uid=ldap_extrauser0,ou=users,dc=zulip,dc=com": {
|
||||||
|
"cn": [
|
||||||
|
"Extra User 0"
|
||||||
|
],
|
||||||
|
"userPassword": "ldap_extrauser0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"c": {
|
||||||
|
"uid=ldap_zoe,ou=users,dc=zulip,dc=com": {
|
||||||
|
"cn": [
|
||||||
|
"Zoe"
|
||||||
|
],
|
||||||
|
"userPassword": "ldap_zoe_test",
|
||||||
|
"email": "ldap_zoe@zulip.com"
|
||||||
|
},
|
||||||
|
"uid=ldap_othello,ou=users,dc=zulip,dc=com": {
|
||||||
|
"cn": [
|
||||||
|
"Othello, the Moor of Venice"
|
||||||
|
],
|
||||||
|
"userPassword": "ldap_othello_test",
|
||||||
|
"email": "ldap_othello@zulip.com"
|
||||||
|
},
|
||||||
|
"uid=ldap_iago,ou=users,dc=zulip,dc=com": {
|
||||||
|
"cn": [
|
||||||
|
"Iago"
|
||||||
|
],
|
||||||
|
"userPassword": "ldap_iago_test",
|
||||||
|
"email": "ldap_iago@zulip.com"
|
||||||
|
},
|
||||||
|
"uid=ldap_prospero,ou=users,dc=zulip,dc=com": {
|
||||||
|
"cn": [
|
||||||
|
"Prospero from The Tempest"
|
||||||
|
],
|
||||||
|
"userPassword": "ldap_prospero_test",
|
||||||
|
"email": "ldap_prospero@zulip.com"
|
||||||
|
},
|
||||||
|
"uid=ldap_cordelia,ou=users,dc=zulip,dc=com": {
|
||||||
|
"cn": [
|
||||||
|
"Cordelia Lear"
|
||||||
|
],
|
||||||
|
"userPassword": "ldap_cordelia_test",
|
||||||
|
"email": "ldap_cordelia@zulip.com"
|
||||||
|
},
|
||||||
|
"uid=ldap_hamlet,ou=users,dc=zulip,dc=com": {
|
||||||
|
"cn": [
|
||||||
|
"King Hamlet"
|
||||||
|
],
|
||||||
|
"userPassword": "ldap_hamlet_test",
|
||||||
|
"email": "ldap_hamlet@zulip.com"
|
||||||
|
},
|
||||||
|
"uid=ldap_aaron,ou=users,dc=zulip,dc=com": {
|
||||||
|
"cn": [
|
||||||
|
"aaron"
|
||||||
|
],
|
||||||
|
"userPassword": "ldap_aaron_test",
|
||||||
|
"email": "ldap_aaron@zulip.com"
|
||||||
|
},
|
||||||
|
"uid=ldap_polonius,ou=users,dc=zulip,dc=com": {
|
||||||
|
"cn": [
|
||||||
|
"Polonius"
|
||||||
|
],
|
||||||
|
"userPassword": "ldap_polonius_test",
|
||||||
|
"email": "ldap_polonius@zulip.com"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -51,7 +51,7 @@ from zproject.backends import ZulipDummyBackend, EmailAuthBackend, \
|
|||||||
ZulipLDAPUserPopulator, DevAuthBackend, GitHubAuthBackend, ZulipAuthMixin, \
|
ZulipLDAPUserPopulator, DevAuthBackend, GitHubAuthBackend, ZulipAuthMixin, \
|
||||||
dev_auth_enabled, password_auth_enabled, github_auth_enabled, \
|
dev_auth_enabled, password_auth_enabled, github_auth_enabled, \
|
||||||
require_email_format_usernames, AUTH_BACKEND_NAME_MAP, \
|
require_email_format_usernames, AUTH_BACKEND_NAME_MAP, \
|
||||||
ZulipLDAPConfigurationError
|
ZulipLDAPConfigurationError, generate_dev_ldap_dir
|
||||||
|
|
||||||
from zerver.views.auth import (maybe_send_to_registration,
|
from zerver.views.auth import (maybe_send_to_registration,
|
||||||
login_or_register_remote_user,
|
login_or_register_remote_user,
|
||||||
@@ -2094,6 +2094,18 @@ class TestLDAP(ZulipTestCase):
|
|||||||
realm.string_id = 'zulip'
|
realm.string_id = 'zulip'
|
||||||
realm.save()
|
realm.save()
|
||||||
|
|
||||||
|
def test_generate_dev_ldap_dir(self) -> None:
|
||||||
|
fixtures = ujson.loads(self.fixture_data("ldap_dir.json"))
|
||||||
|
|
||||||
|
ldap_dir = generate_dev_ldap_dir('A', 2)
|
||||||
|
self.assertEqual(ldap_dir, fixtures['a'])
|
||||||
|
|
||||||
|
ldap_dir = generate_dev_ldap_dir('b', 1)
|
||||||
|
self.assertEqual(ldap_dir, fixtures['b'])
|
||||||
|
|
||||||
|
ldap_dir = generate_dev_ldap_dir('c', 0)
|
||||||
|
self.assertEqual(ldap_dir, fixtures['c'])
|
||||||
|
|
||||||
@override_settings(AUTHENTICATION_BACKENDS=('zproject.backends.ZulipLDAPAuthBackend',))
|
@override_settings(AUTHENTICATION_BACKENDS=('zproject.backends.ZulipLDAPAuthBackend',))
|
||||||
def test_login_success(self) -> None:
|
def test_login_success(self) -> None:
|
||||||
self.mock_ldap.directory = {
|
self.mock_ldap.directory = {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import logging
|
import logging
|
||||||
from typing import Any, Dict, List, Set, Tuple, Optional
|
from typing import Any, Dict, List, Set, Tuple, Optional, Sequence
|
||||||
|
|
||||||
from django_auth_ldap.backend import LDAPBackend, _LDAPUser
|
from django_auth_ldap.backend import LDAPBackend, _LDAPUser
|
||||||
import django.contrib.auth
|
import django.contrib.auth
|
||||||
@@ -106,6 +106,50 @@ def common_get_active_user(email: str, realm: Realm,
|
|||||||
return None
|
return None
|
||||||
return user_profile
|
return user_profile
|
||||||
|
|
||||||
|
def generate_dev_ldap_dir(mode: str, extra_users: int=0) -> Dict[str, Dict[str, Sequence[str]]]:
|
||||||
|
mode = mode.lower()
|
||||||
|
names = [
|
||||||
|
("Zoe", "ldap_ZOE@zulip.com"),
|
||||||
|
("Othello, the Moor of Venice", "ldap_othello@zulip.com"),
|
||||||
|
("Iago", "ldap_iago@zulip.com"),
|
||||||
|
("Prospero from The Tempest", "ldap_prospero@zulip.com"),
|
||||||
|
("Cordelia Lear", "ldap_cordelia@zulip.com"),
|
||||||
|
("King Hamlet", "ldap_hamlet@zulip.com"),
|
||||||
|
("aaron", "ldap_AARON@zulip.com"),
|
||||||
|
("Polonius", "ldap_polonius@zulip.com"),
|
||||||
|
]
|
||||||
|
for i in range(extra_users):
|
||||||
|
names.append(('Extra User %d' % (i,), 'ldap_extrauser%d@zulip.com' % (i,)))
|
||||||
|
|
||||||
|
ldap_dir = {}
|
||||||
|
if mode == 'a':
|
||||||
|
for name in names:
|
||||||
|
email = name[1].lower()
|
||||||
|
email_username = email.split('@')[0]
|
||||||
|
ldap_dir['uid=' + email + ',ou=users,dc=zulip,dc=com'] = {
|
||||||
|
'cn': [name[0], ],
|
||||||
|
'userPassword': email_username,
|
||||||
|
}
|
||||||
|
elif mode == 'b':
|
||||||
|
for name in names:
|
||||||
|
email = name[1].lower()
|
||||||
|
email_username = email.split('@')[0]
|
||||||
|
ldap_dir['uid=' + email_username + ',ou=users,dc=zulip,dc=com'] = {
|
||||||
|
'cn': [name[0], ],
|
||||||
|
'userPassword': email_username,
|
||||||
|
}
|
||||||
|
elif mode == 'c':
|
||||||
|
for name in names:
|
||||||
|
email = name[1].lower()
|
||||||
|
email_username = email.split('@')[0]
|
||||||
|
ldap_dir['uid=' + email_username + ',ou=users,dc=zulip,dc=com'] = {
|
||||||
|
'cn': [name[0], ],
|
||||||
|
'userPassword': email_username + '_test',
|
||||||
|
'email': email,
|
||||||
|
}
|
||||||
|
|
||||||
|
return ldap_dir
|
||||||
|
|
||||||
class ZulipAuthMixin:
|
class ZulipAuthMixin:
|
||||||
def get_user(self, user_profile_id: int) -> Optional[UserProfile]:
|
def get_user(self, user_profile_id: int) -> Optional[UserProfile]:
|
||||||
""" Get a UserProfile object from the user_profile_id. """
|
""" Get a UserProfile object from the user_profile_id. """
|
||||||
|
|||||||
Reference in New Issue
Block a user