fix(file-manager): repair uploads and shared SFTP access
Build and publish Docker image / build-and-publish (push) Successful in 12s
Build and publish Docker image / build-and-publish (push) Successful in 12s
This commit is contained in:
1 parent
4c2f5f2c01
commit
2fcd0c3ef9
6 files changed
+275
-9
No files matched your search
@@ -0,0 +1,118 @@
|
||||
import asyncio
|
||||
import base64
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import websocket_service
|
||||
|
||||
|
||||
class ChunkedUploadTests(unittest.TestCase):
|
||||
def _upload(
|
||||
self,
|
||||
content: bytes,
|
||||
write_success: bool = True,
|
||||
chunk_data: str | None = None,
|
||||
) -> tuple[list[dict], bytes]:
|
||||
client_id = "upload-test-client"
|
||||
server_id = "upload-test-server"
|
||||
file_path = "/data/upload-check.bin"
|
||||
captured_content = b""
|
||||
messages: list[dict] = []
|
||||
|
||||
async def capture_message(_client_id: str, message: dict) -> None:
|
||||
messages.append(message)
|
||||
|
||||
def capture_upload(_namespace: str, _pod: str, _path: str, local_path: str, **kwargs: str) -> bool:
|
||||
nonlocal captured_content
|
||||
self.assertEqual(kwargs.get("container_name"), server_id)
|
||||
with open(local_path, "rb") as uploaded_file:
|
||||
captured_content = uploaded_file.read()
|
||||
return write_success
|
||||
|
||||
with tempfile.TemporaryDirectory() as upload_dir:
|
||||
old_upload_dir = websocket_service.CHUNK_UPLOAD_DIR
|
||||
websocket_service.CHUNK_UPLOAD_DIR = upload_dir
|
||||
websocket_service.chunked_uploads.clear()
|
||||
websocket_service.connected_clients[client_id] = {
|
||||
"authenticated": True,
|
||||
"user_id": "upload-test-user",
|
||||
}
|
||||
|
||||
try:
|
||||
with (
|
||||
patch.object(websocket_service, "send_message", new=AsyncMock(side_effect=capture_message)),
|
||||
patch.object(websocket_service, "validate_path_for_server", side_effect=lambda _server, path: path),
|
||||
patch.object(
|
||||
websocket_service,
|
||||
"get_server_access_and_namespace",
|
||||
return_value=("upload-test-namespace", {}),
|
||||
),
|
||||
patch.object(websocket_service.k8s_manager, "get_pods_for_server", return_value=["upload-test-pod"]),
|
||||
patch.object(
|
||||
websocket_service.k8s_manager,
|
||||
"write_file_binary_from_disk",
|
||||
side_effect=capture_upload,
|
||||
) as write_file,
|
||||
):
|
||||
asyncio.run(
|
||||
websocket_service.handle_start_chunked_upload(
|
||||
client_id,
|
||||
{
|
||||
"serverId": server_id,
|
||||
"filePath": file_path,
|
||||
"totalChunks": 1,
|
||||
"totalSize": len(content),
|
||||
},
|
||||
)
|
||||
)
|
||||
asyncio.run(
|
||||
websocket_service.handle_upload_file_chunk(
|
||||
client_id,
|
||||
{
|
||||
"serverId": server_id,
|
||||
"filePath": file_path,
|
||||
"chunkIndex": 0,
|
||||
"chunkData": chunk_data if chunk_data is not None else base64.b64encode(content).decode("ascii"),
|
||||
"totalChunks": 1,
|
||||
},
|
||||
)
|
||||
)
|
||||
if write_success and chunk_data is None:
|
||||
write_file.assert_called_once()
|
||||
elif chunk_data is not None:
|
||||
write_file.assert_not_called()
|
||||
finally:
|
||||
websocket_service.connected_clients.pop(client_id, None)
|
||||
websocket_service.chunked_uploads.clear()
|
||||
websocket_service.CHUNK_UPLOAD_DIR = old_upload_dir
|
||||
|
||||
return messages, captured_content
|
||||
|
||||
def test_upload_writes_regular_file_to_server_container(self) -> None:
|
||||
content = b"binary upload test\x00\xff"
|
||||
messages, uploaded = self._upload(content)
|
||||
|
||||
self.assertEqual(uploaded, content)
|
||||
self.assertEqual(messages[-1]["type"], "file_uploaded")
|
||||
|
||||
def test_upload_creates_empty_file(self) -> None:
|
||||
messages, uploaded = self._upload(b"")
|
||||
|
||||
self.assertEqual(uploaded, b"")
|
||||
self.assertEqual(messages[-1]["type"], "file_uploaded")
|
||||
|
||||
def test_upload_reports_storage_failure(self) -> None:
|
||||
messages, _uploaded = self._upload(b"payload", write_success=False)
|
||||
|
||||
self.assertEqual(messages[-1]["type"], "upload_error")
|
||||
self.assertEqual(messages[-1]["error"], "Failed to upload file")
|
||||
|
||||
def test_upload_rejects_invalid_base64(self) -> None:
|
||||
messages, _uploaded = self._upload(b"payload", chunk_data="not-base64!")
|
||||
|
||||
self.assertEqual(messages[-1]["type"], "upload_error")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user