Blame view

src/core/pyros_django/user_manager/tests.py 40.7 KB
e419a2f6   Alexis Koralewski   Add new version f...
1
2
3
4
from django.test import TestCase
from common.models import *
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
7
8
9
10
11


class UserManagerTests(TestCase):

    def setUp(self):
        institute = Institute.objects.create(name="CNRS",quota=999.0)
8aa9bbc0   Alexis Koralewski   Add new tests of F01
12
        period = Period.objects.create()
684e8b01   Alexis Koralewski   fixing tests and ...
13
        science_theme = ScienceTheme.objects.create(name="Solar System")
e419a2f6   Alexis Koralewski   Add new version f...
14
        UserLevel.objects.create(name="Visitor",desc="Visitor description",priority=0,quota=0.0)
8aa9bbc0   Alexis Koralewski   Add new tests of F01
15
16
17
        UserLevel.objects.create(name="Observer",desc="Observer description",priority=2,quota=10.0)
        UserLevel.objects.create(name="Unit-PI",desc="Unit-PI description",priority=6,quota=100.0)
        UserLevel.objects.create(name="TAC",desc="TAC description",priority=3,quota=0)
e419a2f6   Alexis Koralewski   Add new version f...
18
        UserLevel.objects.create(name="Admin",desc="Admin description",priority=7,quota=1000.0)
8aa9bbc0   Alexis Koralewski   Add new tests of F01
19
        Country.objects.create(name="France")
e419a2f6   Alexis Koralewski   Add new version f...
20
21
22
23
24
        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 ...
25
                                           "roles":"Admin","question":"six","timer":"11","iambot":""
e419a2f6   Alexis Koralewski   Add new version f...
26
27
                                           })
        
8aa9bbc0   Alexis Koralewski   Add new tests of F01
28
        
684e8b01   Alexis Koralewski   fixing tests and ...
29
30
31
32
        #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...
33
34
35
36
        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 ...
37
38
39
40
        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...
41
42
43
44
45
46

        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
47
        self.assertEqual(UserLevel.objects.count(), 5, "There should be 5 UserLevel")
e419a2f6   Alexis Koralewski   Add new version f...
48
49
50
51
52
53
54

        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
55
        self.assertEqual(UserLevel.objects.count(), 5, "There should be 5 UserLevel")
e419a2f6   Alexis Koralewski   Add new version f...
56
57
58
59
60
61

        # 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 ...
62
        current_user.set_password("aze")
e419a2f6   Alexis Koralewski   Add new version f...
63
64
65
66
        current_user.save()
        self.assertEqual(PyrosUser.objects.all()[0].is_active, True, "user should be active")

        # Log user
684e8b01   Alexis Koralewski   fixing tests and ...
67
        path = reverse("login_validation")
e419a2f6   Alexis Koralewski   Add new version f...
68
        response = self.client.post(path, {"email": "toto@titi.fr", "password": "aze"})
684e8b01   Alexis Koralewski   fixing tests and ...
69
        #self.assertTrue(response.context.get("success"))
e419a2f6   Alexis Koralewski   Add new version f...
70
71
72
73
        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
74
        self.assertEqual(UserLevel.objects.count(), 5, "There should be 5 UserLevel")
e419a2f6   Alexis Koralewski   Add new version f...
75
76
77
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
        path = "/user_manager/login"
        response = self.client.post(path, {"email": "toto@titi.fr", "password": "aze"})

684e8b01   Alexis Koralewski   fixing tests and ...
86
        #self.assertFalse(response.context.get("success"))
e419a2f6   Alexis Koralewski   Add new version f...
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
        self.assertNotIn('_auth_user_id', self.client.session, "The user should be logged in")

    def test_wrong_email(self):
        path = "/user_manager/login"
        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):
        path = "/user_manager/login"
        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")
        path = "/user_manager/logout"
        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")

    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' ")

    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' ")

        
    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)
8aa9bbc0   Alexis Koralewski   Add new tests of F01
161
162
163
164
165
166
167
168
169
170
171
        self.assertNotEqual(user.get_max_priority_quota(),UserLevel.objects.get(name="Unit-PI").desc,"The quota of user_level shouldn't be 1000.0 ")


    # 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 ...
172
                                           "roles":"Admin","question":"six","timer":"11","iambot":""
8aa9bbc0   Alexis Koralewski   Add new tests of F01
173
174
175
176
177
178
179
180
181
182
183
                                           })
        # 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 ...
184
                                           "roles":"Admin","question":"six","timer":"11","iambot":""
8aa9bbc0   Alexis Koralewski   Add new tests of F01
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
                                           })
        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")
        self.client.get(path)
        # 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 ...
211
        response = self.client.get(reverse("index_scientific_program"))
8aa9bbc0   Alexis Koralewski   Add new tests of F01
212
213
214
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
        # 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 ...
241
        self.assertEqual(response.status_code,302)
8aa9bbc0   Alexis Koralewski   Add new tests of F01
242
        # test if user can access his profile page