from unittest.mock import MagicMock, patch import pytest from app.tasks.upload_to_nextcloud import upload_to_nextcloud @pytest.fixture def mock_settings(tmp_path): with patch("app.tasks.upload_to_nextcloud.settings") as mock: mock.nextcloud_upload_url = "http://nextcloud.local/" mock.nextcloud_username = "testuser" mock.nextcloud_password = "testpassword" mock.nextcloud_folder = "uploads" mock.workdir = str(tmp_path) mock.http_request_timeout = 30 yield mock @pytest.fixture def mock_requests(): with patch("app.tasks.upload_to_nextcloud.requests") as mock: # Mock PROPFIND to always return false (file doesn't exist) mock.request.return_value = MagicMock(text="") # Mock PUT to return success put_response = MagicMock() put_response.status_code = 201 mock.put.return_value = put_response yield mock def test_upload_to_nextcloud_url_construction(tmp_path, mock_settings, mock_requests): file_path = str(tmp_path / "test_file.txt") # Create dummy file with open(file_path, "w") as f: f.write("test content") # Call the task directly with patch("celery.app.task.Task.request", new_callable=MagicMock) as mock_req: mock_req.id = "test-task-123" result = upload_to_nextcloud(file_path) assert result["status"] == "Completed" assert result["nextcloud_path"] == "uploads/test_file.txt" # Verify requests.put was called with the correct URL mock_requests.put.assert_called_once() args, kwargs = mock_requests.put.call_args url = args[0] assert url == "http://nextcloud.local/uploads/test_file.txt"