Blame view

src/core/pyros_django/user_mgmt/tests.py 41 KB
eefbbbd2   Etienne Pallier   Model splitting g...
1
# Django imports
e419a2f6   Alexis Koralewski   Add new version f...
2
from django.test import TestCase
e419a2f6   Alexis Koralewski   Add new version f...
3
4
from django.contrib.auth.models import User
from django.urls import reverse
8aa9bbc0   Alexis Koralewski   Add new tests of F01
5
from django.core import mail
e419a2f6   Alexis Koralewski   Add new version f...
6

eefbbbd2   Etienne Pallier   Model splitting g...
7
# Project imports
b95a693f   Alexis Koralewski   restructuration d...
8
from user_mgmt.models import *
eefbbbd2   Etienne Pallier   Model splitting g...
9
from common.models import *
e419a2f6   Alexis Koralewski   Add new version f...
10
11
12
13

class UserManagerTests(TestCase):

    def setUp(self):
c39802e5   Alexis Koralewski   update tests fixt...
14
        institute = Institute.objects.create(name="CNRS",quota_f=1)
8aa9bbc0   Alexis Koralewski   Add new tests of F01
15
        period = Period.objects.create()
684e8b01   Alexis Koralewski   fixing tests and ...
16
        science_theme = ScienceTheme.objects.create(name="Solar System")
c39802e5   Alexis Koralewski   update tests fixt...
17
18
19
20
21
        UserLevel.objects.create(name="Visitor",desc="Visitor description",priority=0)
        UserLevel.objects.create(name="Observer",desc="Observer description",priority=2)
        UserLevel.objects.create(name="Unit-PI",desc="Unit-PI description",priority=6)
        UserLevel.objects.create(name="TAC",desc="TAC description",priority=3)
        UserLevel.objects.create(name="Admin",desc="Admin description",priority=7)
8aa9bbc0   Alexis Koralewski   Add new tests of F01
22
        Country.objects.create(name="France")
e419a2f6   Alexis Koralewski   Add new version f...
23
24
25
26
27
        self.assertEqual(PyrosUser.objects.count(), 0, "There should be no User")
        path = reverse("user_signup_validation")
        response = self.client.post(path, {"email": "toto@titi.fr", "password": "aze", "password_confirm": "aze",
                                           "first_name": "toto", "last_name": "titi", "tel": "0123456789",
                                           "laboratory": "IRAP", "address": "ici","institute":institute.id,"reason":"this is a test",
35daee3f   Alexis Koralewski   Add bot security ...
28
                                           "roles":"Admin","question":"six","timer":"11","iambot":""
e419a2f6   Alexis Koralewski   Add new version f...
29
30
                                           })
        
8aa9bbc0   Alexis Koralewski   Add new tests of F01
31
        
684e8b01   Alexis Koralewski   fixing tests and ...
32
33
34
35
        #sp1 = ScientificProgram.objects.create(name="sp1",institute=institute,sp_pi=PyrosUser.objects.all().first(),science_theme=science_theme)
        #sp2 = ScientificProgram.objects.create(name="sp2",institute=institute,sp_pi=PyrosUser.objects.all().first(),science_theme=science_theme)
        #sp3 = ScientificProgram.objects.create(name="sp3",institute=institute,sp_pi=PyrosUser.objects.all().first(),science_theme=science_theme)
        #sp4 = ScientificProgram.objects.create(name="sp4",institute=institute,sp_pi=PyrosUser.objects.all().first(),science_theme=science_theme)
e9c8f9bc   Alexis Koralewski   fixing tests issu...
36
37
38
39
        sp1 = ScientificProgram.objects.create(name="sp1",institute=institute,sp_pi=PyrosUser.objects.all().first(),science_theme=science_theme)
        sp2 = ScientificProgram.objects.create(name="sp2",institute=institute,sp_pi=PyrosUser.objects.all().first(),science_theme=science_theme)
        sp3 = ScientificProgram.objects.create(name="sp3",institute=institute,sp_pi=PyrosUser.objects.all().first(),science_theme=science_theme)
        sp4 = ScientificProgram.objects.create(name="sp4",institute=institute,sp_pi=PyrosUser.objects.all().first(),science_theme=science_theme)
684e8b01   Alexis Koralewski   fixing tests and ...
40
41
42
43
        SP_Period.objects.create(scientific_program=sp1,period=period)
        SP_Period.objects.create(scientific_program=sp2,period=period)
        SP_Period.objects.create(scientific_program=sp3,period=period)
        SP_Period.objects.create(scientific_program=sp4,period=period)
e419a2f6   Alexis Koralewski   Add new version f...
44
45
46
47
48
49

        self.assertTrue("success" in response.context.keys(), "There should be a success")
        self.assertEqual(PyrosUser.objects.count(), 1, "Theroue shld be one User")

    def test_creation(self):
        self.assertEqual(Country.objects.count(), 1, "There should be 1 Country")
8aa9bbc0   Alexis Koralewski   Add new tests of F01
50
        self.assertEqual(UserLevel.objects.count(), 5, "There should be 5 UserLevel")
e419a2f6   Alexis Koralewski   Add new version f...
51
52
53
54
55
56
57

        self.assertEqual(PyrosUser.objects.all()[0].first_name, 'toto')
        self.assertEqual(PyrosUser.objects.all()[0].email, 'toto@titi.fr')
        self.assertEqual(PyrosUser.objects.all()[0].user_level.filter(name="Visitor").count(),1,"There should be one UserLevel (=Visitor)")

    def test_login(self):
        self.assertEqual(Country.objects.count(), 1, "There should be 1 Country")
8aa9bbc0   Alexis Koralewski   Add new tests of F01
58
        self.assertEqual(UserLevel.objects.count(), 5, "There should be 5 UserLevel")
e419a2f6   Alexis Koralewski   Add new version f...
59
60
61
62
63
64

        # Activate user
        # La variable qui régit l'activation d'un compte est contenue dans pyrosUsers 
        # et s'appelle is_active, il suffit de passer cette variable à True
        current_user = PyrosUser.objects.all()[0]
        current_user.is_active=True
684e8b01   Alexis Koralewski   fixing tests and ...
65
        current_user.set_password("aze")
e419a2f6   Alexis Koralewski   Add new version f...
66
67
68
69
        current_user.save()
        self.assertEqual(PyrosUser.objects.all()[0].is_active, True, "user should be active")

        # Log user
684e8b01   Alexis Koralewski   fixing tests and ...
70
        path = reverse("login_validation")
