-
Notifications
You must be signed in to change notification settings - Fork 329
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added whitelist of file types in file upload (#1824)
* feat: added whitelist of file types in file upload * test: added test for file upload whitelist * feat: use mime type for files whitelisting * test: use mime_type in test instead of extension * fix: allow audio files * fix: added all the allowed file types to whitelist * fix: minor changes (implement review suggestions) * Fix Typo --------- Co-authored-by: Vignesh Hari <[email protected]>
- Loading branch information
1 parent
e64fc1f
commit 662e0c5
Showing
4 changed files
with
107 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from rest_framework import status | ||
from rest_framework.test import APITestCase | ||
|
||
from care.utils.tests.test_utils import TestUtils | ||
|
||
|
||
class FileUploadApiTestCase(TestUtils, APITestCase): | ||
@classmethod | ||
def setUpTestData(cls) -> None: | ||
cls.state = cls.create_state() | ||
cls.district = cls.create_district(cls.state) | ||
cls.local_body = cls.create_local_body(cls.district) | ||
cls.super_user = cls.create_super_user("su", cls.district) | ||
cls.facility = cls.create_facility(cls.super_user, cls.district, cls.local_body) | ||
cls.user = cls.create_user("nurse", cls.district, home_facility=cls.facility) | ||
cls.patient = cls.create_patient( | ||
cls.district, cls.facility, local_body=cls.local_body | ||
) | ||
cls.consultation = cls.create_consultation(cls.patient, cls.facility) | ||
|
||
def test_file_upload_whitelist(self): | ||
for mime_type in ["application/pdf", "image/png", "image/jpeg"]: | ||
response = self.client.post( | ||
"/api/v1/files/", | ||
{ | ||
"original_name": f"test.{mime_type.split('/')[-1]}", | ||
"file_type": "CONSULTATION", | ||
"name": "Test File", | ||
"associating_id": self.consultation.external_id, | ||
"file_category": "UNSPECIFIED", | ||
"mime_type": mime_type, | ||
}, | ||
) | ||
self.assertEqual(response.status_code, status.HTTP_201_CREATED) | ||
|
||
for mime_type in ["image/gif"]: | ||
response = self.client.post( | ||
"/api/v1/files/", | ||
{ | ||
"original_name": f"test.{mime_type.split('/')[-1]}", | ||
"file_type": "CONSULTATION", | ||
"name": "Test File", | ||
"associating_id": self.consultation.external_id, | ||
"file_category": "UNSPECIFIED", | ||
mime_type: mime_type, | ||
}, | ||
) | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters