|
| 1 | +from unittest.mock import MagicMock, patch |
| 2 | + |
| 3 | +import pytest |
| 4 | +import requests |
| 5 | + |
| 6 | +from app.platforms.implementations.openeo import OpenEOPlatform |
| 7 | +from app.schemas import ProcessingStatusEnum, ServiceDetails |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture |
| 11 | +def platform(): |
| 12 | + return OpenEOPlatform() |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture(autouse=True) |
| 16 | +def mock_env(monkeypatch): |
| 17 | + # Default environment variable for CDSEFED credentials |
| 18 | + monkeypatch.setenv( |
| 19 | + "OPENEO_AUTH_CLIENT_CREDENTIALS_CDSEFED", |
| 20 | + "provider123/client123/secret123" |
| 21 | + ) |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def service_details(): |
| 26 | + return ServiceDetails( |
| 27 | + service="https://openeo.dataspace.copernicus.eu", |
| 28 | + service_id="service-1", |
| 29 | + application="https://example.com/process.json" |
| 30 | + ) |
| 31 | + |
| 32 | + |
| 33 | +def test_get_client_credentials_success(platform): |
| 34 | + creds = platform._get_client_credentials("https://openeo.dataspace.copernicus.eu") |
| 35 | + assert creds == ("provider123", "client123", "secret123") |
| 36 | + |
| 37 | + |
| 38 | +def test_get_client_credentials_missing_env(platform, monkeypatch): |
| 39 | + monkeypatch.delenv("OPENEO_AUTH_CLIENT_CREDENTIALS_CDSEFED") |
| 40 | + with pytest.raises(ValueError, match="not set"): |
| 41 | + platform._get_client_credentials("https://openeo.dataspace.copernicus.eu") |
| 42 | + |
| 43 | + |
| 44 | +def test_get_client_credentials_invalid_format(platform, monkeypatch): |
| 45 | + monkeypatch.setenv("OPENEO_AUTH_CLIENT_CREDENTIALS_CDSEFED", "invalid_format") |
| 46 | + with pytest.raises(ValueError, match="Invalid client credentials format"): |
| 47 | + platform._get_client_credentials("https://openeo.dataspace.copernicus.eu") |
| 48 | + |
| 49 | + |
| 50 | +def test_get_client_credentials_env_var_success(platform): |
| 51 | + env_var = platform._get_client_credentials_env_var("https://openeo.dataspace.copernicus.eu") |
| 52 | + assert env_var == "OPENEO_AUTH_CLIENT_CREDENTIALS_CDSEFED" |
| 53 | + |
| 54 | + |
| 55 | +def test_get_client_credentials_env_var_unsupported_backend(platform): |
| 56 | + with pytest.raises(ValueError, match="Unsupported backend"): |
| 57 | + platform._get_client_credentials_env_var("https://unsupported.example.com") |
| 58 | + |
| 59 | + |
| 60 | +@patch("app.platforms.implementations.openeo.requests.get") |
| 61 | +def test_get_process_id_success(mock_get, platform): |
| 62 | + mock_get.return_value.json.return_value = {"id": "process123"} |
| 63 | + mock_get.return_value.raise_for_status.return_value = None |
| 64 | + |
| 65 | + process_id = platform._get_process_id("https://example.com/process.json") |
| 66 | + assert process_id == "process123" |
| 67 | + |
| 68 | + |
| 69 | +@patch("app.platforms.implementations.openeo.requests.get") |
| 70 | +def test_get_process_id_no_id(mock_get, platform): |
| 71 | + mock_get.return_value.json.return_value = {} |
| 72 | + mock_get.return_value.raise_for_status.return_value = None |
| 73 | + |
| 74 | + with pytest.raises(ValueError, match="No 'id' field"): |
| 75 | + platform._get_process_id("https://example.com/process.json") |
| 76 | + |
| 77 | + |
| 78 | +@patch("app.platforms.implementations.openeo.requests.get") |
| 79 | +def test_get_process_id_http_error(mock_get, platform): |
| 80 | + mock_get.side_effect = requests.RequestException("Network error") |
| 81 | + with pytest.raises(ValueError, match="Failed to fetch process ID"): |
| 82 | + platform._get_process_id("https://example.com/process.json") |
| 83 | + |
| 84 | + |
| 85 | +@patch("app.platforms.implementations.openeo.openeo.connect") |
| 86 | +@patch.object(OpenEOPlatform, "_get_process_id", return_value="process123") |
| 87 | +def test_execute_job_success(mock_pid, mock_connect, platform, service_details): |
| 88 | + mock_connection = MagicMock() |
| 89 | + mock_connect.return_value = mock_connection |
| 90 | + mock_connection.datacube_from_process.return_value.create_job.return_value.job_id = "job123" |
| 91 | + |
| 92 | + summary = platform.execute_job( |
| 93 | + title="Test Job", |
| 94 | + details=service_details, |
| 95 | + parameters={"param1": "value1"} |
| 96 | + ) |
| 97 | + |
| 98 | + assert summary.id == "job123" |
| 99 | + assert summary.status == ProcessingStatusEnum.CREATED |
| 100 | + mock_connect.assert_called_once_with(service_details.service) |
| 101 | + |
| 102 | + |
| 103 | +@patch("app.platforms.implementations.openeo.openeo.connect") |
| 104 | +@patch.object(OpenEOPlatform, "_get_process_id", side_effect=ValueError("Invalid process")) |
| 105 | +def test_execute_job_process_id_failure(mock_pid, mock_connect, platform, service_details): |
| 106 | + with pytest.raises(SystemError, match="Failed to execute openEO job"): |
| 107 | + platform.execute_job( |
| 108 | + title="Test Job", |
| 109 | + details=service_details, |
| 110 | + parameters={} |
| 111 | + ) |
0 commit comments