e419a2f6   Alexis Koralewski   Add new version f...
71
        response = self.client.post(path, {"email": "toto@titi.fr", "password": "aze"})
684e8b01   Alexis Koralewski   fixing tests and ...
72
        #self.assertTrue(response.context.get("success"))
e419a2f6   Alexis Koralewski   Add new version f...
73
74
75
76
        self.assertIn('_auth_user_id', self.client.session, "The user should be logged in")

    def test_login_not_active(self):
        self.assertEqual(Country.objects.count(), 1, "There should be 1 Country")
8aa9bbc0   Alexis Koralewski   Add new tests of F01
77
        self.assertEqual(UserLevel.objects.count(), 5, "There should be 5 UserLevel")
e419a2f6   Alexis Koralewski   Add new version f...
78
79
80
81
82
83
84
85

        # Activate user
        # La variable qui régit l'activation d'un compte est contenue dans pyrosUsers
        # et s'appelle is_active, il suffit de passer cette variable à True

        self.assertEqual(PyrosUser.objects.all()[0].is_active, False, "user should not be active")

        # Log user
b95a693f   Alexis Koralewski   restructuration d...
86
        path = "/user_mgmt/login"
e419a2f6   Alexis Koralewski   Add new version f...
87
88
        response = self.client.post(path, {"email": "toto@titi.fr", "password": "aze"})

684e8b01   Alexis Koralewski   fixing tests and ...
89
        #self.assertFalse(response.context.get("success"))
e419a2f6   Alexis Koralewski   Add new version f...
90
91
92
        self.assertNotIn('_auth_user_id', self.client.session, "The user should be logged in")

    def test_wrong_email(self):
b95a693f   Alexis Koralewski   restructuration d...
93
        path = "/user_mgmt/login"
e419a2f6   Alexis Koralewski   Add new version f...
94
95
96
97
98
        response = self.client.post(path, {"email": "toto@tti.fr", "password": "aze"})
        self.assertIn("error", response.context.keys(), "There should be an error")
        self.assertNotIn('_auth_user_id', self.client.session, "There shouldn't be an authentified user")

    def test_wrong_password(self):
b95a693f   Alexis Koralewski   restructuration d...
99
        path = "/user_mgmt/login"
e419a2f6   Alexis Koralewski   Add new version f...
100
101
102
103
104
105
        response = self.client.post(path, {"email": "toto@titi.fr", "password": "azee"})
        self.assertIn("error", response.context.keys(), "There should be an error")
        self.assertNotIn('_auth_user_id', self.client.session, "There shouldn't be an authentified user")

    def test_logout(self):
        self.client.login(username="toto@titi.fr", password="aze")
b95a693f   Alexis Koralewski   restructuration d...
106
        path = "/user_mgmt/logout"
e419a2f6   Alexis Koralewski   Add new version f...
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
        self.client.get(path)
        self.assertNotIn('_auth_user_id', self.client.session, "There shouldn't be an authentified user")

    def test_get_user_priority(self):
        user = PyrosUser.objects.all()[0]
        # add Unit-PI role to user
        UserLevel.objects.get(name="Unit-PI").pyros_users.add(user)
        self.assertEqual(user.get_priority(),UserLevel.objects.get(name="Unit-PI").priority,"The priority should be equal to Unit-PI's priority")

    def test_wrong_get_user_priority(self):
        user = PyrosUser.objects.all()[0]
        # add Unit-PI role to user
        UserLevel.objects.get(name="Unit-PI").pyros_users.add(user)
        self.assertNotEqual(user.get_priority(),UserLevel.objects.get(name="Visitor").priority,"The priority shouldn't be equal to Visitor's priority")

    def test_get_roles_str(self):
        user = PyrosUser.objects.all()[0]
        # add Unit-PI role to user
        UserLevel.objects.get(name="Unit-PI").pyros_users.add(user)
        roles_str = user.get_roles_str()
        for role in user.user_level.all():
            self.assertIn(role.name,roles_str,f"The role {role} should be in the str representation")
    
    def test_wrong_get_roles_str(self):
        user = PyrosUser.objects.all()[0]
        # add Unit-PI role to user
        UserLevel.objects.get(name="Unit-PI").pyros_users.add(user)
        roles_str = user.get_roles_str()
        self.assertNotIn(UserLevel.objects.get(name="Admin").name,roles_str,"The role Admin shouldn't be in the str representation")

c39802e5   Alexis Koralewski   update tests fixt...
137
138
139
140
141
    # def test_max_priority_desc(self):
    #     user = PyrosUser.objects.all()[0]
    #     # add Unit-PI role to user
    #     UserLevel.objects.get(name="Unit-PI").pyros_users.add(user)
    #     self.assertEqual(user.get_max_priority_desc(),UserLevel.objects.get(name="Unit-PI").desc,"The desc of user_level should be 'Unit-PI description' ")
e419a2f6   Alexis Koralewski   Add new version f...
142

c39802e5   Alexis Koralewski   update tests fixt...
143
144
145
146
147
148
149
    # def test_wrong_max_priority_desc(self):
    #     user = PyrosUser.objects.all()[0]
    #     # add Unit-PI role to user
    #     UserLevel.objects.get(name="Unit-PI").pyros_users.add(user)
    #     # add Admin role to user (has a greatier priority than Unit-PI and Visitor roles)
    #     UserLevel.objects.get(name="Admin").pyros_users.add(user)
    #     self.assertNotEqual(user.get_max_priority_desc(),UserLevel.objects.get(name="Unit-PI").desc,"The desc of user_level shouldn't be 'Unit-PI description' ")
e419a2f6   Alexis Koralewski   Add new version f...
150
151

        
c39802e5   Alexis Koralewski   update tests fixt...
152
153
154
155
156
157
158
159
160
161
162
163
164
    # def test_max_priority_quota(self):
    #     user = PyrosUser.objects.all()[0]
    #     # add Unit-PI role to user
    #     UserLevel.objects.get(name="Unit-PI").pyros_users.add(user)
    #     self.assertEqual(user.get_max_priority_quota(),UserLevel.objects.get(name="Unit-PI").quota,"The quota of user_level should be 100.0 ")

    # def test_wrong_max_priority_quota(self):
    #     user = PyrosUser.objects.all()[0]
    #     # add Unit-PI role to user
    #     UserLevel.objects.get(name="Unit-PI").pyros_users.add(user)
    #     # add Admin role to user (has a greatier priority than Unit-PI and Visitor roles)
    #     UserLevel.objects.get(name="Admin").pyros_users.add(user)
    #     self.assertNotEqual(user.get_max_priority_quota(),UserLevel.objects.get(name="Unit-PI").desc,"The quota of user_level shouldn't be 1000.0 ")
