1
|
|
|
#!/usr/bin/env python3 |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
""" |
4
|
|
|
Created on Thu Nov 8 16:35:48 2018 |
5
|
|
|
|
6
|
|
|
@author: Paolo Cozzi <[email protected]> |
7
|
|
|
""" |
8
|
|
|
|
9
|
|
|
import os |
10
|
|
|
|
11
|
|
|
from unittest.mock import patch |
12
|
|
|
|
13
|
|
|
from django.test import TestCase, Client |
14
|
|
|
from django.urls import resolve, reverse |
15
|
|
|
|
16
|
|
|
from common.tests import ( |
17
|
|
|
FormMixinTestCase, OwnerMixinTestCase, InvalidFormMixinTestCase, |
18
|
|
|
MessageMixinTestCase, StatusMixinTestCase) |
19
|
|
|
from common.constants import ( |
20
|
|
|
CRB_ANIM_TYPE, CRYOWEB_TYPE, TEMPLATE_TYPE, ERROR, WAITING) |
21
|
|
|
from image_app.models import Submission |
22
|
|
|
from image_app.tests import DataSourceMixinTestCase |
23
|
|
|
|
24
|
|
|
from .common import SubmissionFormMixin |
25
|
|
|
from ..views import ReloadSubmissionView |
26
|
|
|
from ..forms import ReloadForm |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
class TestBase(SubmissionFormMixin, DataSourceMixinTestCase, TestCase): |
30
|
|
|
fixtures = [ |
31
|
|
|
"image_app/user", |
32
|
|
|
"image_app/dictcountry", |
33
|
|
|
"image_app/dictrole", |
34
|
|
|
"image_app/organization", |
35
|
|
|
"image_app/submission" |
36
|
|
|
] |
37
|
|
|
|
38
|
|
|
def setUp(self): |
39
|
|
|
# call base method |
40
|
|
|
super().setUp() |
41
|
|
|
|
42
|
|
|
# login a test user (defined in fixture) |
43
|
|
|
self.client = Client() |
44
|
|
|
self.client.login(username='test', password='test') |
45
|
|
|
|
46
|
|
|
self.url = reverse('submissions:reload', kwargs={'pk': 1}) |
47
|
|
|
|
48
|
|
|
def tearDown(self): |
49
|
|
|
if hasattr(self, "submission"): |
50
|
|
|
# read written file |
51
|
|
|
self.submission.refresh_from_db() |
52
|
|
|
|
53
|
|
|
# delete uploaded file if exists |
54
|
|
|
fullpath = self.submission.uploaded_file.path |
55
|
|
|
|
56
|
|
|
if os.path.exists(fullpath): |
57
|
|
|
os.remove(fullpath) |
58
|
|
|
|
59
|
|
|
# call super method |
60
|
|
|
super().tearDown() |
61
|
|
|
|
62
|
|
|
def get_data(self, ds_file=CRYOWEB_TYPE): |
63
|
|
|
"""Get data dictionary""" |
64
|
|
|
|
65
|
|
|
ds_type, ds_path = super().get_data(ds_file) |
66
|
|
|
|
67
|
|
|
# define test data |
68
|
|
|
data = { |
69
|
|
|
'uploaded_file': open(ds_path, "rb"), |
70
|
|
|
'datasource_type': ds_type, |
71
|
|
|
'datasource_version': "reload", |
72
|
|
|
'agree_reload': True |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return data |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
class ReloadSubmissionViewTest( |
79
|
|
|
FormMixinTestCase, OwnerMixinTestCase, TestBase): |
80
|
|
|
|
81
|
|
|
form_class = ReloadForm |
82
|
|
|
|
83
|
|
|
def setUp(self): |
84
|
|
|
# call base method |
85
|
|
|
super().setUp() |
86
|
|
|
|
87
|
|
|
self.response = self.client.get(self.url) |
88
|
|
|
|
89
|
|
|
def test_url_resolves_view(self): |
90
|
|
|
view = resolve('/submissions/1/reload/') |
91
|
|
|
self.assertIsInstance(view.func.view_class(), ReloadSubmissionView) |
92
|
|
|
|
93
|
|
|
def test_form_inputs(self): |
94
|
|
|
|
95
|
|
|
# total input is n of form fields + (CSRF) + 1 file + 1 checkbox |
96
|
|
|
# +1 select + text |
97
|
|
|
self.assertContains(self.response, '<input', 4) |
98
|
|
|
self.assertContains(self.response, '<select', 1) |
99
|
|
|
self.assertContains(self.response, 'type="file"', 1) |
100
|
|
|
self.assertContains(self.response, 'type="checkbox"', 1) |
101
|
|
|
|
102
|
|
|
def test_reload_not_found_status_code(self): |
103
|
|
|
url = reverse('submissions:reload', kwargs={'pk': 99}) |
104
|
|
|
response = self.client.get(url) |
105
|
|
|
self.assertEqual(response.status_code, 404) |
106
|
|
|
|
107
|
|
|
def test_contains_navigation_links(self): |
108
|
|
|
"""Contain links to DetailSubmissionView""" |
109
|
|
|
|
110
|
|
|
link = reverse('submissions:detail', kwargs={'pk': 1}) |
111
|
|
|
self.assertContains(self.response, 'href="{0}"'.format(link)) |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
class SuccessfulReloadMixin(StatusMixinTestCase): |
115
|
|
|
def test_redirect(self): |
116
|
|
|
url = reverse('submissions:detail', kwargs={'pk': 1}) |
117
|
|
|
self.assertRedirects(self.response, url) |
118
|
|
|
|
119
|
|
|
def test_task_called(self): |
120
|
|
|
self.assertTrue(self.my_task.called) |
121
|
|
|
|
122
|
|
|
def test_submission_status(self): |
123
|
|
|
self.submission.refresh_from_db() |
124
|
|
|
self.assertEqual(self.submission.status, WAITING) |
125
|
|
|
self.assertEqual( |
126
|
|
|
self.submission.message, |
127
|
|
|
"waiting for data loading") |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
class SuccessfulReloadSubmissionViewTest( |
131
|
|
|
SuccessfulReloadMixin, OwnerMixinTestCase, TestBase): |
132
|
|
|
# patch to simulate data load |
133
|
|
|
@patch('cryoweb.tasks.import_from_cryoweb.delay') |
134
|
|
|
def setUp(self, my_task): |
135
|
|
|
# call base method |
136
|
|
|
super().setUp() |
137
|
|
|
|
138
|
|
|
# this post request create a new data_source file |
139
|
|
|
self.response = self.client.post( |
140
|
|
|
self.url, |
141
|
|
|
self.get_data(), |
142
|
|
|
follow=True) |
143
|
|
|
|
144
|
|
|
# setting task |
145
|
|
|
self.my_task = my_task |
146
|
|
|
|
147
|
|
|
# get submission, with the new data_source file |
148
|
|
|
self.submission = Submission.objects.get(pk=1) |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
class InvalidReloadSubmissionViewTest( |
152
|
|
|
InvalidFormMixinTestCase, OwnerMixinTestCase, TestBase): |
153
|
|
|
|
154
|
|
|
# patch to simulate data load |
155
|
|
|
@patch('cryoweb.tasks.import_from_cryoweb.delay') |
156
|
|
|
def setUp(self, my_task): |
157
|
|
|
# call base method |
158
|
|
|
super().setUp() |
159
|
|
|
|
160
|
|
|
self.response = self.client.post(self.url, {}) |
161
|
|
|
self.my_task = my_task |
162
|
|
|
|
163
|
|
|
def test_task_called(self): |
164
|
|
|
self.assertFalse(self.my_task.called) |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
class ReloadValidationTest(TestBase): |
168
|
|
|
@patch('submissions.views.ImportCRBAnimTask.delay') |
169
|
|
|
def test_crb_anim_wrong_encoding(self, my_task): |
170
|
|
|
# submit a cryoweb like dictionary |
171
|
|
|
response = self.client.post( |
172
|
|
|
self.url, |
173
|
|
|
self.get_data(ds_file="latin_type")) |
174
|
|
|
|
175
|
|
|
# check errors |
176
|
|
|
form = response.context.get('form') |
177
|
|
|
self.assertGreater(len(form.errors), 0) |
178
|
|
|
|
179
|
|
|
# test task |
180
|
|
|
self.assertFalse(my_task.called) |
181
|
|
|
|
182
|
|
|
@patch('submissions.views.ImportCRBAnimTask.delay') |
183
|
|
|
def test_crb_anim_wrong_columns(self, my_task): |
184
|
|
|
# submit a cryoweb like dictionary |
185
|
|
|
response = self.client.post( |
186
|
|
|
self.url, |
187
|
|
|
self.get_data(ds_file="not_valid_crbanim")) |
188
|
|
|
|
189
|
|
|
# check errors |
190
|
|
|
form = response.context.get('form') |
191
|
|
|
self.assertGreater(len(form.errors), 0) |
192
|
|
|
|
193
|
|
|
# test task |
194
|
|
|
self.assertFalse(my_task.called) |
195
|
|
|
|
196
|
|
|
|
197
|
|
|
class CRBAnimReloadSubmissionViewTest( |
198
|
|
|
SuccessfulReloadMixin, TestBase): |
199
|
|
|
|
200
|
|
|
@patch('submissions.views.ImportCRBAnimTask.delay') |
201
|
|
|
def setUp(self, my_task): |
202
|
|
|
# call base method |
203
|
|
|
super().setUp() |
204
|
|
|
|
205
|
|
|
# get a submission object |
206
|
|
|
self.submission = Submission.objects.get(pk=1) |
207
|
|
|
|
208
|
|
|
# change template type |
209
|
|
|
self.submission.datasource_type = CRB_ANIM_TYPE |
210
|
|
|
self.submission.save() |
211
|
|
|
|
212
|
|
|
# this post request create a new data_source file |
213
|
|
|
self.response = self.client.post( |
214
|
|
|
self.url, |
215
|
|
|
self.get_data(ds_file=CRB_ANIM_TYPE), |
216
|
|
|
follow=True) |
217
|
|
|
|
218
|
|
|
# track task |
219
|
|
|
self.my_task = my_task |
220
|
|
|
|
221
|
|
|
|
222
|
|
|
class TemplateReloadSubmissionViewTest( |
223
|
|
|
SuccessfulReloadMixin, TestBase): |
224
|
|
|
|
225
|
|
|
@patch('submissions.views.ImportTemplateTask.delay') |
226
|
|
|
def setUp(self, my_task): |
227
|
|
|
# call base method |
228
|
|
|
super().setUp() |
229
|
|
|
|
230
|
|
|
# get a submission object |
231
|
|
|
self.submission = Submission.objects.get(pk=1) |
232
|
|
|
|
233
|
|
|
# change template type |
234
|
|
|
self.submission.datasource_type = TEMPLATE_TYPE |
235
|
|
|
self.submission.save() |
236
|
|
|
|
237
|
|
|
# this post request create a new data_source file |
238
|
|
|
self.response = self.client.post( |
239
|
|
|
self.url, |
240
|
|
|
self.get_data(ds_file=TEMPLATE_TYPE), |
241
|
|
|
follow=True) |
242
|
|
|
|
243
|
|
|
# track task |
244
|
|
|
self.my_task = my_task |
245
|
|
|
|