Blame view

src/core/pyros_django/scientific_program/tests.py 34.8 KB
a6e63604   Alexis Koralewski   adding agentSP an...
1
2
from logging import log
from sys import stdout
874a445f   Alexis Koralewski   Fixing tests of o...
3
from unittest import skip
5a434264   Alexis Koralewski   New version of Sc...
4
from django.http import response
d73ed066   ALEXIS-PC\alexis   adding scientific...
5
from django.test import TestCase
a6e63604   Alexis Koralewski   adding agentSP an...
6
from django.utils import timezone, tree
5a434264   Alexis Koralewski   New version of Sc...
7
8
9
10
from common.models import PyrosUser,ScientificProgram,Period,SP_Period,SP_Period_User,SP_Period_Guest,UserLevel,Institute,Country
from django.urls import reverse
from django.core import mail
from django.conf import settings
a6e63604   Alexis Koralewski   adding agentSP an...
11
12
13
14
15
16
from datetime import date
from datetime import datetime
from unittest.mock import patch
import subprocess,time,os
import multiprocessing
from .AgentSP import *
d73ed066   ALEXIS-PC\alexis   adding scientific...
17
# Create your tests here.
5a434264   Alexis Koralewski   New version of Sc...
18
19
20
class ScientificProgramTests(TestCase):
    fixtures = ['tests/scientific_program_TZ.json']
    def setUp(self) -> None:
5a434264   Alexis Koralewski   New version of Sc...
21
22
        password = "password123"
        Period.objects.create()
a6e63604   Alexis Koralewski   adding agentSP an...
23
24
        self.period2 = Period.objects.create(start_date=Period.objects.all().first().end_date)
        self.period3 = Period.objects.create(start_date=self.period2.end_date,submission_duration=550)
5a434264   Alexis Koralewski   New version of Sc...
25
26
27
28
29
        self.usr1 = PyrosUser.objects.get(username="observer")
        self.usr2 = PyrosUser.objects.get(username="unit_pi")
        self.usr3 = PyrosUser.objects.get(username="tac")
        self.usr4 = PyrosUser.objects.get(username="operator")
        self.usr5 = PyrosUser.objects.get(username="observer2")
a6e63604   Alexis Koralewski   adding agentSP an...
30
31
32
        self.usr6 = PyrosUser.objects.get(username="tac2")
        self.usr7 = PyrosUser.objects.get(username="observer3")
        self.usr8 = PyrosUser.objects.get(username="observer4")
5a434264   Alexis Koralewski   New version of Sc...
33
34
35
36
37
38
39
40
41
42
43
44


        self.usr1.set_password(password)
        self.usr1.save()
        self.usr2.set_password(password)
        self.usr2.save()
        self.usr3.set_password(password)
        self.usr3.save()
        self.usr4.set_password(password)
        self.usr4.save()
        self.usr5.set_password(password)
        self.usr5.save()
a6e63604   Alexis Koralewski   adding agentSP an...
45
46
47
48
49
50
51
52
        self.usr6.set_password(password)
        self.usr6.save()
        self.usr7.set_password(password)
        self.usr7.save()
        self.usr8.set_password(password)
        self.usr8.save()

    def logout(self):
5a434264   Alexis Koralewski   New version of Sc...
53
        self.client.get(reverse("user_logout"))
a6e63604   Alexis Koralewski   adding agentSP an...
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
        print("Log out")
    def login_as_user(self,number):
        if number == 1:
            self.client.post(reverse("login_validation"),{"email":self.usr1,"password":"password123"})
        elif number == 2:
            self.client.post(reverse("login_validation"),{"email":self.usr2,"password":"password123"})
        elif number == 3:
            self.client.post(reverse("login_validation"),{"email":self.usr3,"password":"password123"})
        elif number == 4:
            self.client.post(reverse("login_validation"),{"email":self.usr4,"password":"password123"})
        elif number == 5:
            self.client.post(reverse("login_validation"),{"email":self.usr5,"password":"password123"})
        elif number == 6:
            self.client.post(reverse("login_validation"),{"email":self.usr6,"password":"password123"})
        elif number == 7:
            self.client.post(reverse("login_validation"),{"email":self.usr7,"password":"password123"})
        elif number == 8:
            self.client.post(reverse("login_validation"),{"email":self.usr8,"password":"password123"})
        print(f'Log in as {self.client.session["user"]}')

    def create_dummy_SCP(self,name,user_number):
        """
        Generate a SCP
        """
        self.login_as_user(user_number)
5a434264   Alexis Koralewski   New version of Sc...
79
        path = reverse('create_scientific_program')
