|
1
|
|
|
#!/usr/bin/env python3 |
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
|
|
""" |
|
4
|
|
|
Created on Tue Jul 9 16:38:40 2019 |
|
5
|
|
|
|
|
6
|
|
|
@author: Paolo Cozzi <[email protected]> |
|
7
|
|
|
""" |
|
8
|
|
|
|
|
9
|
|
|
from unittest.mock import patch |
|
10
|
|
|
from billiard.einfo import ExceptionInfo |
|
11
|
|
|
|
|
12
|
|
|
from django.core import mail |
|
13
|
|
|
|
|
14
|
|
|
from common.constants import ERROR |
|
15
|
|
|
from image_app.models import Submission |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
class ImportGenericTaskMixinTestCase(): |
|
19
|
|
|
# the method use for importing data as a string |
|
20
|
|
|
upload_method = None |
|
21
|
|
|
action = None |
|
22
|
|
|
|
|
23
|
|
|
def setUp(self): |
|
24
|
|
|
# calling my base class setup |
|
25
|
|
|
super().setUp() |
|
26
|
|
|
|
|
27
|
|
|
# setting channels methods |
|
28
|
|
|
self.my_upload_patcher = patch(self.upload_method) |
|
29
|
|
|
self.my_upload = self.my_upload_patcher.start() |
|
30
|
|
|
|
|
31
|
|
|
def tearDown(self): |
|
32
|
|
|
# stopping mock objects |
|
33
|
|
|
self.my_upload_patcher.stop() |
|
34
|
|
|
|
|
35
|
|
|
# calling base methods |
|
36
|
|
|
super().tearDown() |
|
37
|
|
|
|
|
38
|
|
View Code Duplication |
def test_on_failure(self): |
|
|
|
|
|
|
39
|
|
|
"""Testing on failure methods""" |
|
40
|
|
|
|
|
41
|
|
|
exc = Exception("Test") |
|
42
|
|
|
task_id = "test_task_id" |
|
43
|
|
|
args = [self.submission_id] |
|
44
|
|
|
kwargs = {} |
|
45
|
|
|
einfo = ExceptionInfo |
|
46
|
|
|
|
|
47
|
|
|
# call on_failure method |
|
48
|
|
|
self.my_task.on_failure(exc, task_id, args, kwargs, einfo) |
|
49
|
|
|
|
|
50
|
|
|
# check submission status and message |
|
51
|
|
|
submission = Submission.objects.get(pk=self.submission_id) |
|
52
|
|
|
|
|
53
|
|
|
# check submission.state changed |
|
54
|
|
|
self.assertEqual(submission.status, ERROR) |
|
55
|
|
|
self.assertEqual( |
|
56
|
|
|
submission.message, |
|
57
|
|
|
"Error in %s: Test" % (self.action)) |
|
58
|
|
|
|
|
59
|
|
|
# test email sent. One mail for admin, une for users |
|
60
|
|
|
self.assertEqual(len(mail.outbox), 2) |
|
61
|
|
|
|
|
62
|
|
|
# read email |
|
63
|
|
|
email = mail.outbox[-1] |
|
64
|
|
|
|
|
65
|
|
|
self.assertEqual( |
|
66
|
|
|
"Error in %s for submission %s" % ( |
|
67
|
|
|
self.action, self.submission_id), |
|
68
|
|
|
email.subject) |
|
69
|
|
|
|
|
70
|
|
|
message = 'Error' |
|
71
|
|
|
notification_message = 'Error in %s: Test' % ( |
|
72
|
|
|
self.action) |
|
73
|
|
|
|
|
74
|
|
|
self.check_message(message, notification_message) |
|
75
|
|
|
|
|
76
|
|
|
def test_import_from_file(self): |
|
77
|
|
|
"""Testing file import""" |
|
78
|
|
|
|
|
79
|
|
|
self.my_upload.return_value = True |
|
80
|
|
|
|
|
81
|
|
|
# NOTE that I'm calling the function directly, without delay |
|
82
|
|
|
# (AsyncResult). I've patched the time consuming task |
|
83
|
|
|
res = self.my_task.run(submission_id=1) |
|
84
|
|
|
|
|
85
|
|
|
# assert a success with data uploading |
|
86
|
|
|
self.assertEqual(res, "success") |
|
87
|
|
|
|
|
88
|
|
|
# assert that method were called |
|
89
|
|
|
self.assertTrue(self.my_upload.called) |
|
90
|
|
|
|
|
91
|
|
|
def test_import_from_file_errors(self): |
|
92
|
|
|
"""Testing file import with errors""" |
|
93
|
|
|
|
|
94
|
|
|
self.my_upload.return_value = False |
|
95
|
|
|
|
|
96
|
|
|
# NOTE that I'm calling the function directly, without delay |
|
97
|
|
|
# (AsyncResult). I've patched the time consuming task |
|
98
|
|
|
res = self.my_task.run(submission_id=1) |
|
99
|
|
|
|
|
100
|
|
|
# assert a success with data uploading |
|
101
|
|
|
self.assertEqual(res, "Error in %s" % ( |
|
102
|
|
|
self.action)) |
|
103
|
|
|
|
|
104
|
|
|
# assert that method were called |
|
105
|
|
|
self.assertTrue(self.my_upload.called) |
|
106
|
|
|
|
|
107
|
|
|
def test_mail_to_owner(self): |
|
108
|
|
|
"""Testing a message to owner""" |
|
109
|
|
|
|
|
110
|
|
|
# test truncating message |
|
111
|
|
|
self.my_task.max_body_size = 9 |
|
112
|
|
|
self.my_task.mail_to_owner(self.submission, "subject", "1234567890") |
|
113
|
|
|
|
|
114
|
|
|
# read email |
|
115
|
|
|
email = mail.outbox[0] |
|
116
|
|
|
self.assertEqual(email.body, "123456789...[truncated]") |
|
117
|
|
|
|