|
@@ -2,8 +2,9 @@
|
|
|
import os
|
|
|
import datetime
|
|
|
from django import db
|
|
|
-from django.test import TestCase, Client
|
|
|
+from django.test import TestCase, Client, override_settings
|
|
|
from django.contrib.auth.models import User
|
|
|
+# from django.contrib.auth.tests.custom_user import ExtensionUser
|
|
|
from coin.members.models import Member, MembershipFee, LdapUser, LdapGroup
|
|
|
import logging
|
|
|
import ldapdb
|
|
@@ -23,21 +24,21 @@ class MemberTests(TestCase):
|
|
|
#~ Créé un membre
|
|
|
first_name = u'Gérard'
|
|
|
last_name = u'Majax'
|
|
|
- ldap_cn = MemberTestsUtils.get_random_ldap_cn()
|
|
|
+ username = MemberTestsUtils.get_random_username()
|
|
|
member = Member(first_name=first_name,
|
|
|
last_name=last_name,
|
|
|
- ldap_cn=ldap_cn)
|
|
|
+ username=username)
|
|
|
member.save()
|
|
|
|
|
|
#~ Récupère l'utilisateur LDAP et fait les tests
|
|
|
- ldap_user = LdapUser.objects.get(pk=ldap_cn)
|
|
|
+ ldap_user = LdapUser.objects.get(pk=username)
|
|
|
|
|
|
self.assertEqual(ldap_user.first_name, first_name)
|
|
|
self.assertEqual(ldap_user.last_name, last_name)
|
|
|
- self.assertEqual(ldap_user.pk, ldap_cn)
|
|
|
+ self.assertEqual(ldap_user.pk, username)
|
|
|
|
|
|
member.delete()
|
|
|
- ldap_user.delete()
|
|
|
+
|
|
|
|
|
|
def test_when_modifiying_member_corresponding_ldap_user_is_also_modified_with_same_data(self):
|
|
|
"""
|
|
@@ -48,9 +49,9 @@ class MemberTests(TestCase):
|
|
|
#~ Créé un membre
|
|
|
first_name = u'Ronald'
|
|
|
last_name = u'Mac Donald'
|
|
|
- ldap_cn = MemberTestsUtils.get_random_ldap_cn()
|
|
|
+ username = MemberTestsUtils.get_random_username()
|
|
|
member = Member(first_name=first_name,
|
|
|
- last_name=last_name, ldap_cn=ldap_cn)
|
|
|
+ last_name=last_name, username=username)
|
|
|
member.save()
|
|
|
|
|
|
#~ Le modifie
|
|
@@ -61,64 +62,81 @@ class MemberTests(TestCase):
|
|
|
member.save()
|
|
|
|
|
|
#~ Récupère l'utilisateur LDAP et fait les tests
|
|
|
- ldap_user = LdapUser.objects.get(pk=ldap_cn)
|
|
|
+ ldap_user = LdapUser.objects.get(pk=username)
|
|
|
|
|
|
self.assertEqual(ldap_user.first_name, new_first_name)
|
|
|
self.assertEqual(ldap_user.last_name, new_last_name)
|
|
|
|
|
|
member.delete()
|
|
|
- ldap_user.delete()
|
|
|
|
|
|
- def test_when_creating_member_corresponding_ldap_user_is_in_coin_ldap_group(self):
|
|
|
- """
|
|
|
- Test que l'utilisateur Ldap fraichement créé est bien dans le group "coin"
|
|
|
- Et que lors de la supression d'un membre, l'utilisateur LDAP correspondant
|
|
|
- est bien retiré du groupe.
|
|
|
- """
|
|
|
- #~ Créé un membre
|
|
|
- ldap_cn = MemberTestsUtils.get_random_ldap_cn()
|
|
|
- member = Member(first_name='Canard',
|
|
|
- last_name='WC', ldap_cn=ldap_cn)
|
|
|
- member.save()
|
|
|
+ # def test_when_creating_member_corresponding_ldap_user_is_in_coin_ldap_group(self):
|
|
|
+ # """
|
|
|
+ # Test que l'utilisateur Ldap fraichement créé est bien dans le group "coin"
|
|
|
+ # Et que lors de la supression d'un membre, l'utilisateur LDAP correspondant
|
|
|
+ # est bien retiré du groupe.
|
|
|
+ # """
|
|
|
+ # #~ Créé un membre
|
|
|
+ # username = MemberTestsUtils.get_random_username()
|
|
|
+ # member = Member(first_name='Canard',
|
|
|
+ # last_name='WC', username=username)
|
|
|
+ # member.save()
|
|
|
|
|
|
- #~ Récupère le group "coin" et test que l'utilisateur y est présent
|
|
|
- ldap_group = LdapGroup.objects.get(pk="coin")
|
|
|
- self.assertEqual(ldap_cn in ldap_group.members, True)
|
|
|
+ # #~ Récupère le group "coin" et test que l'utilisateur y est présent
|
|
|
+ # ldap_group = LdapGroup.objects.get(pk="coin")
|
|
|
+ # self.assertEqual(username in ldap_group.members, True)
|
|
|
|
|
|
- #~ Supprime l'utilisateur
|
|
|
- member.delete()
|
|
|
+ # #~ Supprime l'utilisateur
|
|
|
+ # member.delete()
|
|
|
|
|
|
- #~ Récupère le group "coin" et test que l'utilisateur n'y est plus
|
|
|
- ldap_group = LdapGroup.objects.get(pk="coin")
|
|
|
- self.assertEqual(ldap_cn in ldap_group.members, False)
|
|
|
+ # #~ Récupère le group "coin" et test que l'utilisateur n'y est plus
|
|
|
+ # ldap_group = LdapGroup.objects.get(pk="coin")
|
|
|
+ # self.assertEqual(username in ldap_group.members, False)
|
|
|
|
|
|
- LdapUser.objects.get(pk=ldap_cn).delete()
|
|
|
+ # LdapUser.objects.get(pk=username).delete()
|
|
|
|
|
|
def test_change_password_and_auth(self):
|
|
|
"""
|
|
|
Test que la fonction change_password de member fonctionne et permet
|
|
|
l'authentification avec le nouveau mot de passe
|
|
|
"""
|
|
|
- ldap_cn = MemberTestsUtils.get_random_ldap_cn()
|
|
|
+ username = MemberTestsUtils.get_random_username()
|
|
|
password = "1234"
|
|
|
|
|
|
#~ Créé un nouveau membre
|
|
|
member = Member(first_name=u'Passe-partout',
|
|
|
- last_name=u'Du fort Boyard', ldap_cn=ldap_cn)
|
|
|
+ last_name=u'Du fort Boyard', username=username)
|
|
|
member.save()
|
|
|
|
|
|
#~ Récupère l'utilisateur LDAP
|
|
|
- ldap_user = LdapUser.objects.get(pk=ldap_cn)
|
|
|
+ ldap_user = LdapUser.objects.get(pk=username)
|
|
|
|
|
|
#~ Change son mot de passe
|
|
|
- member.change_password(password)
|
|
|
+ member.set_password(password)
|
|
|
+ member.save()
|
|
|
|
|
|
- #~ Test l'authentification
|
|
|
+ #~ Test l'authentification django
|
|
|
c = Client()
|
|
|
- self.assertEqual(c.login(username=ldap_cn, password=password), True)
|
|
|
+ self.assertEqual(c.login(username=username, password=password), True)
|
|
|
+
|
|
|
+ # Test l'authentification ldap
|
|
|
+ import ldap
|
|
|
+ ldap_conn_settings = db.connections['ldap'].settings_dict
|
|
|
+ l = ldap.initialize(ldap_conn_settings['NAME'])
|
|
|
+ options = ldap_conn_settings.get('CONNECTION_OPTIONS', {})
|
|
|
+ for opt, value in options.items():
|
|
|
+ l.set_option(opt, value)
|
|
|
+
|
|
|
+ if ldap_conn_settings.get('TLS', False):
|
|
|
+ l.start_tls_s()
|
|
|
+
|
|
|
+ # Raise "Invalid credentials" exception if auth fail
|
|
|
+ l.simple_bind_s(ldap_conn_settings['USER'],
|
|
|
+ ldap_conn_settings['PASSWORD'])
|
|
|
+
|
|
|
+ l.unbind_s()
|
|
|
|
|
|
member.delete()
|
|
|
- ldap_user.delete()
|
|
|
+
|
|
|
|
|
|
def test_when_creating_member_ldap_display_name_is_well_defined(self):
|
|
|
"""
|
|
@@ -127,40 +145,41 @@ class MemberTests(TestCase):
|
|
|
"""
|
|
|
first_name = u'Gérard'
|
|
|
last_name = u'Majax'
|
|
|
- ldap_cn = MemberTestsUtils.get_random_ldap_cn()
|
|
|
+ username = MemberTestsUtils.get_random_username()
|
|
|
member = Member(first_name=first_name,
|
|
|
- last_name=last_name, ldap_cn=ldap_cn)
|
|
|
+ last_name=last_name, username=username)
|
|
|
member.save()
|
|
|
|
|
|
#~ Récupère l'utilisateur LDAP
|
|
|
- ldap_user = LdapUser.objects.get(pk=ldap_cn)
|
|
|
+ ldap_user = LdapUser.objects.get(pk=username)
|
|
|
|
|
|
self.assertEqual(ldap_user.display_name, '%s %s' %
|
|
|
(first_name, last_name))
|
|
|
|
|
|
member.delete()
|
|
|
- ldap_user.delete()
|
|
|
|
|
|
- def test_when_creating_member_ldap_cn_is_well_defined(self):
|
|
|
+
|
|
|
+ def test_when_creating_member_username_is_well_defined(self):
|
|
|
"""
|
|
|
- Lors de la création d'un membre, le champ "ldap_cn", s'il n'est pas
|
|
|
+ Lors de la création d'un membre, le champ "username", s'il n'est pas
|
|
|
définit doit être généré avec les contraintes suivantes :
|
|
|
premières lettres du prénom + nom le tout en minuscule,
|
|
|
sans caractères accentués et sans espaces.
|
|
|
"""
|
|
|
random = os.urandom(4).encode('hex')
|
|
|
first_name = u'Gérard-Étienne'
|
|
|
- last_name = u'Majax de la Boétie-Blop' + random
|
|
|
+ last_name = u'Majax de la Boétie!B' + random
|
|
|
|
|
|
- control = 'gemajaxdelaboetieblop' + random
|
|
|
+ control = 'gemajaxdelaboetieb' + random
|
|
|
+ control = control[:30]
|
|
|
|
|
|
member = Member(first_name=first_name, last_name=last_name)
|
|
|
member.save()
|
|
|
|
|
|
- self.assertEqual(member.ldap_cn, control)
|
|
|
+ self.assertEqual(member.username, control)
|
|
|
|
|
|
member.delete()
|
|
|
- LdapUser.objects.get(pk=member.ldap_cn).delete()
|
|
|
+
|
|
|
|
|
|
def test_when_saving_member_and_ldap_fail_dont_save(self):
|
|
|
"""
|
|
@@ -178,9 +197,9 @@ class MemberTests(TestCase):
|
|
|
# Créé un membre
|
|
|
first_name = u'Du'
|
|
|
last_name = u'Pont'
|
|
|
- ldap_cn = MemberTestsUtils.get_random_ldap_cn()
|
|
|
+ username = MemberTestsUtils.get_random_username()
|
|
|
member = Member(first_name=first_name,
|
|
|
- last_name=last_name, ldap_cn=ldap_cn)
|
|
|
+ last_name=last_name, username=username)
|
|
|
|
|
|
# Le sauvegarde en base de donnée
|
|
|
# Le save devrait renvoyer une exception parceque le LDAP échoue
|
|
@@ -188,7 +207,7 @@ class MemberTests(TestCase):
|
|
|
|
|
|
# On s'assure, malgré l'exception, que le membre n'est pas en base
|
|
|
with self.assertRaises(Member.DoesNotExist):
|
|
|
- Member.objects.get(ldap_cn=ldap_cn)
|
|
|
+ Member.objects.get(username=username)
|
|
|
|
|
|
# Restaure le mot de passe pour les tests suivants
|
|
|
for dbconnection in db.connections.all():
|
|
@@ -196,33 +215,33 @@ class MemberTests(TestCase):
|
|
|
ldapdb.backends.ldap.base.DatabaseWrapper):
|
|
|
dbconnection.settings_dict['PASSWORD'] = dbconnection.settings_dict['PREVIOUSPASSWORD']
|
|
|
|
|
|
- def test_when_user_login_member_user_field_is_updated(self):
|
|
|
- """
|
|
|
- Test que lorqu'un utilisateur se connect, le champ user du membre
|
|
|
- correspondant est mis à jour convenablement
|
|
|
- """
|
|
|
- # Créé un membre
|
|
|
- first_name = u'Du'
|
|
|
- last_name = u'Pond'
|
|
|
- password = '1234'
|
|
|
- ldap_cn = MemberTestsUtils.get_random_ldap_cn()
|
|
|
- member = Member(first_name=first_name,
|
|
|
- last_name=last_name, ldap_cn=ldap_cn)
|
|
|
- member.save()
|
|
|
- member.change_password(password)
|
|
|
-
|
|
|
- # Vérifie que user non définit
|
|
|
- self.assertIsNone(member.user)
|
|
|
-
|
|
|
- # Connection
|
|
|
- c = Client()
|
|
|
- self.assertEqual(c.login(username=ldap_cn, password=password), True)
|
|
|
-
|
|
|
- # Vérifie que user définit
|
|
|
- member = Member.objects.get(ldap_cn=ldap_cn)
|
|
|
- self.assertIsNotNone(member.user)
|
|
|
-
|
|
|
- LdapUser.objects.get(pk=member.ldap_cn).delete()
|
|
|
+ # def test_when_user_login_member_user_field_is_updated(self):
|
|
|
+ # """
|
|
|
+ # Test que lorqu'un utilisateur se connect, le champ user du membre
|
|
|
+ # correspondant est mis à jour convenablement
|
|
|
+ # """
|
|
|
+ # # Créé un membre
|
|
|
+ # first_name = u'Du'
|
|
|
+ # last_name = u'Pond'
|
|
|
+ # password = '1234'
|
|
|
+ # username = MemberTestsUtils.get_random_username()
|
|
|
+ # member = Member(first_name=first_name,
|
|
|
+ # last_name=last_name, username=username)
|
|
|
+ # member.save()
|
|
|
+ # member.change_password(password)
|
|
|
+
|
|
|
+ # # Vérifie que user non définit
|
|
|
+ # self.assertIsNone(member.user)
|
|
|
+
|
|
|
+ # # Connection
|
|
|
+ # c = Client()
|
|
|
+ # self.assertEqual(c.login(username=username, password=password), True)
|
|
|
+
|
|
|
+ # # Vérifie que user définit
|
|
|
+ # member = Member.objects.get(username=username)
|
|
|
+ # self.assertIsNotNone(member.user)
|
|
|
+
|
|
|
+ # LdapUser.objects.get(pk=member.username).delete()
|
|
|
|
|
|
def test_member_end_date_of_memberhip(self):
|
|
|
"""
|
|
@@ -231,9 +250,9 @@ class MemberTests(TestCase):
|
|
|
#Créer un membre
|
|
|
first_name = u'Tin'
|
|
|
last_name = u'Tin'
|
|
|
- ldap_cn = MemberTestsUtils.get_random_ldap_cn()
|
|
|
+ username = MemberTestsUtils.get_random_username()
|
|
|
member = Member(first_name=first_name,
|
|
|
- last_name=last_name, ldap_cn=ldap_cn)
|
|
|
+ last_name=last_name, username=username)
|
|
|
member.save()
|
|
|
|
|
|
start_date = datetime.date.today()
|
|
@@ -247,7 +266,6 @@ class MemberTests(TestCase):
|
|
|
|
|
|
self.assertEqual(member.end_date_of_membership(), end_date)
|
|
|
|
|
|
- LdapUser.objects.get(pk=member.ldap_cn).delete()
|
|
|
|
|
|
def test_member_is_paid_up(self):
|
|
|
"""
|
|
@@ -256,9 +274,9 @@ class MemberTests(TestCase):
|
|
|
#Créé un membre
|
|
|
first_name = u'Capitain'
|
|
|
last_name = u'Haddock'
|
|
|
- ldap_cn = MemberTestsUtils.get_random_ldap_cn()
|
|
|
+ username = MemberTestsUtils.get_random_username()
|
|
|
member = Member(first_name=first_name,
|
|
|
- last_name=last_name, ldap_cn=ldap_cn)
|
|
|
+ last_name=last_name, username=username)
|
|
|
member.save()
|
|
|
|
|
|
start_date = datetime.date.today()
|
|
@@ -285,8 +303,6 @@ class MemberTests(TestCase):
|
|
|
# de cotisation
|
|
|
self.assertEqual(member.is_paid_up(), True)
|
|
|
|
|
|
- LdapUser.objects.get(pk=member.ldap_cn).delete()
|
|
|
-
|
|
|
|
|
|
class MemberAdminTests(TestCase):
|
|
|
|
|
@@ -295,37 +311,45 @@ class MemberAdminTests(TestCase):
|
|
|
self.client = Client()
|
|
|
#~ Créé un superuser
|
|
|
self.admin_user_password = '1234'
|
|
|
- self.admin_user = User.objects.create_superuser(
|
|
|
- 'test_admin_user', 'i@mail.com', self.admin_user_password)
|
|
|
+ self.admin_user = Member.objects.create_superuser(
|
|
|
+ username = 'test_admin_user',
|
|
|
+ first_name = 'test',
|
|
|
+ last_name = 'Admin user',
|
|
|
+ email='i@mail.com',
|
|
|
+ password=self.admin_user_password)
|
|
|
#~ Connection
|
|
|
self.assertEqual(self.client.login(
|
|
|
username=self.admin_user.username, password=self.admin_user_password), True)
|
|
|
+
|
|
|
+ def tearDown(self):
|
|
|
+ # Supprime le superuser
|
|
|
+ self.admin_user.delete()
|
|
|
|
|
|
- def test_cant_change_ldap_cn_when_editing(self):
|
|
|
+ def test_cant_change_username_when_editing(self):
|
|
|
"""
|
|
|
- Vérifie que dans l'admin Django, le champ ldap_cn n'est pad modifiable
|
|
|
+ Vérifie que dans l'admin Django, le champ username n'est pad modifiable
|
|
|
sur une fiche existante
|
|
|
"""
|
|
|
#~ Créé un membre
|
|
|
first_name = u'Gérard'
|
|
|
last_name = u'Majax'
|
|
|
- ldap_cn = MemberTestsUtils.get_random_ldap_cn()
|
|
|
+ username = MemberTestsUtils.get_random_username()
|
|
|
member = Member(first_name=first_name,
|
|
|
- last_name=last_name, ldap_cn=ldap_cn)
|
|
|
+ last_name=last_name, username=username)
|
|
|
member.save()
|
|
|
|
|
|
edit_page = self.client.get('/admin/members/member/%i/' % member.id)
|
|
|
self.assertNotContains(edit_page,
|
|
|
- '''<input id="id_ldap_cn" />''',
|
|
|
+ '''<input id="id_username" />''',
|
|
|
html=True)
|
|
|
|
|
|
- LdapUser.objects.get(pk=ldap_cn).delete()
|
|
|
+ LdapUser.objects.get(pk=username).delete()
|
|
|
|
|
|
|
|
|
class MemberTestsUtils(object):
|
|
|
|
|
|
@staticmethod
|
|
|
- def get_random_ldap_cn():
|
|
|
+ def get_random_username():
|
|
|
"""
|
|
|
Renvoi une clé aléatoire pour un utilisateur LDAP
|
|
|
"""
|