a6e63604   Alexis Koralewski   adding agentSP an...
80
81
82
83
84
85
86
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
        post_data = {
            "name":name,
            "description_short":"sp short desc",
            "description_long":"sp long desc",
            "science_theme":1,
        }
        response = self.client.post(path,post_data)
        self.logout()
        return ScientificProgram.objects.get(name=name)
    
    def create_dummy_SCP_Period(self,scientific_program,user_number):
        self.login_as_user(user_number)

        path = reverse('create_scientific_program_period',kwargs={"id_SP":scientific_program.id})
        post_data = {
            "period":Period.objects.next_period().id,
            "public_visibility":SP_Period.VISIBILITY_YES,
            "quota_minimal":2,
            "quota_nominal":5,
            "over_quota_duration":2,
            "token":2
        }
         
        response = self.client.post(path,post_data)
        sp_period = SP_Period.objects.get(scientific_program=scientific_program,period=Period.objects.next_period())
        
        self.logout()
        return sp_period

    def create_dummies_SCP_and_SCP_Period(self,name_of_SCP,user_number):
        self.create_dummy_SCP(name_of_SCP,user_number)
        scientific_program = ScientificProgram.objects.get(name=name_of_SCP)
        sp_period = self.create_dummy_SCP_Period(scientific_program,user_number)
        return sp_period

    def test_SCP_Observer_can_submit_new_SP_from_scratch(self):
        self.login_as_user(1)
        path = reverse('create_scientific_program')
        post_data = {
            "name":"test",
            "description_short":"sp short desc",
            "description_long":"sp long desc",
            "science_theme":1,
        }
        response = self.client.post(path,post_data)
5a434264   Alexis Koralewski   New version of Sc...
125
        self.assertEqual(response.status_code,302)
5a434264   Alexis Koralewski   New version of Sc...
126
        self.assertEqual(ScientificProgram.objects.all().count(),1)
a6e63604   Alexis Koralewski   adding agentSP an...
127
128
        current_sp = ScientificProgram.objects.all().first()
        self.assertEqual(current_sp.sp_pi,self.usr1)
5a434264   Alexis Koralewski   New version of Sc...
129

a6e63604   Alexis Koralewski   adding agentSP an...
130
131
132
        self.logout()

        self.login_as_user(2)
5a434264   Alexis Koralewski   New version of Sc...
133
        path = reverse('create_scientific_program')
a6e63604   Alexis Koralewski   adding agentSP an...
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
161
162
163
164
165
166
        post_data = {
            "name":"test 2",
            "description_short":"test2 short desc",
            "description_long":"test2 long desc",
            "science_theme":2,
        }
        response = self.client.post(path,post_data)
        self.assertEqual(response.status_code,302)
        # user can't access to this page
        self.assertEqual(response.context,None)
        self.logout()

    def test_SCP_Observer_can_submit_new_SP_from_template(self):
        self.create_dummy_SCP("test",1)
        current_sp = ScientificProgram.objects.all().first()

        self.login_as_user(1)
        path = reverse('create_scientific_program_with_template',kwargs={"id_SP":current_sp.id})
        post_data = {
            "name":"test 2",
            "description_short":"test2 short desc",
            "description_long":"test2 long desc",
            "science_theme":2,
        }
        response = self.client.post(path,post_data)
        self.assertEqual(response.status_code,302)
        self.assertEqual(ScientificProgram.objects.all().count(),2)
        current_sp = ScientificProgram.objects.all().first()
        self.assertEqual(current_sp.sp_pi,self.usr1)

        self.logout()

        self.login_as_user(2)
5a434264   Alexis Koralewski   New version of Sc...
167
        
a6e63604   Alexis Koralewski   adding agentSP an...
168
169
170
171
172
173
174
175
176
177
178
179
180
        path = reverse('create_scientific_program_with_template',kwargs={"id_SP":current_sp.id})
        post_data = {
            "name":"test 3",
            "description_short":"test3 short desc",
            "description_long":"test3 long desc",
            "science_theme":2,
        }
        response = self.client.post(path,post_data)
        self.assertEqual(response.status_code,302)
        # user can't access to this page
        self.assertEqual(response.context,None)
        self.assertEqual(ScientificProgram.objects.all().count(),2)
        self.logout()
5a434264   Alexis Koralewski   New version of Sc...
181

a6e63604   Alexis Koralewski   adding agentSP an...
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
    def test_SCP_Observer_can_submit_SP_Period_from_scratch(self):
        self.create_dummy_SCP("test",1)
        self.logout()
        self.login_as_user(1)
        current_sp = ScientificProgram.objects.all().first()
        path = reverse('create_scientific_program_period',kwargs={"id_SP":current_sp.id})
        post_data = {
            "period":Period.objects.next_period().id,
            "public_visibility":SP_Period.VISIBILITY_YES,
            "quota_minimal":2,
            "quota_nominal":5,
            "over_quota_duration":2,
            "token":2
        }
         
        response = self.client.post(path,post_data)
   
        self.assertEqual(response.status_code,302)
        self.assertEqual(SP_Period.objects.filter(scientific_program=current_sp).count(),1)
        current_sp_period = SP_Period.objects.filter(scientific_program=current_sp).first()
        self.assertEqual(current_sp_period.quota_minimal,2)
        self.assertEqual(current_sp_period.status,SP_Period.STATUSES_DRAFT)
5a434264   Alexis Koralewski   New version of Sc...
204

a6e63604   Alexis Koralewski   adding agentSP an...
205
        self.logout()
5a434264   Alexis Koralewski   New version of Sc...
206

