Add unit tests
This commit adds test coverage for gvaldap and ldapentities and starts to provide coverage for ldaptasks using mockldap.
This commit is contained in:
parent
32366e93dd
commit
4b060c51f4
13 changed files with 309 additions and 11 deletions
|
@ -16,8 +16,11 @@ os.environ.setdefault('DJANGO_SETTINGS_MODULE',
|
|||
'gvaldap.settings.production')
|
||||
|
||||
|
||||
#: The Celery application
|
||||
app = Celery('gvaldap')
|
||||
|
||||
|
||||
def get_installed_apps():
|
||||
return settings.INSTALLED_APPS
|
||||
|
||||
app.config_from_object('django.conf:settings')
|
||||
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
|
||||
app.autodiscover_tasks(get_installed_apps)
|
||||
|
|
|
@ -12,16 +12,15 @@ PASSWORD_HASHERS = (
|
|||
'django.contrib.auth.hashers.MD5PasswordHasher',
|
||||
)
|
||||
# ######### IN-MEMORY TEST DATABASE
|
||||
DATABASES = {
|
||||
"default": {
|
||||
DATABASES['default'] = {
|
||||
"ENGINE": "django.db.backends.sqlite3",
|
||||
"NAME": ":memory:",
|
||||
"USER": "",
|
||||
"PASSWORD": "",
|
||||
"HOST": "",
|
||||
"PORT": "",
|
||||
},
|
||||
}
|
||||
|
||||
LOGGING['handlers'].update({
|
||||
'console': {
|
||||
'level': 'ERROR',
|
||||
|
|
0
gvaldap/gvaldap/tests/__init__.py
Normal file
0
gvaldap/gvaldap/tests/__init__.py
Normal file
16
gvaldap/gvaldap/tests/test_celery.py
Normal file
16
gvaldap/gvaldap/tests/test_celery.py
Normal file
|
@ -0,0 +1,16 @@
|
|||
"""
|
||||
This module implements tests for :py:mod:`gvaldap.celery`.
|
||||
|
||||
"""
|
||||
from __future__ import absolute_import
|
||||
|
||||
from unittest import TestCase
|
||||
|
||||
from gvaldap.celery import get_installed_apps
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
class GetInstalledAppsTest(TestCase):
|
||||
|
||||
def test_get_installed_apps(self):
|
||||
self.assertEqual(get_installed_apps(), settings.INSTALLED_APPS)
|
27
gvaldap/gvaldap/tests/test_settings.py
Normal file
27
gvaldap/gvaldap/tests/test_settings.py
Normal file
|
@ -0,0 +1,27 @@
|
|||
"""
|
||||
This module implements tests for :py:mod:`gvaldap.settings`.
|
||||
|
||||
"""
|
||||
from __future__ import absolute_import
|
||||
|
||||
import os
|
||||
from unittest import TestCase
|
||||
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
|
||||
from gvaldap.settings.base import get_env_variable
|
||||
|
||||
|
||||
class GetEnvVariableTest(TestCase):
|
||||
|
||||
def test_get_existing_env_variable(self):
|
||||
os.environ['testvariable'] = 'myvalue'
|
||||
self.assertEqual(get_env_variable('testvariable'), 'myvalue')
|
||||
|
||||
def test_get_missing_env_variable(self):
|
||||
if 'missingvariable' in os.environ:
|
||||
del os.environ['missingvariable']
|
||||
with self.assertRaises(ImproperlyConfigured) as e:
|
||||
get_env_variable('missingvariable')
|
||||
self.assertEqual(
|
||||
str(e.exception), 'Set the missingvariable environment variable')
|
14
gvaldap/gvaldap/tests/test_wsgi.py
Normal file
14
gvaldap/gvaldap/tests/test_wsgi.py
Normal file
|
@ -0,0 +1,14 @@
|
|||
"""
|
||||
This module provides tests for :py:mod:`gvaldap.wsgi`.
|
||||
|
||||
"""
|
||||
from __future__ import absolute_import
|
||||
|
||||
from unittest import TestCase
|
||||
|
||||
|
||||
class WSGITest(TestCase):
|
||||
|
||||
def test_wsgi_application(self):
|
||||
from gvaldap import wsgi
|
||||
self.assertIsNotNone(wsgi.application)
|
|
@ -6,6 +6,7 @@ The models are based on :py:class:`ldapmodels.Model` from `django-ldapdb`_.
|
|||
.. _django-ldapdb: https://github.com/jlaine/django-ldapdb#readme
|
||||
|
||||
"""
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.conf import settings
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
|
0
gvaldap/ldapentities/tests/__init__.py
Normal file
0
gvaldap/ldapentities/tests/__init__.py
Normal file
62
gvaldap/ldapentities/tests/test_admin.py
Normal file
62
gvaldap/ldapentities/tests/test_admin.py
Normal file
|
@ -0,0 +1,62 @@
|
|||
"""
|
||||
This module provides tests for :py:mod:`ldapentities.admin`.
|
||||
|
||||
"""
|
||||
from __future__ import absolute_import
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.test import TestCase
|
||||
from django.contrib.auth import get_user_model
|
||||
|
||||
from mockldap import MockLdap
|
||||
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
TEST_USER = 'admin'
|
||||
TEST_EMAIL = 'admin@example.org'
|
||||
TEST_PASSWORD = 'secret'
|
||||
|
||||
admin = (
|
||||
settings.DATABASES['ldap']['USER'], {
|
||||
'userPassword': [settings.DATABASES['ldap']['PASSWORD']]})
|
||||
groups = (
|
||||
settings.GROUP_BASE_DN, {
|
||||
'objectClass': ['top', 'organizationalUnit'], 'ou': ['groups']})
|
||||
users = (
|
||||
settings.USER_BASE_DN, {
|
||||
'objectClass': ['top', 'organizationalUnit'], 'ou': ['users']})
|
||||
|
||||
|
||||
class LdapUserAdminTest(TestCase):
|
||||
directory = dict([admin, groups, users])
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.mockldap = MockLdap(cls.directory)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.mockldap
|
||||
|
||||
def setUp(self):
|
||||
User.objects.create_superuser(
|
||||
TEST_USER, email=TEST_EMAIL, password=TEST_PASSWORD)
|
||||
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
||||
self.mockldap.start()
|
||||
self.ldapobj = self.mockldap[settings.DATABASES['ldap']['NAME']]
|
||||
|
||||
def tearDown(self):
|
||||
self.mockldap.stop()
|
||||
del self.ldapobj
|
||||
|
||||
def test_can_administer_groups(self):
|
||||
response = self.client.get(reverse(
|
||||
'admin:ldapentities_ldapgroup_changelist'))
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_can_administer_users(self):
|
||||
response = self.client.get(reverse(
|
||||
'admin:ldapentities_ldapuser_changelist'))
|
||||
self.assertEqual(response.status_code, 200)
|
37
gvaldap/ldapentities/tests/test_models.py
Normal file
37
gvaldap/ldapentities/tests/test_models.py
Normal file
|
@ -0,0 +1,37 @@
|
|||
"""
|
||||
This model provides tests for :py:mod:`ldapentities.models`.
|
||||
|
||||
"""
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
from django.test import TestCase
|
||||
|
||||
from passlib.hash import ldap_salted_sha1
|
||||
|
||||
from ldapentities.models import LdapGroup, LdapUser
|
||||
|
||||
|
||||
class LdapGroupTest(TestCase):
|
||||
|
||||
def test___str__(self):
|
||||
ldapgroup = LdapGroup(
|
||||
gid=5000, name='test', description='test group')
|
||||
self.assertEqual(str(ldapgroup), 'test')
|
||||
|
||||
|
||||
class LdapUserTest(TestCase):
|
||||
|
||||
def test___str__(self):
|
||||
ldapuser = LdapUser(
|
||||
uid=5000, group=5000, gecos="a test user",
|
||||
home_directory='/home/test', login_shell='/bin/bash',
|
||||
username='test', password='test', common_name='Test')
|
||||
self.assertEqual(str(ldapuser), 'test')
|
||||
|
||||
def test_set_password(self):
|
||||
ldapuser = LdapUser(
|
||||
uid=5000, group=5000, gecos="a test user",
|
||||
home_directory='/home/test', login_shell='/bin/bash',
|
||||
username='test', password='test', common_name='Test')
|
||||
self.assertEqual(ldapuser.password, 'test')
|
||||
ldapuser.set_password('test2')
|
||||
self.assertTrue(ldap_salted_sha1.verify('test2', ldapuser.password))
|
0
gvaldap/ldaptasks/tests/__init__.py
Normal file
0
gvaldap/ldaptasks/tests/__init__.py
Normal file
138
gvaldap/ldaptasks/tests/test_tasks.py
Normal file
138
gvaldap/ldaptasks/tests/test_tasks.py
Normal file
|
@ -0,0 +1,138 @@
|
|||
"""
|
||||
This module provides tests for :py:mod:`ldaptasks.tasks`.
|
||||
|
||||
"""
|
||||
from __future__ import absolute_import
|
||||
|
||||
from django.conf import settings
|
||||
from django.test import TestCase
|
||||
from celery.exceptions import Reject
|
||||
|
||||
from mockldap import MockLdap
|
||||
|
||||
from ldaptasks.tasks import create_ldap_group, create_ldap_user
|
||||
|
||||
|
||||
class LdapTaskTestCase(TestCase):
|
||||
directory = {
|
||||
settings.DATABASES['ldap']['USER']: {
|
||||
'userPassword': [settings.DATABASES['ldap']['PASSWORD']]
|
||||
},
|
||||
settings.GROUP_BASE_DN: {
|
||||
'objectClass': ['top', 'organizationalUnit'],
|
||||
'ou': ['groups']
|
||||
},
|
||||
settings.USER_BASE_DN: {
|
||||
'objectClass': ['top', 'organizationalUnit'],
|
||||
'ou': ['users']
|
||||
},
|
||||
'cn=existing,' + settings.GROUP_BASE_DN: {
|
||||
'objectClass': ['posixGroup'],
|
||||
'gidNumber': ['4711'],
|
||||
'cn': ['existing'],
|
||||
'description': ['existing test group'],
|
||||
'memberUid': ['existing'],
|
||||
},
|
||||
'uid=existing,' + settings.USER_BASE_DN: {
|
||||
'objectClass': ['account', 'posixAccount'],
|
||||
'uidNumber': ['815'],
|
||||
'gidNumber': ['4711'],
|
||||
'gecos': ['existing test user'],
|
||||
'homeDirectory': ['/home/existing'],
|
||||
'loginShell': ['/bin/bash'],
|
||||
'uid': ['existing'],
|
||||
'userPassword': ['secret'],
|
||||
'cn': ['existing']
|
||||
}
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.mockldap = MockLdap(cls.directory)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.mockldap
|
||||
|
||||
def setUp(self):
|
||||
self.mockldap.start()
|
||||
self.ldapobj = self.mockldap[settings.DATABASES['ldap']['NAME']]
|
||||
|
||||
def tearDown(self):
|
||||
self.mockldap.stop()
|
||||
del self.ldapobj
|
||||
|
||||
def test_create_ldap_group(self):
|
||||
dn = create_ldap_group('test', 5000, 'test group')
|
||||
self.assertEqual('cn=test,%s' % settings.GROUP_BASE_DN, dn)
|
||||
self.assertEquals(
|
||||
self.ldapobj.methods_called(),
|
||||
['initialize', 'simple_bind_s', 'search_s', 'add_s']
|
||||
)
|
||||
|
||||
def test_create_ldap_group_existing(self):
|
||||
dn = create_ldap_group('existing', 4711, 'existing test group')
|
||||
self.assertEqual('cn=existing,%s' % settings.GROUP_BASE_DN, dn)
|
||||
self.assertEquals(
|
||||
self.ldapobj.methods_called(), [
|
||||
'initialize', 'simple_bind_s', 'search_s', 'search_s',
|
||||
'search_s', 'search_s'
|
||||
]
|
||||
)
|
||||
|
||||
def test_create_ldap_group_existing_modify(self):
|
||||
dn = create_ldap_group('existing', 4711, 'change existing test group')
|
||||
self.assertEqual('cn=existing,%s' % settings.GROUP_BASE_DN, dn)
|
||||
self.assertEquals(
|
||||
self.ldapobj.methods_called(), [
|
||||
'initialize', 'simple_bind_s', 'search_s', 'search_s',
|
||||
'search_s', 'search_s', 'modify_s'
|
||||
]
|
||||
)
|
||||
|
||||
def test_create_ldap_user(self):
|
||||
dn = create_ldap_user(
|
||||
'test', 5000, 4711, 'Test User', '/home/test', '/bin/bash',
|
||||
'secret')
|
||||
self.assertEqual('uid=test,%s' % settings.USER_BASE_DN, dn)
|
||||
self.assertEquals(
|
||||
self.ldapobj.methods_called(), [
|
||||
'initialize', 'simple_bind_s', 'search_s', 'search_s',
|
||||
'search_s', 'add_s', 'search_s', 'search_s', 'modify_s'
|
||||
]
|
||||
)
|
||||
|
||||
def test_create_ldap_user_invalid_group(self):
|
||||
with self.assertRaises(Reject):
|
||||
create_ldap_user(
|
||||
'test', 5000, 5000, 'Test User', '/home/test', '/bin/bash',
|
||||
'secret')
|
||||
self.assertEqual(self.ldapobj.methods_called(), [
|
||||
'initialize', 'simple_bind_s', 'search_s', 'search_s'
|
||||
]
|
||||
)
|
||||
|
||||
def test_create_ldap_user_no_password(self):
|
||||
dn = create_ldap_user(
|
||||
'test', 5000, 4711, 'Test User', '/home/test', '/bin/bash',
|
||||
None)
|
||||
self.assertEqual('uid=test,%s' % settings.USER_BASE_DN, dn)
|
||||
self.assertEquals(
|
||||
self.ldapobj.methods_called(), [
|
||||
'initialize', 'simple_bind_s', 'search_s', 'search_s',
|
||||
'search_s', 'add_s', 'search_s', 'search_s', 'modify_s'
|
||||
]
|
||||
)
|
||||
|
||||
def test_create_ldap_user_existing(self):
|
||||
dn = create_ldap_user(
|
||||
'existing', 815, 4711, 'existing test user', '/home/existing',
|
||||
'/bin/bash', 'secret'
|
||||
)
|
||||
self.assertEqual('uid=existing,%s' % settings.USER_BASE_DN, dn)
|
||||
self.assertEquals(
|
||||
self.ldapobj.methods_called(), [
|
||||
'initialize', 'simple_bind_s', 'search_s', 'search_s',
|
||||
'search_s', 'search_s', 'search_s', 'search_s', 'modify_s'
|
||||
]
|
||||
)
|
|
@ -1,3 +1,4 @@
|
|||
# Test dependencies go here.
|
||||
-r base.txt
|
||||
coverage==4.0.3
|
||||
mockldap==0.2.6
|
||||
|
|
Loading…
Reference in a new issue