|
1 |
| -import base64 |
2 | 1 | from unittest.mock import patch
|
3 | 2 |
|
4 | 3 | import pytest
|
5 | 4 |
|
6 | 5 | from tests.helpers import DummyHttpResponse
|
7 | 6 | from filestack import config
|
8 |
| -from filestack.models import Security |
9 | 7 | from filestack.uploads.external_url import upload_external_url
|
10 | 8 |
|
11 | 9 | url = 'http://image.url'
|
12 |
| -encoded_url = 'b64://{}'.format(base64.urlsafe_b64encode(url.encode()).decode()) |
13 | 10 | apikey = 'TESTAPIKEY'
|
14 | 11 |
|
15 | 12 |
|
16 |
| -@patch('filestack.uploads.external_url.requests.get') |
17 |
| -def test_upload(post_mock): |
18 |
| - post_mock.return_value = DummyHttpResponse(json_dict={'handle': 'newHandle'}) |
19 |
| - |
20 |
| - handle = upload_external_url(url, apikey) |
21 |
| - assert handle == 'newHandle' |
22 |
| - post_mock.assert_called_once_with('{}/{}/store/{}'.format(config.CDN_URL, apikey, encoded_url)) |
23 |
| - |
24 |
| - |
25 |
| -@pytest.mark.parametrize('store_params, expected_store_task', [ |
26 |
| - [{'location': 'S3'}, 'store=location:s3'], |
27 |
| - [{'path': 'store/path/image.jpg'}, 'store=path:"store/path/image.jpg"'], |
28 |
| - [{'base64decode': True, 'access': 'public'}, 'store=access:public,base64decode:true'], |
29 |
| - [ |
30 |
| - {'workflows': ['uuid-1', 'uuid-2'], 'container': 'bucket-name'}, |
31 |
| - 'store=container:bucket-name,workflows:["uuid-1","uuid-2"]' |
32 |
| - ], |
| 13 | +@pytest.mark.parametrize('store_params, security, expected_store_tasks', [ |
| 14 | + ( |
| 15 | + {'location': 's3'}, |
| 16 | + None, |
| 17 | + [ |
| 18 | + { |
| 19 | + 'name': 'store', 'params': {'location': 's3'} |
| 20 | + } |
| 21 | + ] |
| 22 | + ), |
| 23 | + ( |
| 24 | + {'path': 'new-path/', 'mimetype': 'application/json'}, |
| 25 | + type('SecurityMock', (), {'policy_b64': 'abc', 'signature': '123'}), |
| 26 | + [ |
| 27 | + { |
| 28 | + 'name': 'store', 'params': {'path': 'new-path/'} |
| 29 | + }, |
| 30 | + { |
| 31 | + 'name': 'security', 'params': {'policy': 'abc', 'signature': '123'} |
| 32 | + } |
| 33 | + ] |
| 34 | + ) |
33 | 35 | ])
|
34 |
| -@patch('filestack.uploads.external_url.requests.get') |
35 |
| -def test_upload_with_store_params(post_mock, store_params, expected_store_task): |
| 36 | +@patch('filestack.uploads.external_url.requests.post') |
| 37 | +def test_upload_with_store_params(post_mock, store_params, security, expected_store_tasks): |
| 38 | + expected_payload = { |
| 39 | + 'apikey': 'TESTAPIKEY', |
| 40 | + 'sources': ['http://image.url'], |
| 41 | + 'tasks': expected_store_tasks |
| 42 | + } |
36 | 43 | post_mock.return_value = DummyHttpResponse(json_dict={'handle': 'newHandle'})
|
37 | 44 |
|
38 |
| - handle = upload_external_url(url, apikey, store_params=store_params) |
| 45 | + handle = upload_external_url(url, apikey, store_params=store_params, security=security) |
39 | 46 | assert handle == 'newHandle'
|
40 | 47 | post_args, _ = post_mock.call_args
|
41 |
| - req_url = post_args[0] |
42 |
| - assert expected_store_task in req_url |
43 |
| - |
44 |
| - |
45 |
| -@patch('filestack.uploads.external_url.requests.get') |
46 |
| -def test_upload_with_security(post_mock): |
47 |
| - post_mock.return_value = DummyHttpResponse(json_dict={'handle': 'newHandle'}) |
48 |
| - security = Security({'expiry': 123123123123, 'call': ['write']}, 'SECRET') |
49 |
| - handle = upload_external_url(url, apikey, security=security) |
50 |
| - assert handle == 'newHandle' |
51 |
| - expected_url = '{}/{}/store/{}/{}'.format( |
52 |
| - config.CDN_URL, apikey, security.as_url_string(), encoded_url |
53 |
| - ) |
54 |
| - post_mock.assert_called_once_with(expected_url) |
55 |
| - |
56 |
| - |
57 |
| -@patch('filestack.uploads.external_url.requests.get') |
58 |
| -def test_upload_exception(post_mock): |
59 |
| - error_message = 'Oops!' |
60 |
| - post_mock.side_effect = Exception(error_message) |
61 |
| - |
62 |
| - with pytest.raises(Exception, match=error_message): |
63 |
| - upload_external_url(url, apikey) |
| 48 | + post_mock.assert_called_once_with('{}/process'.format(config.CDN_URL), json=expected_payload) |
0 commit comments