a6e63604   Alexis Koralewski   adding agentSP an...
207
208
209
210
211
212
        self.login_as_user(2)
        path = reverse('create_scientific_program_period',args=[1])
        post_data = {
            "public_visibility":SP_Period.VISIBILITY_YES,
            "quota_minimal":2,
            "quota_nominal":5,
a6603b26   Alexis Koralewski   Django and Python...
213
            "quota_allocated":"",
a6e63604   Alexis Koralewski   adding agentSP an...
214
            "over_quota_duration":2,
a6603b26   Alexis Koralewski   Django and Python...
215
            "over_quota_duration_allocated":"",
a6e63604   Alexis Koralewski   adding agentSP an...
216
            "token":2,
a6603b26   Alexis Koralewski   Django and Python...
217
218
219
220
221
222
            "token_allocated":"",
            "vote_referee1":"",
            "reason_referee1":"",
            "vote_referee2":"",
            "reason_referee2":"",
            "priority":"",
a6e63604   Alexis Koralewski   adding agentSP an...
223
224
225
            "is_valid":False
        }
        response = self.client.post(path,post_data)
5a434264   Alexis Koralewski   New version of Sc...
226
        self.assertEqual(response.status_code,302)
a6e63604   Alexis Koralewski   adding agentSP an...
227
        # user can't access to this page
5a434264   Alexis Koralewski   New version of Sc...
228
        self.assertEqual(response.context,None)
a6e63604   Alexis Koralewski   adding agentSP an...
229
        self.logout()
5a434264   Alexis Koralewski   New version of Sc...
230

a6e63604   Alexis Koralewski   adding agentSP an...
231
232
233
234
235
    def test_SCP_Observer_can_submit_existing_SP_for_new_Period(self):
        self.create_dummy_SCP("test",1)
        current_scientific_program = ScientificProgram.objects.all().first()
        self.create_dummy_SCP_Period(current_scientific_program,1)
        sp_period = SP_Period.objects.all().first()
5a434264   Alexis Koralewski   New version of Sc...
236
        
a6e63604   Alexis Koralewski   adding agentSP an...
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
        self.login_as_user(1)
        path = reverse('repropose_scientific_program',kwargs={"id_SP":current_scientific_program.id,"id_SP_Period":sp_period.id})
        post_data = {
            "period":self.period3.id,
            "public_visibility":SP_Period.VISIBILITY_YES,
            "quota_minimal":2,
            "quota_nominal":5,
            "over_quota_duration":2,
            "token":2
        }
        response = self.client.post(path,post_data)
        self.assertEqual(SP_Period.objects.filter(scientific_program=current_scientific_program).count(),2)
        current_sp_period = SP_Period.objects.filter(scientific_program=current_scientific_program)[1]
        SP_Period_User(SP_Period=current_sp_period,user=self.usr5).save()
        self.assertEqual(current_sp_period.quota_minimal,2)
        self.assertEqual(current_sp_period.status,SP_Period.STATUSES_DRAFT)
        # We should have one user which is from the previous period 
        self.assertEqual(SP_Period_User.objects.filter(SP_Period=current_sp_period).count(),1)

    def test_SCP_Unit_PI_can_add_Period(self):
        self.login_as_user(2)
        path = reverse("create_period")
        today = timezone.now().date().strftime("%d/%m/%Y")
        post_data = {
            "start_date_picker":today,
            "exploitation_duration":182,
            "submission_duration":182,
            "evaluation_duration":31,
            "validation_duration":5,
            "notification_duration":10,
            "property_of_data_duration":365,
            "data_accessibility_duration":365*10
        }
5a434264   Alexis Koralewski   New version of Sc...
270
        
a6e63604   Alexis Koralewski   adding agentSP an...
271
272
273
274

        response = self.client.post(path,post_data)
        # This creation should fail, we should have 3 periods
        self.assertEqual(Period.objects.all().count(),3)
5a434264   Alexis Koralewski   New version of Sc...
275
        self.assertEqual(response.status_code,200)
a6e63604   Alexis Koralewski   adding agentSP an...
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
        self.assertTrue(response.context["error"])

        start_date = Period.objects.latest_period().end_date.strftime("%d/%m/%Y")
        post_data = {
            "start_date_picker":start_date,
            "exploitation_duration":182,
            "submission_duration":182,
            "evaluation_duration":31,
            "validation_duration":5,
            "notification_duration":10,
            "property_of_data_duration":365,
            "data_accessibility_duration":365*10
        }
        response = self.client.post(path,post_data)
        # This creation should success, we should have 4 periods and should be redirected to period_list
        self.assertEqual(Period.objects.all().count(),4)
        self.assertEqual(response.status_code,302)
        self.logout()

    def test_SCP_view_main_menu(self):
        self.create_dummy_SCP("test",1)
        sp_period = self.create_dummy_SCP_Period(ScientificProgram.objects.all().first(),1)
        self.login_as_user(1)
        path = reverse("index_scientific_program")
5a434264   Alexis Koralewski   New version of Sc...
300

5a434264   Alexis Koralewski   New version of Sc...
301
        response = self.client.get(path)
a6e63604   Alexis Koralewski   adding agentSP an...
302
303
304
305
306
307
308
309
310
        # u1 should be able to add, view scientific programs and view Periods
        self.assertTrue(response.context["CAN_SUBMIT_SP"])
        self.assertTrue(response.context["CAN_VIEW_SP"])
        self.assertTrue(response.context["CAN_VIEW_EXPLOITATION_PERIOD"])
        self.assertFalse(response.context["CAN_VIEW_ALL_SP"])
        self.assertFalse(response.context["CAN_EVALUATE_SP"])
        self.assertFalse(response.context["CAN_VALIDATE_SP"])
        self.assertFalse(response.context["CAN_CREATE_EXPLOITATION_PERIOD"])
        self.assertFalse(response.context["CAN_ASSOCIATE_TAC_TO_SP"])
