1+ import shutil
12from django .conf import settings
23from django .test import TestCase , override_settings
34from django .urls import reverse
@@ -34,7 +35,7 @@ def test_standard_upload(self):
3435
3536 # Create a user
3637 credentials = {
37- "email" : "some_email @hacksoft.io" ,
38+ "email" : "test @hacksoft.io" ,
3839 "password" : "123456"
3940 }
4041 user_create (** credentials )
@@ -57,9 +58,7 @@ def test_standard_upload(self):
5758 )
5859
5960 with self .subTest ("1. Upload a file, below the size limit, assert models gets created accordingly" ):
60- response = self .client .post (
61- self .standard_upload_url , {"file" : file_1 }, enctype = "multipart/form-data" , ** auth_headers
62- )
61+ response = self .client .post (self .standard_upload_url , {"file" : file_1 }, ** auth_headers )
6362
6463 self .assertEqual (201 , response .status_code )
6564 self .assertEqual (1 , File .objects .count ())
@@ -70,26 +69,25 @@ def test_standard_upload(self):
7069 )
7170
7271 with self .subTest ("2. Upload a file, above the size limit, assert API error, nothing gets created" ):
73- response = self .client .post (
74- self .standard_upload_url , {"file" : file_2 }, enctype = "multipart/form-data" , ** auth_headers
75- )
72+ response = self .client .post (self .standard_upload_url , {"file" : file_2 }, ** auth_headers )
7673
7774 self .assertEqual (400 , response .status_code )
7875 self .assertEqual (1 , File .objects .count ())
7976
8077 # Create a file equal to the size limit
8178 file_3 = SimpleUploadedFile (
82- name = "file_equal.txt" , content = file_max_size * "b " .encode (), content_type = "text/plain"
79+ name = "file_equal.txt" , content = file_max_size * "a " .encode (), content_type = "text/plain"
8380 )
8481
8582 with self .subTest ("3. Upload a file, equal to the size limit, assert models gets created accordingly" ):
86- response = self .client .post (
87- self .standard_upload_url , {"file" : file_3 }, enctype = "multipart/form-data" , ** auth_headers
88- )
83+ response = self .client .post (self .standard_upload_url , {"file" : file_3 }, ** auth_headers )
8984
9085 self .assertEqual (201 , response .status_code )
9186 self .assertEqual (2 , File .objects .count ())
9287
88+ def tearDown (self ):
89+ shutil .rmtree (settings .MEDIA_ROOT , ignore_errors = True )
90+
9391
9492class StandardUploadAdminTests (TestCase ):
9593 """
@@ -123,7 +121,7 @@ def test_standard_admin_upload_and_update(self):
123121
124122 # Create a superuser
125123 credentials = {
126- "email" : "admin_email @hacksoft.io" ,
124+ "email" : "test @hacksoft.io" ,
127125 "password" : "123456" ,
128126 "is_admin" : True ,
129127 "is_superuser" : True
@@ -171,7 +169,7 @@ def test_standard_admin_upload_and_update(self):
171169 self .assertEqual (file_2 .name , File .objects .last ().original_file_name )
172170
173171 file_3 = SimpleUploadedFile (
174- name = "oversized_file.txt" , content = (file_max_size + 10 ) * "b " .encode (), content_type = "text/plain"
172+ name = "oversized_file.txt" , content = (file_max_size + 1 ) * "a " .encode (), content_type = "text/plain"
175173 )
176174
177175 data_oversized_file = {
@@ -188,14 +186,14 @@ def test_standard_admin_upload_and_update(self):
188186 self .assertEqual (file_2 .name , File .objects .last ().original_file_name )
189187
190188 file_4 = SimpleUploadedFile (
191- name = "new_oversized_file.txt" , content = (file_max_size + 20 ) * "c " .encode (), content_type = "text/plain"
189+ name = "new_oversized_file.txt" , content = (file_max_size + 1 ) * "a " .encode (), content_type = "text/plain"
192190 )
193191
194192 data_new_oversized_file = {
195193 "file" : file_4 ,
196194 "uploaded_by" : user .id
197195 }
198-
196+ # Comment here
199197 with self .subTest (
200198 "4. Update an existing file with an oversized one via the Django admin, assert error, nothing gets created"
201199 ):
@@ -205,3 +203,6 @@ def test_standard_admin_upload_and_update(self):
205203 self .assertContains (response_2 , "File is too large" )
206204 self .assertEqual (1 , File .objects .count ())
207205 self .assertEqual (file_2 .name , File .objects .last ().original_file_name )
206+
207+ def tearDown (self ):
208+ shutil .rmtree (settings .MEDIA_ROOT , ignore_errors = True )
0 commit comments