-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
263 lines (215 loc) · 9.39 KB
/
tests.py
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
262
263
import jwt
import json
from io import BytesIO
from aiohttp import web, FormData
from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop
from unittest.mock import patch, MagicMock, Mock
from asyncio import coroutine
import settings
from files.routes import routes
from middleware import error_middleware
from storage import TestStorage
from utils import format_bytes
def mocked_method(*args, **kwargs):
pass
class Image:
info = {}
def __init__(self, info):
self.info = info
def save(self, *args, **kwargs):
pass
class ImageWithError(Image):
def save(self):
raise Exception()
class PngReader(object):
def __init__(self, *args, **kwargs):
pass
def read(self, **kwargs):
return (
'500',
'500',
MagicMock(),
{
'interlace': False
}
)
class PngWriter(PngReader):
def write(self, *args, **kwargs):
raise Exception()
def CoroMock():
coro = Mock(name="CoroutineResult")
corofunc = Mock(name="CoroutineFunction", side_effect=coroutine(coro))
corofunc.coro = coro
return corofunc
class BaseTestsViews(AioHTTPTestCase):
async def get_application(self):
app = web.Application(
middlewares=[error_middleware],
client_max_size=settings.MAX_FILE_SIZE
)
app['file_storage'] = app['redis_storage'] = TestStorage()
for route in routes:
app.router.add_route(**route)
return app
async def create_upload_request(self, data=None, headers=None, token=None):
if not headers:
if not token:
token = self.jwt_encode().decode()
headers = {'Authorization': 'Bearer ' + token}
resp = await self.client.request('POST', '/upload/', data=data, headers=headers)
return resp
def validate_return_object(self, data):
self.assertIn('id', data)
self.assertIn('file_size', data)
self.assertIn('file_name', data)
self.assertIn('orig_file_name', data)
self.assertIn('file_type', data)
async def upload_and_test(self, file_type):
data = FormData()
data.add_field('file',
BytesIO(b'my file contents'),
filename='hello_world.jpg',
content_type=file_type)
response = await self.create_upload_request(data)
response_data = await response.content.readline()
self.assertEqual(response.status, 200)
data = json.loads(response_data)
self.validate_return_object(data)
self.assertEqual(data['file_type'], file_type)
def jwt_encode(self, headers=None):
return jwt.encode({'test': 'data'}, settings.SECRET_KEY, headers=headers)
@patch('files.handlers.png')
@patch('files.handlers.magic.from_buffer')
@patch('files.handlers.PngHandler.get_extra_data')
class PngTestViews(BaseTestsViews):
@unittest_run_loop
async def test_upload_png_when_not_interlace(self, get_extra_data_mock, magic_mock, png_mock):
get_extra_data_mock.return_value = {}
magic_mock.return_value = 'image/png'
file_type = 'image/png'
await self.upload_and_test(file_type)
@unittest_run_loop
async def test_upload_png_when_interlace(self, get_extra_data_mock, magic_mock, png_mock):
get_extra_data_mock.return_value = {}
magic_mock.return_value = 'image/png'
png_mock.Reader = PngReader
file_type = 'image/png'
await self.upload_and_test(file_type)
@unittest_run_loop
async def test_upload_png_when_make_interlace_raises_error(self, get_extra_data_mock, magic_mock, png_mock):
get_extra_data_mock.return_value = {}
magic_mock.return_value = 'image/png'
png_mock.Reader = PngReader
png_mock.Writer = PngWriter
file_type = 'image/png'
await self.upload_and_test(file_type)
@patch('files.handlers.Image')
@patch('files.handlers.magic.from_buffer')
@patch('files.handlers.JpegHandler.get_extra_data')
class JpgTestViews(BaseTestsViews):
@unittest_run_loop
async def test_upload_jpg(self, get_extra_data_mock, magic_mock, image_mock):
get_extra_data_mock.return_value = {}
image_mock.open.return_value = Image({'is_progressive': 1})
magic_mock.return_value = 'image/jpeg'
file_type = 'image/jpeg'
await self.upload_and_test(file_type)
@unittest_run_loop
async def test_upload_non_progressive_jpg(self, get_extra_data_mock, magic_mock, image_mock):
get_extra_data_mock.return_value = {}
magic_mock.return_value = 'image/jpeg'
image_mock.open.return_value = Image({'progressive': True})
file_type = 'image/jpeg'
await self.upload_and_test(file_type)
@unittest_run_loop
async def test_upload_non_progressive_jpg_when_save_raises_error(self, get_extra_data_mock, magic_mock, image_mock):
get_extra_data_mock.return_value = {}
magic_mock.return_value = 'image/jpeg'
image_mock.open = MagicMock(return_value=ImageWithError({}))
file_type = 'image/jpeg'
await self.upload_and_test(file_type)
@patch('files.handlers.magic.from_buffer')
class PdfTestViews(BaseTestsViews):
@unittest_run_loop
async def test_upload_pdf(self, magic_mock):
magic_mock.return_value = 'application/pdf'
file_type = 'application/pdf'
await self.upload_and_test(file_type)
class AppTestViews(BaseTestsViews):
@unittest_run_loop
async def test_index(self):
response = await self.client.request('GET', '/')
response_data = await response.content.readline()
self.assertEqual(response.status, 200)
self.assertEqual(response_data.decode('ASCII'), 'File Server')
@unittest_run_loop
@patch('files.handlers.magic.from_buffer')
async def test_upload_unnknown_type_should_return_400(self, magic_mock):
magic_mock.return_value = 'unknown/format'
data = FormData()
data.add_field('file',
BytesIO(b'my file contents'),
filename='hello_world.jpg',
content_type='unknown/format')
response = await self.create_upload_request(data)
self.assertEqual(response.status, 400)
@unittest_run_loop
@patch('files.handlers.magic.from_buffer')
async def test_upload_oversize_file(self, magic_mock):
magic_mock.return_value = 'image/jpeg'
data = FormData()
data.add_field('file',
BytesIO(b'*'*(settings.MAX_FILE_SIZE*2)),
filename='hello_world.jpg',
content_type='image/jpeg')
response = await self.create_upload_request(data)
self.assertEqual(response.status, 400)
response_data = await response.content.readline()
self.assertEqual(json.loads(response_data)['fields']['file'],
[f'Размер файла не должен превышать {format_bytes(settings.MAX_FILE_SIZE)}'])
@unittest_run_loop
async def test_bad_request(self):
response = await self.create_upload_request()
self.assertEqual(response.status, 400)
response_data = await response.content.readline()
self.assertEqual(json.loads(response_data)['fields']['file'], ['Файл не был получен'])
@unittest_run_loop
async def test_authentication(self):
response = await self.create_upload_request(headers={'Authorization': 'Bearer '})
response_data = await response.content.readline()
self.assertEqual(response.status, 401)
self.assertEqual(response_data.decode('ASCII'),
'"Invalid Authorization header. No credentials provided."')
response = await self.create_upload_request(
headers={'Authorization': 'Bearer extra ' + self.jwt_encode().decode()})
response_data = await response.content.readline()
self.assertEqual(response.status, 401)
self.assertEqual(response_data.decode('ASCII'),
'"Invalid Authorization header. Credentials string should not contain spaces."')
response = await self.create_upload_request(
headers={'Authorization': 'OtherPrefix ' + self.jwt_encode().decode()})
self.assertEqual(response.status, 401)
response = await self.create_upload_request(headers={'Authorization': 'Bearer wrong_signature'})
response_data = await response.content.readline()
self.assertEqual(response.status, 401)
self.assertEqual(response_data.decode('ASCII'),
'"Error decoding signature."')
expired_token = jwt.encode({
'test': 'data',
'exp': 0
}, settings.SECRET_KEY)
response = await self.create_upload_request(
headers={'Authorization': 'Bearer ' + expired_token.decode()})
response_data = await response.content.readline()
self.assertEqual(response.status, 401)
self.assertEqual(response_data.decode('ASCII'),
'"Signature has expired."')
@unittest_run_loop
async def test_wrong_auth_backend(self):
backend = settings.AUTHENTICATION_BACKEND
try:
settings.AUTHENTICATION_BACKEND = None
response = await self.create_upload_request()
self.assertEqual(response.status, 401)
finally:
settings.AUTHENTICATION_BACKEND = backend