tests: PEP8ness and style on test_ldap (#30535)
This commit is contained in:
parent
0861171b5b
commit
9d96274c7b
|
@ -1,4 +1,20 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright (C) 2017-2019 Entr'ouvert
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify it
|
||||
# under the terms of the GNU Affero General Public License as published
|
||||
# by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import os
|
||||
|
||||
import pytest
|
||||
|
@ -6,7 +22,6 @@ import mock
|
|||
|
||||
import ldap
|
||||
from ldap.dn import escape_dn_chars
|
||||
import ldap
|
||||
|
||||
from ldaptools.slapd import Slapd, has_slapd
|
||||
from django.contrib.auth import get_user_model
|
||||
|
@ -18,10 +33,12 @@ from django.utils import timezone
|
|||
from authentic2.a2_rbac.utils import get_default_ou
|
||||
from django_rbac.utils import get_ou_model
|
||||
from authentic2.backends import ldap_backend
|
||||
from authentic2 import crypto
|
||||
from authentic2 import crypto, models
|
||||
|
||||
import utils
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
pytestmark = pytest.mark.skipunless(has_slapd(), reason='slapd is not installed')
|
||||
|
||||
USERNAME = u'etienne.michu'
|
||||
|
@ -97,8 +114,7 @@ def test_connection(slapd):
|
|||
conn.simple_bind_s(DN, PASS)
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_simple(slapd, settings, client):
|
||||
def test_simple(slapd, settings, client, db):
|
||||
settings.LDAP_AUTH_SETTINGS = [{
|
||||
'url': [slapd.ldap_url],
|
||||
'basedn': u'o=ôrga',
|
||||
|
@ -109,7 +125,6 @@ def test_simple(slapd, settings, client):
|
|||
'password': PASS}, follow=True)
|
||||
assert result.status_code == 200
|
||||
assert 'Étienne Michu' in str(result)
|
||||
User = get_user_model()
|
||||
assert User.objects.count() == 1
|
||||
user = User.objects.get()
|
||||
assert user.username == u'%s@ldap' % USERNAME
|
||||
|
@ -138,7 +153,6 @@ def test_simple_with_binddn(slapd, settings, client):
|
|||
'password': PASS}, follow=True)
|
||||
assert result.status_code == 200
|
||||
assert 'Étienne Michu' in str(result)
|
||||
User = get_user_model()
|
||||
assert User.objects.count() == 1
|
||||
user = User.objects.get()
|
||||
assert user.username == u'%s@ldap' % USERNAME
|
||||
|
@ -152,8 +166,8 @@ def test_simple_with_binddn(slapd, settings, client):
|
|||
assert not user.check_password(PASS)
|
||||
assert 'password' not in client.session['ldap-data']
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_double_login(slapd, simple_user, settings, app):
|
||||
|
||||
def test_double_login(slapd, simple_user, settings, app, db):
|
||||
settings.LDAP_AUTH_SETTINGS = [{
|
||||
'url': [slapd.ldap_url],
|
||||
'basedn': u'o=ôrga',
|
||||
|
@ -165,8 +179,7 @@ def test_double_login(slapd, simple_user, settings, app):
|
|||
utils.login(app, UID, password=PASS, path='/admin/')
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_keep_password_in_session(slapd, settings, client):
|
||||
def test_keep_password_in_session(slapd, settings, client, db):
|
||||
settings.LDAP_AUTH_SETTINGS = [{
|
||||
'url': [slapd.ldap_url],
|
||||
'basedn': u'o=ôrga',
|
||||
|
@ -178,7 +191,6 @@ def test_keep_password_in_session(slapd, settings, client):
|
|||
'password': PASS.decode('utf-8')}, follow=True)
|
||||
assert result.status_code == 200
|
||||
assert 'Étienne Michu' in str(result)
|
||||
User = get_user_model()
|
||||
assert User.objects.count() == 1
|
||||
user = User.objects.get()
|
||||
assert user.username == u'%s@ldap' % USERNAME
|
||||
|
@ -207,7 +219,6 @@ def test_custom_ou(slapd, settings, client):
|
|||
'password': PASS}, follow=True)
|
||||
assert result.status_code == 200
|
||||
assert 'Étienne Michu' in str(result)
|
||||
User = get_user_model()
|
||||
assert User.objects.count() == 1
|
||||
user = User.objects.get()
|
||||
assert user.username == u'%s@ldap' % USERNAME
|
||||
|
@ -217,8 +228,7 @@ def test_custom_ou(slapd, settings, client):
|
|||
assert not user.check_password(PASS)
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_wrong_ou(slapd, settings, client):
|
||||
def test_wrong_ou(slapd, settings, client, db):
|
||||
settings.LDAP_AUTH_SETTINGS = [{
|
||||
'url': [slapd.ldap_url],
|
||||
'basedn': u'o=ôrga',
|
||||
|
@ -246,8 +256,7 @@ def test_dn_formatter():
|
|||
assert formatter.format('uid={uid}', uid=['john doé!#$"\'-_']) == 'uid=john doé!#$\\"\'-_'
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_group_mapping(slapd, settings, client):
|
||||
def test_group_mapping(slapd, settings, client, db):
|
||||
from django.contrib.auth.models import Group
|
||||
|
||||
settings.LDAP_AUTH_SETTINGS = [{
|
||||
|
@ -268,8 +277,7 @@ def test_group_mapping(slapd, settings, client):
|
|||
assert response.context['user'].groups.count() == 1
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_posix_group_mapping(slapd, settings, client):
|
||||
def test_posix_group_mapping(slapd, settings, client, db):
|
||||
from django.contrib.auth.models import Group
|
||||
|
||||
settings.LDAP_AUTH_SETTINGS = [{
|
||||
|
@ -291,8 +299,7 @@ def test_posix_group_mapping(slapd, settings, client):
|
|||
assert response.context['user'].groups.count() == 1
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_group_to_role_mapping(slapd, settings, client):
|
||||
def test_group_to_role_mapping(slapd, settings, client, db):
|
||||
from authentic2.a2_rbac.models import Role
|
||||
|
||||
Role.objects.get_or_create(name='Role1')
|
||||
|
@ -311,8 +318,7 @@ def test_group_to_role_mapping(slapd, settings, client):
|
|||
assert response.context['user'].roles.count() == 1
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_posix_group_to_role_mapping(slapd, settings, client):
|
||||
def test_posix_group_to_role_mapping(slapd, settings, client, db):
|
||||
from authentic2.a2_rbac.models import Role
|
||||
|
||||
Role.objects.get_or_create(name='Role2')
|
||||
|
@ -332,8 +338,7 @@ def test_posix_group_to_role_mapping(slapd, settings, client):
|
|||
assert response.context['user'].roles.count() == 1
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_group_su(slapd, settings, client):
|
||||
def test_group_su(slapd, settings, client, db):
|
||||
from django.contrib.auth.models import Group
|
||||
|
||||
settings.LDAP_AUTH_SETTINGS = [{
|
||||
|
@ -351,8 +356,7 @@ def test_group_su(slapd, settings, client):
|
|||
assert not response.context['user'].is_staff
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_group_staff(slapd, settings, client):
|
||||
def test_group_staff(slapd, settings, client, db):
|
||||
from django.contrib.auth.models import Group
|
||||
|
||||
settings.LDAP_AUTH_SETTINGS = [{
|
||||
|
@ -370,12 +374,10 @@ def test_group_staff(slapd, settings, client):
|
|||
assert not response.context['user'].is_superuser
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_get_users(slapd, settings):
|
||||
def test_get_users(slapd, settings, db):
|
||||
import django.db.models.base
|
||||
from types import MethodType
|
||||
|
||||
User = get_user_model()
|
||||
settings.LDAP_AUTH_SETTINGS = [{
|
||||
'url': [slapd.ldap_url],
|
||||
'basedn': u'o=ôrga',
|
||||
|
@ -434,10 +436,10 @@ def test_get_users(slapd, settings):
|
|||
save.reset_mock()
|
||||
bulk_create.reset_mock()
|
||||
u = ldap_backend.LDAPUser.objects.create(username=UID.capitalize())
|
||||
eid = ldap_backend.UserExternalId.objects.create(external_id=UID.capitalize(),
|
||||
source='ldap', user=u)
|
||||
ldap_backend.UserExternalId.objects.create(external_id=UID.capitalize(),
|
||||
source='ldap', user=u)
|
||||
# set user login time as if he logged in
|
||||
user = ldap_backend.LDAPUser.objects.get(username='%s@ldap'%UID)
|
||||
user = ldap_backend.LDAPUser.objects.get(username='%s@ldap' % UID)
|
||||
user.last_login = timezone.now()
|
||||
user.save()
|
||||
assert ldap_backend.LDAPUser.objects.count() == 102
|
||||
|
@ -445,13 +447,12 @@ def test_get_users(slapd, settings):
|
|||
assert len(users) == 101
|
||||
assert ldap_backend.LDAPUser.objects.filter(username='%s' % UID.capitalize()).count() == 0
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_set_mandatory_roles(slapd, settings):
|
||||
|
||||
def test_set_mandatory_roles(slapd, settings, db):
|
||||
from authentic2.a2_rbac.models import Role
|
||||
|
||||
Role.objects.get_or_create(name='tech')
|
||||
Role.objects.get_or_create(name='admin')
|
||||
User = get_user_model()
|
||||
settings.LDAP_AUTH_SETTINGS = [{
|
||||
'url': [slapd.ldap_url],
|
||||
'basedn': u'o=ôrga',
|
||||
|
@ -468,9 +469,7 @@ def test_set_mandatory_roles(slapd, settings):
|
|||
assert User.objects.first().roles.count() == 2
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_nocreate_mandatory_roles(slapd, settings):
|
||||
User = get_user_model()
|
||||
def test_nocreate_mandatory_roles(slapd, settings, db):
|
||||
settings.LDAP_AUTH_SETTINGS = [{
|
||||
'url': [slapd.ldap_url],
|
||||
'basedn': u'o=ôrga',
|
||||
|
@ -542,7 +541,6 @@ def test_reset_password_ldap_user(slapd, settings, app, db):
|
|||
'basedn': u'o=ôrga',
|
||||
'use_tls': False,
|
||||
}]
|
||||
User = get_user_model()
|
||||
assert User.objects.count() == 0
|
||||
# first login
|
||||
response = app.get('/login/')
|
||||
|
@ -590,7 +588,6 @@ def test_user_cannot_change_password(slapd, settings, app, db):
|
|||
'use_tls': False,
|
||||
'user_can_change_password': False,
|
||||
}]
|
||||
User = get_user_model()
|
||||
assert User.objects.count() == 0
|
||||
# first login
|
||||
response = app.get('/login/')
|
||||
|
|
Loading…
Reference in New Issue