5a434264   Alexis Koralewski   New version of Sc...
311

a6e63604   Alexis Koralewski   adding agentSP an...
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
        self.logout()
        with patch('django.utils.timezone.now', return_value=datetime.strptime(str(Period.objects.next_period().submission_start_date),"%Y-%m-%d")):
            # u2 should be able to associate TAC to SP
            # Submission period
            self.login_as_user(2)
            response = self.client.get(path)
            self.assertTrue(response.context["CAN_ASSOCIATE_TAC_TO_SP"])
            self.logout()

        with patch('django.utils.timezone.now', return_value=datetime.strptime(str(Period.objects.next_period().unit_pi_validation_start_date),"%Y-%m-%d")):
            self.login_as_user(2)
            # Validation period
            sp_period.status = SP_Period.STATUSES_EVALUATED
            sp_period.save()
            response = self.client.get(path)
            self.assertEqual(self.client.session["role"],"Unit-PI")
            # u2 should be able to do everything outside submitting, evaluate SP and see the obsever's view for sp 
            self.assertTrue(response.context["CAN_VIEW_EXPLOITATION_PERIOD"])
            self.assertTrue(response.context["CAN_VIEW_ALL_SP"])
            self.assertTrue(response.context["CAN_CREATE_EXPLOITATION_PERIOD"])
            self.assertTrue(response.context["CAN_VALIDATE_SP"])
            self.assertFalse(response.context["CAN_ASSOCIATE_TAC_TO_SP"])
            self.assertFalse(response.context["CAN_VIEW_SP"])
            self.assertFalse(response.context["CAN_SUBMIT_SP"])
            self.assertFalse(response.context["CAN_EVALUATE_SP"])
            self.logout()

        with patch('django.utils.timezone.now', return_value=datetime.strptime(str(Period.objects.next_period().submission_end_date),"%Y-%m-%d")):
            self.login_as_user(3)
            sp_period.status = SP_Period.STATUSES_SUBMITTED
            sp_period.save()
            
            response = self.client.get(path)
            # u3 should be able to only evaluate SP 
            self.assertTrue(response.context["CAN_EVALUATE_SP"])
            self.assertFalse(response.context["CAN_VIEW_EXPLOITATION_PERIOD"])
            self.assertFalse(response.context["CAN_VIEW_ALL_SP"])
            self.assertFalse(response.context["CAN_CREATE_EXPLOITATION_PERIOD"])
            self.assertFalse(response.context["CAN_VALIDATE_SP"])
            self.assertFalse(response.context["CAN_VIEW_SP"])
            self.assertFalse(response.context["CAN_SUBMIT_SP"])
            self.assertFalse(response.context["CAN_ASSOCIATE_TAC_TO_SP"])
5a434264   Alexis Koralewski   New version of Sc...
354

a6e63604   Alexis Koralewski   adding agentSP an...
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
            self.logout()

    def test_SCP_view_sp_list_and_detail(self):
        sp1 = self.create_dummies_SCP_and_SCP_Period("test",1)
        sp2 = self.create_dummies_SCP_and_SCP_Period("test2",1)    
        sp3 = self.create_dummies_SCP_and_SCP_Period("test3",1)
        sp4 = self.create_dummies_SCP_and_SCP_Period("test4",1)
        sp5 = self.create_dummies_SCP_and_SCP_Period("test5",1)
        sp6 = self.create_dummies_SCP_and_SCP_Period("test6",8)
        sp7 = self.create_dummies_SCP_and_SCP_Period("test7",7)
        sp2.status = SP_Period.STATUSES_SUBMITTED
        sp2.referee2 = self.usr3
        sp2.referee1 = self.usr6
        sp2.save()
        sp3.status = SP_Period.STATUSES_EVALUATED
        sp3.referee1 = self.usr6
        sp3.referee2 = self.usr3
        sp3.save()
        sp4.status = SP_Period.STATUSES_ACCEPTED
        sp4.save()
        SP_Period_User.objects.create(SP_Period=sp4,user=self.usr5)
        sp5.status = SP_Period.STATUSES_REJECTED
        sp6.status = SP_Period.STATUSES_ACCEPTED
        sp6.save()
        SP_Period_User.objects.create(SP_Period=sp6,user=self.usr1)
        sp7.status = SP_Period.STATUSES_ACCEPTED
        sp7.save()
        SP_Period_User.objects.create(SP_Period=sp7,user=self.usr5)

        self.login_as_user(1)
        path = reverse("own_scientific_program_list")
5a434264   Alexis Koralewski   New version of Sc...
386
        response = self.client.get(path)
a6e63604   Alexis Koralewski   adding agentSP an...
387
388
        # Can't compare 2 queryset, it returns an error despite they are equal.. so we transform those queryset into list and compare them.
        self.assertEqual(list(ScientificProgram.objects.filter(sp_pi=self.usr1).order_by("-id")),list(response.context["sp_of_user"]))
