1
1
import io
2
+ import re
2
3
from unittest .mock import mock_open , patch , ANY
3
4
4
5
import pytest
6
+ import responses
7
+
5
8
from httmock import urlmatch , HTTMock , response
6
9
7
- from tests .helpers import DummyHttpResponse
8
10
from filestack import Filelink , Security
9
11
from filestack import config
10
12
@@ -93,25 +95,27 @@ def api_bad(url, request):
93
95
}
94
96
),
95
97
])
96
- @patch ('filestack.models.filelink.requests.get' )
97
- def test_metadata (get_mock , attributes , security , expected_params , filelink ):
98
- get_mock .return_value = DummyHttpResponse (json_dict = {'metadata' : 'content' })
98
+ @responses .activate
99
+ def test_metadata (attributes , security , expected_params , filelink ):
100
+ responses .add (
101
+ responses .GET ,
102
+ re .compile ('https://cdn.filestackcontent.com/SOMEHANDLE/metadata.*' ),
103
+ json = {'metadata' : 'content' }
104
+ )
99
105
metadata_response = filelink .metadata (attributes_list = attributes , security = security )
100
106
assert metadata_response == {'metadata' : 'content' }
101
- expected_url = '{}/SOMEHANDLE/metadata' .format (config .CDN_URL )
102
- get_mock .assert_called_once_with (expected_url , params = expected_params )
107
+ assert responses .calls [0 ].request .params == expected_params
103
108
104
109
110
+ @responses .activate
105
111
def test_download (filelink ):
106
- @urlmatch (netloc = r'cdn\.filestackcontent\.com' , method = 'get' , scheme = 'https' )
107
- def api_download (url , request ):
108
- return response (200 , b'file-content' )
109
-
112
+ responses .add (
113
+ responses .GET , 'https://cdn.filestackcontent.com/{}' .format (HANDLE ), body = b'file-content'
114
+ )
110
115
m = mock_open ()
111
116
with patch ('filestack.mixins.common.open' , m ):
112
- with HTTMock (api_download ):
113
- file_size = filelink .download ('tests/data/test_download.jpg' )
114
- assert file_size == 12
117
+ file_size = filelink .download ('tests/data/test_download.jpg' )
118
+ assert file_size == 12
115
119
m ().write .assert_called_once_with (b'file-content' )
116
120
117
121
@@ -120,22 +124,27 @@ def test_tags_without_security(filelink):
120
124
filelink .tags ()
121
125
122
126
123
- @patch ('filestack.utils.requests.get' )
124
- def test_tags (get_mock , secure_filelink ):
125
- image_tags = {'tags' : {'cat' : 99 }}
126
- get_mock .return_value = DummyHttpResponse (json_dict = image_tags )
127
- assert secure_filelink .tags () == image_tags
128
- get_mock .assert_called_once_with ('{}/tags/{}/{}' .format (config .CDN_URL , SECURITY .as_url_string (), HANDLE ))
127
+ @responses .activate
128
+ def test_tags (secure_filelink ):
129
+ responses .add (
130
+ responses .GET , re .compile ('https://cdn.filestackcontent.com/tags/.*/{}$' .format (HANDLE )),
131
+ json = {'tags' : {'cat' : 98 }}
132
+ )
133
+ assert secure_filelink .tags () == {'tags' : {'cat' : 98 }}
134
+ assert responses .calls [0 ].request .url == '{}/tags/{}/{}' .format (config .CDN_URL , SECURITY .as_url_string (), HANDLE )
129
135
130
136
131
- @patch ( 'filestack.utils.requests.get' )
132
- def test_tags_on_transformation (get_mock , secure_filelink ):
137
+ @responses . activate
138
+ def test_tags_on_transformation (secure_filelink ):
133
139
transformation = secure_filelink .resize (width = 100 )
134
140
image_tags = {'tags' : {'cat' : 99 }}
135
- get_mock .return_value = DummyHttpResponse (json_dict = image_tags )
141
+ responses .add (
142
+ responses .GET , re .compile ('{}/resize.*/{}$' .format (config .CDN_URL , HANDLE )),
143
+ json = image_tags
144
+ )
136
145
assert transformation .tags () == image_tags
137
- get_mock . assert_called_once_with (
138
- '{}/resize=width:100/tags/{}/{}' . format ( config .CDN_URL , SECURITY .as_url_string (), HANDLE )
146
+ assert responses . calls [ 0 ]. request . url == '{}/resize=width:100/tags/{}/{}' . format (
147
+ config .CDN_URL , SECURITY .as_url_string (), HANDLE
139
148
)
140
149
141
150
@@ -144,22 +153,22 @@ def test_sfw_without_security(filelink):
144
153
filelink .sfw ()
145
154
146
155
147
- @patch ( 'filestack.utils.requests.get' )
148
- def test_sfw (get_mock , secure_filelink ):
156
+ @responses . activate
157
+ def test_sfw (secure_filelink ):
149
158
sfw_response = {'sfw' : False }
150
- get_mock . return_value = DummyHttpResponse ( json_dict = sfw_response )
159
+ responses . add ( responses . GET , re . compile ( '{}.*' . format ( config . CDN_URL )), json = sfw_response )
151
160
assert secure_filelink .sfw () == sfw_response
152
- get_mock . assert_called_once_with ( '{}/sfw/{}/{}' .format (config .CDN_URL , SECURITY .as_url_string (), HANDLE ) )
161
+ assert responses . calls [ 0 ]. request . url == '{}/sfw/{}/{}' .format (config .CDN_URL , SECURITY .as_url_string (), HANDLE )
153
162
154
163
155
- @patch ( 'filestack.utils.requests.get' )
156
- def test_sfw_on_transformation (get_mock , secure_filelink ):
164
+ @responses . activate
165
+ def test_sfw_on_transformation (secure_filelink ):
157
166
transformation = secure_filelink .resize (width = 100 )
158
167
sfw_response = {'sfw' : True }
159
- get_mock . return_value = DummyHttpResponse ( json_dict = sfw_response )
168
+ responses . add ( responses . GET , re . compile ( '{}/resize.*' . format ( config . CDN_URL )), json = sfw_response )
160
169
assert transformation .sfw () == sfw_response
161
- get_mock . assert_called_once_with (
162
- '{}/resize=width:100/sfw/{}/{}' . format ( config .CDN_URL , SECURITY .as_url_string (), HANDLE )
170
+ assert responses . calls [ 0 ]. request . url == '{}/resize=width:100/sfw/{}/{}' . format (
171
+ config .CDN_URL , SECURITY .as_url_string (), HANDLE
163
172
)
164
173
165
174
0 commit comments