|
| 1 | +import io |
| 2 | +import os |
| 3 | +import zipfile |
| 4 | + |
| 5 | +from django.core.files.uploadedfile import SimpleUploadedFile |
| 6 | +from django.http import FileResponse, HttpResponseNotFound |
| 7 | +from django.test import TestCase |
| 8 | + |
| 9 | +from cohiva.views.generic import UploadedFileProcessorMixin |
| 10 | + |
| 11 | + |
| 12 | +class UploadedFileProcessorMixinTestCase(TestCase): |
| 13 | + def test_no_input_files(self): |
| 14 | + cls = UploadedFileProcessorMixin() |
| 15 | + input_files = [f for f in cls.get_input_files()] |
| 16 | + self.assertEqual(len(input_files), 0) |
| 17 | + |
| 18 | + def test_no_output_files(self): |
| 19 | + cls = UploadedFileProcessorMixin() |
| 20 | + response = cls.get_response() |
| 21 | + self.assertTrue(isinstance(response, HttpResponseNotFound)) |
| 22 | + |
| 23 | + def test_single_file(self): |
| 24 | + test_upload = SimpleUploadedFile( |
| 25 | + "test.txt", |
| 26 | + b"Testing", |
| 27 | + content_type="text/plain", |
| 28 | + ) |
| 29 | + cls = UploadedFileProcessorMixin() |
| 30 | + cls.set_uploaded_file(test_upload) |
| 31 | + input_files = [f for f in cls.get_input_files()] |
| 32 | + self.assertEqual(len(input_files), 1) |
| 33 | + self.assertEqual(input_files[0], test_upload) |
| 34 | + |
| 35 | + response_file = "/tmp/response.pdf" |
| 36 | + response_content = b"%PDF-1.4\n%Dummy PDF" |
| 37 | + with open(response_file, "wb") as f: |
| 38 | + f.write(response_content) |
| 39 | + cls.add_output_file(response_file, "test.pdf", "application/pdf") |
| 40 | + response = cls.get_response() |
| 41 | + self.assertTrue(isinstance(response, FileResponse)) |
| 42 | + self.assertEqual(response.headers["Content-Type"], "application/pdf") |
| 43 | + self.assertEqual( |
| 44 | + response.headers["Content-Disposition"], 'attachment; filename="test.pdf"' |
| 45 | + ) |
| 46 | + self.assertEqual(response.getvalue(), response_content) |
| 47 | + response.close() |
| 48 | + # Make sure the file was not deleted |
| 49 | + self.assertTrue(os.path.isfile(response_file)) |
| 50 | + os.unlink(response_file) |
| 51 | + |
| 52 | + def test_multiple_files(self): |
| 53 | + file_like_object = io.BytesIO() |
| 54 | + with zipfile.ZipFile(file_like_object, "w") as archive: |
| 55 | + archive.writestr("testA.txt", b"Testing A") |
| 56 | + archive.writestr("testB.txt", b"Testing B") |
| 57 | + file_like_object.seek(0) |
| 58 | + test_upload = SimpleUploadedFile( |
| 59 | + "test_files.zip", |
| 60 | + file_like_object.getvalue(), |
| 61 | + content_type="application/zip", |
| 62 | + ) |
| 63 | + cls = UploadedFileProcessorMixin() |
| 64 | + cls.set_uploaded_file(test_upload) |
| 65 | + input_file = [f for f in cls.get_input_files()] |
| 66 | + self.assertEqual(len(input_file), 2) |
| 67 | + |
| 68 | + response = cls.get_response() |
| 69 | + self.assertTrue(isinstance(response, HttpResponseNotFound)) |
| 70 | + |
| 71 | + response_file_a = "/tmp/responseA.pdf" |
| 72 | + response_content_a = b"%PDF-1.4\n%Dummy PDF A" |
| 73 | + with open(response_file_a, "wb") as f: |
| 74 | + f.write(response_content_a) |
| 75 | + response_file_b = "/tmp/responseB.pdf" |
| 76 | + response_content_b = b"%PDF-1.4\n%Dummy PDF B" |
| 77 | + with open(response_file_b, "wb") as f: |
| 78 | + f.write(response_content_b) |
| 79 | + cls.add_output_file(response_file_a, "testA.pdf", "application/pdf") |
| 80 | + cls.add_output_file(response_file_b, "testB.pdf", "application/pdf") |
| 81 | + response = cls.get_response() |
| 82 | + self.assertTrue(isinstance(response, FileResponse)) |
| 83 | + self.assertEqual(response.headers["Content-Type"], "application/zip") |
| 84 | + self.assertEqual( |
| 85 | + response.headers["Content-Disposition"], |
| 86 | + 'attachment; filename="test_files_resultat.zip"', |
| 87 | + ) |
| 88 | + with zipfile.ZipFile(io.BytesIO(response.getvalue())) as zip_file: |
| 89 | + self.assertEqual(zip_file.read("testA.pdf"), response_content_a) |
| 90 | + self.assertEqual(zip_file.read("testB.pdf"), response_content_b) |
| 91 | + # Make sure the files were not deleted |
| 92 | + self.assertTrue(os.path.isfile(response_file_a)) |
| 93 | + self.assertTrue(os.path.isfile(response_file_b)) |
| 94 | + os.unlink(response_file_a) |
| 95 | + os.unlink(response_file_b) |
| 96 | + |
| 97 | + def test_delete_after_single_file(self): |
| 98 | + cls = UploadedFileProcessorMixin() |
| 99 | + response_file = "/tmp/response.pdf" |
| 100 | + response_content = b"%PDF-1.4\n%Dummy PDF" |
| 101 | + with open(response_file, "wb") as f: |
| 102 | + f.write(response_content) |
| 103 | + cls.add_output_file(response_file, "test.pdf", "application/pdf", delete_after=True) |
| 104 | + response = cls.get_response() |
| 105 | + self.assertTrue(isinstance(response, FileResponse)) |
| 106 | + response.close() |
| 107 | + # Make sure the file was deleted |
| 108 | + self.assertFalse(os.path.isfile(response_file)) |
| 109 | + |
| 110 | + def test_delete_after_multiple_files(self): |
| 111 | + cls = UploadedFileProcessorMixin() |
| 112 | + response_file_a = "/tmp/responseA.pdf" |
| 113 | + response_content_a = b"%PDF-1.4\n%Dummy PDF A" |
| 114 | + with open(response_file_a, "wb") as f: |
| 115 | + f.write(response_content_a) |
| 116 | + response_file_b = "/tmp/responseB.pdf" |
| 117 | + response_content_b = b"%PDF-1.4\n%Dummy PDF B" |
| 118 | + with open(response_file_b, "wb") as f: |
| 119 | + f.write(response_content_b) |
| 120 | + cls.add_output_file(response_file_a, "testA.pdf", "application/pdf", delete_after=True) |
| 121 | + cls.add_output_file(response_file_b, "testB.pdf", "application/pdf", delete_after=True) |
| 122 | + response = cls.get_response() |
| 123 | + self.assertTrue(isinstance(response, FileResponse)) |
| 124 | + # Make sure the files were deleted |
| 125 | + self.assertFalse(os.path.isfile(response_file_a)) |
| 126 | + self.assertFalse(os.path.isfile(response_file_b)) |
| 127 | + |
| 128 | + def test_custom_archive_filename(self): |
| 129 | + cls = UploadedFileProcessorMixin() |
| 130 | + response_file_a = "/tmp/responseA.pdf" |
| 131 | + response_content_a = b"%PDF-1.4\n%Dummy PDF A" |
| 132 | + with open(response_file_a, "wb") as f: |
| 133 | + f.write(response_content_a) |
| 134 | + response_file_b = "/tmp/responseB.pdf" |
| 135 | + response_content_b = b"%PDF-1.4\n%Dummy PDF B" |
| 136 | + with open(response_file_b, "wb") as f: |
| 137 | + f.write(response_content_b) |
| 138 | + cls.add_output_file(response_file_a, "testA.pdf", "application/pdf") |
| 139 | + cls.add_output_file(response_file_b, "testB.pdf", "application/pdf") |
| 140 | + response = cls.get_response(archive_file_name="custom.zip") |
| 141 | + self.assertEqual( |
| 142 | + response.headers["Content-Disposition"], 'attachment; filename="custom.zip"' |
| 143 | + ) |
| 144 | + os.unlink(response_file_a) |
| 145 | + os.unlink(response_file_b) |
| 146 | + |
| 147 | + def test_no_custom_archive_filename(self): |
| 148 | + cls = UploadedFileProcessorMixin() |
| 149 | + response_file_a = "/tmp/responseA.pdf" |
| 150 | + response_content_a = b"%PDF-1.4\n%Dummy PDF A" |
| 151 | + with open(response_file_a, "wb") as f: |
| 152 | + f.write(response_content_a) |
| 153 | + response_file_b = "/tmp/responseB.pdf" |
| 154 | + response_content_b = b"%PDF-1.4\n%Dummy PDF B" |
| 155 | + with open(response_file_b, "wb") as f: |
| 156 | + f.write(response_content_b) |
| 157 | + cls.add_output_file(response_file_a, "testA.pdf", "application/pdf") |
| 158 | + cls.add_output_file(response_file_b, "testB.pdf", "application/pdf") |
| 159 | + response = cls.get_response() |
| 160 | + self.assertEqual( |
| 161 | + response.headers["Content-Disposition"], 'attachment; filename="resultat.zip"' |
| 162 | + ) |
| 163 | + os.unlink(response_file_a) |
| 164 | + os.unlink(response_file_b) |
| 165 | + |
| 166 | + def test_custom_archive_filename_single_file(self): |
| 167 | + cls = UploadedFileProcessorMixin() |
| 168 | + response_file_a = "/tmp/responseA.pdf" |
| 169 | + response_content_a = b"%PDF-1.4\n%Dummy PDF A" |
| 170 | + with open(response_file_a, "wb") as f: |
| 171 | + f.write(response_content_a) |
| 172 | + cls.add_output_file(response_file_a, "testA.pdf", "application/pdf") |
| 173 | + response = cls.get_response(archive_file_name="custom.zip") |
| 174 | + self.assertEqual( |
| 175 | + response.headers["Content-Disposition"], 'attachment; filename="testA.pdf"' |
| 176 | + ) |
| 177 | + response.close() |
| 178 | + os.unlink(response_file_a) |
0 commit comments