8aa9bbc0   Alexis Koralewski   Add new tests of F01
165
166
167
168
169
170
171
172
173
174


    # new test of 2021

    def test_USR_can_register(self):
        institute = Institute.objects.get(name="CNRS")
        path = reverse("user_signup_validation")
        response = self.client.post(path, {"email": "unit_pi@toto.fr", "password": "aze", "password_confirm": "aze",
                                           "first_name": "unit", "last_name": "pi", "tel": "0123456789",
                                           "laboratory": "IRAP", "address": "ici","institute":institute.id,"reason":"this is a test",
35daee3f   Alexis Koralewski   Add bot security ...
175
                                           "roles":"Admin","question":"six","timer":"11","iambot":""
8aa9bbc0   Alexis Koralewski   Add new tests of F01
176
177
178
179
180
181
182
183
184
185
186
                                           })
        # We need to create an Unit-PI user
        unit_pi = PyrosUser.objects.get(username="unit_pi@toto.fr")
        unit_pi.user_level.set([UserLevel.objects.get(name="Unit-PI")])
        unit_pi.is_active = True
        unit_pi.set_password("password")
        unit_pi.save()
        path = reverse("user_signup_validation")
        response = self.client.post(path, {"email": "toto@titi.fr", "password": "aze", "password_confirm": "aze",
                                           "first_name": "toto", "last_name": "titi", "tel": "0123456789",
                                           "laboratory": "IRAP", "address": "ici","institute":institute.id,"reason":"this is a test",
35daee3f   Alexis Koralewski   Add bot security ...
187
                                           "roles":"Admin","question":"six","timer":"11","iambot":""
8aa9bbc0   Alexis Koralewski   Add new tests of F01
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
                                           })
        new_user = PyrosUser.objects.filter(username="toto@titi.fr").order_by('-id')[0]
        #new_user = PyrosUser.objects.get(username="toto@titi.fr")
        self.assertEqual(new_user.get_roles_str(),"Visitor")
        self.assertEqual(len(mail.outbox),2)
        self.assertIn("toto@titi.fr",mail.outbox[0].recipients())
        self.assertIn("unit_pi@toto.fr",mail.outbox[1].recipients())
        # log in as unit pi
        self.client.post(reverse("login_validation"),{"email":"unit_pi@toto.fr","password":"password"})
        self.assertEqual(int(self.client.session['_auth_user_id']), unit_pi.pk)
        path = reverse("user-edit",kwargs={"pk":new_user.id})
        response = self.client.post(path, {"username":new_user.username,"first_name":new_user.first_name,"last_name":new_user.last_name,"email":new_user.email,"country":Country.objects.all()[0].id,"institute":institute.id,"roles":[UserLevel.objects.get(name="Observer").id],"desc":"","tel":"","adress":"","laboratory":"test"})
        # new user should be Observer now
        self.assertIn("Observer",new_user.get_roles_str())
        # set new user to active
        path = reverse("change_activate",kwargs={"pk":new_user.id,"current_user_id":unit_pi.id})
        response = self.client.get(path)
        new_user = PyrosUser.objects.filter(username="toto@titi.fr").order_by('-id')[0]
        self.assertEqual(response.status_code,302)
        self.assertTrue(new_user.is_active)
        # log out as unit_pi
        path = reverse("user_logout")
190c6ece   Alexis Koralewski   Fixing test with ...
210
        self.client.post(path)
8aa9bbc0   Alexis Koralewski   Add new tests of F01
211
212
213
        # log in as new user (toto)
        response = self.client.post(reverse("login_validation"),{"email":"toto@titi.fr","password":"aze"})
        self.assertEqual(int(self.client.session['_auth_user_id']), new_user.pk)
684e8b01   Alexis Koralewski   fixing tests and ...
214
        response = self.client.get(reverse("index_scientific_program"))
8aa9bbc0   Alexis Koralewski   Add new tests of F01
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
        # new user has access to the page
        self.assertEqual(response.status_code,200)

    def test_USR_user_cannot_connect_with_bad_login(self):
        user = PyrosUser.objects.get(username="toto@titi.fr")
        user.is_active = True
        user.save()
        # user password is hashed in database, we need to use the raw password
        user_password = "aze"
        response = self.client.post(reverse("login_validation"),{"email":user.email+"bad","password":user_password})
        self.assertEqual(response.status_code,200)
        self.assertContains(response,"Your email and/or password were incorrect.")

    def test_USR_user_cannot_connect_with_bad_pass(self):
        user = PyrosUser.objects.get(username="toto@titi.fr")
        user.is_active = True
        user.save()
        # user password is hashed in database, we need to use the raw password
        user_password = "aze"
        response = self.client.post(reverse("login_validation"),{"email":user.email,"password":user_password+"bad"})
        self.assertEqual(response.status_code,200)
        self.assertContains(response,"Your email and/or password were incorrect.")
        
    def test_USR_can_connect_and_disconnect(self):
        user = PyrosUser.objects.get(username="toto@titi.fr")
        user.is_active = True
        user.save()
        response = self.client.post(reverse("login_validation"),{"email":"toto@titi.fr","password":"aze"})
        self.assertEqual(int(self.client.session['_auth_user_id']), user.pk)
684e8b01   Alexis Koralewski   fixing tests and ...
244
        self.assertEqual(response.status_code,302)
8aa9bbc0   Alexis Koralewski   Add new tests of F01
245
        # test if user can access his profile page
684e8b01   Alexis Koralewski   fixing tests and ...
246
        response = self.client.get(reverse("user_detail",args=[user.id]))
8aa9bbc0   Alexis Koralewski   Add new tests of F01
247
248
        self.assertEqual(response.status_code,200)
        # disconnect
190c6ece   Alexis Koralewski   Fixing test with ...
249
        response = self.client.post(reverse("user_logout"))
4e16078e   Alexis Koralewski   Logout redirect t...
250
251
        # redirected to home page
        self.assertEqual(response.status_code,302)
