1
+ import shutil
1
2
from django .conf import settings
2
3
from django .test import TestCase , override_settings
3
4
from django .urls import reverse
@@ -34,7 +35,7 @@ def test_standard_upload(self):
34
35
35
36
# Create a user
36
37
credentials = {
37
- "email" : "some_email @hacksoft.io" ,
38
+ "email" : "test @hacksoft.io" ,
38
39
"password" : "123456"
39
40
}
40
41
user_create (** credentials )
@@ -57,9 +58,7 @@ def test_standard_upload(self):
57
58
)
58
59
59
60
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 )
63
62
64
63
self .assertEqual (201 , response .status_code )
65
64
self .assertEqual (1 , File .objects .count ())
@@ -70,26 +69,25 @@ def test_standard_upload(self):
70
69
)
71
70
72
71
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 )
76
73
77
74
self .assertEqual (400 , response .status_code )
78
75
self .assertEqual (1 , File .objects .count ())
79
76
80
77
# Create a file equal to the size limit
81
78
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"
83
80
)
84
81
85
82
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 )
89
84
90
85
self .assertEqual (201 , response .status_code )
91
86
self .assertEqual (2 , File .objects .count ())
92
87
88
+ def tearDown (self ):
89
+ shutil .rmtree (settings .MEDIA_ROOT , ignore_errors = True )
90
+
93
91
94
92
class StandardUploadAdminTests (TestCase ):
95
93
"""
@@ -123,7 +121,7 @@ def test_standard_admin_upload_and_update(self):
123
121
124
122
# Create a superuser
125
123
credentials = {
126
- "email" : "admin_email @hacksoft.io" ,
124
+ "email" : "test @hacksoft.io" ,
127
125
"password" : "123456" ,
128
126
"is_admin" : True ,
129
127
"is_superuser" : True
@@ -171,7 +169,7 @@ def test_standard_admin_upload_and_update(self):
171
169
self .assertEqual (file_2 .name , File .objects .last ().original_file_name )
172
170
173
171
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"
175
173
)
176
174
177
175
data_oversized_file = {
@@ -188,14 +186,14 @@ def test_standard_admin_upload_and_update(self):
188
186
self .assertEqual (file_2 .name , File .objects .last ().original_file_name )
189
187
190
188
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"
192
190
)
193
191
194
192
data_new_oversized_file = {
195
193
"file" : file_4 ,
196
194
"uploaded_by" : user .id
197
195
}
198
-
196
+ # Comment here
199
197
with self .subTest (
200
198
"4. Update an existing file with an oversized one via the Django admin, assert error, nothing gets created"
201
199
):
@@ -205,3 +203,6 @@ def test_standard_admin_upload_and_update(self):
205
203
self .assertContains (response_2 , "File is too large" )
206
204
self .assertEqual (1 , File .objects .count ())
207
205
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