test unitaires droits
[auf_rh_dae.git] / project / rh / tests.py
1 # -*- coding: utf-8 -*-
2
3 import datetime
4 from django.core.urlresolvers import reverse
5 from django.test.client import RequestFactory, Client
6 from django.contrib.auth.models import User
7 from project.rh import groups
8 from auf.django.references import models as ref
9 from django.test import TestCase
10 from project.rh import models as rh
11
12
13 class RhTest(TestCase):
14
15 def setUp(self):
16 """
17 POSTES
18 ======
19 self.poste_cnf_ngaoundere
20 self.poste_cnf_bangui
21 self.poste_bap_bureau
22 self.poste_bap_ifi
23
24 EMPLOYES
25 ========
26 self.employe_cnf_ngaoundere
27 self.employe_cnf_bangui
28 self.employe_bap_bureau
29 self.employe_bap_ifi
30
31 USERS DJANGO
32 ============
33 cf. EMPLOYES
34
35 DOSSIERS
36 ========
37 self.dossier_cnf_ngaoundere
38 self.dossier_cnf_bangui
39 self.dossier_bap_bureau
40 self.dossier_bap_ifi
41
42
43 """
44 self.factory = RequestFactory()
45 self.password = "0000"
46 today = datetime.datetime.now()
47 #########################
48 # Régions / Implantations
49 #########################
50 self.REGION_ACGL = ref.Region.objects.get(id=1)
51 self.IMPLANTATION_ACGL_CNF_NGAOUNDERE = ref.Implantation.objects.get(id=90)
52 self.IMPLANTATION_ACGL_CNF_BANGUI = ref.Implantation.objects.get(id=85)
53
54 self.REGION_BAP = ref.Region.objects.get(id=4)
55 self.IMPLANTATION_BAP_BUREAU = ref.Implantation.objects.get(id=51)
56 self.IMPLANTATION_BAP_IFI = ref.Implantation.objects.get(id=55)
57
58
59 ##########
60 # Employés
61 ##########
62 self.employe_cnf_ngaoundere = rh.Employe(
63 nom="Employé",
64 prenom="ngaoundere",
65 )
66 self.employe_cnf_ngaoundere.save()
67 self.employe_cnf_bangui = rh.Employe(
68 nom="Employé",
69 prenom="bangui",
70 )
71 self.employe_cnf_bangui.save()
72 self.employe_bap_bureau = rh.Employe(
73 nom="Employé",
74 prenom="BAP bureau",
75 )
76 self.employe_bap_bureau.save()
77 self.employe_bap_ifi = rh.Employe(
78 nom="Employé",
79 prenom="BAP IFI",
80 )
81 self.employe_bap_ifi.save()
82
83 ##############
84 # Users Django
85 ##############
86 for idx, e in enumerate(rh.Employe.objects.all()):
87 email = u"%s@test.auf" % idx
88 u = User(first_name=e.prenom,
89 is_staff=True,
90 last_name=e.nom,
91 username=email,
92 email=email)
93 u.set_password(self.password)
94 u.save()
95 auth = ref.Authentification(
96 courriel=u.email,
97 motdepasse=u.password,
98 actif=1,
99 )
100 auth.id_id = e.id
101 auth.save()
102
103 ######
104 # ACGL
105 ######
106 self.poste_cnf_ngaoundere = rh.Poste(nom="poste à NGAOUNDERE",
107 implantation=self.IMPLANTATION_ACGL_CNF_NGAOUNDERE)
108
109 self.poste_cnf_bangui = rh.Poste(nom="poste à BANGUI",
110 implantation=self.IMPLANTATION_ACGL_CNF_BANGUI)
111
112 self.poste_cnf_ngaoundere.save()
113 self.poste_cnf_bangui.save()
114
115 self.dossier_cnf_ngaoundere = rh.Dossier(poste=self.poste_cnf_ngaoundere,
116 employe=self.employe_cnf_ngaoundere, date_debut=today)
117 self.dossier_cnf_bangui = rh.Dossier(poste=self.poste_cnf_bangui,
118 employe=self.employe_cnf_bangui, date_debut=today)
119
120 self.dossier_cnf_ngaoundere.save()
121 self.dossier_cnf_bangui.save()
122
123 #####
124 # BAP
125 #####
126 self.poste_bap_bureau = rh.Poste(nom="poste au Bureau BAP",
127 implantation=self.IMPLANTATION_BAP_BUREAU)
128 self.poste_bap_ifi = rh.Poste(nom="poste à l'IFI",
129 implantation=self.IMPLANTATION_BAP_IFI)
130
131 self.poste_bap_bureau.save()
132 self.poste_bap_ifi.save()
133
134 self.dossier_bap_bureau = rh.Dossier(poste=self.poste_bap_bureau,
135 employe=self.employe_bap_bureau, date_debut=today)
136 self.dossier_bap_ifi = rh.Dossier(poste=self.poste_bap_ifi,
137 employe=self.employe_bap_ifi, date_debut=today)
138
139 self.dossier_bap_bureau.save()
140 self.dossier_bap_ifi.save()
141
142 def _test_acces_ok(self, url):
143 response = self.client.get(url, follow=True)
144 self.assertEqual(response.status_code, 200)
145 self.assertEqual('next' in response.context, False)
146
147
148 class PortalTest(RhTest):
149
150 def test_anonyme_frontend(self):
151 response = self.client.get('/')
152 self.assertEqual(response.status_code, 200)
153
154 def test_anonyme_backend(self):
155 admin_index_url = reverse('admin:index')
156 response = self.client.get(admin_index_url)
157 self.assertEqual(response.context['next'], admin_index_url)
158
159 def test_logged_sans_groupe_backend(self):
160 email = "0@test.auf"
161 admin_index_url = reverse('admin:index')
162 self.client.login(username=email, password=self.password)
163 response = self.client.get(admin_index_url)
164 self.assertEqual(response.context['next'], admin_index_url)
165
166 class EmployeAddTest(RhTest):
167 """
168 Test l'ajout d'un employé
169 """
170
171 def test_correspondant_rh(self):
172 """
173 Un correspodant RH peut ajouter un employé
174 """
175 email = "0@test.auf"
176
177 u = User.objects.get(email=email)
178 groups.grp_correspondants_rh.user_set.add(u)
179 groups.grp_correspondants_rh.save()
180
181 credentials = {'username': email, 'password': self.password}
182 url = reverse('admin:rh_employe_add')
183 self.assertTrue(self.client.login(**credentials), "login failed")
184 self._test_acces_ok(url)
185
186 def test_administrateur_regional(self):
187 """
188 Un administrateur peut ajouter un employé
189 """
190 email = "0@test.auf"
191
192 u = User.objects.get(email=email)
193 groups.grp_administrateurs.user_set.add(u)
194 groups.grp_administrateurs.save()
195
196 credentials = {'username': email, 'password': self.password}
197 url = reverse('admin:rh_employe_add')
198 self.assertTrue(self.client.login(**credentials), "login failed")
199 self._test_acces_ok(url)
200
201 def test_directeur_bureau(self):
202 """
203 Un directeur de bureau peut ajouter un employé
204 """
205 email = "0@test.auf"
206
207 u = User.objects.get(email=email)
208 groups.grp_directeurs_bureau.user_set.add(u)
209 groups.grp_directeurs_bureau.save()
210
211 credentials = {'username': email, 'password': self.password}
212 url = reverse('admin:rh_employe_add')
213 self.assertTrue(self.client.login(**credentials), "login failed")
214 self._test_acces_ok(url)
215
216 def test_drh(self):
217 """
218 Un DRH peut ajouter un employé
219 """
220 email = "0@test.auf"
221
222 u = User.objects.get(email=email)
223 groups.grp_drh.user_set.add(u)
224 groups.grp_drh.save()
225
226 credentials = {'username': email, 'password': self.password}
227 url = reverse('admin:rh_employe_add')
228 self.assertTrue(self.client.login(**credentials), "login failed")
229 self._test_acces_ok(url)
230
231 def test_drh2(self):
232 """
233 Un DRH (2ieme niveau) peut ajouter un employé
234 """
235 email = "0@test.auf"
236
237 u = User.objects.get(email=email)
238 groups.grp_drh2.user_set.add(u)
239 groups.grp_drh2.save()
240
241 credentials = {'username': email, 'password': self.password}
242 url = reverse('admin:rh_employe_add')
243 self.assertTrue(self.client.login(**credentials), "login failed")
244 self._test_acces_ok(url)