tests.py
15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
79
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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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
211
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
from django.http import response
from django.test import TestCase
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
import unittest,os
# Create your tests here.
class ScientificProgramTests(TestCase):
fixtures = ['tests/scientific_program_TZ.json']
def setUp(self) -> None:
"""
institute = Institute.objects.get(id=1)
country = Country.objects.create()
self.usr1 = PyrosUser.objects.create(username="toto", country=country, institute=institute)
UserLevel.objects.get(name = "Observer").pyros_users.add(self.usr1)
self.usr1.save()
self.usr2 = PyrosUser.objects.create(username="titi", country=country, institute=institute)
UserLevel.objects.get(name = "Unit-PI").pyros_users.add(self.usr2)
self.usr2.save()
self.usr3 = PyrosUser.objects.create(username="tata", country=country, institute=institute)
UserLevel.objects.get(name = "Operator").pyros_users.add(self.usr3)
self.usr3.save()
self.usr4 = PyrosUser.objects.create(username="tutu", country=country, institute=institute)
UserLevel.objects.get(name = "TAC").pyros_users.add(self.usr4)
self.usr4.save()
"""
password = "password123"
Period.objects.create()
Period.objects.create(start_date=Period.objects.all().first().end_date)
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")
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()
def test_SCP_observer_can_create_sp(self):
self.client.login(username=self.usr1,password="password123")
path = reverse('create_scientific_program')
response = self.client.post(path, {"name": "test", "description_short": "sp short desc", "description_long": "sp long desc",
"public_visibility": "All", "quota_minimal": 10, "quota_nominal": 15,
"over_quota_duration": 5, "token": 3,
"users":"test@example.com,test2@example.com,test3@example.com"
})
self.assertEqual(response.status_code,302)
self.assertEqual(len(mail.outbox),3)
self.assertIn("test@example.com",mail.outbox[0].recipients())
self.assertIn("test2@example.com",mail.outbox[1].recipients())
self.assertIn("test3@example.com",mail.outbox[2].recipients())
domain = settings.DEFAULT_DOMAIN
url = f"{domain}{reverse('sp_register',args=(ScientificProgram.objects.all().order_by('-id').first().pk,Period.objects.all().order_by('-id').first().pk))}"
self.assertIn(url,mail.outbox[2].body)
self.assertEqual(ScientificProgram.objects.all().count(),1)
self.assertEqual(list(SP_Period_Guest.objects.all().values_list("email",flat=True)),["test@example.com","test2@example.com","test3@example.com"])
self.assertEqual(SP_Period.objects.get(scientific_program__name="test").status,SP_Period.STATUSES_DRAFT)
self.assertEqual(ScientificProgram.objects.get(name="test").sp_pi,self.usr1)
self.client.get(reverse("user_logout"))
# u2 shouldn't be able to create a new scientific program
self.client.login(username=self.usr2,password="password123")
path = reverse('create_scientific_program')
response = self.client.post(path, {"name": "test2", "description_short": "sp short desc", "description_long": "sp long desc",
"public_visibility": "All", "quota_minimal": 10, "quota_nominal": 15,
"over_quota_duration": 5, "token": 3,
"users":"test@example.com,test2@example.com,test3@example.com"
})
self.assertEqual(response.status_code,302)
self.assertEqual(len(mail.outbox),3)
self.assertEqual(ScientificProgram.objects.all().count(),1)
def test_SCP_view_sp_list(self):
self.client.login(username=self.usr1,password="password123")
path = reverse('create_scientific_program')
self.client.post(path, {"name": "test", "description_short": "sp short desc", "description_long": "sp long desc",
"public_visibility": "All", "quota_minimal": 10, "quota_nominal": 15,
"over_quota_duration": 5, "token": 3,
"users":"test@example.com,test2@example.com,test3@example.com"
})
self.client.post(path, {"name": "sp2", "description_short": "sp short desc", "description_long": "sp long desc",
"public_visibility": "All", "quota_minimal": 10, "quota_nominal": 15,
"over_quota_duration": 5, "token": 3,
"users":"test@example.com,test2@example.com,test3@example.com"
})
self.client.get(reverse("user_logout"))
# login with another user to create a SP that user 1 shouldn't be able to see
self.client.login(username=self.usr5,password="password123")
self.client.post(path, {"name": "third_project", "description_short": "sp short desc", "description_long": "sp long desc",
"public_visibility": "All", "quota_minimal": 10, "quota_nominal": 15,
"over_quota_duration": 5, "token": 3,
"users":"test@example.com,test2@example.com,test3@example.com"
})
self.client.get(reverse("user_logout"))
# log in again as user 1 to see the scientific program list
self.client.login(username=self.usr1,password="password123")
path = reverse("scientific_program_list")
response = self.client.get(path)
self.assertContains(response,"test")
self.assertContains(response,"sp2")
self.assertNotContains(response,"third_project")
self.client.get(reverse("user_logout"))
# log in as unit pi (user 2)
self.client.login(username=self.usr2,password="password123")
path = reverse("scientific_program_list")
response = self.client.get(path)
self.assertContains(response,"test")
self.assertContains(response,"sp2")
self.assertContains(response,"third_project")
self.client.get(reverse("user_logout"))
# log in as operator (shoudln't be able to see anything)
self.client.login(username=self.usr4,password="password123")
path = reverse("scientific_program_list")
response = self.client.get(path)
# user is redirected to his previous page, however it was the first page he visited to he is redirected to a None page
self.assertEqual(response.status_code,302)
self.assertEqual(response.context,None)
def test_SCP_view_sp_details(self):
self.client.login(username=self.usr1,password="password123")
path = reverse('create_scientific_program')
self.client.post(path, {"name": "test", "description_short": "sp short desc", "description_long": "sp long desc",
"public_visibility": "All", "quota_minimal": 10, "quota_nominal": 15,
"over_quota_duration": 5, "token": 3,
"users":"test@example.com,test2@example.com,test3@example.com"
})
self.client.post(path, {"name": "sp2", "description_short": "sp short desc", "description_long": "sp long desc",
"public_visibility": "All", "quota_minimal": 10, "quota_nominal": 15,
"over_quota_duration": 5, "token": 3,
"users":"test@example.com,test2@example.com,test3@example.com"
})
self.client.get(reverse("user_logout"))
# login with another user to create a SP that user 1 shouldn't be able to see
self.client.login(username=self.usr5,password="password123")
self.client.post(path, {"name": "third_project", "description_short": "sp short desc", "description_long": "sp long desc",
"public_visibility": "All", "quota_minimal": 10, "quota_nominal": 15,
"over_quota_duration": 5, "token": 3,
"users":"test@example.com,test2@example.com,test3@example.com"
})
self.client.get(reverse("user_logout"))
# log in as user 1
self.client.login(username=self.usr1,password="password123")
path = reverse("detail_scientific_program",args=[ScientificProgram.objects.get(name="test").id])
response = self.client.get(path)
self.assertEqual(response.status_code,200)
self.assertContains(response,"sp long desc")
self.assertContains(response,"test")
path = reverse("detail_scientific_program",args=[ScientificProgram.objects.get(name="sp2").id])
response = self.client.get(path)
self.assertEqual(response.status_code,200)
self.assertContains(response,"sp long desc")
self.assertContains(response,"sp2")
self.client.get(reverse("user_logout"))
# log in as user 2
self.client.login(username=self.usr2,password="password123")
path = reverse("detail_scientific_program",args=[ScientificProgram.objects.get(name="test").id])
response = self.client.get(path)
self.assertEqual(response.status_code,200)
self.assertContains(response,"sp long desc")
self.assertContains(response,"test")
path = reverse("detail_scientific_program",args=[ScientificProgram.objects.get(name="sp2").id])
response = self.client.get(path)
self.assertEqual(response.status_code,200)
self.assertContains(response,"sp long desc")
self.assertContains(response,"sp2")
path = reverse("detail_scientific_program",args=[ScientificProgram.objects.get(name="third_project").id])
response = self.client.get(path)
self.assertEqual(response.status_code,200)
self.assertContains(response,"sp long desc")
self.assertContains(response,"third_project")
self.client.get(reverse("user_logout"))
# log in as user 4 (operator)
self.client.login(username=self.usr4,password="password123")
path = reverse("detail_scientific_program",args=[ScientificProgram.objects.get(name="test").id])
response = self.client.get(path)
# user is redirected to his previous page, however it was the first page he visited to he is redirected to a None page
self.assertEqual(response.status_code,302)
self.assertEqual(response.context,None)
path = reverse("detail_scientific_program",args=[ScientificProgram.objects.get(name="sp2").id])
response = self.client.get(path)
# user is redirected to his previous page, however it was the first page he visited to he is redirected to a None page
self.assertEqual(response.status_code,302)
self.assertEqual(response.context,None)
path = reverse("detail_scientific_program",args=[ScientificProgram.objects.get(name="third_project").id])
response = self.client.get(path)
# user is redirected to his previous page, however it was the first page he visited to he is redirected to a None page
self.assertEqual(response.status_code,302)
self.assertEqual(response.context,None)
def test_SCP_sp_update(self):
self.client.login(username=self.usr1,password="password123")
path = reverse('create_scientific_program')
self.client.post(path, {"name": "sp1", "description_short": "sp short desc", "description_long": "sp long desc",
"public_visibility": "All", "quota_minimal": 10, "quota_nominal": 15,
"over_quota_duration": 5, "token": 3,
"users":"test@example.com,test2@example.com,test3@example.com"
})
self.client.post(path, {"name": "sp2", "description_short": "sp short desc", "description_long": "sp long desc",
"public_visibility": "All", "quota_minimal": 10, "quota_nominal": 15,
"over_quota_duration": 5, "token": 3,
"users":"test@example.com,test2@example.com,test3@example.com"
})
self.client.post(path, {"name": "sp3", "description_short": "sp short desc", "description_long": "sp long desc",
"public_visibility": "All", "quota_minimal": 10, "quota_nominal": 15,
"over_quota_duration": 5, "token": 3,
"users":"test@example.com,test2@example.com,test3@example.com"
})
self.client.post(path, {"name": "sp4", "description_short": "sp short desc", "description_long": "sp long desc",
"public_visibility": "All", "quota_minimal": 10, "quota_nominal": 15,
"over_quota_duration": 5, "token": 3,
"users":"test@example.com,test2@example.com,test3@example.com"
})
self.client.get(reverse("user_logout"))
# login with another user to create a SP that user 1 shouldn't be able to see
self.client.login(username=self.usr5,password="password123")
self.client.post(path, {"name": "sp5", "description_short": "sp short desc", "description_long": "sp long desc",
"public_visibility": "All", "quota_minimal": 10, "quota_nominal": 15,
"over_quota_duration": 5, "token": 3,
"users":"test@example.com,test2@example.com,test3@example.com"
})
self.client.get(reverse("user_logout"))
# log in as user 1
self.client.login(username=self.usr1,password="password123")
period = Period.objects.all().order_by('-id').first()
sp1 = ScientificProgram.objects.get(name="sp1")
path = reverse("edit_scientific_program_period",args=[sp1.id,period.id])
self.assertEqual(SP_Period.objects.get(scientific_program=sp1,period=period).status,SP_Period.STATUSES_DRAFT)
self.client.post(path, {"name": "sp4", "description_short": "sp modified short desc", "description_long": "sp long desc",
"public_visibility": "All", "quota_minimal": 15, "quota_nominal": 20,
"over_quota_duration": 5, "token": 3,
"users":"test@example.com,test2@example.com"
})