fix group machinerie
[auf_rh_dae.git] / project / rh / test / common.py
1 # -*- coding: utf-8 -*-
2
3 import datetime
4 from django.contrib.auth.models import User, Group
5 from project import groups
6 from auf.django.references import models as ref
7 from django.test import TestCase
8 from project.rh import models as rh
9
10 class RhTest(TestCase):
11
12 def setUp(self):
13 """
14 GROUPES
15 =======
16 self.grp_correspondants_rh
17 self.grp_administrateurs
18 self.grp_directeurs_bureau
19 self.grp_drh
20 self.grp_drh2
21 self.grp_accior
22 self.grp_abf
23 self.grp_haute_direction
24 self.grp_service_utilisateurs
25
26 POSTES
27 ======
28 self.poste_cnf_ngaoundere
29 self.poste_cnf_bangui
30 self.poste_bap_bureau
31 self.poste_bap_ifi
32
33 EMPLOYES
34 ========
35 0@test.auf: self.employe_cnf_ngaoundere
36 1@test.auf: self.employe_cnf_bangui
37 2@test.auf: self.employe_bap_bureau
38 3@test.auf: self.employe_bap_ifi
39
40 USERS DJANGO
41 ============
42 cf. EMPLOYES
43
44 DOSSIERS
45 ========
46 self.dossier_cnf_ngaoundere
47 self.dossier_cnf_bangui
48 self.dossier_bap_bureau
49 self.dossier_bap_ifi
50
51
52 """
53 self.grp_correspondants_rh = Group(name=groups.CORRESPONDANT_RH)
54 self.grp_correspondants_rh.save()
55
56 self.grp_administrateurs = Group(name=groups.ADMINISTRATEURS)
57 self.grp_administrateurs.save()
58
59 self.grp_directeurs_bureau = Group(name=groups.DIRECTEUR_DE_BUREAU)
60 self.grp_directeurs_bureau.save()
61
62 self.grp_drh = Group(name=groups.DRH_NIVEAU_1)
63 self.grp_drh.save()
64
65 self.grp_drh2 = Group(name=groups.DRH_NIVEAU_2)
66 self.grp_drh2.save()
67
68 self.grp_accior = Group(name=groups.ACCIOR)
69 self.grp_accior.save()
70
71 self.grp_abf = Group(name=groups.ABF)
72 self.grp_abf.save()
73
74 self.grp_haute_direction = Group(name=groups.HAUTE_DIRECTION)
75 self.grp_haute_direction.save()
76
77 self.grp_service_utilisateurs = Group(name=groups.SERVICE_UTILISATEURS)
78 self.grp_service_utilisateurs.save()
79
80 self.password = "0000"
81 self.today = datetime.datetime.now()
82 #########################
83 # Régions / Implantations
84 #########################
85 self.REGION_ACGL = ref.Region.objects.get(id=1)
86 self.IMPLANTATION_ACGL_CNF_NGAOUNDERE = ref.Implantation.objects.get(id=90)
87 self.IMPLANTATION_ACGL_CNF_BANGUI = ref.Implantation.objects.get(id=85)
88
89 self.REGION_BAP = ref.Region.objects.get(id=4)
90 self.IMPLANTATION_BAP_BUREAU = ref.Implantation.objects.get(id=51)
91 self.IMPLANTATION_BAP_IFI = ref.Implantation.objects.get(id=55)
92
93
94 ##########
95 # Employés
96 ##########
97 self.employe_cnf_ngaoundere = rh.Employe(
98 id=1,
99 nom="Employé",
100 prenom="ngaoundere",
101 )
102 self.employe_cnf_ngaoundere.save()
103 self.employe_cnf_bangui = rh.Employe(
104 id=2,
105 nom="Employé",
106 prenom="bangui",
107 )
108 self.employe_cnf_bangui.save()
109 self.employe_bap_bureau = rh.Employe(
110 id=3,
111 nom="Employé",
112 prenom="BAP bureau",
113 )
114 self.employe_bap_bureau.save()
115 self.employe_bap_ifi = rh.Employe(
116 id=4,
117 nom="Employé",
118 prenom="BAP IFI",
119 )
120 self.employe_bap_ifi.save()
121
122 ######
123 # ACGL
124 ######
125 self.poste_cnf_ngaoundere = rh.Poste(nom="poste à NGAOUNDERE",
126 implantation=self.IMPLANTATION_ACGL_CNF_NGAOUNDERE)
127
128 self.poste_cnf_bangui = rh.Poste(nom="poste à BANGUI",
129 implantation=self.IMPLANTATION_ACGL_CNF_BANGUI)
130
131 self.poste_cnf_ngaoundere.save()
132 self.poste_cnf_bangui.save()
133
134 self.dossier_cnf_ngaoundere = rh.Dossier(poste=self.poste_cnf_ngaoundere,
135 employe=self.employe_cnf_ngaoundere, date_debut=self.today)
136 self.dossier_cnf_bangui = rh.Dossier(poste=self.poste_cnf_bangui,
137 employe=self.employe_cnf_bangui, date_debut=self.today)
138
139 self.dossier_cnf_ngaoundere.save()
140 self.dossier_cnf_bangui.save()
141
142 #####
143 # BAP
144 #####
145 self.poste_bap_bureau = rh.Poste(nom="poste au Bureau BAP",
146 implantation=self.IMPLANTATION_BAP_BUREAU)
147 self.poste_bap_ifi = rh.Poste(nom="poste à l'IFI",
148 implantation=self.IMPLANTATION_BAP_IFI)
149
150 self.poste_bap_bureau.save()
151 self.poste_bap_ifi.save()
152
153 self.dossier_bap_bureau = rh.Dossier(poste=self.poste_bap_bureau,
154 employe=self.employe_bap_bureau, date_debut=self.today)
155 self.dossier_bap_ifi = rh.Dossier(poste=self.poste_bap_ifi,
156 employe=self.employe_bap_ifi, date_debut=self.today)
157
158 self.dossier_bap_bureau.save()
159 self.dossier_bap_ifi.save()
160
161 ##############
162 # Users Django
163 ##############
164 self._clean_refs()
165 for idx, e in enumerate(rh.Employe.objects.all().order_by('id')):
166 email = u"%s@test.auf" % idx
167 u = User(first_name=e.prenom,
168 is_staff=True,
169 last_name=e.nom,
170 username=email,
171 email=email)
172 u.set_password(self.password)
173 u.save()
174
175 # on le porte dans le référentiel employé
176 ref_e = ref.Employe(id=u.id,
177 nom=e.nom, prenom=e.prenom,
178 implantation=e.poste_principal().implantation,
179 implantation_physique=e.poste_principal().implantation,
180 service=ref.Service.objects.get(id=1))
181 ref_e.save()
182
183 # on le porte dans le référentiel auth
184 ref.Authentification(id=ref_e, courriel=u.email).save()
185
186 def _clean_refs(self):
187 courriels = [u"%s@test.auf" % idx for idx, e in
188 enumerate(rh.Employe.objects.all())]
189 ref.Employe.objects.filter(courriel__in=courriels).delete()
190 ref.Authentification.objects.filter(courriel__in=courriels).delete()
191
192 def tearDown(self):
193 self._clean_refs()
194
195 def _test_acces_ok(self, url):
196 response = self.client.get(url, follow=True)
197 self.assertEqual(response.status_code, 200)
198 self.assertEqual('next' in response.context, False)
199
200 def _test_acces_ko(self, url):
201 response = self.client.get(url, follow=True)
202 is_ko = response.status_code == 403 or 'next' in response.context
203 self.assertEqual(is_ko, True)
204
205 def _test_anonyme(self):
206 pass
207
208 def _test_correspondant_rh(self, email="0@test.auf"):
209 u = User.objects.get(email=email)
210 self.grp_correspondants_rh.user_set.add(u)
211 self.grp_correspondants_rh.save()
212
213 credentials = {'username': email, 'password': self.password}
214 self.assertTrue(self.client.login(**credentials), "login failed")
215
216 def _test_administrateur_regional(self, email="0@test.auf"):
217 u = User.objects.get(email=email)
218 self.grp_administrateurs.user_set.add(u)
219 self.grp_administrateurs.save()
220
221 credentials = {'username': email, 'password': self.password}
222 self.assertTrue(self.client.login(**credentials), "login failed")
223
224 def _test_directeur_bureau(self, email="0@test.auf"):
225 u = User.objects.get(email=email)
226 self.grp_directeurs_bureau.user_set.add(u)
227 self.grp_directeurs_bureau.save()
228
229 credentials = {'username': email, 'password': self.password}
230 self.assertTrue(self.client.login(**credentials), "login failed")
231
232 def _test_drh(self):
233 email = "0@test.auf"
234
235 u = User.objects.get(email=email)
236 self.grp_drh.user_set.add(u)
237 self.grp_drh.save()
238
239 credentials = {'username': email, 'password': self.password}
240 self.assertTrue(self.client.login(**credentials), "login failed")
241
242 def _test_drh2(self):
243 email = "0@test.auf"
244
245 u = User.objects.get(email=email)
246 self.grp_drh2.user_set.add(u)
247 self.grp_drh2.save()
248
249 credentials = {'username': email, 'password': self.password}
250 self.assertTrue(self.client.login(**credentials), "login failed")
251
252 def _test_grp_accior(self):
253 email = "0@test.auf"
254
255 u = User.objects.get(email=email)
256 self.grp_accior.user_set.add(u)
257 self.grp_accior.save()
258
259 credentials = {'username': email, 'password': self.password}
260 self.assertTrue(self.client.login(**credentials), "login failed")
261
262 def _test_grp_abf(self):
263 email = "0@test.auf"
264
265 u = User.objects.get(email=email)
266 self.grp_abf.user_set.add(u)
267 self.grp_abf.save()
268
269 credentials = {'username': email, 'password': self.password}
270 self.assertTrue(self.client.login(**credentials), "login failed")
271
272 def _test_grp_haute_direction(self):
273 email = "0@test.auf"
274
275 u = User.objects.get(email=email)
276 self.grp_haute_direction.user_set.add(u)
277 self.grp_haute_direction.save()
278
279 credentials = {'username': email, 'password': self.password}
280 self.assertTrue(self.client.login(**credentials), "login failed")
281
282 def _test_grp_service_utilisateurs(self):
283 email = "0@test.auf"
284
285 u = User.objects.get(email=email)
286 self.grp_service_utilisateurs.user_set.add(u)
287 self.grp_service_utilisateurs.save()
288
289 credentials = {'username': email, 'password': self.password}
290 self.assertTrue(self.client.login(**credentials), "login failed")