# encoding=utf-8
from __future__ import unicode_literals
from unittest import skipUnless
from io import StringIO, BytesIO
from django.test import TestCase
from django.contrib.auth import authenticate
from django.contrib.auth.models import User
from django.conf import settings as django_settings
from django.core.management import call_command, CommandError
from django.utils import six
from django_python3_ldap.conf import settings
from django_python3_ldap.ldap import connection
from django_python3_ldap.utils import clean_ldap_name, import_func
@skipUnless(settings.LDAP_AUTH_TEST_USER_USERNAME, "No settings.LDAP_AUTH_TEST_USER_USERNAME supplied.")
@skipUnless(settings.LDAP_AUTH_TEST_USER_PASSWORD, "No settings.LDAP_AUTH_TEST_USER_PASSWORD supplied.")
@skipUnless(settings.LDAP_AUTH_USER_LOOKUP_FIELDS == ("username",), "Cannot test using custom lookup fields.")
@skipUnless(django_settings.AUTH_USER_MODEL == "auth.User", "Cannot test using a custom user model.")
class TestLdap(TestCase):
def setUp(self):
super(TestLdap, self).setUp()
User.objects.all().delete()
# Lazy settings tests.
def testLazySettingsInstanceLookup(self):
self.assertTrue(settings.LDAP_AUTH_TEST_USER_USERNAME)
def testLazySettingsClassLookup(self):
self.assertEqual(settings.__class__.LDAP_AUTH_TEST_USER_USERNAME.name, "LDAP_AUTH_TEST_USER_USERNAME")
self.assertEqual(settings.__class__.LDAP_AUTH_TEST_USER_USERNAME.default, "")
# Utils tests.
def testCleanLdapName(self):
self.assertEqual(clean_ldap_name("foo@bar.com"), r'foo@bar.com')
self.assertEqual(clean_ldap_name("café"), r'caf\E9')
# LDAP tests.
def testGetUserKwargsSuccess(self):
with connection() as c:
user = c.get_user(
username=settings.LDAP_AUTH_TEST_USER_USERNAME,
)
self.assertIsInstance(user, User)
self.assertEqual(user.username, settings.LDAP_AUTH_TEST_USER_USERNAME)
def testGetUserKwargsIncorrectUsername(self):
with connection() as c:
user = c.get_user(
username="bad" + settings.LDAP_AUTH_TEST_USER_USERNAME,
)
self.assertEqual(user, None)
# Authentication tests.
def testAuthenticateUserSuccess(self):
user = authenticate(
username=settings.LDAP_AUTH_TEST_USER_USERNAME,
password=settings.LDAP_AUTH_TEST_USER_PASSWORD,
)
self.assertIsInstance(user, User)
self.assertEqual(user.username, settings.LDAP_AUTH_TEST_USER_USERNAME)
def testAuthenticateUserBadUsername(self):
user = authenticate(
username="bad" + settings.LDAP_AUTH_TEST_USER_USERNAME,
password=settings.LDAP_AUTH_TEST_USER_PASSWORD,
)
self.assertEqual(user, None)
def testAuthenticateUserBadPassword(self):
user = authenticate(
username=settings.LDAP_AUTH_TEST_USER_USERNAME,
password="bad" + settings.LDAP_AUTH_TEST_USER_PASSWORD,
)
self.assertEqual(user, None)
def testRepeatedUserAuthenticationDoestRecreateUsers(self):
user_1 = authenticate(
username=settings.LDAP_AUTH_TEST_USER_USERNAME,
password=settings.LDAP_AUTH_TEST_USER_PASSWORD,
)
user_2 = authenticate(
username=settings.LDAP_AUTH_TEST_USER_USERNAME,
password=settings.LDAP_AUTH_TEST_USER_PASSWORD,
)
# Ensure that the user isn't recreated on second access.
self.assertEqual(user_1.pk, user_2.pk)
def testAuthenticateWithTLS(self):
with self.settings(LDAP_AUTH_USE_TLS=True):
user = authenticate(
username=settings.LDAP_AUTH_TEST_USER_USERNAME,
password=settings.LDAP_AUTH_TEST_USER_PASSWORD,
)
self.assertIsInstance(user, User)
self.assertEqual(user.username, settings.LDAP_AUTH_TEST_USER_USERNAME)
def testAuthenticateWithRebind(self):
with self.settings(
LDAP_AUTH_USE_TLS=True,
LDAP_AUTH_CONNECTION_USERNAME=settings.LDAP_AUTH_TEST_USER_USERNAME,
LDAP_AUTH_CONNECTION_PASSWORD=settings.LDAP_AUTH_TEST_USER_PASSWORD,
):
user = authenticate(
username=settings.LDAP_AUTH_TEST_USER_USERNAME,
password=settings.LDAP_AUTH_TEST_USER_PASSWORD,
)
self.assertIsInstance(user, User)
self.assertEqual(user.username, settings.LDAP_AUTH_TEST_USER_USERNAME)
def testAuthenticateWithFailedRebind(self):
with self.settings(
LDAP_AUTH_USE_TLS=True,
LDAP_AUTH_CONNECTION_USERNAME="bad" + settings.LDAP_AUTH_TEST_USER_USERNAME,
LDAP_AUTH_CONNECTION_PASSWORD=settings.LDAP_AUTH_TEST_USER_PASSWORD,
):
user = authenticate(
username=settings.LDAP_AUTH_TEST_USER_USERNAME,
password=settings.LDAP_AUTH_TEST_USER_PASSWORD,
)
self.assertIs(user, None)
# User synchronisation.
def testSyncUsersCreatesUsers(self):
call_command("ldap_sync_users", verbosity=0)
self.assertGreater(User.objects.count(), 0)
def testSyncUsersCommandOutput(self):
out = StringIO() if six.PY3 else BytesIO()
call_command("ldap_sync_users", verbosity=1, stdout=out)
rows = out.getvalue().split("\n")[:-1]
self.assertEqual(len(rows), User.objects.count())
for row in rows:
six.assertRegex(self, row, r'^Synced [^\s]+$')
def testReSyncUsersDoesntRecreateUsers(self):
call_command("ldap_sync_users", verbosity=0)
user_count_1 = User.objects.count()
call_command("ldap_sync_users", verbosity=0)
user_count_2 = User.objects.count()
self.assertEqual(user_count_1, user_count_2)
# User promotion.
def testPromoteUser(self):
user = User.objects.create(
username="test",
)
self.assertFalse(user.is_staff)
self.assertFalse(user.is_superuser)
# Promote the user.
call_command("ldap_promote", "test", stdout=StringIO() if six.PY3 else BytesIO())
user = User.objects.get(username="test")
self.assertTrue(user.is_staff)
self.assertTrue(user.is_superuser)
def testPromoteMissingUser(self):
with self.assertRaises(CommandError, msg="User with username missing_user does not exist"):
call_command("ldap_promote", "missing_user", verbosity=0)
def testSyncUserRelations(self):
def check_sync_user_relation(user, data):
# id have been created
self.assertIsNotNone(user.id)
# model is saved
self.assertEqual(user.username, User.objects.get(pk=user.id).username)
# save all groups
self.assertIn('cn', data)
ldap_groups = list(data.get('memberOf', ()))
ldap_groups.append('default_group')
for group in ldap_groups:
user.groups.create(name=group)
with self.settings(LDAP_AUTH_SYNC_USER_RELATIONS=check_sync_user_relation):
user = authenticate(
username=settings.LDAP_AUTH_TEST_USER_USERNAME,
password=settings.LDAP_AUTH_TEST_USER_PASSWORD,
)
self.assertIsInstance(user, User)
self.assertGreaterEqual(user.groups.count(), 1)
self.assertEqual(user.groups.filter(name='default_group').count(), 1)
def testImportFunc(self):
self.assertIs(clean_ldap_name, import_func(clean_ldap_name))
self.assertIs(clean_ldap_name, import_func('django_python3_ldap.utils.clean_ldap_name'))
self.assertTrue(callable(import_func('django.contrib.auth.get_user_model')))
self.assertRaises(AttributeError, import_func, 123)
self.assertTrue(callable(import_func(settings.LDAP_AUTH_SYNC_USER_RELATIONS)))
with
没有合适的资源?快使用搜索试试~ 我知道了~
django-python3-ldap, python 3的Django LDAP用户身份验证后端.zip
共23个文件
py:14个
rst:2个
yml:1个
需积分: 34 10 下载量 194 浏览量
2019-09-18
05:47:49
上传
评论 1
收藏 20KB ZIP 举报
温馨提示
django-python3-ldap, python 3的Django LDAP用户身份验证后端 django-python3-ldapdjango-python3-ldap 为 python 2和 3提供了一个 Django LDAP用户身份验证后端。特性使用LDAP服务器验证用户身份。将LDAP用户与本地 Django
资源推荐
资源详情
资源评论
收起资源包目录
django-python3-ldap.zip (23个子文件)
django-python3-ldap-master
.gitignore 134B
setup.cfg 53B
README.rst 8KB
django_python3_ldap
conf.py 3KB
utils.py 3KB
tests.py 8KB
ldap.py 7KB
auth.py 496B
__init__.py 89B
management
__init__.py 0B
commands
ldap_sync_users.py 1KB
ldap_promote.py 1KB
__init__.py 0B
tests
django_python3_ldap_test
settings.py 2KB
__init__.py 0B
manage.py 267B
LICENSE 1KB
.coveragerc 419B
CHANGELOG.rst 3KB
MANIFEST.in 86B
setup.py 1KB
.travis.yml 2KB
.python-version 19B
共 23 条
- 1
资源评论
weixin_38743506
- 粉丝: 350
- 资源: 2万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 所有算法均在 Python 3 中实现,是 hacktoberfest2020 的一个项目 - 没有针对 hacktoberfest 2021 的问题或 PR.zip
- 用springmvc实现的校园选课管理系统
- 我的所有 Python 代码都存储在这个文件夹中 .zip
- 以下是关于毕业设计项目开发的详细资源.docx
- 嵌入式系统安全-C2000 MCU利用JTAGLOCK特征增强设备安全性
- 在Android Studio中创建一个简单的计算器应用.docx
- 我的 Python 演示.zip
- 以下是关于MySQL的详细学习资源.docx
- 西安电子科技大学的微机原理实验.docx
- OpenCV入门教程及案例.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功