Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Fix missing licences test #53

Merged
merged 7 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ The `ApiClient` needs the `url` to the API root and a valid API `key` to access
You can also set the `CADS_API_URL` and `CADS_API_KEY` environment variables.

It is possible (but not recommended) to use the API key of one of the test users,
`00000000-0000-4000-a000-000000000000`. This is used in anonymous tests and
`00112233-4455-6677-c899-aabbccddeeff`. This is used in anonymous tests and
it is designed to be the least performant option to access the system.

Draft Python API:

```python
>>> import os
>>> cads_api_key = os.getenv("CADS_API_KEY", "00000000-0000-4000-a000-000000000000")
>>> cads_api_key = os.getenv("CADS_API_KEY", "00112233-4455-6677-c899-aabbccddeeff")

>>> import cads_api_client
>>> client = cads_api_client.ApiClient(cads_api_key)
Expand Down
13 changes: 1 addition & 12 deletions notebooks/cads_api_client_test.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,6 @@
"import cads_api_client"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "10798c3d",
"metadata": {},
"outputs": [],
"source": [
"api_url = os.getenv(\"CADS_API_ROOT_URL\", \"http://cds2-dev.copernicus-climate.eu/api\")\n",
"api_url"
]
},
{
"cell_type": "markdown",
"id": "622e6c14",
Expand All @@ -51,7 +40,7 @@
"metadata": {},
"outputs": [],
"source": [
"client = cads_api_client.ApiClient(url=api_url, key=\"00112233-4455-6677-c899-aabbccddeeff\")\n",
"client = cads_api_client.ApiClient(key=\"00112233-4455-6677-c899-aabbccddeeff\")\n",
"client"
]
},
Expand Down
40 changes: 3 additions & 37 deletions notebooks/cads_api_constraints_test.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,6 @@
"import cads_processing_api_service"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "b76bb911",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'http://cds2-dev.copernicus-climate.eu/api'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"api_url = os.getenv(\"CADS_API_URL\", \"http://cds2-dev.copernicus-climate.eu/api\")\n",
"api_url"
]
},
{
"cell_type": "markdown",
"id": "4336b457",
Expand All @@ -65,24 +43,12 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"id": "4916eb9b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"ApiClient(key='00112233-4455-6677-c899-aabbccddeeff', url='http://cds2-dev.copernicus-climate.eu/api')"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"client = cads_api_client.ApiClient(url=api_url, key=\"00112233-4455-6677-c899-aabbccddeeff\")\n",
"client"
"client = cads_api_client.ApiClient(key=\"00112233-4455-6677-c899-aabbccddeeff\")"
]
},
{
Expand Down
24 changes: 1 addition & 23 deletions notebooks/cads_api_filters_and_pagination_tests.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,14 @@
"## Client instantiation"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "a873e0ad",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'http://cds2-dev.copernicus-climate.eu/api'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"api_url = os.getenv(\"CADS_API_ROOT_URL\", \"http://cds2-dev.copernicus-climate.eu/api\")\n",
"api_url"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "5bad9658",
"metadata": {},
"outputs": [],
"source": [
"client = cads_api_client.ApiClient(url=api_url, key=\"00112233-4455-6677-c899-aabbccddeeff\")"
"client = cads_api_client.ApiClient(key=\"00112233-4455-6677-c899-aabbccddeeff\")"
]
},
{
Expand Down
15 changes: 1 addition & 14 deletions notebooks/cads_api_licences_tests.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,14 @@
"## Client instantiation"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a873e0ad",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"api_url = os.getenv(\"CADS_API_ROOT_URL\", \"http://cds2-dev.copernicus-climate.eu/api\")\n",
"api_url"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5bad9658",
"metadata": {},
"outputs": [],
"source": [
"client = cads_api_client.ApiClient(url=api_url, key=\"00000000-0000-4000-a000-000000000000\")\n",
"client = cads_api_client.ApiClient()\n",
"client"
]
},
Expand Down
45 changes: 5 additions & 40 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from __future__ import annotations

import os
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING

import pytest
import requests

if TYPE_CHECKING:
from cads_api_client import ApiClient
Expand All @@ -18,46 +17,12 @@ def api_root_url() -> str:


@pytest.fixture
def api_key(api_root_url: str) -> str:
if key := os.getenv("CADS_API_KEY"):
return key

# default to test user 1
key = "00000000-0000-4000-a000-000000000000"

# Accept all licences
result = requests.get(f"{api_root_url}/catalogue/v1/vocabularies/licences")
requests.patch(
f"{api_root_url}/profiles/v1/account/licences",
json=result.json(),
headers={"PRIVATE-TOKEN": key},
)
return key


@pytest.fixture
def api_key_anon() -> str:
return "00112233-4455-6677-c899-aabbccddeeff"


@pytest.fixture
def request_year() -> str:
return os.environ.get("CADS_TEST_YEAR", "2016")


@pytest.fixture()
def temp_environ() -> Any:
"""Create a modifiable environment that affect only the test scope."""
old_environ = dict(os.environ)

yield os.environ

os.environ.clear()
os.environ.update(old_environ)
def api_anon_key() -> str:
return os.getenv("CADS_API_ANON_KEY", "00112233-4455-6677-c899-aabbccddeeff")


@pytest.fixture
def api_client(api_root_url: str, api_key: str) -> ApiClient:
def api_anon_client(api_root_url: str, api_anon_key: str) -> ApiClient:
from cads_api_client import ApiClient

return ApiClient(url=api_root_url, key=api_key)
return ApiClient(url=api_root_url, key=api_anon_key)
27 changes: 7 additions & 20 deletions tests/integration_test_20_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,18 @@ def test_validate_constraints(api_root_url: str) -> None:
assert set(["product_type", "variable", "year", "month", "time"]) <= set(res)


def test_collection_missing_licence(
api_root_url: str, api_key_anon: str, request_year: str
) -> None:
def test_collection_anonymous_user(api_root_url: str, api_anon_key: str) -> None:
collection_id = "test-adaptor-mars"
headers = {"PRIVATE-TOKEN": api_key_anon}
headers = {"PRIVATE-TOKEN": api_anon_key}
proc = processing.Processing(f"{api_root_url}/retrieve", headers=headers)
process = proc.process(collection_id)
response = process.execute(inputs={})
assert "message" in response.json

with pytest.raises(RuntimeError, match="403 Client Error"):
_ = process.execute(
inputs=dict(
product_type="reanalysis",
variable="temperature",
year=request_year,
month="01",
day="01",
time="00:00",
level="1000",
),
)


def test_jobs_list(api_root_url: str, api_key: str, request_year: str) -> None:

def test_jobs_list(api_root_url: str, api_anon_key: str) -> None:
collection_id = "test-adaptor-dummy"
headers = {"PRIVATE-TOKEN": api_key}
headers = {"PRIVATE-TOKEN": api_anon_key}
proc = processing.Processing(f"{api_root_url}/retrieve", headers=headers)
process = proc.process(collection_id)

Expand Down
43 changes: 22 additions & 21 deletions tests/integration_test_30_remote.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import py
import pathlib

import pytest

from cads_api_client import catalogue, processing
Expand All @@ -14,9 +15,9 @@ def test_from_collection_to_process(api_root_url: str) -> None:
assert isinstance(res, processing.Process)


def test_collection_submit(api_root_url: str, api_key: str, request_year: str) -> None:
def test_collection_submit(api_root_url: str, api_anon_key: str) -> None:
collection_id = "test-adaptor-dummy"
headers = {"PRIVATE-TOKEN": api_key}
headers = {"PRIVATE-TOKEN": api_anon_key}

cat = catalogue.Catalogue(f"{api_root_url}/catalogue", headers=headers)
dataset = cat.collection(collection_id)
Expand All @@ -30,14 +31,14 @@ def test_collection_submit(api_root_url: str, api_key: str, request_year: str) -


def test_collection_retrieve_with_dummy_adaptor(
api_root_url: str, api_key: str, request_year: str, tmpdir: py.path.local
api_root_url: str, api_anon_key: str, tmp_path: pathlib.Path
) -> None:
collection_id = "test-adaptor-dummy"
headers = {"PRIVATE-TOKEN": api_key}
headers = {"PRIVATE-TOKEN": api_anon_key}

cat = catalogue.Catalogue(f"{api_root_url}/catalogue", headers=headers)
dataset = cat.collection(collection_id)
target = str(tmpdir.join("dummy.txt"))
target = str(tmp_path / "dummy.txt")

res = dataset.retrieve(
target=target,
Expand All @@ -49,14 +50,14 @@ def test_collection_retrieve_with_dummy_adaptor(


def test_collection_retrieve_with_url_cds_adaptor(
api_root_url: str, api_key: str, request_year: str, tmpdir: py.path.local
api_root_url: str, api_anon_key: str, tmp_path: pathlib.Path
) -> None:
collection_id = "test-adaptor-url"
headers = {"PRIVATE-TOKEN": api_key}
headers = {"PRIVATE-TOKEN": api_anon_key}

cat = catalogue.Catalogue(f"{api_root_url}/catalogue", headers=headers)
dataset = cat.collection(collection_id)
target = str(tmpdir.join("wfde1.zip"))
target = str(tmp_path / "wfde1.zip")

res = dataset.retrieve(
variable="grid_point_altitude",
Expand All @@ -69,7 +70,7 @@ def test_collection_retrieve_with_url_cds_adaptor(
assert isinstance(res, str)
assert res.endswith(target)

target = str(tmpdir.join("wfde2.zip"))
target = str(tmp_path / "wfde2.zip")

res = dataset.retrieve(
variable="grid_point_altitude",
Expand All @@ -84,14 +85,14 @@ def test_collection_retrieve_with_url_cds_adaptor(


def test_collection_retrieve_with_direct_mars_cds_adaptor(
api_root_url: str, api_key: str, request_year: str, tmpdir: py.path.local
api_root_url: str, api_anon_key: str, tmp_path: pathlib.Path
) -> None:
collection_id = "test-adaptor-direct-mars"
headers = {"PRIVATE-TOKEN": api_key}
headers = {"PRIVATE-TOKEN": api_anon_key}

cat = catalogue.Catalogue(f"{api_root_url}/catalogue", headers=headers)
dataset = cat.collection(collection_id)
target = str(tmpdir.join("era5-complete.grib"))
target = str(tmp_path / "era5-complete.grib")

res = dataset.retrieve(
levelist="1",
Expand All @@ -112,19 +113,19 @@ def test_collection_retrieve_with_direct_mars_cds_adaptor(


def test_collection_retrieve_with_mars_cds_adaptor(
api_root_url: str, api_key: str, request_year: str, tmpdir: py.path.local
api_root_url: str, api_anon_key: str, tmp_path: pathlib.Path
) -> None:
collection_id = "test-adaptor-mars"
headers = {"PRIVATE-TOKEN": api_key}
headers = {"PRIVATE-TOKEN": api_anon_key}

cat = catalogue.Catalogue(f"{api_root_url}/catalogue", headers=headers)
dataset = cat.collection(collection_id)
target = str(tmpdir.join("era5.grib"))
target = str(tmp_path / "era5.grib")

res = dataset.retrieve(
product_type="reanalysis",
variable="2m_temperature",
year=request_year,
year="2016",
month="01",
day="02",
time="00:00",
Expand All @@ -138,19 +139,19 @@ def test_collection_retrieve_with_mars_cds_adaptor(

@pytest.mark.skip(reason="discontinued adaptor")
def test_collection_retrieve_with_legacy_cds_adaptor(
api_root_url: str, api_key: str, request_year: str, tmpdir: py.path.local
api_root_url: str, api_anon_key: str, tmp_path: pathlib.Path
) -> None:
collection_id = "test-adaptor-legacy"
headers = {"PRIVATE-TOKEN": api_key}
headers = {"PRIVATE-TOKEN": api_anon_key}

cat = catalogue.Catalogue(f"{api_root_url}/catalogue", headers=headers)
dataset = cat.collection(collection_id)
target = str(tmpdir.join("era5.grib"))
target = str(tmp_path / "era5.grib")

res = dataset.retrieve(
product_type="reanalysis",
variable="temperature",
year=request_year,
year="2016",
month="01",
day="02",
time="00:00",
Expand Down
Loading