5a434264   Alexis Koralewski   New version of Sc...
389

a6e63604   Alexis Koralewski   adding agentSP an...
390
        path = reverse("detail_scientific_program_period",kwargs={"id_sp":sp1.scientific_program.id,"id_period":sp1.period.id})
5a434264   Alexis Koralewski   New version of Sc...
391
        response = self.client.get(path)
a6e63604   Alexis Koralewski   adding agentSP an...
392
        self.assertFalse(response.context["CAN_VIEW_TAC_VOTES"])
5a434264   Alexis Koralewski   New version of Sc...
393

a6e63604   Alexis Koralewski   adding agentSP an...
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
        self.logout()
        
        # login as TAC 1 and put today date to evaluation period
        with patch('django.utils.timezone.now', return_value=datetime.strptime(str(Period.objects.next_period().submission_end_date),"%Y-%m-%d")):
            self.login_as_user(3)
            path = reverse("list_submitted_scientific_program")
            response = self.client.get(path)
            self.assertEqual(list(SP_Period.objects.filter(referee2=self.usr3,period=Period.objects.next_period(),status=SP_Period.STATUSES_SUBMITTED)),response.context["list_of_evaluated_sp"])
            path = reverse("detail_scientific_program_period",kwargs={"id_sp":sp2.scientific_program.id,"id_period":sp2.period.id})
            
            response = self.client.get(path)
            self.assertEqual(response.status_code,302)
            # user can't access to this page
            self.assertEqual(response.context,None)
            self.logout()

        # log in as user observer
        self.login_as_user(5)
        path = reverse("scientific_program_list")
5a434264   Alexis Koralewski   New version of Sc...
413
        response = self.client.get(path)
a6e63604   Alexis Koralewski   adding agentSP an...
414
415
416
417
418
419
420
421
422
423
424
425
426
427
        sp_of_user = list(("test4","test7"))
        self.assertListEqual(sp_of_user,list(response.context["sp_of_user"].values_list("name",flat=True)))
        path = reverse("detail_scientific_program_period",kwargs={"id_sp":sp4.scientific_program.id,"id_period":sp4.period.id})
        response = self.client.get(path)
        self.assertFalse(response.context["CAN_VIEW_TAC_VOTES"])
        self.assertFalse(response.context["CAN_SUBMIT_PROPOSAL"])
        self.logout()
        
        # log in as unit pi
        self.login_as_user(2)
        path = reverse("scientific_program_list")
        response = self.client.get(path)
        self.assertListEqual(list(ScientificProgram.objects.all().order_by("-id")),list(response.context["scientific_programs"]))
        self.logout()
5a434264   Alexis Koralewski   New version of Sc...
428

