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