8aa9bbc0   Alexis Koralewski   Add new tests of F01
252
253
254
255
256
257
258
259
260
261
262
        self.assertNotIn('_auth_user_id',self.client.session.keys())

    def test_USR_view_user_list(self):
        france = Country.objects.get(name="France")
        institute = Institute.objects.get(name="CNRS")

        for i in range(1,6):
            
            response = self.client.post(reverse("user_signup_validation"), {"email":f"u{i}@test.fr", "password": "password123", "password_confirm": "password123",
                                           "first_name": f"u{i}", "last_name": "test", "tel": "0123456789",
                                           "laboratory": "IRAP", "address": "ici","institute":institute.id,"reason":"this is a test",
35daee3f   Alexis Koralewski   Add bot security ...
263
                                           "roles":"Admin","question":"six","timer":"11","iambot":""
8aa9bbc0   Alexis Koralewski   Add new tests of F01
264
265
266
267
268
269
270
                                           })
            user = PyrosUser.objects.get(username=f"u{i}@test.fr")
            user.is_active = True
            user.user_level.set([UserLevel.objects.get(name="Observer")])
            user.save()
        PyrosUser.objects.get(username="u2@test.fr").user_level.set([UserLevel.objects.get(name="Unit-PI")])
        # SP 1
684e8b01   Alexis Koralewski   fixing tests and ...
271
272
        SP_Period_User.objects.create(SP_Period=SP_Period.objects.get(scientific_program=ScientificProgram.objects.get(name="sp1")),user=PyrosUser.objects.get(username="u2@test.fr"))
        SP_Period_User.objects.create(SP_Period=SP_Period.objects.get(scientific_program=ScientificProgram.objects.get(name="sp1")),user=PyrosUser.objects.get(username="u1@test.fr"))
8aa9bbc0   Alexis Koralewski   Add new tests of F01
273
        # SP 2
684e8b01   Alexis Koralewski   fixing tests and ...
274
275
        SP_Period_User.objects.create(SP_Period=SP_Period.objects.get(scientific_program=ScientificProgram.objects.get(name="sp2")),user=PyrosUser.objects.get(username="u2@test.fr"))
        SP_Period_User.objects.create(SP_Period=SP_Period.objects.get(scientific_program=ScientificProgram.objects.get(name="sp2")),user=PyrosUser.objects.get(username="u4@test.fr"))
8aa9bbc0   Alexis Koralewski   Add new tests of F01
276
        # SP 3
684e8b01   Alexis Koralewski   fixing tests and ...
277
278
        SP_Period_User.objects.create(SP_Period=SP_Period.objects.get(scientific_program=ScientificProgram.objects.get(name="sp3")),user=PyrosUser.objects.get(username="u1@test.fr"))
        SP_Period_User.objects.create(SP_Period=SP_Period.objects.get(scientific_program=ScientificProgram.objects.get(name="sp3")),user=PyrosUser.objects.get(username="u3@test.fr"))
8aa9bbc0   Alexis Koralewski   Add new tests of F01
279
        # SP 4
684e8b01   Alexis Koralewski   fixing tests and ...
280
        SP_Period_User.objects.create(SP_Period=SP_Period.objects.get(scientific_program=ScientificProgram.objects.get(name="sp4")),user=PyrosUser.objects.get(username="u4@test.fr"))
b95a693f   Alexis Koralewski   restructuration d...
281
        SP_Period_User.objects.create(SP_Period=SP_Period.objects.get(scientific_program=ScientificProgram.objects.get(name="sp4")),user=PyrosUser.objects.get(username="u5@test.fr"))
8aa9bbc0   Alexis Koralewski   Add new tests of F01
282
283
284
        u1 = PyrosUser.objects.get(username="u1@test.fr")

        response = self.client.post(reverse("login_validation"),{"email":u1.email,"password":"password123"})
684e8b01   Alexis Koralewski   fixing tests and ...
285
        self.assertEqual(response.status_code,302)
8aa9bbc0   Alexis Koralewski   Add new tests of F01
286
287
288
289
290
291
292
293
        response = self.client.get(reverse("users"))
        self.assertEqual(response.status_code,200)
        self.assertContains(response,"u2")
        self.assertContains(response,"u3")
        self.assertNotContains(response,"u4")
        self.assertNotContains(response,"u5")

        # disconnect u1
190c6ece   Alexis Koralewski   Fixing test with ...
294
        self.client.post(reverse("user_logout"))
8aa9bbc0   Alexis Koralewski   Add new tests of F01
295
296
297
298
299

        # connect as u2

        u2 = PyrosUser.objects.get(username="u2@test.fr")

a6e63604   Alexis Koralewski   adding agentSP an...
300
301
        response =  self.client.post(reverse("login_validation"),{"email":u2.email,"password":"password123"})
        
684e8b01   Alexis Koralewski   fixing tests and ...
302
        self.assertEqual(response.status_code,302)
8aa9bbc0   Alexis Koralewski   Add new tests of F01
303
304
305
306
307
308
309
310
311
312
        response = self.client.get(reverse("users"))
        self.assertEqual(response.status_code,200)
        for i in range(1,6):
          self.assertContains(response,f"u{i}")
       
    def test_USR_can_view_himself(self):
        user = PyrosUser.objects.get(username="toto@titi.fr")
        user.is_active = True
        user.save()
        response = self.client.post(reverse("login_validation"),{"email":user.email,"password":"aze"})
684e8b01   Alexis Koralewski   fixing tests and ...
313
        self.assertEqual(response.status_code,302)
8aa9bbc0   Alexis Koralewski   Add new tests of F01
314
        # test if user can access his profile page
684e8b01   Alexis Koralewski   fixing tests and ...
315
        response = self.client.get(reverse("user_detail",args=[user.id]))
8aa9bbc0   Alexis Koralewski   Add new tests of F01
316
        self.assertEqual(response.status_code,200)
684e8b01   Alexis Koralewski   fixing tests and ...
317
        
8aa9bbc0   Alexis Koralewski   Add new tests of F01
318
319
320
321
322
323
324
325
326
327
328
329
        self.assertContains(response,"toto")
    
    def test_USR_can_view_another_user(self):
        # create u1 to u5
        france = Country.objects.get(name="France")
        institute = Institute.objects.get(name="CNRS")

        for i in range(1,6):
            
            response = self.client.post(reverse("user_signup_validation"), {"email":f"u{i}@test.fr", "password": "password123", "password_confirm": "password123",
                                           "first_name": f"u{i}", "last_name": "test", "tel": "0123456789",
                                           "laboratory": "IRAP", "address": "ici","institute":institute.id,"reason":"this is a test",
35daee3f   Alexis Koralewski   Add bot security ...
330
                                           "roles":"Admin","question":"six","timer":"11","iambot":""
8aa9bbc0   Alexis Koralewski   Add new tests of F01
331
332
333
334
335
336
337
                                           })
            user = PyrosUser.objects.get(username=f"u{i}@test.fr")
            user.is_active = True
            user.user_level.set([UserLevel.objects.get(name="Observer")])
            user.save()
        PyrosUser.objects.get(username="u2@test.fr").user_level.set([UserLevel.objects.get(name="Unit-PI")])
        # SP 1
