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
@@ -27,14 +28,13 @@ def setUp(self):
27
28
28
29
@override_settings (FILE_MAX_SIZE = 10 )
29
30
def test_standard_upload (self ):
30
- file_max_size = settings .FILE_MAX_SIZE
31
31
32
32
self .assertEqual (0 , File .objects .count ())
33
33
self .assertEqual (0 , BaseUser .objects .count ())
34
34
35
35
# Create a user
36
36
credentials = {
37
- "email" : "some_email @hacksoft.io" ,
37
+ "email" : "test @hacksoft.io" ,
38
38
"password" : "123456"
39
39
}
40
40
user_create (** credentials )
@@ -53,43 +53,46 @@ def test_standard_upload(self):
53
53
54
54
# Create a small sized file
55
55
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"
57
59
)
58
60
59
61
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 )
63
63
64
64
self .assertEqual (201 , response .status_code )
65
65
self .assertEqual (1 , File .objects .count ())
66
66
67
67
# Create a file above the size limit
68
68
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"
70
72
)
71
73
72
74
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 )
76
76
77
77
self .assertEqual (400 , response .status_code )
78
78
self .assertEqual (1 , File .objects .count ())
79
79
80
80
# Create a file equal to the size limit
81
81
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"
83
85
)
84
86
85
87
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 )
89
89
90
90
self .assertEqual (201 , response .status_code )
91
91
self .assertEqual (2 , File .objects .count ())
92
92
93
+ def tearDown (self ):
94
+ shutil .rmtree (settings .MEDIA_ROOT , ignore_errors = True )
95
+
93
96
94
97
class StandardUploadAdminTests (TestCase ):
95
98
"""
@@ -117,13 +120,12 @@ def setUp(self):
117
120
118
121
@override_settings (FILE_MAX_SIZE = 10 )
119
122
def test_standard_admin_upload_and_update (self ):
120
- file_max_size = settings .FILE_MAX_SIZE
121
123
122
124
self .assertEqual (0 , File .objects .count ())
123
125
124
126
# Create a superuser
125
127
credentials = {
126
- "email" : "admin_email @hacksoft.io" ,
128
+ "email" : "test @hacksoft.io" ,
127
129
"password" : "123456" ,
128
130
"is_admin" : True ,
129
131
"is_superuser" : True
@@ -133,7 +135,9 @@ def test_standard_admin_upload_and_update(self):
133
135
self .assertEqual (1 , BaseUser .objects .count ())
134
136
135
137
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"
137
141
)
138
142
139
143
data_file_1 = {
@@ -149,12 +153,13 @@ def test_standard_admin_upload_and_update(self):
149
153
successfully_uploaded_file = File .objects .last ()
150
154
151
155
self .assertEqual (302 , response .status_code )
152
- self .assertEqual (self .admin_files_list_url , response .url )
153
156
self .assertEqual (1 , File .objects .count ())
154
157
self .assertEqual (file_1 .name , successfully_uploaded_file .original_file_name )
155
158
156
159
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"
158
163
)
159
164
160
165
data_file_2 = {
@@ -166,12 +171,13 @@ def test_standard_admin_upload_and_update(self):
166
171
response = self .client .post (self .admin_update_file_url (successfully_uploaded_file ), data_file_2 )
167
172
168
173
self .assertEqual (302 , response .status_code )
169
- self .assertRedirects (response , self .admin_files_list_url )
170
174
self .assertEqual (1 , File .objects .count ())
171
175
self .assertEqual (file_2 .name , File .objects .last ().original_file_name )
172
176
173
177
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"
175
181
)
176
182
177
183
data_oversized_file = {
@@ -188,14 +194,16 @@ def test_standard_admin_upload_and_update(self):
188
194
self .assertEqual (file_2 .name , File .objects .last ().original_file_name )
189
195
190
196
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"
192
200
)
193
201
194
202
data_new_oversized_file = {
195
203
"file" : file_4 ,
196
204
"uploaded_by" : user .id
197
205
}
198
-
206
+ # Comment here
199
207
with self .subTest (
200
208
"4. Update an existing file with an oversized one via the Django admin, assert error, nothing gets created"
201
209
):
@@ -205,3 +213,6 @@ def test_standard_admin_upload_and_update(self):
205
213
self .assertContains (response_2 , "File is too large" )
206
214
self .assertEqual (1 , File .objects .count ())
207
215
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