diff --git a/document_merge_service/api/data/2023.test.test.docx-template.docx b/document_merge_service/api/data/2023.test.test.docx-template.docx new file mode 100644 index 00000000..aa15e622 Binary files /dev/null and b/document_merge_service/api/data/2023.test.test.docx-template.docx differ diff --git a/document_merge_service/api/tests/test_convert.py b/document_merge_service/api/tests/test_convert.py index 11662f48..de4d012a 100644 --- a/document_merge_service/api/tests/test_convert.py +++ b/document_merge_service/api/tests/test_convert.py @@ -6,20 +6,35 @@ @pytest.mark.parametrize( - "target_format,response_content_type", + "filename,target_filename,target_format,response_content_type", [ - ("pdf", "application/pdf"), + ( + "docx-template.docx", + "docx-template.pdf", + "pdf", + "application/pdf", + ), + ( + "2023.test.test.docx-template.docx", + "2023.test.test.docx-template.pdf", + "pdf", + "application/pdf", + ), ], ) -def test_convert(db, client, target_format, response_content_type): +def test_convert( + db, client, filename, target_filename, target_format, response_content_type +): url = reverse("convert") - file_to_convert = django_file("docx-template.docx") + file_to_convert = django_file(filename) data = {"file": file_to_convert.file, "target_format": target_format} response = client.post(url, data=data, format="multipart") assert response.status_code == status.HTTP_200_OK assert response.headers.get("Content-Type") == response_content_type + print("response", response, flush=True) + assert response.headers.get("Content-Disposition") == f'attachment; filename="{target_filename}"' def test_incorrect_file_type(db, client): diff --git a/document_merge_service/api/views.py b/document_merge_service/api/views.py index ec2eda67..2c3720cb 100644 --- a/document_merge_service/api/views.py +++ b/document_merge_service/api/views.py @@ -1,4 +1,5 @@ import mimetypes +from os.path import splitext import jinja2 from django.http import HttpResponse @@ -102,6 +103,7 @@ def post(self, request, **kwargs): response = FileConverter.convert(file.read(), target_format) - filename = f"{file.name.split('.')[0]}.{target_format}" + filename = f"{splitext(file.name)[0]}.{target_format}" response["Content-Disposition"] = f'attachment; filename="{filename}"' + print("response", response, flush=True) return response