1+ import shutil
12from django .conf import settings
23from django .test import TestCase , override_settings
34from django .urls import reverse
@@ -27,14 +28,13 @@ def setUp(self):
2728
2829 @override_settings (FILE_MAX_SIZE = 10 )
2930 def test_standard_upload (self ):
30- file_max_size = settings .FILE_MAX_SIZE
3131
3232 self .assertEqual (0 , File .objects .count ())
3333 self .assertEqual (0 , BaseUser .objects .count ())
3434
3535 # Create a user
3636 credentials = {
37- "email" : "some_email @hacksoft.io" ,
37+ "email" : "test @hacksoft.io" ,
3838 "password" : "123456"
3939 }
4040 user_create (** credentials )
@@ -53,43 +53,46 @@ def test_standard_upload(self):
5353
5454 # Create a small sized file
5555 file_1 = SimpleUploadedFile (
56- name = "file_small.txt" , content = b"Test" , content_type = "text/plain"
56+ name = "file_small.txt" ,
57+ content = (settings .FILE_MAX_SIZE - 5 ) * "a" .encode (),
58+ content_type = "text/plain"
5759 )
5860
5961 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- )
62+ response = self .client .post (self .standard_upload_url , {"file" : file_1 }, ** auth_headers )
6363
6464 self .assertEqual (201 , response .status_code )
6565 self .assertEqual (1 , File .objects .count ())
6666
6767 # Create a file above the size limit
6868 file_2 = SimpleUploadedFile (
69- name = "file_big.txt" , content = (file_max_size + 1 ) * "a" .encode (), content_type = "text/plain"
69+ name = "file_big.txt" ,
70+ content = (settings .FILE_MAX_SIZE + 1 ) * "a" .encode (),
71+ content_type = "text/plain"
7072 )
7173
7274 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- )
75+ response = self .client .post (self .standard_upload_url , {"file" : file_2 }, ** auth_headers )
7676
7777 self .assertEqual (400 , response .status_code )
7878 self .assertEqual (1 , File .objects .count ())
7979
8080 # Create a file equal to the size limit
8181 file_3 = SimpleUploadedFile (
82- name = "file_equal.txt" , content = file_max_size * "b" .encode (), content_type = "text/plain"
82+ name = "file_equal.txt" ,
83+ content = settings .FILE_MAX_SIZE * "a" .encode (),
84+ content_type = "text/plain"
8385 )
8486
8587 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- )
88+ response = self .client .post (self .standard_upload_url , {"file" : file_3 }, ** auth_headers )
8989
9090 self .assertEqual (201 , response .status_code )
9191 self .assertEqual (2 , File .objects .count ())
9292
93+ def tearDown (self ):
94+ shutil .rmtree (settings .MEDIA_ROOT , ignore_errors = True )
95+
9396
9497class StandardUploadAdminTests (TestCase ):
9598 """
@@ -117,13 +120,12 @@ def setUp(self):
117120
118121 @override_settings (FILE_MAX_SIZE = 10 )
119122 def test_standard_admin_upload_and_update (self ):
120- file_max_size = settings .FILE_MAX_SIZE
121123
122124 self .assertEqual (0 , File .objects .count ())
123125
124126 # Create a superuser
125127 credentials = {
126- "email" : "admin_email @hacksoft.io" ,
128+ "email" : "test @hacksoft.io" ,
127129 "password" : "123456" ,
128130 "is_admin" : True ,
129131 "is_superuser" : True
@@ -133,7 +135,9 @@ def test_standard_admin_upload_and_update(self):
133135 self .assertEqual (1 , BaseUser .objects .count ())
134136
135137 file_1 = SimpleUploadedFile (
136- name = "first_file.txt" , content = b"Test!" , content_type = "text/plain"
138+ name = "first_file.txt" ,
139+ content = (settings .FILE_MAX_SIZE - 5 ) * "a" .encode (),
140+ content_type = "text/plain"
137141 )
138142
139143 data_file_1 = {
@@ -149,12 +153,13 @@ def test_standard_admin_upload_and_update(self):
149153 successfully_uploaded_file = File .objects .last ()
150154
151155 self .assertEqual (302 , response .status_code )
152- self .assertEqual (self .admin_files_list_url , response .url )
153156 self .assertEqual (1 , File .objects .count ())
154157 self .assertEqual (file_1 .name , successfully_uploaded_file .original_file_name )
155158
156159 file_2 = SimpleUploadedFile (
157- name = "second_file.txt" , content = (file_max_size - 1 ) * "a" .encode (), content_type = "text/plain"
160+ name = "second_file.txt" ,
161+ content = (settings .FILE_MAX_SIZE - 1 ) * "a" .encode (),
162+ content_type = "text/plain"
158163 )
159164
160165 data_file_2 = {
@@ -166,12 +171,13 @@ def test_standard_admin_upload_and_update(self):
166171 response = self .client .post (self .admin_update_file_url (successfully_uploaded_file ), data_file_2 )
167172
168173 self .assertEqual (302 , response .status_code )
169- self .assertRedirects (response , self .admin_files_list_url )
170174 self .assertEqual (1 , File .objects .count ())
171175 self .assertEqual (file_2 .name , File .objects .last ().original_file_name )
172176
173177 file_3 = SimpleUploadedFile (
174- name = "oversized_file.txt" , content = (file_max_size + 10 ) * "b" .encode (), content_type = "text/plain"
178+ name = "oversized_file.txt" ,
179+ content = (settings .FILE_MAX_SIZE + 1 ) * "a" .encode (),
180+ content_type = "text/plain"
175181 )
176182
177183 data_oversized_file = {
@@ -188,14 +194,16 @@ def test_standard_admin_upload_and_update(self):
188194 self .assertEqual (file_2 .name , File .objects .last ().original_file_name )
189195
190196 file_4 = SimpleUploadedFile (
191- name = "new_oversized_file.txt" , content = (file_max_size + 20 ) * "c" .encode (), content_type = "text/plain"
197+ name = "new_oversized_file.txt" ,
198+ content = (settings .FILE_MAX_SIZE + 1 ) * "a" .encode (),
199+ content_type = "text/plain"
192200 )
193201
194202 data_new_oversized_file = {
195203 "file" : file_4 ,
196204 "uploaded_by" : user .id
197205 }
198-
206+ # Comment here
199207 with self .subTest (
200208 "4. Update an existing file with an oversized one via the Django admin, assert error, nothing gets created"
201209 ):
@@ -205,3 +213,6 @@ def test_standard_admin_upload_and_update(self):
205213 self .assertContains (response_2 , "File is too large" )
206214 self .assertEqual (1 , File .objects .count ())
207215 self .assertEqual (file_2 .name , File .objects .last ().original_file_name )
216+
217+ def tearDown (self ):
218+ shutil .rmtree (settings .MEDIA_ROOT , ignore_errors = True )
0 commit comments