|
6 | 6 | import flask |
7 | 7 | import shutil |
8 | 8 | import uuid |
| 9 | +import glob |
| 10 | +import zipfile |
| 11 | +import io |
9 | 12 |
|
10 | 13 | # Local application imports |
11 | 14 | from opengeodeweb_microservice.database.data import Data |
@@ -206,3 +209,45 @@ def test_generate_native_viewable_and_light_viewable_from_file(client): |
206 | 209 | assert isinstance(result["object_type"], str) |
207 | 210 | assert isinstance(result["binary_light_viewable"], str) |
208 | 211 | assert isinstance(result["input_file"], str) |
| 212 | + |
| 213 | + |
| 214 | +def test_send_file_multiple_returns_zip(client, tmp_path): |
| 215 | + app = client.application |
| 216 | + with app.app_context(): |
| 217 | + app.config["UPLOAD_FOLDER"] = str(tmp_path) |
| 218 | + file_paths = [] |
| 219 | + for i, content in [(1, b"hello 1"), (2, b"hello 2")]: |
| 220 | + file_path = tmp_path / f"tmp_send_file_{i}.txt" |
| 221 | + file_path.write_bytes(content) |
| 222 | + file_paths.append(str(file_path)) |
| 223 | + with app.test_request_context(): |
| 224 | + response = utils_functions.send_file(app.config["UPLOAD_FOLDER"], file_paths, "bundle") |
| 225 | + assert response.status_code == 200 |
| 226 | + assert response.mimetype == "application/zip" |
| 227 | + assert response.headers.get("new-file-name") == "bundle.zip" |
| 228 | + |
| 229 | + response.direct_passthrough = False |
| 230 | + zip_bytes = response.get_data() |
| 231 | + with zipfile.ZipFile(io.BytesIO(zip_bytes), "r") as zip_file: |
| 232 | + zip_entries = zip_file.namelist() |
| 233 | + assert "tmp_send_file_1.txt" in zip_entries |
| 234 | + assert "tmp_send_file_2.txt" in zip_entries |
| 235 | + response.close() |
| 236 | + |
| 237 | + |
| 238 | +def test_send_file_single_returns_octet_binary(client, tmp_path): |
| 239 | + app = client.application |
| 240 | + with app.app_context(): |
| 241 | + app.config["UPLOAD_FOLDER"] = str(tmp_path) |
| 242 | + file_path = tmp_path / "tmp_send_file_1.txt" |
| 243 | + file_path.write_bytes(b"hello 1") |
| 244 | + with app.test_request_context(): |
| 245 | + response = utils_functions.send_file(app.config["UPLOAD_FOLDER"], [str(file_path)], "tmp_send_file_1.txt") |
| 246 | + assert response.status_code == 200 |
| 247 | + assert response.mimetype == "application/octet-binary" |
| 248 | + assert response.headers.get("new-file-name") == "tmp_send_file_1.txt" |
| 249 | + |
| 250 | + response.direct_passthrough = False |
| 251 | + file_bytes = response.get_data() |
| 252 | + assert file_bytes == b"hello 1" |
| 253 | + response.close() |
0 commit comments