684e8b01   Alexis Koralewski   fixing tests and ...
338
339
        SP_Period_User.objects.create(SP_Period=SP_Period.objects.get(scientific_program=ScientificProgram.objects.get(name="sp1")),user=PyrosUser.objects.get(username="u2@test.fr"))
        SP_Period_User.objects.create(SP_Period=SP_Period.objects.get(scientific_program=ScientificProgram.objects.get(name="sp1")),user=PyrosUser.objects.get(username="u1@test.fr"))
8aa9bbc0   Alexis Koralewski   Add new tests of F01
340
        # SP 2
684e8b01   Alexis Koralewski   fixing tests and ...
341
342
        SP_Period_User.objects.create(SP_Period=SP_Period.objects.get(scientific_program=ScientificProgram.objects.get(name="sp2")),user=PyrosUser.objects.get(username="u2@test.fr"))
        SP_Period_User.objects.create(SP_Period=SP_Period.objects.get(scientific_program=ScientificProgram.objects.get(name="sp2")),user=PyrosUser.objects.get(username="u4@test.fr"))
8aa9bbc0   Alexis Koralewski   Add new tests of F01
343
        # SP 3
684e8b01   Alexis Koralewski   fixing tests and ...
344
345
        SP_Period_User.objects.create(SP_Period=SP_Period.objects.get(scientific_program=ScientificProgram.objects.get(name="sp3")),user=PyrosUser.objects.get(username="u1@test.fr"))
        SP_Period_User.objects.create(SP_Period=SP_Period.objects.get(scientific_program=ScientificProgram.objects.get(name="sp3")),user=PyrosUser.objects.get(username="u3@test.fr"))
8aa9bbc0   Alexis Koralewski   Add new tests of F01
346
        # SP 4
684e8b01   Alexis Koralewski   fixing tests and ...
347
        SP_Period_User.objects.create(SP_Period=SP_Period.objects.get(scientific_program=ScientificProgram.objects.get(name="sp4")),user=PyrosUser.objects.get(username="u4@test.fr"))
8aa9bbc0   Alexis Koralewski   Add new tests of F01
348
349
350
        u1 = PyrosUser.objects.get(username="u1@test.fr")
       
        response = self.client.post(reverse("login_validation"),{"email":u1.email,"password":"password123"})
684e8b01   Alexis Koralewski   fixing tests and ...
351
        self.assertEqual(response.status_code,302)
8aa9bbc0   Alexis Koralewski   Add new tests of F01
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
        # test if user can access u2 profile page
        response = self.client.get(reverse("user_detail",kwargs={"pk":PyrosUser.objects.get(username="u2@test.fr").id}))
        self.assertEqual(response.status_code,200)
        self.assertContains(response,"u2")
        # test if user can access u3 profile page
        response = self.client.get(reverse("user_detail",kwargs={"pk":PyrosUser.objects.get(username="u3@test.fr").id}))
        self.assertEqual(response.status_code,200)
        self.assertContains(response,"u3")

    def test_USR_can_update_himself(self):
        # create u1
        france = Country.objects.get(name="France")
        institute = Institute.objects.get(name="CNRS")
        response = self.client.post(reverse("user_signup_validation"), {"email":"u1@test.fr", "password": "password123", "password_confirm": "password123",
                                        "first_name": "u1", "last_name": "test", "tel": "0123456789",
                                        "laboratory": "IRAP", "address": "ici","institute":institute.id,"reason":"this is a test",
35daee3f   Alexis Koralewski   Add bot security ...
368
                                        "roles":"Admin","question":"six","timer":"11","iambot":""
8aa9bbc0   Alexis Koralewski   Add new tests of F01
369
370
371
372
373
374
                                        })
        user = PyrosUser.objects.get(username="u1@test.fr")
        user.is_active = True
        user.user_level.set([UserLevel.objects.get(name="Observer")])
        user.save()
        response = self.client.post(reverse("login_validation"),{"email":user.email,"password":"password123"})
684e8b01   Alexis Koralewski   fixing tests and ...
375
        self.assertEqual(response.status_code,302)
8aa9bbc0   Alexis Koralewski   Add new tests of F01
376
377
378
379
380
381
382
383
384
385
386
387
388
        response = self.client.post(reverse("user-edit",kwargs={"pk":user.id}),{"username":user.username,"first_name":user.first_name,"last_name":user.last_name,"email":user.email,"country":france.id,"institute":institute.id,"roles":[UserLevel.objects.get(name="Observer").id],"desc":"I am user 1","tel":"","adress":"","laboratory":"test"})
        # if successful, user is redirected to his profile page
        self.assertEqual(response.status_code,302)
        user = PyrosUser.objects.get(username="u1@test.fr")
        self.assertEqual(user.desc,"I am user 1")

    def test_USR_can_update_another_user(self):
        france = Country.objects.get(name="France")
        institute = Institute.objects.get(name="CNRS")
        # U1 creation
        response = self.client.post(reverse("user_signup_validation"), {"email":"u1@test.fr", "password": "password123", "password_confirm": "password123",
                                        "first_name": "u1", "last_name": "test", "tel": "0123456789",
                                        "laboratory": "IRAP", "address": "ici","institute":institute.id,"reason":"this is a test",
35daee3f   Alexis Koralewski   Add bot security ...
389
                                        "roles":"Admin","question":"six","timer":"11","iambot":""
8aa9bbc0   Alexis Koralewski   Add new tests of F01
390
391
392
393
394
395
396
397
398
                                        })
        user = PyrosUser.objects.get(username="u1@test.fr")
        user.is_active = True
        user.user_level.set([UserLevel.objects.get(name="Observer")])
        user.save()
        # U2 creation
        response = self.client.post(reverse("user_signup_validation"), {"email":"u2@test.fr", "password": "password123", "password_confirm": "password123",
                                        "first_name": "u2", "last_name": "test", "tel": "0123456789",
                                        "laboratory": "IRAP", "address": "ici","institute":institute.id,"reason":"this is a test",
35daee3f   Alexis Koralewski   Add bot security ...
399
                                        "roles":"Admin","question":"six","timer":"11","iambot":""
8aa9bbc0   Alexis Koralewski   Add new tests of F01
400
401
402
403
404
405
406
407
408
409
                                        })
        user = PyrosUser.objects.get(username="u2@test.fr")
        user.is_active = True
        user.user_level.set([UserLevel.objects.get(name="Unit-PI")])
        user.save()

        user = PyrosUser.objects.get(username="u1@test.fr")
        user2 = PyrosUser.objects.get(username="u2@test.fr")
        # connect as u1
        response = self.client.post(reverse("login_validation"),{"email":user.email,"password":"password123"})
