|
1 | 1 | import pathlib |
| 2 | +import zipfile |
| 3 | +import io |
2 | 4 |
|
3 | 5 | from django.core.files.uploadedfile import UploadedFile |
4 | 6 | from rest_framework import status |
@@ -26,18 +28,29 @@ def test_map_file_upload_happy_path(client_user, file_map_desert, game_yuri, ext |
26 | 28 |
|
27 | 29 | uploaded_file_url: str = response.data["result"]["cnc_map_file"] |
28 | 30 | uploaded_image_url: str = response.data["result"]["extracted_preview_file"] |
| 31 | + |
| 32 | + # We need to strip the url path off of the files, |
| 33 | + # then check the tmp directory to make sure the uploaded files were saved |
29 | 34 | strip_media_url = f"/{settings.MEDIA_URL}" |
30 | | - uploaded_file = pathlib.Path(tmp_media_root) / uploaded_file_url.lstrip(strip_media_url) |
| 35 | + uploaded_zipped_file = pathlib.Path(tmp_media_root) / uploaded_file_url.lstrip(strip_media_url) |
31 | 36 | uploaded_image = pathlib.Path(tmp_media_root) / uploaded_image_url.lstrip(strip_media_url) |
32 | | - assert uploaded_file.exists() |
| 37 | + assert uploaded_zipped_file.exists() |
33 | 38 | assert uploaded_image.exists() |
34 | 39 |
|
| 40 | + # We need to unzip the map so that we can actually verify the saved map contents. |
| 41 | + map_filename = pathlib.Path(uploaded_file_url).name.replace(".zip", "") |
| 42 | + # Extract the map from the zipfile, and convert to something that the map parser understands |
| 43 | + _unzip_io = io.BytesIO() |
| 44 | + _unzip_io.write(zipfile.ZipFile(uploaded_zipped_file).read(map_filename)) |
| 45 | + _unzip_io.seek(0) |
| 46 | + uploaded_file = UploadedFile(_unzip_io) |
| 47 | + |
35 | 48 | file_response = client_user.get(uploaded_file_url) |
36 | 49 | image_response = client_user.get(uploaded_image_url) |
37 | 50 | assert file_response.status_code == status.HTTP_200_OK |
38 | 51 | assert image_response.status_code == status.HTTP_200_OK |
39 | 52 |
|
40 | | - parser = CncGen2MapParser(UploadedFile(open(uploaded_file, "rb"))) |
| 53 | + parser = CncGen2MapParser(uploaded_file) |
41 | 54 | assert parser.ini.get("CnCNet", "ID") == str(response.data["result"]["cnc_map_id"]) |
42 | 55 |
|
43 | 56 | map_object = CncMap.objects.get(id=response.data["result"]["cnc_map_id"]) |
|
0 commit comments