runner.py 6.64 KB
Newer Older
Maël Kervella's avatar
Maël Kervella committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
# se veut agnostique au réseau considéré, de manière à être installable en
# quelques clics.
#
# Copyright © 2017  Gabriel Détraz
# Copyright © 2017  Goulven Kermarec
# Copyright © 2017  Augustin Lemesle
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

"""Defines the custom runners for Re2o.
"""

import volatildap
import os.path

from django.test.runner import DiscoverRunner
from django.conf import settings

from users.models import LdapUser, LdapUserGroup, LdapServiceUser, LdapServiceUserGroup

# The path of this file
__here = os.path.dirname(os.path.realpath(__file__))
# The absolute path where to find the schemas for the LDAP
schema_path = os.path.abspath(os.path.join(__here, 'ldap', 'schema'))
# The absolute path of the "radius.schema" file
radius_schema_path = os.path.join(schema_path, 'radius.schema')
# The absolute path of the "samba.schema" file
samba_schema_path = os.path.join(schema_path, 'samba.schema')

# The suffix for the LDAP
suffix = 'dc=example,dc=net'
# The admin CN of the LDAP
rootdn = 'cn=admin,'+suffix

# Defines all ldap_entry mandatory for Re2o under a key-value list format
# that can be used directly by volatildap. For more on how to generate this
# data, see https://gitlab.federez.net/re2o/scripts/blob/master/print_ldap_entries.py
ldapentry_Utilisateurs = ('cn=Utilisateurs,'+suffix, {
    'cn': ['Utilisateurs'],
    'sambaSID': ['500'],
    'uid': ['Users'],
    'objectClass': ['posixGroup', 'top', 'sambaSamAccount', 'radiusprofile'],
    'gidNumber': ['500'],
})
ldapentry_groups = ('ou=groups,'+suffix, {
    'ou': ['groups'],
    'objectClass': ['organizationalUnit'],
    'description': ["Groupes d'utilisateurs"],
})
ldapentry_services = ('ou=services,ou=groups,'+suffix, {
    'ou': ['services'],
    'objectClass': ['organizationalUnit'],
    'description': ['Groupes de comptes techniques'],
})
ldapentry_service_users = ('ou=service-users,'+suffix, {
    'ou': ['service-users'],
    'objectClass': ['organizationalUnit'],
    'description': ["Utilisateurs techniques de l'annuaire"],
})
ldapentry_freeradius = ('cn=freeradius,ou=service-users,'+suffix, {
    'cn': ['freeradius'],
    'objectClass': ['applicationProcess', 'simpleSecurityObject'],
    'userPassword': ['FILL_IT'],
})
ldapentry_nssauth = ('cn=nssauth,ou=service-users,'+suffix, {
    'cn': ['nssauth'],
    'objectClass': ['applicationProcess', 'simpleSecurityObject'],
    'userPassword': ['FILL_IT'],
})
ldapentry_auth = ('cn=auth,ou=services,ou=groups,'+suffix, {
    'cn': ['auth'],
    'objectClass': ['groupOfNames'],
    'member': ['cn=nssauth,ou=service-users,'+suffix],
})
ldapentry_posix = ('ou=posix,ou=groups,'+suffix, {
    'ou': ['posix'],
    'objectClass': ['organizationalUnit'],
    'description': ['Groupes de comptes POSIX'],
})
ldapentry_wifi = ('cn=wifi,ou=service-users,'+suffix, {
    'cn': ['wifi'],
    'objectClass': ['applicationProcess', 'simpleSecurityObject'],
    'userPassword': ['FILL_IT'],
})
ldapentry_usermgmt = ('cn=usermgmt,ou=services,ou=groups,'+suffix, {
    'cn': ['usermgmt'],
    'objectClass': ['groupOfNames'],
    'member': ['cn=wifi,ou=service-users,'+suffix],
})
ldapentry_replica = ('cn=replica,ou=service-users,'+suffix, {
    'cn': ['replica'],
    'objectClass': ['applicationProcess', 'simpleSecurityObject'],
    'userPassword': ['FILL_IT'],
})
ldapentry_readonly = ('cn=readonly,ou=services,ou=groups,'+suffix, {
    'cn': ['readonly'],
    'objectClass': ['groupOfNames'],
    'member': ['cn=replica,ou=service-users,'+suffix, 'cn=freeradius,ou=service-users,'+suffix],
})
ldapbasic = dict([ldapentry_Utilisateurs, ldapentry_groups,
                  ldapentry_services, ldapentry_service_users,
                  ldapentry_freeradius, ldapentry_nssauth, ldapentry_auth,
                  ldapentry_posix, ldapentry_wifi, ldapentry_usermgmt,
                  ldapentry_replica, ldapentry_readonly])


class DiscoverLdapRunner(DiscoverRunner):
    """Discovers all the tests in the project

    This is a simple subclass of the default test runner
    `django.test.runner.DiscoverRunner` that creates a test LDAP
    right after the test databases are setup and destroys it right
    before the test databases are setup.
    It also ensure re2o's settings are using this new LDAP.
    """

    # The `volatildap.LdapServer` instance initiated with the minimal
    # structure required by Re2o
    ldap_server = volatildap.LdapServer(
        suffix=suffix,
        rootdn=rootdn,
        initial_data=ldapbasic,
        schemas=['core.schema', 'cosine.schema', 'inetorgperson.schema',
                 'nis.schema', radius_schema_path, samba_schema_path]
    )

    def __init__(self, *args, **kwargs):
        settings.DATABASES['ldap']['USER'] = self.ldap_server.rootdn
        settings.DATABASES['ldap']['PASSWORD'] = self.ldap_server.rootpw
        settings.DATABASES['ldap']['NAME'] = self.ldap_server.uri
        settings.LDAP['base_user_dn'] = ldapentry_Utilisateurs[0]
        settings.LDAP['base_userservice_dn'] = ldapentry_service_users[0]
        settings.LDAP['base_usergroup_dn'] = ldapentry_posix[0]
        settings.LDAP['base_userservicegroup_dn'] = ldapentry_services[0]
        settings.LDAP['user_gid'] = ldapentry_Utilisateurs[1].get('gidNumber', ["500"])[0]
        LdapUser.base_dn = settings.LDAP['base_user_dn']
        LdapUserGroup.base_dn = settings.LDAP['base_usergroup_dn']
        LdapServiceUser.base_dn = settings.LDAP['base_userservice_dn']
        LdapServiceUserGroup.base_dn = settings.LDAP['base_userservicegroup_dn']
        super(DiscoverLdapRunner, self).__init__(*args, **kwargs)


    def setup_databases(self, *args, **kwargs):
        ret = super(DiscoverLdapRunner, self).setup_databases(*args, **kwargs)        
158
        print("Creating test LDAP with volatildap...")
Maël Kervella's avatar
Maël Kervella committed
159 160 161 162 163
        self.ldap_server.start()
        return ret

    def teardown_databases(self, *args, **kwargs):
        self.ldap_server.stop()
164
        print("Destroying test LDAP...")
Maël Kervella's avatar
Maël Kervella committed
165 166
        super(DiscoverLdapRunner, self).teardown_databases(*args, **kwargs)