684e8b01   Alexis Koralewski   fixing tests and ...
410
        self.assertEqual(response.status_code,302)
8aa9bbc0   Alexis Koralewski   Add new tests of F01
411
412
413
414
415
        response = self.client.post(reverse("user-edit",kwargs={"pk":user2.id}),{"username":user2.username,"first_name":user2.first_name,"last_name":user2.last_name,"email":user2.email,"country":france.id,"institute":institute.id,"roles":[UserLevel.objects.get(name="Observer").id],"desc":"I am user 2","tel":"","adress":"","laboratory":"test"})
        self.assertNotEqual(response.status_code,200)
        # When a user attempt to modify another user's attribute, he will be redirected
        self.assertEqual(response.status_code,302)
        # log out as u1
190c6ece   Alexis Koralewski   Fixing test with ...
416
        self.client.post(reverse("user_logout"))
8aa9bbc0   Alexis Koralewski   Add new tests of F01
417
418
        # log in as u2
        response = self.client.post(reverse("login_validation"),{"email":user2.email,"password":"password123"})
684e8b01   Alexis Koralewski   fixing tests and ...
419
        self.assertEqual(response.status_code,302)
8aa9bbc0   Alexis Koralewski   Add new tests of F01
420
421
422
423
424
425
426
427
428
429
430
431
        response = self.client.post(reverse("user-edit",kwargs={"pk":user.id}),{"username":user.username,"first_name":user.first_name,"last_name":user.last_name,"email":user.email,"country":france.id,"institute":institute.id,"roles":[UserLevel.objects.get(name="Observer").id],"desc":"I am user 1 version 2","tel":"","adress":"","laboratory":"test"})
        # if successful, user is redirected to his profile page
        self.assertEqual(response.status_code,302)
        self.assertEqual(PyrosUser.objects.get(username="u1@test.fr").desc,"I am user 1 version 2")
    
    def test_USR_can_activate_or_desactivate_another_user(self):
        france = Country.objects.get(name="France")
        institute = Institute.objects.get(name="CNRS")
        # U1 creation
        response = self.client.post(reverse("user_signup_validation"), {"email":"u1@test.fr", "password": "password123", "password_confirm": "password123",
                                        "first_name": "u1", "last_name": "test", "tel": "0123456789",
                                        "laboratory": "IRAP", "address": "ici","institute":institute.id,"reason":"this is a test",
35daee3f   Alexis Koralewski   Add bot security ...
432
                                        "roles":"Admin","question":"six","timer":"11","iambot":""
8aa9bbc0   Alexis Koralewski   Add new tests of F01
433
434
435
436
437
438
439
440
441
                                        })
        user = PyrosUser.objects.get(username="u1@test.fr")
        user.is_active = True
        user.user_level.set([UserLevel.objects.get(name="Observer")])
        user.save()
        # U2 creation
        response = self.client.post(reverse("user_signup_validation"), {"email":"u2@test.fr", "password": "password123", "password_confirm": "password123",
                                        "first_name": "u2", "last_name": "test", "tel": "0123456789",
                                        "laboratory": "IRAP", "address": "ici","institute":institute.id,"reason":"this is a test",
35daee3f   Alexis Koralewski   Add bot security ...
442
                                        "roles":"Admin","question":"six","timer":"11","iambot":""
8aa9bbc0   Alexis Koralewski   Add new tests of F01
443
444
445
446
447
448
449
450
451
452
                                        })
        user = PyrosUser.objects.get(username="u2@test.fr")
        user.is_active = True
        user.user_level.set([UserLevel.objects.get(name="Unit-PI")])
        user.save()

        user = PyrosUser.objects.get(username="u1@test.fr")
        user2 = PyrosUser.objects.get(username="u2@test.fr")
        # log in as u2
        response = self.client.post(reverse("login_validation"),{"email":user2.email,"password":"password123"})
684e8b01   Alexis Koralewski   fixing tests and ...
453
        self.assertEqual(response.status_code,302)
8aa9bbc0   Alexis Koralewski   Add new tests of F01
454
455
456
457
458
459
460
        # deactivate u1
        path = reverse("change_activate",kwargs={"pk":user.id,"current_user_id":user2.id})
        response = self.client.get(path)
        user = PyrosUser.objects.get(username="u1@test.fr")
        self.assertEqual(response.status_code,302)
        self.assertFalse(user.is_active)
        # log out as u2 
190c6ece   Alexis Koralewski   Fixing test with ...
461
        self.client.post(reverse("user_logout"))
8aa9bbc0   Alexis Koralewski   Add new tests of F01
462
463
464
465
466
467
468
        # u1 attempt to connect on PyROS
        response = self.client.post(reverse("login_validation"),{"email":user.email,"password":"password123"})
        self.assertEqual(response.status_code,200)
        
        self.assertContains(response,"Your account is not active, please contact the Unit-PI.")
        # log in as u2
        response = self.client.post(reverse("login_validation"),{"email":user2.email,"password":"password123"})
684e8b01   Alexis Koralewski   fixing tests and ...
469
        self.assertEqual(response.status_code,302)
8aa9bbc0   Alexis Koralewski   Add new tests of F01
470
471
472
473
474
475
476
        # activate u1
        path = reverse("change_activate",kwargs={"pk":user.id,"current_user_id":user2.id})
        response = self.client.get(path)
        user = PyrosUser.objects.get(username="u1@test.fr")
        self.assertEqual(response.status_code,302)
        self.assertTrue(user.is_active)
        # log out as u2 
190c6ece   Alexis Koralewski   Fixing test with ...
477
        self.client.post(reverse("user_logout"))
8aa9bbc0   Alexis Koralewski   Add new tests of F01
478
479
        # connect as u1
        response = self.client.post(reverse("login_validation"),{"email":user.email,"password":"password123"})
684e8b01   Alexis Koralewski   fixing tests and ...
480
        self.assertEqual(response.status_code,302)
8aa9bbc0   Alexis Koralewski   Add new tests of F01
481
482
483
484
        # u1 try to deactivate u2... and fail
        path = reverse("change_activate",kwargs={"pk":user2.id,"current_user_id":user.id})
        response = self.client.get(path)
        user2 = PyrosUser.objects.get(username="u2@test.fr")
684e8b01   Alexis Koralewski   fixing tests and ...
485
        self.assertEqual(response.status_code,302)