a6e63604   Alexis Koralewski   adding agentSP an...
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
    def test_SCP_list_Periods(self):
        self.login_as_user(1)
        path = reverse("period_list")
        response = self.client.get(path)
        self.assertContains(response,"(Current period)")
        
        self.assertEqual(Period.objects.all().count(),len(response.context["future_periods"]))
        self.logout()

    def test_SCP_detail_Period(self):
        sp1 = self.create_dummies_SCP_and_SCP_Period("test",1)
        sp2 = self.create_dummies_SCP_and_SCP_Period("test2",1)    
        sp3 = self.create_dummies_SCP_and_SCP_Period("test3",1)
        sp4 = self.create_dummies_SCP_and_SCP_Period("test4",1)
        sp5 = self.create_dummies_SCP_and_SCP_Period("test5",1)
        
        self.login_as_user(1)
        period = sp1.period
        path = reverse("detail_period",kwargs={"id":period.id})
        response = self.client.get(path)
        # we should see the same number of SP_Period (5) that are associated to user 1 
        self.assertEqual(SP_Period.objects.all().count(),len(response.context["sp_periods"]))
        self.logout()

    def test_SCP_edit(self):
        sp1 = self.create_dummies_SCP_and_SCP_Period("test",1)
        sp2 = self.create_dummies_SCP_and_SCP_Period("test2",1)    
        sp3 = self.create_dummies_SCP_and_SCP_Period("test3",1)
        sp4 = self.create_dummies_SCP_and_SCP_Period("test4",1)
        sp5 = self.create_dummies_SCP_and_SCP_Period("test5",5)
        sp2.status = SP_Period.STATUSES_SUBMITTED
        sp2.save()
        sp3.status = SP_Period.STATUSES_ACCEPTED
        sp3.save()
        sp4.status = SP_Period.STATUSES_ACCEPTED
        sp4.save()
        sp5.status = SP_Period.STATUSES_ACCEPTED
        sp5.save()
        post_data = {

            "name":	"test",
            "description_short":"modified test",
            "description_long":"modified test",
            "science_theme"	:"2",
            "public_visibility":"Yes",
            "quota_minimal":"0",
            "quota_nominal":"0",
            "over_quota_duration":"0",
            "token":"0",
            "vote_referee1":"",
            "reason_referee1":"",
            "users":"",
        }
        self.login_as_user(1)
        path = reverse("edit_scientific_program_period",kwargs={"id_sp":sp1.scientific_program.id,"id_period":sp1.period.id})
        response = self.client.post(path,post_data)
        self.assertEqual(response.status_code,302)
        updated_sp = SP_Period.objects.get(id=sp1.id)
        self.assertEqual(updated_sp.scientific_program.description_short,"modified test")

        path = reverse("edit_scientific_program_period",kwargs={"id_sp":sp2.scientific_program.id,"id_period":sp2.period.id})
        post_data = {

            "name":	"test",
            "description_short":"modified test2",
            "description_long":"modified test2",
            "science_theme"	:"2",
            "public_visibility":"Yes",
            "quota_minimal":"10",
            "quota_nominal":"0",
            "over_quota_duration":"0",
            "token":"0",
            "vote_referee1":"",
            "reason_referee1":"",
            "users":"",
        }
        response = self.client.post(path,post_data)
        self.assertEqual(response.status_code,302)
        updated_sp = SP_Period.objects.get(id=sp2.id)
        # user can't modify this SP_Period because the status isn't DRAFT
        self.assertNotEqual(updated_sp.scientific_program.description_short,"modified test2")
        self.assertNotEqual(updated_sp.quota_minimal,10)

        path = reverse("edit_scientific_program_period",kwargs={"id_sp":sp5.scientific_program.id,"id_period":sp5.period.id})
        response = self.client.post(path,post_data)
        # u1 can't edit SP5 because he is not the SP_PI
        self.assertEqual(response.status_code,302)
        self.assertNotEqual(updated_sp.scientific_program.description_short,"modified test2")
        self.assertNotEqual(updated_sp.quota_minimal,10)
        self.assertEqual(response.url,reverse("detail_scientific_program_period",kwargs={"id_sp":sp5.scientific_program.id,"id_period":sp5.period.id}))
        self.logout()
    
    def test_SCP_add_users(self):
        sp1 = self.create_dummies_SCP_and_SCP_Period("test",1)
        post_data = {
5a434264   Alexis Koralewski   New version of Sc...
524

a6e63604   Alexis Koralewski   adding agentSP an...
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
            "name":	"test",
            "description_short":"modified test",
            "description_long":"modified test",
            "science_theme"	:"2",
            "public_visibility":"Yes",
            "quota_minimal":"0",
            "quota_nominal":"0",
            "over_quota_duration":"0",
            "token":"0",
            "vote_referee1":"",
            "reason_referee1":"",
            "users":"observer2@test.com",
        }
        self.login_as_user(1)
        path = reverse("edit_scientific_program_period",kwargs={"id_sp":sp1.scientific_program.id,"id_period":sp1.period.id})
        response = self.client.post(path,post_data)
        self.assertEqual(SP_Period_Guest.objects.filter(SP_Period=sp1).first().email,"observer2@test.com")
        self.assertEqual(len(mail.outbox),1)
        self.assertIn("observer2@test.com",mail.outbox[0].recipients())
        self.logout()
5a434264   Alexis Koralewski   New version of Sc...
545

a6e63604   Alexis Koralewski   adding agentSP an...
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
        url = reverse("sp_register",kwargs={"id_SP":sp1.scientific_program.id,"id_period":sp1.period.id})
        self.login_as_user(5)
        response = self.client.get(url)
        self.assertEqual(response.status_code,302)
        self.assertEqual(response.url,reverse("detail_scientific_program_period",kwargs={"id_sp":sp1.scientific_program.id,"id_period":sp1.period.id}))
        self.assertEqual(SP_Period_Guest.objects.all().count(),0)
        self.assertEqual(SP_Period_User.objects.filter(SP_Period=sp1).count(),1)
        self.assertEqual(SP_Period_User.objects.filter(SP_Period=sp1).first().user,self.usr5)
        self.logout()

    def test_SCP_delete(self):
        sp1 = self.create_dummies_SCP_and_SCP_Period("test",1)
        sp2 = self.create_dummy_SCP("test2",1)
        self.login_as_user(1)
        path = reverse("delete_scientific_program",kwargs={"id_sp":sp1.scientific_program.id})
5a434264   Alexis Koralewski   New version of Sc...
561
        response = self.client.get(path)
a6e63604   Alexis Koralewski   adding agentSP an...
562
        # Can't delete this SP because it's linked to a period
5a434264   Alexis Koralewski   New version of Sc...
563
        self.assertEqual(response.status_code,302)
a6e63604   Alexis Koralewski   adding agentSP an...
564
565
        self.assertEqual(response.url,reverse("detail_scientific_program", args=[sp1.scientific_program.id]))
        self.assertEqual(ScientificProgram.objects.all().count(),2)
5a434264   Alexis Koralewski   New version of Sc...
566

a6e63604   Alexis Koralewski   adding agentSP an...
567
        path = reverse("delete_scientific_program",kwargs={"id_sp":sp2.id})
5a434264   Alexis Koralewski   New version of Sc...
568
        response = self.client.get(path)
a6e63604   Alexis Koralewski   adding agentSP an...
569
        # Can delete this SP
5a434264   Alexis Koralewski   New version of Sc...
570
        self.assertEqual(response.status_code,302)
a6e63604   Alexis Koralewski   adding agentSP an...
571
572
573
574
        self.assertEqual(response.url,reverse("index_scientific_program"))
        self.assertEqual(ScientificProgram.objects.all().count(),1)

        self.logout()
