44
55import re
66import uuid
7+ from unittest import mock
78
89from django .core .files .storage import default_storage
910from django .core .files .uploadedfile import SimpleUploadedFile
1213from rest_framework .test import APIClient
1314
1415from core import factories
16+ from core .api .viewsets import malware_detection
1517from core .tests .conftest import TEAM , USER , VIA
1618
1719pytestmark = pytest .mark .django_db
@@ -59,7 +61,8 @@ def test_api_documents_attachment_upload_anonymous_success():
5961 file = SimpleUploadedFile (name = "test.png" , content = PIXEL , content_type = "image/png" )
6062
6163 url = f"/api/v1.0/documents/{ document .id !s} /attachment-upload/"
62- response = APIClient ().post (url , {"file" : file }, format = "multipart" )
64+ with mock .patch .object (malware_detection , "analyse_file" ) as mock_analyse_file :
65+ response = APIClient ().post (url , {"file" : file }, format = "multipart" )
6366
6467 assert response .status_code == 201
6568
@@ -74,12 +77,13 @@ def test_api_documents_attachment_upload_anonymous_success():
7477 assert document .attachments == [f"{ document .id !s} /attachments/{ file_id !s} .png" ]
7578
7679 # Now, check the metadata of the uploaded file
77- key = file_path .replace ("/media" , "" )
80+ key = file_path .replace ("/media/" , "" )
81+ mock_analyse_file .assert_called_once_with (key , document_id = document .id )
7882 file_head = default_storage .connection .meta .client .head_object (
7983 Bucket = default_storage .bucket_name , Key = key
8084 )
8185
82- assert file_head ["Metadata" ] == {"owner" : "None" }
86+ assert file_head ["Metadata" ] == {"owner" : "None" , "status" : "processing" }
8387 assert file_head ["ContentType" ] == "image/png"
8488 assert file_head ["ContentDisposition" ] == 'inline; filename="test.png"'
8589
@@ -139,14 +143,19 @@ def test_api_documents_attachment_upload_authenticated_success(reach, role):
139143 file = SimpleUploadedFile (name = "test.png" , content = PIXEL , content_type = "image/png" )
140144
141145 url = f"/api/v1.0/documents/{ document .id !s} /attachment-upload/"
142- response = client .post (url , {"file" : file }, format = "multipart" )
146+ with mock .patch .object (malware_detection , "analyse_file" ) as mock_analyse_file :
147+ response = client .post (url , {"file" : file }, format = "multipart" )
143148
144149 assert response .status_code == 201
145150
146151 pattern = re .compile (rf"^/media/{ document .id !s} /attachments/(.*)\.png" )
147152 match = pattern .search (response .json ()["file" ])
148153 file_id = match .group (1 )
149154
155+ mock_analyse_file .assert_called_once_with (
156+ f"{ document .id !s} /attachments/{ file_id !s} .png" , document_id = document .id
157+ )
158+
150159 # Validate that file_id is a valid UUID
151160 uuid .UUID (file_id )
152161
@@ -210,7 +219,8 @@ def test_api_documents_attachment_upload_success(via, role, mock_user_teams):
210219 file = SimpleUploadedFile (name = "test.png" , content = PIXEL , content_type = "image/png" )
211220
212221 url = f"/api/v1.0/documents/{ document .id !s} /attachment-upload/"
213- response = client .post (url , {"file" : file }, format = "multipart" )
222+ with mock .patch .object (malware_detection , "analyse_file" ) as mock_analyse_file :
223+ response = client .post (url , {"file" : file }, format = "multipart" )
214224
215225 assert response .status_code == 201
216226
@@ -226,11 +236,12 @@ def test_api_documents_attachment_upload_success(via, role, mock_user_teams):
226236 assert document .attachments == [f"{ document .id !s} /attachments/{ file_id !s} .png" ]
227237
228238 # Now, check the metadata of the uploaded file
229- key = file_path .replace ("/media" , "" )
239+ key = file_path .replace ("/media/" , "" )
240+ mock_analyse_file .assert_called_once_with (key , document_id = document .id )
230241 file_head = default_storage .connection .meta .client .head_object (
231242 Bucket = default_storage .bucket_name , Key = key
232243 )
233- assert file_head ["Metadata" ] == {"owner" : str (user .id )}
244+ assert file_head ["Metadata" ] == {"owner" : str (user .id ), "status" : "processing" }
234245 assert file_head ["ContentType" ] == "image/png"
235246 assert file_head ["ContentDisposition" ] == 'inline; filename="test.png"'
236247
@@ -304,7 +315,8 @@ def test_api_documents_attachment_upload_fix_extension(
304315 url = f"/api/v1.0/documents/{ document .id !s} /attachment-upload/"
305316
306317 file = SimpleUploadedFile (name = name , content = content )
307- response = client .post (url , {"file" : file }, format = "multipart" )
318+ with mock .patch .object (malware_detection , "analyse_file" ) as mock_analyse_file :
319+ response = client .post (url , {"file" : file }, format = "multipart" )
308320
309321 assert response .status_code == 201
310322
@@ -324,11 +336,16 @@ def test_api_documents_attachment_upload_fix_extension(
324336 uuid .UUID (file_id )
325337
326338 # Now, check the metadata of the uploaded file
327- key = file_path .replace ("/media" , "" )
339+ key = file_path .replace ("/media/" , "" )
340+ mock_analyse_file .assert_called_once_with (key , document_id = document .id )
328341 file_head = default_storage .connection .meta .client .head_object (
329342 Bucket = default_storage .bucket_name , Key = key
330343 )
331- assert file_head ["Metadata" ] == {"owner" : str (user .id ), "is_unsafe" : "true" }
344+ assert file_head ["Metadata" ] == {
345+ "owner" : str (user .id ),
346+ "is_unsafe" : "true" ,
347+ "status" : "processing" ,
348+ }
332349 assert file_head ["ContentType" ] == content_type
333350 assert file_head ["ContentDisposition" ] == f'attachment; filename="{ name :s} "'
334351
@@ -364,7 +381,8 @@ def test_api_documents_attachment_upload_unsafe():
364381 file = SimpleUploadedFile (
365382 name = "script.exe" , content = b"\x4d \x5a \x90 \x00 \x03 \x00 \x00 \x00 "
366383 )
367- response = client .post (url , {"file" : file }, format = "multipart" )
384+ with mock .patch .object (malware_detection , "analyse_file" ) as mock_analyse_file :
385+ response = client .post (url , {"file" : file }, format = "multipart" )
368386
369387 assert response .status_code == 201
370388
@@ -381,11 +399,16 @@ def test_api_documents_attachment_upload_unsafe():
381399 file_id = file_id .replace ("-unsafe" , "" )
382400 uuid .UUID (file_id )
383401
402+ key = file_path .replace ("/media/" , "" )
403+ mock_analyse_file .assert_called_once_with (key , document_id = document .id )
384404 # Now, check the metadata of the uploaded file
385- key = file_path .replace ("/media" , "" )
386405 file_head = default_storage .connection .meta .client .head_object (
387406 Bucket = default_storage .bucket_name , Key = key
388407 )
389- assert file_head ["Metadata" ] == {"owner" : str (user .id ), "is_unsafe" : "true" }
408+ assert file_head ["Metadata" ] == {
409+ "owner" : str (user .id ),
410+ "is_unsafe" : "true" ,
411+ "status" : "processing" ,
412+ }
390413 assert file_head ["ContentType" ] == "application/octet-stream"
391414 assert file_head ["ContentDisposition" ] == 'attachment; filename="script.exe"'
0 commit comments