8aa9bbc0   Alexis Koralewski   Add new tests of F01
486
487
488
489
490
491
492
493
494
        self.assertTrue(user2.is_active)

    def test_USR_can_change_another_user_roles(self):
        france = Country.objects.get(name="France")
        institute = Institute.objects.get(name="CNRS")
        # U1 creation
        response = self.client.post(reverse("user_signup_validation"), {"email":"u1@test.fr", "password": "password123", "password_confirm": "password123",
                                        "first_name": "u1", "last_name": "test", "tel": "0123456789",
                                        "laboratory": "IRAP", "address": "ici","institute":institute.id,"reason":"this is a test",
35daee3f   Alexis Koralewski   Add bot security ...
495
                                        "roles":"Admin","question":"six","timer":"11","iambot":""
8aa9bbc0   Alexis Koralewski   Add new tests of F01
496
497
498
499
500
501
502
503
504
                                        })
        user = PyrosUser.objects.get(username="u1@test.fr")
        user.is_active = True
        user.user_level.set([UserLevel.objects.get(name="Observer")])
        user.save()
        # U2 creation
        response = self.client.post(reverse("user_signup_validation"), {"email":"u2@test.fr", "password": "password123", "password_confirm": "password123",
                                        "first_name": "u2", "last_name": "test", "tel": "0123456789",
                                        "laboratory": "IRAP", "address": "ici","institute":institute.id,"reason":"this is a test",
35daee3f   Alexis Koralewski   Add bot security ...
505
                                        "roles":"Admin","question":"six","timer":"11","iambot":""
8aa9bbc0   Alexis Koralewski   Add new tests of F01
506
507
508
509
510
511
512
513
514
515
                                        })
        user = PyrosUser.objects.get(username="u2@test.fr")
        user.is_active = True
        user.user_level.set([UserLevel.objects.get(name="Unit-PI")])
        user.save()

        user = PyrosUser.objects.get(username="u1@test.fr")
        user2 = PyrosUser.objects.get(username="u2@test.fr")

         # SP 1
684e8b01   Alexis Koralewski   fixing tests and ...
516
517
        SP_Period_User.objects.create(SP_Period=SP_Period.objects.get(scientific_program=ScientificProgram.objects.get(name="sp1")),user=PyrosUser.objects.get(username="u2@test.fr"))
        SP_Period_User.objects.create(SP_Period=SP_Period.objects.get(scientific_program=ScientificProgram.objects.get(name="sp1")),user=PyrosUser.objects.get(username="u1@test.fr"))
8aa9bbc0   Alexis Koralewski   Add new tests of F01
518
519

        # log in as u1
684e8b01   Alexis Koralewski   fixing tests and ...
520
521
        response = self.client.post(reverse("login_validation"),{"email":user,"password":"password123"})
        self.assertEqual(response.status_code,302)
8aa9bbc0   Alexis Koralewski   Add new tests of F01
522
523
524
525
526
527
528
        # try to modify roles of u2
        path = reverse("user-edit",kwargs={"pk":user2.id})
        response = self.client.post(path, {"username":user2.username,"first_name":user2.first_name,"last_name":user2.last_name,"email":user2.email,"country":Country.objects.all()[0].id,"institute":institute.id,"roles":[UserLevel.objects.get(name="Observer").id],"desc":"","tel":"","adress":"","laboratory":"test"})
        # user2 should only be Unit-PI
        self.assertEqual("Unit-PI",user2.get_roles_str())

        # log out as u1
190c6ece   Alexis Koralewski   Fixing test with ...
529
        self.client.post(reverse("user_logout"))
8aa9bbc0   Alexis Koralewski   Add new tests of F01
530
531
        # log in a u2
        response = self.client.post(reverse("login_validation"),{"email":user2.email,"password":"password123"})
684e8b01   Alexis Koralewski   fixing tests and ...
532
        self.assertEqual(response.status_code,302)
8aa9bbc0   Alexis Koralewski   Add new tests of F01
533
534
535
536
537
538
539
540
541
542
543
544
545
        # u2 modify roles of u1
        path = reverse("user-edit",kwargs={"pk":user.id})
        response = self.client.post(path, {"username":user.username,"first_name":user.first_name,"last_name":user.last_name,"email":user.email,"country":Country.objects.all()[0].id,"institute":institute.id,"roles":[UserLevel.objects.get(name="TAC").id],"desc":"","tel":"","adress":"","laboratory":"test"})
        # user 1 should only be TAC
        self.assertEqual("TAC",user.get_roles_str())
    
    def test_USR_user_can_change_his_current_role(self):
        france = Country.objects.get(name="France")
        institute = Institute.objects.get(name="CNRS")
        # U1 creation
        response = self.client.post(reverse("user_signup_validation"), {"email":"u1@test.fr", "password": "password123", "password_confirm": "password123",
                                        "first_name": "u1", "last_name": "test", "tel": "0123456789",
                                        "laboratory": "IRAP", "address": "ici","institute":institute.id,"reason":"this is a test",
35daee3f   Alexis Koralewski   Add bot security ...
546
                                        "roles":"Admin","question":"six","timer":"11","iambot":""
8aa9bbc0   Alexis Koralewski   Add new tests of F01
547
548
549
550
551
552
553
                                        })
        user = PyrosUser.objects.get(username="u1@test.fr")
        user.is_active = True
        user.user_level.set([UserLevel.objects.get(name="Observer"),UserLevel.objects.get(name="Unit-PI")])
        user.save()

        # log in 
684e8b01   Alexis Koralewski   fixing tests and ...
554
555
        response = self.client.post(reverse("login_validation"),{"email":user,"password":"password123"})
        self.assertEqual(response.status_code,302)
8aa9bbc0   Alexis Koralewski   Add new tests of F01
556
557
558
559
560
561
562
563
564
565
566
567
        # user should be log in with his higher priority role (= Unit-PI)
        self.assertEqual(self.client.session.get("role"),"Unit-PI")
        # test that Unit PI can access to an Unit-PI page
        response = self.client.get(reverse("period_list"))
        self.assertEqual(response.status_code,200)

        # user change his role to observer
        response = self.client.post(reverse("set_active_role"),{"role":"Observer"})
        self.assertEqual(response.status_code,200)
        self.assertEqual(self.client.session.get("role"),"Observer")

        # test if user as an Observer can access to an Unit-PI page
684e8b01   Alexis Koralewski   fixing tests and ...
568
        response = self.client.get(reverse("list_evaluated_scientific_program"))
