@@ -222,6 +222,128 @@ def test_binary_upload_succeeds(self):
222222 )
223223 self .assertEqual (url , responses .calls [0 ].request .url )
224224
225+ @responses .activate
226+ def test_upload_succeeds_with_url (self ):
227+ """
228+ Tests if upload succeeds
229+ """
230+ URL .UPLOAD_BASE_URL = "http://test.com"
231+ url = "%s%s" % (URL .UPLOAD_BASE_URL , "/api/v1/files/upload" )
232+ headers = create_headers_for_test ()
233+ responses .add (
234+ responses .POST ,
235+ url ,
236+ body = """{
237+ "fileId": "fake_file_id1234",
238+ "name": "file_name.jpg",
239+ "size": 102117,
240+ "versionInfo": {
241+ "id": "62d670648cdb697522602b45",
242+ "name": "Version 11"
243+ },
244+ "filePath": "/testing-python-folder/file_name.jpg",
245+ "url": "https://ik.imagekit.io/your_imagekit_id/testing-python-folder/file_name.jpg",
246+ "fileType": "image",
247+ "height": 700,
248+ "width": 1050,
249+ "thumbnailUrl": "https://ik.imagekit.io/your_imagekit_id/tr:n-ik_ml_thumbnail/testing-python-folder/file_name.jpg",
250+ "tags": [
251+ "abc",
252+ "def"
253+ ],
254+ "AITags": [
255+ {
256+ "name": "Computer",
257+ "confidence": 97.66,
258+ "source": "google-auto-tagging"
259+ },
260+ {
261+ "name": "Personal computer",
262+ "confidence": 94.96,
263+ "source": "google-auto-tagging"
264+ }
265+ ],
266+ "isPrivateFile": true,
267+ "extensionStatus": {
268+ "remove-bg": "pending",
269+ "google-auto-tagging": "success"
270+ }
271+ }""" ,
272+ headers = headers ,
273+ )
274+
275+ file_upload_url = "https://file-examples.com/wp-content/uploads/2017/10/file_example_JPG_100kB.jpg"
276+ resp = self .client .upload_file (
277+ file = file_upload_url ,
278+ file_name = "file_name.jpg" ,
279+ options = UploadFileRequestOptions (
280+ use_unique_file_name = False ,
281+ tags = ["abc" , "def" ],
282+ folder = "/testing-python-folder/" ,
283+ is_private_file = True ,
284+ response_fields = ["is_private_file" , "tags" ],
285+ extensions = (
286+ {
287+ "name" : "remove-bg" ,
288+ "options" : {"add_shadow" : True , "bg_color" : "pink" },
289+ },
290+ {"name" : "google-auto-tagging" , "minConfidence" : 80 , "maxTags" : 10 },
291+ ),
292+ webhook_url = "url" ,
293+ overwrite_file = True ,
294+ overwrite_ai_tags = False ,
295+ overwrite_tags = False ,
296+ overwrite_custom_metadata = True ,
297+ custom_metadata = {"test100" : 11 },
298+ ),
299+ )
300+ mock_response_metadata = {
301+ "headers" : {
302+ "Content-Type" : "text/plain" ,
303+ "Accept-Encoding" : "gzip, deflate" ,
304+ "Authorization" : "Basic ZmFrZTEyMjo=" ,
305+ },
306+ "http_status_code" : 200 ,
307+ "raw" : {
308+ "AITags" : [
309+ {
310+ "confidence" : 97.66 ,
311+ "name" : "Computer" ,
312+ "source" : "google-auto-tagging" ,
313+ },
314+ {
315+ "confidence" : 94.96 ,
316+ "name" : "Personal computer" ,
317+ "source" : "google-auto-tagging" ,
318+ },
319+ ],
320+ "extensionStatus" : {
321+ "google-auto-tagging" : "success" ,
322+ "remove-bg" : "pending" ,
323+ },
324+ "fileId" : "fake_file_id1234" ,
325+ "filePath" : "/testing-python-folder/file_name.jpg" ,
326+ "fileType" : "image" ,
327+ "height" : 700 ,
328+ "isPrivateFile" : True ,
329+ "name" : "file_name.jpg" ,
330+ "size" : 102117 ,
331+ "tags" : ["abc" , "def" ],
332+ "thumbnailUrl" : "https://ik.imagekit.io/your_imagekit_id/tr:n-ik_ml_thumbnail/testing-python-folder/file_name.jpg" ,
333+ "url" : "https://ik.imagekit.io/your_imagekit_id/testing-python-folder/file_name.jpg" ,
334+ "versionInfo" : {"id" : "62d670648cdb697522602b45" , "name" : "Version 11" },
335+ "width" : 1050 ,
336+ },
337+ }
338+ request_body = b'----randomBoundary---------------------\r \n Content-Disposition: form-data; name="file"\r \n \r \n https://file-examples.com/wp-content/uploads/2017/10/file_example_JPG_100kB.jpg\r \n ----randomBoundary---------------------\r \n Content-Disposition: form-data; name="fileName"\r \n \r \n file_name.jpg\r \n ----randomBoundary---------------------\r \n Content-Disposition: form-data; name="useUniqueFileName"\r \n \r \n false\r \n ----randomBoundary---------------------\r \n Content-Disposition: form-data; name="tags"\r \n \r \n abc,def\r \n ----randomBoundary---------------------\r \n Content-Disposition: form-data; name="folder"\r \n \r \n /testing-python-folder/\r \n ----randomBoundary---------------------\r \n Content-Disposition: form-data; name="isPrivateFile"\r \n \r \n true\r \n ----randomBoundary---------------------\r \n Content-Disposition: form-data; name="responseFields"\r \n \r \n isPrivateFile,tags\r \n ----randomBoundary---------------------\r \n Content-Disposition: form-data; name="extensions"\r \n \r \n [{"name": "remove-bg", "options": {"add_shadow": true, "bg_color": "pink"}}, {"name": "google-auto-tagging", "minConfidence": 80, "maxTags": 10}]\r \n ----randomBoundary---------------------\r \n Content-Disposition: form-data; name="webhookUrl"\r \n \r \n url\r \n ----randomBoundary---------------------\r \n Content-Disposition: form-data; name="overwriteFile"\r \n \r \n true\r \n ----randomBoundary---------------------\r \n Content-Disposition: form-data; name="overwriteTags"\r \n \r \n false\r \n ----randomBoundary---------------------\r \n Content-Disposition: form-data; name="overwriteCustomMetadata"\r \n \r \n true\r \n ----randomBoundary---------------------\r \n Content-Disposition: form-data; name="customMetadata"\r \n \r \n {"test100": 11}\r \n ----randomBoundary---------------------\r \n Content-Disposition: form-data; name="overwriteAITags"\r \n \r \n false\r \n ----randomBoundary-----------------------\r \n '
339+
340+ self .assertEqual (request_body , responses .calls [0 ].request .body )
341+ self .assertEqual (
342+ camel_dict_to_snake_dict (mock_response_metadata ),
343+ resp .response_metadata .__dict__ ,
344+ )
345+ self .assertEqual (url , responses .calls [0 ].request .url )
346+
225347 def test_upload_fails_without_file_name (self ) -> None :
226348 """Test upload raises error on missing required params"""
227349 try :
0 commit comments