Skip to content

Commit bfd98e9

Browse files
authored
Use responses to mock requests in upload tests (#64)
1 parent e9a69a2 commit bfd98e9

File tree

3 files changed

+67
-81
lines changed

3 files changed

+67
-81
lines changed

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ httmock==1.2.6
33
pytest==4.6.3
44
pytest-cov==2.5.0
55
requests==2.25.1
6-
responses==0.10.6
6+
responses==0.14.0
77
trafaret==2.0.2

tests/intelligent_ingestion_test.py

Lines changed: 58 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,78 @@
1-
from unittest.mock import patch, call, ANY
2-
from collections import defaultdict
1+
import json
32

43
import pytest
4+
import responses
55

66
from filestack.uploads.intelligent_ingestion import upload_part
7-
from tests.helpers import DummyHttpResponse
87

98

10-
@patch('filestack.uploads.intelligent_ingestion.requests.put')
11-
@patch('filestack.uploads.intelligent_ingestion.requests.post')
12-
def test_upload_part_success(post_mock, put_mock):
13-
post_mock.side_effect = [
14-
DummyHttpResponse(json_dict={'url': 'http://upload.url', 'headers': {'upload': 'headers'}}),
15-
DummyHttpResponse()
16-
]
17-
18-
put_mock.return_value = DummyHttpResponse()
9+
@responses.activate
10+
def test_upload_part_success():
11+
responses.add(
12+
responses.POST, 'https://fs-upload.com/multipart/upload',
13+
json={'url': 'http://s3.url', 'headers': {'filestack': 'headers'}}
14+
)
15+
responses.add(responses.PUT, 'http://s3.url')
16+
responses.add(responses.POST, 'https://fs-upload.com/multipart/commit')
1917

2018
part = {'seek_point': 0, 'num': 1}
21-
upload_part(
22-
'Aaaaapikey', 'file.txt', 'tests/data/doom.mp4', 1234, 's3', defaultdict(lambda: 'fs-upload.com'), part
23-
)
24-
assert post_mock.call_args_list == [
25-
call(
26-
'https://fs-upload.com/multipart/upload',
27-
json={
28-
'apikey': 'Aaaaapikey', 'uri': 'fs-upload.com', 'region': 'fs-upload.com',
29-
'upload_id': 'fs-upload.com', 'store': {'location': 's3'},
30-
'part': 1, 'size': 5415034, 'md5': 'IuNjhgPo2wbzGFo6f7WhUA==', 'offset': 0, 'fii': True
31-
},
32-
),
33-
call(
34-
'https://fs-upload.com/multipart/commit',
35-
json={
36-
'apikey': 'Aaaaapikey', 'uri': 'fs-upload.com', 'region': 'fs-upload.com',
37-
'upload_id': 'fs-upload.com', 'store': {'location': 's3'}, 'part': 1, 'size': 1234
38-
},
39-
)
40-
]
41-
put_mock.assert_called_once_with(
42-
'http://upload.url',
43-
data=ANY,
44-
headers={'upload': 'headers'}
45-
)
19+
start_response = {
20+
'uri': 'fs-upload.com', 'location_url': 'fs-upload.com', 'region': 'region', 'upload_id': 'abc'
21+
}
22+
upload_part('Aaaaapikey', 'file.txt', 'tests/data/doom.mp4', 1234, 's3', start_response, part)
23+
multipart_upload_payload = json.loads(responses.calls[0].request.body.decode())
24+
assert multipart_upload_payload == {
25+
'apikey': 'Aaaaapikey', 'uri': 'fs-upload.com', 'region': 'region',
26+
'upload_id': 'abc', 'store': {'location': 's3'},
27+
'part': 1, 'size': 5415034, 'md5': 'IuNjhgPo2wbzGFo6f7WhUA==', 'offset': 0, 'fii': True
28+
}
29+
with open('tests/data/doom.mp4', 'rb') as f:
30+
assert responses.calls[1].request.body == f.read()
31+
multipart_commit_payload = json.loads(responses.calls[2].request.body.decode())
32+
assert multipart_commit_payload == {
33+
'apikey': 'Aaaaapikey', 'uri': 'fs-upload.com', 'region': 'region',
34+
'upload_id': 'abc', 'store': {'location': 's3'}, 'part': 1, 'size': 1234
35+
}
4636

4737

48-
@patch('filestack.uploads.intelligent_ingestion.requests.put')
49-
@patch('filestack.uploads.intelligent_ingestion.requests.post')
50-
def test_upload_part_with_resize(post_mock, put_mock):
51-
# this mock will work fine for commit request too
52-
post_mock.return_value = DummyHttpResponse(
53-
ok=True, json_dict={'url': 'http://upload.url', 'headers': {'upload': 'headers'}}
38+
@responses.activate
39+
def test_upload_part_with_resize():
40+
responses.add(
41+
responses.POST, 'https://fs-upload.com/multipart/upload',
42+
json={'url': 'https://s3.url', 'headers': {'filestack': 'headers'}}
5443
)
44+
responses.add(responses.PUT, 'https://s3.url', status=400)
45+
responses.add(responses.PUT, 'https://s3.url') # chunks 1 & 2 of part 1
46+
responses.add(responses.POST, 'https://fs-upload.com/multipart/commit')
5547

56-
put_mock.side_effect = [
57-
DummyHttpResponse(ok=False), # fail first attempt, should split file part
58-
DummyHttpResponse(), # part 1, chunk 1
59-
DummyHttpResponse(), # part 1, chunk 2
60-
]
61-
48+
start_response = {
49+
'uri': 'fs-upload.com', 'location_url': 'fs-upload.com', 'region': 'region', 'upload_id': 'abc'
50+
}
6251
part = {'seek_point': 0, 'num': 1}
63-
upload_part(
64-
'Aaaaapikey', 'file.txt', 'tests/data/doom.mp4', 5415034, 's3', defaultdict(lambda: 'fs-upload.com'), part
65-
)
52+
upload_part('Aaaaapikey', 'file.txt', 'tests/data/doom.mp4', 5415034, 's3', start_response, part)
6653

67-
assert post_mock.call_count == 4 # 3x upload, 1 commit
68-
# 1st attempt
69-
req_args, req_kwargs = post_mock.call_args_list[0]
70-
assert req_kwargs['json']['size'] == 5415034
71-
# 2nd attempt
72-
req_args, req_kwargs = post_mock.call_args_list[1]
73-
assert req_kwargs['json']['size'] == 4194304
74-
# 3rd attempt
75-
req_args, req_kwargs = post_mock.call_args_list[2]
76-
assert req_kwargs['json']['size'] == 1220730
54+
responses.assert_call_count('https://fs-upload.com/multipart/upload', 3)
55+
responses.assert_call_count('https://s3.url', 3)
56+
assert len(responses.calls[1].request.body) == 5415034
57+
assert len(responses.calls[3].request.body) == 4194304
58+
assert len(responses.calls[5].request.body) == 1220730
7759

7860

79-
@patch('filestack.uploads.intelligent_ingestion.requests.put')
80-
@patch('filestack.uploads.intelligent_ingestion.requests.post')
81-
def test_min_chunk_size_exception(post_mock, put_mock):
82-
post_mock.return_value = DummyHttpResponse(
83-
ok=True, json_dict={'url': 'http://upload.url', 'headers': {'upload': 'headers'}}
61+
@responses.activate
62+
def test_min_chunk_size_exception():
63+
responses.reset()
64+
responses.add(
65+
responses.POST, 'https://fs-upload.com/multipart/upload',
66+
json={'url': 'https://upload.url', 'headers': {'filestack': 'headers'}}
8467
)
85-
put_mock.return_value = DummyHttpResponse(ok=False)
68+
responses.add(responses.PUT, 'https://upload.url', status=400)
8669

8770
part = {'seek_point': 0, 'num': 1}
71+
start_response = {
72+
'uri': 'fs-upload.com', 'location_url': 'fs-upload.com', 'region': 'region', 'upload_id': 'abc'
73+
}
8874
with pytest.raises(Exception, match='Minimal chunk size failed'):
89-
upload_part(
90-
'Aaaaapikey', 'file.txt', 'tests/data/doom.mp4', 5415034, 's3', defaultdict(lambda: 'fs-upload.com'), part
91-
)
75+
upload_part('Aaaaapikey', 'file.txt', 'tests/data/doom.mp4', 5415034, 's3', start_response, part)
76+
77+
chunk_sizes = [len(call.request.body) for call in responses.calls if call.request.method == 'PUT']
78+
assert chunk_sizes[-1] == 32768 # check size of last attempt

tests/uploads/test_upload_external_url.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
from unittest.mock import patch
1+
import json
22

33
import pytest
4+
import responses
45

5-
from tests.helpers import DummyHttpResponse
6-
from filestack import config
76
from filestack.uploads.external_url import upload_external_url
87

98
url = 'http://image.url'
109
apikey = 'TESTAPIKEY'
10+
PROCESS_URL = 'https://cdn.filestackcontent.com/process'
1111

1212

1313
@pytest.mark.parametrize('default_storage, store_params, security, expected_store_tasks', [
@@ -35,18 +35,17 @@
3535
]
3636
)
3737
])
38-
@patch('filestack.uploads.external_url.requests.post')
39-
def test_upload_with_store_params(post_mock, default_storage, store_params, security, expected_store_tasks):
38+
@responses.activate
39+
def test_upload_with_store_params(default_storage, store_params, security, expected_store_tasks):
4040
expected_payload = {
4141
'apikey': 'TESTAPIKEY',
4242
'sources': ['http://image.url'],
4343
'tasks': expected_store_tasks
4444
}
45-
post_mock.return_value = DummyHttpResponse(json_dict={'handle': 'newHandle'})
46-
45+
responses.add(responses.POST, PROCESS_URL, json={'handle': 'newHandle'})
4746
upload_response = upload_external_url(
4847
url, apikey, default_storage, store_params=store_params, security=security
4948
)
5049
assert upload_response['handle'] == 'newHandle'
51-
post_args, _ = post_mock.call_args
52-
post_mock.assert_called_once_with('{}/process'.format(config.CDN_URL), json=expected_payload)
50+
req_payload = json.loads(responses.calls[0].request.body.decode())
51+
assert req_payload == expected_payload

0 commit comments

Comments
 (0)