1ca78040   Alexis Koralewski   comment on F01 test
569
        # user should not have access to this page as an Observer.
684e8b01   Alexis Koralewski   fixing tests and ...
570
        self.assertEqual(response.status_code,302)
8aa9bbc0   Alexis Koralewski   Add new tests of F01
571
572
573
574
575
576
577

    def test_USR_user_cannot_delete_himself(self):
        france = Country.objects.get(name="France")
        institute = Institute.objects.get(name="CNRS")
        response = self.client.post(reverse("user_signup_validation"), {"email":"u1@test.fr", "password": "password123", "password_confirm": "password123",
                                        "first_name": "u1", "last_name": "test", "tel": "0123456789",
                                        "laboratory": "IRAP", "address": "ici","institute":institute.id,"reason":"this is a test",
35daee3f   Alexis Koralewski   Add bot security ...
578
                                        "roles":"Admin","question":"six","timer":"11","iambot":""
8aa9bbc0   Alexis Koralewski   Add new tests of F01
579
580
581
582
583
584
                                        })
        user = PyrosUser.objects.get(username="u1@test.fr")
        user.is_active = True
        user.user_level.set([UserLevel.objects.get(name="Unit-PI")])
        user.save()
        # log in 
684e8b01   Alexis Koralewski   fixing tests and ...
585
586
        response = self.client.post(reverse("login_validation"),{"email":user,"password":"password123"})
        self.assertEqual(response.status_code,302)
8aa9bbc0   Alexis Koralewski   Add new tests of F01
587
588
589
590
591
592
593
594
595
596
597
598
599
600

        # u1 try to delete himself (however he can't see the delete button on the page)

        response = self.client.post(reverse("user-delete",kwargs={"pk":user.id}))
        # user shouldn't be deleted and is redirected to his page
        self.assertEqual(response.status_code,302)
        try: 
            is_user_delete = PyrosUser.objects.get(username=user.username)
        except:
            is_user_delete = True
        self.assertNotEqual(is_user_delete,True)
        

    def test_USR_user_can_delete_another_user(self):
35daee3f   Alexis Koralewski   Add bot security ...
601
        france = Country.objects.get(name="France")
8aa9bbc0   Alexis Koralewski   Add new tests of F01
602
603
604
605
606
        institute = Institute.objects.get(name="CNRS")
        # U1 creation
        response = self.client.post(reverse("user_signup_validation"), {"email":"u1@test.fr", "password": "password123", "password_confirm": "password123",
                                        "first_name": "u1", "last_name": "test", "tel": "0123456789",
                                        "laboratory": "IRAP", "address": "ici","institute":institute.id,"reason":"this is a test",
35daee3f   Alexis Koralewski   Add bot security ...
607
                                        "roles":"Admin","question":"six","timer":"11","iambot":""
8aa9bbc0   Alexis Koralewski   Add new tests of F01
608
609
610
611
612
613
614
615
616
                                        })
        user = PyrosUser.objects.get(username="u1@test.fr")
        user.is_active = True
        user.user_level.set([UserLevel.objects.get(name="Observer")])
        user.save()
        # U2 creation
        response = self.client.post(reverse("user_signup_validation"), {"email":"u2@test.fr", "password": "password123", "password_confirm": "password123",
                                        "first_name": "u2", "last_name": "test", "tel": "0123456789",
                                        "laboratory": "IRAP", "address": "ici","institute":institute.id,"reason":"this is a test",
35daee3f   Alexis Koralewski   Add bot security ...
617
                                        "roles":"Admin","question":"six","timer":"11","iambot":""
8aa9bbc0   Alexis Koralewski   Add new tests of F01
618
619
620
621
622
623
624
625
626
627
                                        })
        user = PyrosUser.objects.get(username="u2@test.fr")
        user.is_active = True
        user.user_level.set([UserLevel.objects.get(name="Unit-PI")])
        user.save()

        user = PyrosUser.objects.get(username="u1@test.fr")
        user2 = PyrosUser.objects.get(username="u2@test.fr")

         # SP 1
684e8b01   Alexis Koralewski   fixing tests and ...
628
629
        SP_Period_User.objects.create(SP_Period=SP_Period.objects.get(scientific_program=ScientificProgram.objects.get(name="sp1")),user=PyrosUser.objects.get(username="u2@test.fr"))
        SP_Period_User.objects.create(SP_Period=SP_Period.objects.get(scientific_program=ScientificProgram.objects.get(name="sp1")),user=PyrosUser.objects.get(username="u1@test.fr"))
8aa9bbc0   Alexis Koralewski   Add new tests of F01
630
631

        # log in as u1
684e8b01   Alexis Koralewski   fixing tests and ...
632
633
        response = self.client.post(reverse("login_validation"),{"email":user,"password":"password123"})
        self.assertEqual(response.status_code,302)
8aa9bbc0   Alexis Koralewski   Add new tests of F01
634
635
636

        # u1 try to delete u2
        response = self.client.post(reverse("user-delete",kwargs={"pk":user2.id}))
684e8b01   Alexis Koralewski   fixing tests and ...
637
638
        # u1 is redirected to previous page because he isn't an Unit-PI or Admin
        self.assertEqual(response.status_code,302)
8aa9bbc0   Alexis Koralewski   Add new tests of F01
639
640
641
642
643
644
645
646
        try: 
            is_user_delete = PyrosUser.objects.get(username=user2.username)
        except:
            is_user_delete = True
        # user shouldn't be deleted 
        self.assertNotEqual(is_user_delete,True)

        # log out as u1
190c6ece   Alexis Koralewski   Fixing test with ...
647
        self.client.post(reverse("user_logout"))
8aa9bbc0   Alexis Koralewski   Add new tests of F01
648
649
650

        # log in as u2 
        response = self.client.post(reverse("login_validation"),{"email":user2.email,"password":"password123"})
684e8b01   Alexis Koralewski   fixing tests and ...
651
        self.assertEqual(response.status_code,302)
8aa9bbc0   Alexis Koralewski   Add new tests of F01
652
653
654
655
656
657

        # u2 try to delete u1
        response = self.client.post(reverse("user-delete",kwargs={"pk":user.id}))
        # u2 is redirected after deletion of u1
        self.assertEqual(response.status_code,302)
        try: 
8aa9bbc0   Alexis Koralewski   Add new tests of F01
658
659
660
661
662
            is_user_delete = PyrosUser.objects.get(id=user.id)
        except:
            is_user_delete = True
        # user should be deleted
        self.assertEqual(is_user_delete,True)
8aa9bbc0   Alexis Koralewski   Add new tests of F01
663