test_generic_uploader.py
5.09 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
# -*- coding: utf8 -*-
# This file is part of PyBossa.
#
# Copyright (C) 2015 SciFabric LTD.
#
# PyBossa is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyBossa is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with PyBossa. If not, see <http://www.gnu.org/licenses/>.
"""This module tests the Uploader class."""
from default import Test, with_context
from pybossa.uploader import Uploader
from werkzeug.datastructures import FileStorage
from mock import patch
from PIL import Image
import tempfile
import os
from nose.tools import assert_raises
class TestUploader(Test):
"""Test PyBossa Uploader module."""
def setUp(self):
"""SetUp method."""
super(TestUploader, self).setUp()
with self.flask_app.app_context():
self.create()
@with_context
def test_uploader_init(self):
"""Test UPLOADER init method works."""
u = Uploader()
new_extensions = ['pdf', 'doe']
new_uploader = Uploader()
with patch.dict(self.flask_app.config,
{'ALLOWED_EXTENSIONS': new_extensions}):
new_uploader.init_app(self.flask_app)
expected_extensions = set.union(u.allowed_extensions, new_extensions)
err_msg = "The new uploader should support two extra extensions"
assert expected_extensions == new_uploader.allowed_extensions, err_msg
@with_context
def test_allowed_file(self):
"""Test UPLOADER allowed_file method works."""
u = Uploader()
for ext in u.allowed_extensions:
# Change extension to uppercase to check that it works too
filename = 'test.%s' % ext.upper()
err_msg = ("This file: %s should be allowed, but it failed"
% filename)
assert u.allowed_file(filename) is True, err_msg
err_msg = "Non allowed extensions should return false"
assert u.allowed_file('wrong.pdf') is False, err_msg
@with_context
def test_get_filename_extension(self):
"""Test UPLOADER get_filename_extension works."""
u = Uploader()
filename = "image.png"
err_msg = "The extension should be PNG"
assert u.get_filename_extension(filename) == 'png', err_msg
filename = "image.jpg"
err_msg = "The extension should be JPEG"
assert u.get_filename_extension(filename) == 'jpeg', err_msg
filename = "imagenoextension"
err_msg = "The extension should be None"
assert u.get_filename_extension(filename) == None, err_msg
@with_context
def test_crop(self):
"""Test UPLOADER crop works."""
u = Uploader()
size = (100, 100)
im = Image.new('RGB', size)
folder = tempfile.mkdtemp()
u.upload_folder = folder
im.save(os.path.join(folder, 'image.png'))
coordinates = (0, 0, 50, 50)
file = FileStorage(filename=os.path.join(folder, 'image.png'))
with patch('pybossa.uploader.Image', return_value=True):
err_msg = "It should crop the image"
assert u.crop(file, coordinates) is True, err_msg
with patch('pybossa.uploader.Image.open', side_effect=IOError):
err_msg = "It should return false"
assert u.crop(file, coordinates) is False, err_msg
@with_context
def test_external_url_handler(self):
"""Test UPLOADER external_url_handler works."""
u = Uploader()
with patch.object(u, '_lookup_url', return_value='url'):
assert u.external_url_handler(BaseException, 'endpoint', 'values') == 'url'
@with_context
def test_external_url_handler_fails(self):
"""Test UPLOADER external_url_handler fails works."""
u = Uploader()
with patch.object(u, '_lookup_url', return_value=None):
with patch('pybossa.uploader.sys') as mysys:
mysys.exc_info.return_value=(BaseException, BaseException, None)
assert_raises(BaseException,
u.external_url_handler,
BaseException,
'endpoint',
'values')
@with_context
def test_external_url_handler_fails_2(self):
"""Test UPLOADER external_url_handler fails works."""
u = Uploader()
with patch.object(u, '_lookup_url', return_value=None):
with patch('pybossa.uploader.sys') as mysys:
mysys.exc_info.return_value=(BaseException, BaseException, None)
assert_raises(IOError,
u.external_url_handler,
IOError,
'endpoint',
'values')