Skip to content

Commit

Permalink
add unit tests and check for dict keys in submitted files list
Browse files Browse the repository at this point in the history
  • Loading branch information
quimmrc committed Jan 21, 2025
1 parent 043373c commit 3660c52
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
14 changes: 14 additions & 0 deletions accounts/tests/test_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ def test_handle_uploaded_file_html(self):
self.assertEqual(resp.status_code, 200)
self.assertEqual(os.path.exists(settings.UPLOADS_PATH + '/%i/%s' % (user.id, filename)), False)

@override_uploads_path_with_temp_directory
def test_handle_uploaded_duplicate_filenames_html(self):
user = User.objects.create_user("testuser", password="testpass")
self.client.force_login(user)

filename = "file.wav"
d_filename = "file(1).wav"
f = SimpleUploadedFile(filename, b"file_content")
f1 = SimpleUploadedFile(filename, b"file_content_1")
resp = self.client.post("/home/upload/html/", {'file': [f, f1]})
self.assertEqual(resp.status_code, 200)
self.assertEqual(os.path.exists(settings.UPLOADS_PATH + '/%i/%s' % (user.id, filename)), True)
self.assertEqual(os.path.exists(settings.UPLOADS_PATH + '/%i/%s' % (user.id, d_filename)), True)

@override_uploads_path_with_temp_directory
def test_select_uploaded_files_to_describe(self):
# Create audio files
Expand Down
4 changes: 4 additions & 0 deletions accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1284,6 +1284,9 @@ def upload(request, no_flash=False):
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
submitted_files = request.FILES.getlist('files')
if not submitted_files:
#this allows upload_tests to work, requests use the dict key in singular
submitted_files = request.FILES.getlist('file')
duplicated_filenames = list()
for file_ in submitted_files:
#check for duplicated names and add an identifier, otherwise, different files with the same
Expand Down Expand Up @@ -1314,6 +1317,7 @@ def upload(request, no_flash=False):
'all_file_extensions': settings.ALLOWED_AUDIOFILE_EXTENSIONS,
'uploads_enabled': settings.UPLOAD_AND_DESCRIPTION_ENABLED
}
#print(tvars)
return render(request, 'accounts/upload.html', tvars)


Expand Down

0 comments on commit 3660c52

Please sign in to comment.