@@ -105,3 +105,103 @@ class StandardUploadAdminTests(TestCase):
105
105
1. Create a new file via the Django admin, assert error, nothing gets created.
106
106
2. Update an existing fila via the Django admin, assert error, nothing gets created.
107
107
"""
108
+ def setUp (self ):
109
+ self .client = APIClient ()
110
+
111
+ self .admin_upload_file_url = reverse ("admin:files_file_add" )
112
+ self .admin_files_list_url = reverse ("admin:files_file_changelist" )
113
+ self .admin_update_file_url = lambda file : reverse (
114
+ "admin:files_file_change" ,
115
+ kwargs = {"object_id" : str (file .id )}
116
+ )
117
+
118
+ @override_settings (FILE_MAX_SIZE = 10 )
119
+ def test_standard_admin_upload_and_update (self ):
120
+ file_max_size = settings .FILE_MAX_SIZE
121
+
122
+ self .assertEqual (0 , File .objects .count ())
123
+
124
+ # Create a superuser
125
+ credentials = {
126
+
127
+ "password" : "123456" ,
128
+ "is_admin" : True ,
129
+ "is_superuser" : True
130
+ }
131
+ user = BaseUser .objects .create (** credentials )
132
+
133
+ self .assertEqual (1 , BaseUser .objects .count ())
134
+
135
+ file_1 = SimpleUploadedFile (
136
+ name = "first_file.txt" , content = b"Test!" , content_type = "text/plain"
137
+ )
138
+
139
+ data_file_1 = {
140
+ "file" : file_1 ,
141
+ "uploaded_by" : user .id
142
+ }
143
+
144
+ # Log in with the superuser account
145
+ self .client .force_login (user )
146
+
147
+ with self .subTest ("1. Create a new file via the Django admin, assert everything gets created" ):
148
+ response = self .client .post (self .admin_upload_file_url , data_file_1 )
149
+ successfully_uploaded_file = File .objects .last ()
150
+
151
+ self .assertEqual (302 , response .status_code )
152
+ self .assertEqual (self .admin_files_list_url , response .url )
153
+ self .assertEqual (1 , File .objects .count ())
154
+ self .assertEqual (file_1 .name , successfully_uploaded_file .original_file_name )
155
+
156
+ file_2 = SimpleUploadedFile (
157
+ name = "second_file.txt" , content = (file_max_size - 1 ) * "a" .encode (), content_type = "text/plain"
158
+ )
159
+
160
+ data_file_2 = {
161
+ "file" : file_2 ,
162
+ "uploaded_by" : user .id
163
+ }
164
+
165
+ with self .subTest ("2. Update an existing file via the Django admin, assert everything gets updated" ):
166
+ response = self .client .post (self .admin_update_file_url (successfully_uploaded_file ), data_file_2 )
167
+
168
+ self .assertEqual (302 , response .status_code )
169
+ self .assertRedirects (response , self .admin_files_list_url )
170
+ self .assertEqual (1 , File .objects .count ())
171
+ self .assertEqual (file_2 .name , File .objects .last ().original_file_name )
172
+
173
+ file_3 = SimpleUploadedFile (
174
+ name = "oversized_file.txt" , content = (file_max_size + 10 ) * "b" .encode (), content_type = "text/plain"
175
+ )
176
+
177
+ data_oversized_file = {
178
+ "file" : file_3 ,
179
+ "uploaded_by" : user .id
180
+ }
181
+
182
+ with self .subTest ("3. Create a new oversized file via the Django admin, assert error, nothing gets created" ):
183
+ response = self .client .post (self .admin_upload_file_url , data_oversized_file )
184
+ response_2 = self .client .get (response .url )
185
+
186
+ self .assertContains (response_2 , "File is too large" )
187
+ self .assertEqual (1 , File .objects .count ())
188
+ self .assertEqual (file_2 .name , File .objects .last ().original_file_name )
189
+
190
+ file_4 = SimpleUploadedFile (
191
+ name = "new_oversized_file.txt" , content = (file_max_size + 20 ) * "c" .encode (), content_type = "text/plain"
192
+ )
193
+
194
+ data_new_oversized_file = {
195
+ "file" : file_4 ,
196
+ "uploaded_by" : user .id
197
+ }
198
+
199
+ with self .subTest (
200
+ "4. Update an existing file with an oversized one via the Django admin, assert error, nothing gets created"
201
+ ):
202
+ response = self .client .post (self .admin_update_file_url (File .objects .last ()), data_new_oversized_file )
203
+ response_2 = self .client .get (response .url )
204
+
205
+ self .assertContains (response_2 , "File is too large" )
206
+ self .assertEqual (1 , File .objects .count ())
207
+ self .assertEqual (file_2 .name , File .objects .last ().original_file_name )
0 commit comments