5a434264   Alexis Koralewski   New version of Sc...
575

a6e63604   Alexis Koralewski   adding agentSP an...
576
577
578
579
580
581
582
    def test_SCP_Period_delete(self):
        sp1 = self.create_dummies_SCP_and_SCP_Period("test",1)
        sp2 = self.create_dummies_SCP_and_SCP_Period("test2",1)
        sp2.status = SP_Period.STATUSES_ACCEPTED
        sp2.save()
        self.login_as_user(1)
        path = reverse("delete_scientific_program_period",kwargs={"id_sp":sp1.scientific_program.id,"id_period":sp1.period.id})
5a434264   Alexis Koralewski   New version of Sc...
583
        response = self.client.get(path)
a6e63604   Alexis Koralewski   adding agentSP an...
584
        # Can delete this SP because it's in draft status
5a434264   Alexis Koralewski   New version of Sc...
585
        self.assertEqual(response.status_code,302)
a6e63604   Alexis Koralewski   adding agentSP an...
586
587
588
589
590
591
592
593
594
595
596
597
        self.assertEqual(response.url,reverse("detail_scientific_program", args=[sp1.scientific_program.id]))
        self.assertEqual(SP_Period.objects.filter(scientific_program=sp1.scientific_program).count(),0)

        path = reverse("delete_scientific_program_period",kwargs={"id_sp":sp2.scientific_program.id,"id_period":sp2.period.id})
        response = self.client.get(path)
        # Can't delete this SP
        self.assertEqual(response.status_code,302)
        self.assertEqual(response.url,reverse("detail_scientific_program", args=[sp2.scientific_program.id]))
        self.assertEqual(SP_Period.objects.filter(scientific_program=sp2.scientific_program).count(),1)
        self.logout()

    def run_agent_SP(self):
874a445f   Alexis Koralewski   Fixing tests of o...
598
        agent = build_agent(AgentSP)
a6e63604   Alexis Koralewski   adding agentSP an...
599
600
        print(agent)
        agent.run()
874a445f   Alexis Koralewski   Fixing tests of o...
601
602
603

    # test not working due to test db and how agent works
    @skip
a6e63604   Alexis Koralewski   adding agentSP an...
604
    def test_SCP_lifecycle(self):
5a434264   Alexis Koralewski   New version of Sc...
605
        
5a434264   Alexis Koralewski   New version of Sc...
606
        
a6e63604   Alexis Koralewski   adding agentSP an...
607
608
609
610
611
612
613
        sp1 = self.create_dummies_SCP_and_SCP_Period("test",1)
        sp2 = self.create_dummies_SCP_and_SCP_Period("test2",1)
        sp3 = self.create_dummies_SCP_and_SCP_Period("test3",1)
        sp3.scientific_program.is_auto_validated = True
        sp3.scientific_program.save()
        os.chdir("./scientific_program/")
        with patch('django.utils.timezone.now', return_value=datetime.strptime(str(Period.objects.next_period().submission_end_date + relativedelta(days=-10)),"%Y-%m-%d")):
874a445f   Alexis Koralewski   Fixing tests of o...
614
615
            # TAC Assignation   
            #p = subprocess.Popen("python3 AgentSP.py test",shell=True)         
