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