plug SAML
[auf_rh_dae.git] / project / rh / test / common.py
CommitLineData
493f7aab
OL
1# -*- coding: utf-8 -*-
2
3import datetime
b1b7cca2 4from auf.django.saml.settings import SAML_REDIRECT_FIELD_NAME
3383b2d1 5from django.contrib.auth.models import User, Group
c3550a05 6from project import groups
493f7aab
OL
7from auf.django.references import models as ref
8from django.test import TestCase
9from project.rh import models as rh
10
493f7aab
OL
11class RhTest(TestCase):
12
13 def setUp(self):
14 """
5ac79652
OL
15 DEVISES
16 =======
17 self.devise_cad
18
19 CLASSEMENTS
20 ===========
21 self.classement_p_5_0
22 self.classement_p_5_10
23
24 VALEUR POINTS
25 =============
26 self.vp_cad_scm_2012
27
28 SERVICES
29 ========
30 self.srv_info
31
32 CATEGORIE EMPLOI
33 ================
34 self.cat_emploi_pro
35
36 FAMILLE PROFESSIONNELLE
37 =======================
38 self.famille_cadre
39
40 TYPE POSTES
41 ===========
42 self.type_poste_tech
43
3383b2d1
OL
44 GROUPES
45 =======
46 self.grp_correspondants_rh
47 self.grp_administrateurs
48 self.grp_directeurs_bureau
49 self.grp_drh
50 self.grp_drh2
51 self.grp_accior
52 self.grp_abf
53 self.grp_haute_direction
54 self.grp_service_utilisateurs
55
493f7aab
OL
56 POSTES
57 ======
58 self.poste_cnf_ngaoundere
59 self.poste_cnf_bangui
60 self.poste_bap_bureau
61 self.poste_bap_ifi
62
63 EMPLOYES
64 ========
bcc0c52d
OL
65 0@test.auf: self.employe_cnf_ngaoundere
66 1@test.auf: self.employe_cnf_bangui
67 2@test.auf: self.employe_bap_bureau
68 3@test.auf: self.employe_bap_ifi
493f7aab
OL
69
70 USERS DJANGO
71 ============
72 cf. EMPLOYES
73
74 DOSSIERS
75 ========
76 self.dossier_cnf_ngaoundere
77 self.dossier_cnf_bangui
78 self.dossier_bap_bureau
79 self.dossier_bap_ifi
80
81
82 """
5ac79652
OL
83 #########
84 # DEVISES
85 #########
86 self.devise_cad = rh.Devise(code="CAD", nom="CAD")
87 self.devise_cad.save()
88
89 #############
90 # CLASSEMENTS
91 #############
92 self.classement_p_5_0 = rh.Classement(type="P", echelon=5, degre=1, coefficient=5.01)
93 self.classement_p_5_0.save()
94
95 self.classement_p_5_10 = rh.Classement(type="P", echelon=5, degre=10, coefficient=5.10)
96 self.classement_p_5_10.save()
97
98 ##########
99 # SERVICES
100 ##########
101 self.srv_info = rh.Service(nom="info")
102 self.srv_info.save()
103
104 ###################
105 # CATEGORIES EMPLOI
106 ###################
107 self.cat_emploi_pro = rh.CategorieEmploi(nom="pro")
108 self.cat_emploi_pro.save()
109
110 #################
111 # FAMILLES EMPLOI
112 #################
113 self.famille_cadre = rh.FamilleProfessionnelle(nom="cadre")
114 self.famille_cadre.save()
115
116 #############
117 # TYPES POSTE
118 #############
119 self.type_poste_tech = rh.TypePoste(nom="tech",
120 categorie_emploi=self.cat_emploi_pro,
121 famille_professionnelle=self.famille_cadre)
122 self.type_poste_tech.save()
123
124 #########
125 # GROUPES
126 #########
3383b2d1
OL
127 self.grp_correspondants_rh = Group(name=groups.CORRESPONDANT_RH)
128 self.grp_correspondants_rh.save()
129
130 self.grp_administrateurs = Group(name=groups.ADMINISTRATEURS)
131 self.grp_administrateurs.save()
132
133 self.grp_directeurs_bureau = Group(name=groups.DIRECTEUR_DE_BUREAU)
134 self.grp_directeurs_bureau.save()
135
136 self.grp_drh = Group(name=groups.DRH_NIVEAU_1)
137 self.grp_drh.save()
138
139 self.grp_drh2 = Group(name=groups.DRH_NIVEAU_2)
140 self.grp_drh2.save()
141
142 self.grp_accior = Group(name=groups.ACCIOR)
143 self.grp_accior.save()
144
145 self.grp_abf = Group(name=groups.ABF)
146 self.grp_abf.save()
147
148 self.grp_haute_direction = Group(name=groups.HAUTE_DIRECTION)
149 self.grp_haute_direction.save()
150
151 self.grp_service_utilisateurs = Group(name=groups.SERVICE_UTILISATEURS)
152 self.grp_service_utilisateurs.save()
153
493f7aab 154 self.password = "0000"
f96ec25c 155 self.today = datetime.datetime.now()
07039f95
OL
156 self.tomorrow = self.today + datetime.timedelta(days=1)
157
493f7aab
OL
158 #########################
159 # Régions / Implantations
160 #########################
161 self.REGION_ACGL = ref.Region.objects.get(id=1)
162 self.IMPLANTATION_ACGL_CNF_NGAOUNDERE = ref.Implantation.objects.get(id=90)
163 self.IMPLANTATION_ACGL_CNF_BANGUI = ref.Implantation.objects.get(id=85)
164
165 self.REGION_BAP = ref.Region.objects.get(id=4)
166 self.IMPLANTATION_BAP_BUREAU = ref.Implantation.objects.get(id=51)
167 self.IMPLANTATION_BAP_IFI = ref.Implantation.objects.get(id=55)
168
5ac79652
OL
169 #############
170 #VALEUR POINT
171 #############
172 self.vp_cad_ngaoundere_2012 = rh.ValeurPoint(valeur=1000.1,
173 devise=self.devise_cad,
174 implantation=self.IMPLANTATION_ACGL_CNF_NGAOUNDERE,
175 annee=2012)
176 self.vp_cad_ngaoundere_2012.save()
177
178
493f7aab
OL
179
180 ##########
181 # Employés
182 ##########
183 self.employe_cnf_ngaoundere = rh.Employe(
bcc0c52d 184 id=1,
493f7aab
OL
185 nom="Employé",
186 prenom="ngaoundere",
187 )
188 self.employe_cnf_ngaoundere.save()
189 self.employe_cnf_bangui = rh.Employe(
bcc0c52d 190 id=2,
493f7aab
OL
191 nom="Employé",
192 prenom="bangui",
193 )
194 self.employe_cnf_bangui.save()
195 self.employe_bap_bureau = rh.Employe(
bcc0c52d 196 id=3,
493f7aab
OL
197 nom="Employé",
198 prenom="BAP bureau",
199 )
200 self.employe_bap_bureau.save()
201 self.employe_bap_ifi = rh.Employe(
bcc0c52d 202 id=4,
493f7aab
OL
203 nom="Employé",
204 prenom="BAP IFI",
205 )
206 self.employe_bap_ifi.save()
207
208 ######
209 # ACGL
210 ######
211 self.poste_cnf_ngaoundere = rh.Poste(nom="poste à NGAOUNDERE",
212 implantation=self.IMPLANTATION_ACGL_CNF_NGAOUNDERE)
213
214 self.poste_cnf_bangui = rh.Poste(nom="poste à BANGUI",
215 implantation=self.IMPLANTATION_ACGL_CNF_BANGUI)
216
217 self.poste_cnf_ngaoundere.save()
218 self.poste_cnf_bangui.save()
219
220 self.dossier_cnf_ngaoundere = rh.Dossier(poste=self.poste_cnf_ngaoundere,
f96ec25c 221 employe=self.employe_cnf_ngaoundere, date_debut=self.today)
493f7aab 222 self.dossier_cnf_bangui = rh.Dossier(poste=self.poste_cnf_bangui,
f96ec25c 223 employe=self.employe_cnf_bangui, date_debut=self.today)
493f7aab
OL
224
225 self.dossier_cnf_ngaoundere.save()
226 self.dossier_cnf_bangui.save()
227
228 #####
229 # BAP
230 #####
231 self.poste_bap_bureau = rh.Poste(nom="poste au Bureau BAP",
232 implantation=self.IMPLANTATION_BAP_BUREAU)
233 self.poste_bap_ifi = rh.Poste(nom="poste à l'IFI",
234 implantation=self.IMPLANTATION_BAP_IFI)
235
236 self.poste_bap_bureau.save()
237 self.poste_bap_ifi.save()
238
239 self.dossier_bap_bureau = rh.Dossier(poste=self.poste_bap_bureau,
f96ec25c 240 employe=self.employe_bap_bureau, date_debut=self.today)
493f7aab 241 self.dossier_bap_ifi = rh.Dossier(poste=self.poste_bap_ifi,
f96ec25c 242 employe=self.employe_bap_ifi, date_debut=self.today)
493f7aab
OL
243
244 self.dossier_bap_bureau.save()
245 self.dossier_bap_ifi.save()
246
247 ##############
248 # Users Django
249 ##############
bcc0c52d
OL
250 self._clean_refs()
251 for idx, e in enumerate(rh.Employe.objects.all().order_by('id')):
493f7aab
OL
252 email = u"%s@test.auf" % idx
253 u = User(first_name=e.prenom,
254 is_staff=True,
255 last_name=e.nom,
256 username=email,
257 email=email)
258 u.set_password(self.password)
259 u.save()
260
261 # on le porte dans le référentiel employé
262 ref_e = ref.Employe(id=u.id,
263 nom=e.nom, prenom=e.prenom,
5e8b78d9
OL
264 implantation=e.dossier_principal().poste.implantation,
265 implantation_physique=e.dossier_principal().poste.implantation,
493f7aab
OL
266 service=ref.Service.objects.get(id=1))
267 ref_e.save()
268
bcc0c52d
OL
269 # on le porte dans le référentiel auth
270 ref.Authentification(id=ref_e, courriel=u.email).save()
271
272 def _clean_refs(self):
493f7aab
OL
273 courriels = [u"%s@test.auf" % idx for idx, e in
274 enumerate(rh.Employe.objects.all())]
275 ref.Employe.objects.filter(courriel__in=courriels).delete()
bcc0c52d
OL
276 ref.Authentification.objects.filter(courriel__in=courriels).delete()
277
278 def tearDown(self):
279 self._clean_refs()
493f7aab
OL
280
281 def _test_acces_ok(self, url):
282 response = self.client.get(url, follow=True)
283 self.assertEqual(response.status_code, 200)
284 self.assertEqual('next' in response.context, False)
285
286 def _test_acces_ko(self, url):
287 response = self.client.get(url, follow=True)
b1b7cca2
OL
288 is_ko = response.status_code in (403, 404) or \
289 'next' in response.context or \
290 SAML_REDIRECT_FIELD_NAME in response.context['request'].GET.keys()
493f7aab
OL
291 self.assertEqual(is_ko, True)
292
293 def _test_anonyme(self):
294 pass
295
bcc0c52d 296 def _test_correspondant_rh(self, email="0@test.auf"):
493f7aab 297 u = User.objects.get(email=email)
3383b2d1
OL
298 self.grp_correspondants_rh.user_set.add(u)
299 self.grp_correspondants_rh.save()
493f7aab
OL
300
301 credentials = {'username': email, 'password': self.password}
302 self.assertTrue(self.client.login(**credentials), "login failed")
303
bcc0c52d 304 def _test_administrateur_regional(self, email="0@test.auf"):
493f7aab 305 u = User.objects.get(email=email)
3383b2d1
OL
306 self.grp_administrateurs.user_set.add(u)
307 self.grp_administrateurs.save()
493f7aab
OL
308
309 credentials = {'username': email, 'password': self.password}
310 self.assertTrue(self.client.login(**credentials), "login failed")
311
bcc0c52d 312 def _test_directeur_bureau(self, email="0@test.auf"):
493f7aab 313 u = User.objects.get(email=email)
3383b2d1
OL
314 self.grp_directeurs_bureau.user_set.add(u)
315 self.grp_directeurs_bureau.save()
493f7aab
OL
316
317 credentials = {'username': email, 'password': self.password}
318 self.assertTrue(self.client.login(**credentials), "login failed")
319
320 def _test_drh(self):
321 email = "0@test.auf"
322
323 u = User.objects.get(email=email)
3383b2d1
OL
324 self.grp_drh.user_set.add(u)
325 self.grp_drh.save()
493f7aab
OL
326
327 credentials = {'username': email, 'password': self.password}
328 self.assertTrue(self.client.login(**credentials), "login failed")
329
330 def _test_drh2(self):
331 email = "0@test.auf"
332
333 u = User.objects.get(email=email)
3383b2d1
OL
334 self.grp_drh2.user_set.add(u)
335 self.grp_drh2.save()
493f7aab
OL
336
337 credentials = {'username': email, 'password': self.password}
338 self.assertTrue(self.client.login(**credentials), "login failed")
339
340 def _test_grp_accior(self):
341 email = "0@test.auf"
342
343 u = User.objects.get(email=email)
3383b2d1
OL
344 self.grp_accior.user_set.add(u)
345 self.grp_accior.save()
493f7aab
OL
346
347 credentials = {'username': email, 'password': self.password}
348 self.assertTrue(self.client.login(**credentials), "login failed")
349
350 def _test_grp_abf(self):
351 email = "0@test.auf"
352
353 u = User.objects.get(email=email)
3383b2d1
OL
354 self.grp_abf.user_set.add(u)
355 self.grp_abf.save()
493f7aab
OL
356
357 credentials = {'username': email, 'password': self.password}
358 self.assertTrue(self.client.login(**credentials), "login failed")
359
360 def _test_grp_haute_direction(self):
361 email = "0@test.auf"
362
363 u = User.objects.get(email=email)
3383b2d1
OL
364 self.grp_haute_direction.user_set.add(u)
365 self.grp_haute_direction.save()
493f7aab
OL
366
367 credentials = {'username': email, 'password': self.password}
368 self.assertTrue(self.client.login(**credentials), "login failed")
369
370 def _test_grp_service_utilisateurs(self):
371 email = "0@test.auf"
372
373 u = User.objects.get(email=email)
3383b2d1
OL
374 self.grp_service_utilisateurs.user_set.add(u)
375 self.grp_service_utilisateurs.save()
493f7aab
OL
376
377 credentials = {'username': email, 'password': self.password}
378 self.assertTrue(self.client.login(**credentials), "login failed")