|
1
|
|
|
#!/usr/bin/env python3 |
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
|
|
""" |
|
4
|
|
|
Created on Fri Oct 5 11:39:34 2018 |
|
5
|
|
|
|
|
6
|
|
|
@author: Paolo Cozzi <[email protected]> |
|
7
|
|
|
""" |
|
8
|
|
|
|
|
9
|
|
|
from unittest.mock import patch |
|
10
|
|
|
|
|
11
|
|
|
from django.test import Client, TestCase |
|
12
|
|
|
from django.urls import resolve, reverse |
|
13
|
|
|
|
|
14
|
|
|
from common.constants import ( |
|
15
|
|
|
WAITING, ERROR, SUBMITTED, LOADED, COMPLETED, READY) |
|
16
|
|
|
from uid.models import Submission |
|
17
|
|
|
|
|
18
|
|
|
from ..forms import ValidateForm |
|
19
|
|
|
from ..views import ValidateView |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
class TestMixin(object): |
|
23
|
|
|
"""Base class for validation tests""" |
|
24
|
|
|
|
|
25
|
|
|
fixtures = [ |
|
26
|
|
|
"uid/user", |
|
27
|
|
|
"uid/dictcountry", |
|
28
|
|
|
"uid/dictrole", |
|
29
|
|
|
"uid/organization", |
|
30
|
|
|
"uid/submission" |
|
31
|
|
|
] |
|
32
|
|
|
|
|
33
|
|
|
def setUp(self): |
|
34
|
|
|
self.client = Client() |
|
35
|
|
|
self.client.login(username='test', password='test') |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
class ValidateViewTest(TestMixin, TestCase): |
|
39
|
|
|
def setUp(self): |
|
40
|
|
|
# call base methods |
|
41
|
|
|
super(ValidateViewTest, self).setUp() |
|
42
|
|
|
|
|
43
|
|
|
# get the url for dashboard |
|
44
|
|
|
self.url = reverse('validation:validate') |
|
45
|
|
|
self.response = self.client.get(self.url) |
|
46
|
|
|
|
|
47
|
|
|
def test_redirection(self): |
|
48
|
|
|
'''Non Authenticated user are directed to login page''' |
|
49
|
|
|
|
|
50
|
|
|
login_url = reverse("login") |
|
51
|
|
|
client = Client() |
|
52
|
|
|
response = client.get(self.url) |
|
53
|
|
|
|
|
54
|
|
|
self.assertRedirects( |
|
55
|
|
|
response, '{login_url}?next={url}'.format( |
|
56
|
|
|
login_url=login_url, url=self.url) |
|
57
|
|
|
) |
|
58
|
|
|
|
|
59
|
|
|
def test_status_code(self): |
|
60
|
|
|
self.assertEqual(self.response.status_code, 200) |
|
61
|
|
|
|
|
62
|
|
|
def test_url_resolves_view(self): |
|
63
|
|
|
view = resolve('/validation/validate') |
|
64
|
|
|
self.assertIsInstance(view.func.view_class(), ValidateView) |
|
65
|
|
|
|
|
66
|
|
|
def test_csrf(self): |
|
67
|
|
|
self.assertContains(self.response, 'csrfmiddlewaretoken') |
|
68
|
|
|
|
|
69
|
|
|
def test_contains_form(self): |
|
70
|
|
|
form = self.response.context.get('form') |
|
71
|
|
|
|
|
72
|
|
|
self.assertIsInstance(form, ValidateForm) |
|
73
|
|
|
|
|
74
|
|
|
def test_form_inputs(self): |
|
75
|
|
|
''' |
|
76
|
|
|
The view two inputs: csrf, submission_id" |
|
77
|
|
|
''' |
|
78
|
|
|
|
|
79
|
|
|
# total input is n of form fields + (CSRF) |
|
80
|
|
|
self.assertContains(self.response, '<input', 2) |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
class SuccessfulValidateViewTest(TestMixin, TestCase): |
|
84
|
|
|
@patch('validation.views.ValidateTask.delay') |
|
85
|
|
|
def setUp(self, my_validation): |
|
86
|
|
|
# call base methods |
|
87
|
|
|
super(SuccessfulValidateViewTest, self).setUp() |
|
88
|
|
|
|
|
89
|
|
|
# get a submission object |
|
90
|
|
|
submission = Submission.objects.get(pk=1) |
|
91
|
|
|
|
|
92
|
|
|
# set a status which I can validate |
|
93
|
|
|
submission.status = LOADED |
|
94
|
|
|
submission.save() |
|
95
|
|
|
|
|
96
|
|
|
# track submission ID |
|
97
|
|
|
self.submission_id = submission.id |
|
98
|
|
|
|
|
99
|
|
|
# get the url for dashboard |
|
100
|
|
|
self.url = reverse('validation:validate') |
|
101
|
|
|
self.response = self.client.post( |
|
102
|
|
|
self.url, { |
|
103
|
|
|
'submission_id': self.submission_id |
|
104
|
|
|
} |
|
105
|
|
|
) |
|
106
|
|
|
|
|
107
|
|
|
# track my patched function |
|
108
|
|
|
self.my_validation = my_validation |
|
109
|
|
|
|
|
110
|
|
|
def test_redirect(self): |
|
111
|
|
|
"""test redirection after validation occurs""" |
|
112
|
|
|
|
|
113
|
|
|
url = reverse('submissions:detail', kwargs={'pk': self.submission_id}) |
|
114
|
|
|
self.assertRedirects(self.response, url) |
|
115
|
|
|
|
|
116
|
|
|
def test_validation_status(self): |
|
117
|
|
|
"""check validation started and submission.state""" |
|
118
|
|
|
|
|
119
|
|
|
# check validation started |
|
120
|
|
|
self.assertTrue(self.my_validation.called) |
|
121
|
|
|
|
|
122
|
|
|
# get submission object |
|
123
|
|
|
submission = Submission.objects.get(pk=self.submission_id) |
|
124
|
|
|
|
|
125
|
|
|
# check submission.state changed |
|
126
|
|
|
self.assertEqual(submission.status, WAITING) |
|
127
|
|
|
self.assertEqual( |
|
128
|
|
|
submission.message, |
|
129
|
|
|
"waiting for data validation") |
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
View Code Duplication |
class NoValidateViewTest(TestMixin, TestCase): |
|
|
|
|
|
|
133
|
|
|
@patch('validation.views.ValidateTask.delay') |
|
134
|
|
|
def setUp(self, my_validation): |
|
135
|
|
|
# call base methods |
|
136
|
|
|
super(NoValidateViewTest, self).setUp() |
|
137
|
|
|
|
|
138
|
|
|
# get a submission object |
|
139
|
|
|
submission = Submission.objects.get(pk=1) |
|
140
|
|
|
|
|
141
|
|
|
# track submission ID |
|
142
|
|
|
self.submission_id = submission.id |
|
143
|
|
|
|
|
144
|
|
|
# get the url for dashboard |
|
145
|
|
|
self.url = reverse('validation:validate') |
|
146
|
|
|
|
|
147
|
|
|
# track my patched function |
|
148
|
|
|
self.my_validation = my_validation |
|
149
|
|
|
|
|
150
|
|
|
def __common_stuff(self, status): |
|
151
|
|
|
"""Common function for statuses""" |
|
152
|
|
|
|
|
153
|
|
|
# get submission |
|
154
|
|
|
submission = Submission.objects.get(pk=self.submission_id) |
|
155
|
|
|
|
|
156
|
|
|
# update status and save |
|
157
|
|
|
submission.status = status |
|
158
|
|
|
submission.save() |
|
159
|
|
|
|
|
160
|
|
|
# call valiate views with cyrrent status WAITING |
|
161
|
|
|
response = self.client.post( |
|
162
|
|
|
self.url, { |
|
163
|
|
|
'submission_id': self.submission_id |
|
164
|
|
|
} |
|
165
|
|
|
) |
|
166
|
|
|
|
|
167
|
|
|
# assert redirect |
|
168
|
|
|
url = reverse('submissions:detail', kwargs={'pk': self.submission_id}) |
|
169
|
|
|
self.assertRedirects(response, url) |
|
170
|
|
|
|
|
171
|
|
|
# get number of call (equal to first call) |
|
172
|
|
|
self.assertEqual(self.my_validation.call_count, 0) |
|
173
|
|
|
|
|
174
|
|
|
def test_submission_waiting(self): |
|
175
|
|
|
"""check no validation with submission status WAITING""" |
|
176
|
|
|
|
|
177
|
|
|
# valutate status and no function called |
|
178
|
|
|
self.__common_stuff(WAITING) |
|
179
|
|
|
|
|
180
|
|
|
def test_submission_ready(self): |
|
181
|
|
|
"""check no validation with submission status READY""" |
|
182
|
|
|
|
|
183
|
|
|
# valutate status and no function called |
|
184
|
|
|
self.__common_stuff(READY) |
|
185
|
|
|
|
|
186
|
|
|
def test_submission_error(self): |
|
187
|
|
|
"""check no validation with submission status ERROR""" |
|
188
|
|
|
|
|
189
|
|
|
# valutate status and no function called |
|
190
|
|
|
self.__common_stuff(ERROR) |
|
191
|
|
|
|
|
192
|
|
|
def test_submission_submitted(self): |
|
193
|
|
|
"""check no validation with submission status SUBMITTED""" |
|
194
|
|
|
|
|
195
|
|
|
# valutate status and no function called |
|
196
|
|
|
self.__common_stuff(SUBMITTED) |
|
197
|
|
|
|
|
198
|
|
|
def test_submission_completed(self): |
|
199
|
|
|
"""check no validation with submission status COMPLETED""" |
|
200
|
|
|
|
|
201
|
|
|
# valutate status and no function called |
|
202
|
|
|
self.__common_stuff(COMPLETED) |
|
203
|
|
|
|
|
204
|
|
|
|
|
205
|
|
|
class InvalidValidateViewTest(TestMixin, TestCase): |
|
206
|
|
|
@patch('validation.views.ValidateTask.delay') |
|
207
|
|
|
def setUp(self, my_validation): |
|
208
|
|
|
# call base methods |
|
209
|
|
|
super(InvalidValidateViewTest, self).setUp() |
|
210
|
|
|
|
|
211
|
|
|
# get the url for dashboard |
|
212
|
|
|
self.url = reverse('validation:validate') |
|
213
|
|
|
self.response = self.client.post(self.url, {}) |
|
214
|
|
|
|
|
215
|
|
|
# track my patched function |
|
216
|
|
|
self.my_validation = my_validation |
|
217
|
|
|
|
|
218
|
|
|
def test_status_code(self): |
|
219
|
|
|
"""Invalid post data returns the form""" |
|
220
|
|
|
|
|
221
|
|
|
self.assertEqual(self.response.status_code, 200) |
|
222
|
|
|
|
|
223
|
|
|
def test_novalidation(self): |
|
224
|
|
|
"""check no validation process started""" |
|
225
|
|
|
|
|
226
|
|
|
self.assertFalse(self.my_validation.called) |
|
227
|
|
|
|
|
228
|
|
|
|
|
229
|
|
View Code Duplication |
class ErrorValidateViewtest(TestMixin, TestCase): |
|
|
|
|
|
|
230
|
|
|
"""A class to test submission not belonging to the user or which doesn't |
|
231
|
|
|
exists""" |
|
232
|
|
|
|
|
233
|
|
|
@patch('validation.views.ValidateTask.delay') |
|
234
|
|
|
def setUp(self, my_validation): |
|
235
|
|
|
self.client = Client() |
|
236
|
|
|
self.client.login(username='test2', password='test2') |
|
237
|
|
|
|
|
238
|
|
|
# get a submission object |
|
239
|
|
|
submission = Submission.objects.get(pk=1) |
|
240
|
|
|
|
|
241
|
|
|
# set a status which I can validate |
|
242
|
|
|
submission.status = LOADED |
|
243
|
|
|
submission.save() |
|
244
|
|
|
|
|
245
|
|
|
# track submission ID |
|
246
|
|
|
self.submission_id = submission.id |
|
247
|
|
|
|
|
248
|
|
|
# get the url for dashboard |
|
249
|
|
|
self.url = reverse('validation:validate') |
|
250
|
|
|
|
|
251
|
|
|
# track my patched function |
|
252
|
|
|
self.my_validation = my_validation |
|
253
|
|
|
|
|
254
|
|
|
def test_ownership(self): |
|
255
|
|
|
"""Test a non-owner having a 404 response""" |
|
256
|
|
|
|
|
257
|
|
|
response = self.client.post( |
|
258
|
|
|
self.url, { |
|
259
|
|
|
'submission_id': self.submission_id |
|
260
|
|
|
} |
|
261
|
|
|
) |
|
262
|
|
|
|
|
263
|
|
|
self.assertEqual(response.status_code, 404) |
|
264
|
|
|
self.assertFalse(self.my_validation.called) |
|
265
|
|
|
|
|
266
|
|
|
def test_does_not_exists(self): |
|
267
|
|
|
"""Test a submission which doesn't exists""" |
|
268
|
|
|
|
|
269
|
|
|
response = self.client.post( |
|
270
|
|
|
self.url, { |
|
271
|
|
|
'submission_id': 99 |
|
272
|
|
|
} |
|
273
|
|
|
) |
|
274
|
|
|
|
|
275
|
|
|
self.assertEqual(response.status_code, 404) |
|
276
|
|
|
self.assertFalse(self.my_validation.called) |
|
277
|
|
|
|