a6e63604   Alexis Koralewski   adding agentSP an...
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
            p = multiprocessing.Process(target= self.run_agent_SP,args=())
            p.daemon = True
            p.start()
            
            time.sleep(10)
            p.terminate()
            for sp_period in SP_Period.objects.all().exclude(id=sp3.id):
                # sp period should have referees
                self.assertNotEqual(sp_period.referee1,None)
                self.assertNotEqual(sp_period.referee2,None)
            

        with patch('django.utils.timezone.now', return_value=datetime.strptime(str(Period.objects.next_period().submission_end_date),"%Y-%m-%d")):
            # Evaluation start 
            p = multiprocessing.Process(target= self.run_agent_SP,args=())
            p.daemon = True
            p.start()
            
            time.sleep(10)
            p.terminate()
            for sp_period in SP_Period.objects.all().exclude(id=sp3.id):
                # sp period should have referees
                self.assertEqual(sp_period.status,SP_Period.STATUSES_SUBMITTED)

            # sp3 should be valid
            self.assertEqual(SP_Period.objects.get(id=sp3.id).status,SP_Period.STATUSES_ACCEPTED)
            self.assertEqual(SP_Period.objects.get(id=sp3.id).is_valid,SP_Period.IS_VALID_ACCEPTED)

            # log in as tac to vote
            self.login_as_user(3)
            path = reverse("edit_scientific_program_period",kwargs={"id_sp":sp1.scientific_program.id,"id_period":sp1.period.id})
            post_data = {

                "name":	sp1.scientific_program.name,
                "description_short":"modified test",
                "description_long":"modified test",
                "science_theme"	:sp1.scientific_program.science_theme,
                "public_visibility":"Yes",
                "quota_minimal":sp1.quota_minimal,
                "quota_nominal":sp1.quota_nominal,
                "over_quota_duration":sp1.over_quota_duration,
                "token":sp1.token,
                "vote_referee1":SP_Period.VOTES_YES,
                "reason_referee1":"i agree",
                "users":"",
            }
            response = self.client.post(path,post_data)
            self.assertEqual(response.status_code,302)
            self.assertEqual(SP_Period.objects.get(id=sp1.id).vote_referee1,SP_Period.VOTES_YES)
            self.assertEqual(SP_Period.objects.get(id=sp1.id).reason_referee1,"i agree")
            self.logout()

            # log in as tac 2 to vote
            self.login_as_user(6)
            path = reverse("edit_scientific_program_period",kwargs={"id_sp":sp1.scientific_program.id,"id_period":sp1.period.id})
            post_data = {

                "name":	sp1.scientific_program.name,
                "description_short":"modified test",
                "description_long":"modified test",
                "science_theme"	:sp1.scientific_program.science_theme,
                "public_visibility":"Yes",
                "quota_minimal":sp1.quota_minimal,
                "quota_nominal":sp1.quota_nominal,
                "over_quota_duration":sp1.over_quota_duration,
                "token":sp1.token,
                "vote_referee2":SP_Period.VOTES_YES,
                "reason_referee2":"i agree",
                "users":"",
            }
            response = self.client.post(path,post_data)
            self.assertEqual(response.status_code,302)
            self.assertEqual(SP_Period.objects.get(id=sp1.id).vote_referee2,SP_Period.VOTES_YES)
            self.assertEqual(SP_Period.objects.get(id=sp1.id).reason_referee2,"i agree")
            self.logout()

        with patch('django.utils.timezone.now', return_value=datetime.strptime(str(Period.objects.next_period().unit_pi_validation_start_date),"%Y-%m-%d")):
            # validation start 
            p = multiprocessing.Process(target= self.run_agent_SP,args=())
            p.daemon = True
            p.start()
            
            time.sleep(10)
            p.terminate()
            for sp_period in SP_Period.objects.all().exclude(id=sp3.id):
                # sp period should be evaluated
                self.assertEqual(sp_period.status,SP_Period.STATUSES_EVALUATED)
            # log in as unit pi

            self.login_as_user(2)
            # valid sp1
            path = reverse("edit_scientific_program_period",kwargs={"id_sp":sp1.scientific_program.id,"id_period":sp1.period.id})
            post_data = {
                "name":	sp1.scientific_program.name,
                "description_short":"modified test",
                "description_long":"modified test",
                "science_theme"	:sp1.scientific_program.science_theme,
                "public_visibility":"Yes",
                "quota_minimal":sp1.quota_minimal,
                "quota_nominal":sp1.quota_nominal,
                "over_quota_duration":sp1.over_quota_duration,
                "token":sp1.token,
                "is_valid":SP_Period.IS_VALID_ACCEPTED,
                "quota_allocated" : 10,
                "token_allocated": 5,
                "over_quota_duration_allocated": 5
            }
            response = self.client.post(path,post_data)
            self.assertEqual(response.status_code,302)
            self.assertEqual(SP_Period.objects.get(id=sp1.id).quota_allocated,10)
            self.assertEqual(SP_Period.objects.get(id=sp1.id).is_valid,SP_Period.IS_VALID_ACCEPTED)
            self.assertEqual(SP_Period.objects.get(id=sp1.id).token_allocated,5)
            self.assertEqual(SP_Period.objects.get(id=sp1.id).over_quota_duration_allocated,5)

            # reject sp 2
            path = reverse("edit_scientific_program_period",kwargs={"id_sp":sp2.scientific_program.id,"id_period":sp2.period.id})
            post_data = {
                "name":	sp2.scientific_program.name,
                "description_short":"modified test",
                "description_long":"modified test",
                "science_theme"	:sp2.scientific_program.science_theme,
                "public_visibility":"Yes",
                "quota_minimal":sp2.quota_minimal,
                "quota_nominal":sp2.quota_nominal,
                "over_quota_duration":sp2.over_quota_duration,
                "token":sp2.token,
                "is_valid":SP_Period.IS_VALID_REJECTED,
                "quota_allocated" : 0,
                "token_allocated": 0,
                "over_quota_duration_allocated": 0
            }
            response = self.client.post(path,post_data)
            self.assertEqual(response.status_code,302)
            self.assertEqual(SP_Period.objects.get(id=sp2.id).is_valid,SP_Period.IS_VALID_REJECTED)
            self.assertEqual(SP_Period.objects.get(id=sp2.id).quota_allocated,0)
            self.assertEqual(SP_Period.objects.get(id=sp2.id).token_allocated,0)
            self.assertEqual(SP_Period.objects.get(id=sp2.id).over_quota_duration_allocated,0)
            self.logout()


        with patch('django.utils.timezone.now', return_value=datetime.strptime(str(Period.objects.next_period().notification_start_date),"%Y-%m-%d")):
            # validation start 
            p = multiprocessing.Process(target= self.run_agent_SP,args=())
            p.daemon = True
            p.start()
            
            time.sleep(10)
            p.terminate()
            self.assertEqual(SP_Period.objects.get(id=sp1.id).status,SP_Period.STATUSES_ACCEPTED)
            self.assertEqual(SP_Period.objects.get(id=sp3.id).status,SP_Period.STATUSES_ACCEPTED)
            self.assertEqual(SP_Period.objects.get(id=sp2.id).status,SP_Period.STATUSES_REJECTED)