def test_load_collection_arguments_100(requests_mock): requests_mock.get(API_URL, json={"api_version": "1.0.0"}) conn = Connection(API_URL) requests_mock.get(API_URL + "collections/FOO", json={ "summaries": { "eo:bands": [{ "name": "red" }, { "name": "green" }, { "name": "blue" }] } }) spatial_extent = {"west": 1, "south": 2, "east": 3, "north": 4} temporal_extent = ["2019-01-01", "2019-01-22"] im = conn.load_collection("FOO", spatial_extent=spatial_extent, temporal_extent=temporal_extent, bands=["red", "green"]) assert im._pg.process_id == "load_collection" assert im._pg.arguments == { "id": "FOO", "spatial_extent": spatial_extent, "temporal_extent": temporal_extent, "bands": ["red", "green"] }
def test_authenticate_oidc_100_single_implicit(requests_mock): requests_mock.get(API_URL, json={"api_version": "1.0.0"}) client_id = "myclient" requests_mock.get(API_URL + 'credentials/oidc', json={ "providers": [{ "id": "fauth", "issuer": "https://fauth.test", "title": "Foo Auth", "scopes": ["openid", "im"] }] }) oidc_mock = OidcMock( requests_mock=requests_mock, expected_grant_type="authorization_code", expected_client_id=client_id, expected_fields={"scope": "im openid"}, oidc_discovery_url= "https://fauth.test/.well-known/openid-configuration", scopes_supported=["openid", "im"], ) # With all this set up, kick off the openid connect flow conn = Connection(API_URL) assert isinstance(conn.auth, NullAuth) conn.authenticate_OIDC(client_id=client_id, webbrowser_open=oidc_mock.webbrowser_open) assert isinstance(conn.auth, BearerAuth) assert conn.auth.bearer == 'oidc/fauth/' + oidc_mock.state["access_token"]
def test_create_public_udp(requests_mock): requests_mock.get(API_URL, json={"api_version": "1.0.0"}) requests_mock.get(API_URL + "processes", json={"processes": [{ "id": "add" }]}) conn = Connection(API_URL) new_udp = load_json_resource("data/1.0.0/udp_details.json") def check_body(request): body = request.json() assert body['process_graph'] == new_udp['process_graph'] assert body['parameters'] == new_udp['parameters'] assert body['public'] return True adapter = requests_mock.put(API_URL + "process_graphs/evi", additional_matcher=check_body) conn.save_user_defined_process(user_defined_process_id='evi', process_graph=new_udp['process_graph'], parameters=new_udp['parameters'], public=True) assert adapter.called
def test_get_udp(requests_mock): requests_mock.get(API_URL, json={"api_version": "1.0.0"}) conn = Connection(API_URL) udp = conn.user_defined_process('evi') assert udp.user_defined_process_id == 'evi'
def test_capabilities_caching(requests_mock): m = requests_mock.get("https://oeo.test/", json={"api_version": "1.0.0"}) con = Connection(API_URL) assert con.capabilities().api_version() == "1.0.0" assert m.call_count == 1 assert con.capabilities().api_version() == "1.0.0" assert m.call_count == 1
def test_authenticate_oidc_100_multiple_wrong_id(requests_mock): requests_mock.get(API_URL, json={"api_version": "1.0.0"}) client_id = "myclient" requests_mock.get(API_URL + 'credentials/oidc', json={ "providers": [ { "id": "fauth", "issuer": "https://fauth.test", "title": "Foo Auth", "scopes": ["openid", "w"] }, { "id": "bauth", "issuer": "https://bauth.test", "title": "Bar Auth", "scopes": ["openid", "w"] }, ] }) # With all this set up, kick off the openid connect flow conn = Connection(API_URL) assert isinstance(conn.auth, NullAuth) match = r"'lol' not available\. Should be one of \[('fauth', 'bauth'|'bauth', 'fauth')\]\." with pytest.raises(OpenEoClientException, match=match): conn.authenticate_OIDC(client_id=client_id, provider_id="lol", webbrowser_open=pytest.fail)
def test_load_collection_arguments_040(requests_mock): requests_mock.get(API_URL, json={"api_version": "0.4.0"}) conn = Connection(API_URL) requests_mock.get(API_URL + "collections/FOO", json={ "properties": { "eo:bands": [{ "name": "red" }, { "name": "green" }, { "name": "blue" }] } }) spatial_extent = {"west": 1, "south": 2, "east": 3, "north": 4} temporal_extent = ["2019-01-01", "2019-01-22"] im = conn.load_collection("FOO", spatial_extent=spatial_extent, temporal_extent=temporal_extent, bands=["red", "green"]) node = im.graph[im.node_id] assert node["process_id"] == "load_collection" assert node["arguments"] == { "id": "FOO", "spatial_extent": spatial_extent, "temporal_extent": temporal_extent, "bands": ["red", "green"] }
def test_execute_100(requests_mock): requests_mock.get(API_URL, json={"api_version": "1.0.0"}) conn = Connection(API_URL) with mock.patch.object(conn, "request") as request: conn.execute({"foo1": {"process_id": "foo"}}) assert request.call_args_list == [ mock.call("post", path="/result", json={"process": {"process_graph": {"foo1": {"process_id": "foo"}}}}) ]
def test_list_file_formats(requests_mock): requests_mock.get(API_URL, json={"api_version": "1.0.0"}) conn = Connection(API_URL) file_formats = { "input": {"GeoJSON": {"gis_data_type": ["vector"]}}, "output": {"GTiff": {"gis_data_types": ["raster"]}}, } requests_mock.get(API_URL + "file_formats", json=file_formats) assert conn.list_file_formats() == file_formats
def test_connection_with_session(): session = mock.Mock() response = session.request.return_value response.status_code = 200 response.json.return_value = {"foo": "bar", "api_version": "0.4.0"} conn = Connection("https://oeo.net/", session=session) assert conn.capabilities().capabilities["foo"] == "bar" session.request.assert_any_call( url="https://oeo.net/", method="get", headers=mock.ANY, stream=mock.ANY, auth=mock.ANY, timeout=None )
def test_list_udps(requests_mock): requests_mock.get(API_URL, json={"api_version": "1.0.0"}) conn = Connection(API_URL) udp = load_json_resource("data/1.0.0/udp_details.json") requests_mock.get(API_URL + "process_graphs", json={'processes': [udp]}) user_udps = conn.list_user_defined_processes() assert len(user_udps) == 1 assert user_udps[0] == udp
def test_authenticate_oidc_100_single_wrong_id(requests_mock): requests_mock.get(API_URL, json={"api_version": "1.0.0"}) client_id = "myclient" requests_mock.get(API_URL + 'credentials/oidc', json={ "providers": [{"id": "foidc", "issuer": "https://auth.foidc.net", "title": "FOIDC", "scopes": ["openid", "w"]}] }) # With all this set up, kick off the openid connect flow conn = Connection(API_URL) assert isinstance(conn.auth, NullAuth) with pytest.raises(OpenEoClientException, match=r"'nopenope' not available\. Should be one of \['foidc'\]\."): conn.authenticate_OIDC(client_id=client_id, provider_id="nopenope", webbrowser_open=pytest.fail)
def test_api_error_non_json(requests_mock): requests_mock.get('https://oeo.net/', json={"api_version": "0.4.0"}) conn = Connection(API_URL) requests_mock.get('https://oeo.net/collections/foobar', status_code=500, text="olapola") with pytest.raises(OpenEoApiError) as exc_info: conn.describe_collection("foobar") exc = exc_info.value assert exc.http_status_code == 500 assert exc.code == "unknown" assert exc.message == "olapola" assert exc.id is None assert exc.url is None
def test_merge_cubes(con100: Connection): a = con100.load_collection("S2") b = con100.load_collection("MASK") c = a.merge(b) assert c.graph["mergecubes1"] == { "process_id": "merge_cubes", "arguments": { "cube1": {"from_node": "loadcollection1"}, "cube2": {"from_node": "loadcollection2"}, }, "result": True }
def test_mask_raster(con100: Connection): img = con100.load_collection("S2") mask = con100.load_collection("MASK") masked = img.mask(mask=mask, replacement=102) assert masked.graph["mask1"] == { "process_id": "mask", "arguments": { "data": {"from_node": "loadcollection1"}, "mask": {"from_node": "loadcollection2"}, "replacement": 102 }, "result": True }
def test_authenticate_basic(requests_mock): conn = Connection(API_URL) def text_callback(request, context): assert request.headers["Authorization"] == "Basic am9objpqMGhu" return '{"access_token":"w3lc0m3"}' requests_mock.get('https://oeo.net/credentials/basic', text=text_callback) assert isinstance(conn.auth, NullAuth) conn.authenticate_basic(username="******", password="******") assert isinstance(conn.auth, BearerAuth) assert conn.auth.bearer == "w3lc0m3"
def test_resample_spatial(con100: Connection): data = con100.load_collection("S2") target = con100.load_collection("MASK") im = data.resample_cube_spatial(target,method='spline') print(im.graph) assert im.graph["resamplecubespatial1"] == { 'arguments': { 'data': {'from_node': 'loadcollection1'}, 'method': 'spline', 'target': {'from_node': 'loadcollection2'} }, 'process_id': 'resample_cube_spatial', 'result': True}
def test_authenticate_oidc(oidc_test_setup): # see test/rest/conftest.py for `oidc_test_setup` fixture client_id = "myclient" oidc_discovery_url = "https://oeo.net/credentials/oidc" state, webbrowser_open = oidc_test_setup( client_id=client_id, oidc_discovery_url=oidc_discovery_url) # With all this set up, kick off the openid connect flow conn = Connection(API_URL) assert isinstance(conn.auth, NullAuth) conn.authenticate_OIDC(client_id=client_id, webbrowser_open=webbrowser_open) assert isinstance(conn.auth, BearerAuth) assert conn.auth.bearer == state["access_token"]
def test_api_error(requests_mock): requests_mock.get('https://oeo.net/', json={"api_version": "0.4.0"}) conn = Connection(API_URL) requests_mock.get('https://oeo.net/collections/foobar', status_code=404, json={ "code": "CollectionNotFound", "message": "No such things as a collection 'foobar'", "id": "54321" }) with pytest.raises(OpenEoApiError) as exc_info: conn.describe_collection("foobar") exc = exc_info.value assert exc.http_status_code == 404 assert exc.code == "CollectionNotFound" assert exc.message == "No such things as a collection 'foobar'" assert exc.id == "54321" assert exc.url is None
def test_authenticate_oidc_100_multiple_no_id(requests_mock): requests_mock.get(API_URL, json={"api_version": "1.0.0"}) client_id = "myclient" requests_mock.get(API_URL + 'credentials/oidc', json={ "providers": [ {"id": "foidc", "issuer": "https://auth.foidc.net", "title": "FOIDC", "scopes": ["openid", "w"]}, {"id": "baroi", "issuer": "https://acco.baroi.net", "title": "BarOI", "scopes": ["openid", "w"]}, ] }) # With all this set up, kick off the openid connect flow conn = Connection(API_URL) assert isinstance(conn.auth, NullAuth) match = r"No provider_id given. Available: \[('foidc', 'baroi'|'baroi', 'foidc')\]\." with pytest.raises(OpenEoClientException, match=match): conn.authenticate_OIDC(client_id=client_id, webbrowser_open=pytest.fail)
def test_filter_bbox_parameter(con100: Connection): expected = { "process_id": "filter_bbox", "arguments": { "data": {"from_node": "loadcollection1"}, "extent": {"from_parameter": "my_bbox"} }, "result": True } bbox_param = Parameter(name="my_bbox", schema={"type": "object"}) cube = con100.load_collection("S2").filter_bbox(bbox_param) assert _get_leaf_node(cube) == expected cube = con100.load_collection("S2").filter_bbox(bbox=bbox_param) assert _get_leaf_node(cube) == expected
def test_mask_polygon(con100: Connection): img = con100.load_collection("S2") polygon = shapely.geometry.box(0, 0, 1, 1) masked = img.mask_polygon(mask=polygon) assert sorted(masked.graph.keys()) == ["loadcollection1", "maskpolygon1"] assert masked.graph["maskpolygon1"] == { "process_id": "mask_polygon", "arguments": { "data": { "from_node": "loadcollection1" }, 'mask': { 'coordinates': (((1.0, 0.0), (1.0, 1.0), (0.0, 1.0), (0.0, 0.0), (1.0, 0.0)), ), 'crs': { 'properties': { 'name': 'EPSG:4326' }, 'type': 'name' }, 'type': 'Polygon' } }, "result": True }
def test_authenticate_oidc_040(requests_mock): client_id = "myclient" oidc_discovery_url = "https://oeo.test/credentials/oidc" oidc_mock = OidcMock(requests_mock=requests_mock, expected_grant_type="authorization_code", expected_client_id=client_id, expected_fields={"scope": "openid"}, oidc_discovery_url=oidc_discovery_url) requests_mock.get(API_URL, json={"api_version": "0.4.0"}) # With all this set up, kick off the openid connect flow conn = Connection(API_URL) assert isinstance(conn.auth, NullAuth) conn.authenticate_OIDC(client_id=client_id, webbrowser_open=oidc_mock.webbrowser_open) assert isinstance(conn.auth, BearerAuth) assert conn.auth.bearer == oidc_mock.state["access_token"]
def test_authenticate_basic(requests_mock, api_version): requests_mock.get(API_URL, json={"api_version": api_version}) conn = Connection(API_URL) def text_callback(request, context): assert request.headers["Authorization"] == "Basic am9objpqMGhu" return '{"access_token":"w3lc0m3"}' requests_mock.get(API_URL + 'credentials/basic', text=text_callback) assert isinstance(conn.auth, NullAuth) conn.authenticate_basic(username="******", password="******") assert isinstance(conn.auth, BearerAuth) if ComparableVersion(api_version).at_least("1.0.0"): assert conn.auth.bearer == "basic//w3lc0m3" else: assert conn.auth.bearer == "w3lc0m3"
def test_ndvi_simple(con100: Connection): ndvi = con100.load_collection("S2").ndvi() assert sorted(ndvi.graph.keys()) == ["loadcollection1", "ndvi1"] assert ndvi.graph["ndvi1"] == { "process_id": "ndvi", "arguments": {"data": {"from_node": "loadcollection1"}}, "result": True, }
def test_custom_process_arguments_datacube_chained(con100: Connection): res = con100.load_collection("S2").process(process_id="foo", arguments={ "data": THIS, "bar": 123 }) expected = load_json_resource('data/1.0.0/process_foo.json') assert res.graph == expected
def test_ndvi_args(con100: Connection): ndvi = con100.load_collection("S2").ndvi(nir="nirr", red="rred", target_band="ndvii") assert sorted(ndvi.graph.keys()) == ["loadcollection1", "ndvi1"] assert ndvi.graph["ndvi1"] == { "process_id": "ndvi", "arguments": {"data": {"from_node": "loadcollection1"}, "nir": "nirr", "red": "rred", "target_band": "ndvii"}, "result": True, }
def test_authenticate_oidc_resource_owner_password_credentials_client_from_config( requests_mock): requests_mock.get(API_URL, json={"api_version": "1.0.0"}) client_id = "myclient" client_secret = "$3cr3t" username, password = "******", "j0hn" issuer = "https://oidc.test" oidc_discovery_url = "https://oidc.test/.well-known/openid-configuration" requests_mock.get(API_URL + 'credentials/oidc', json={ "providers": [{ "id": "oi", "issuer": issuer, "title": "example", "scopes": ["openid"] }] }) oidc_mock = OidcMock( requests_mock=requests_mock, expected_grant_type="password", expected_client_id=client_id, expected_fields={ "username": username, "password": password, "scope": "openid", "client_secret": client_secret }, oidc_discovery_url=oidc_discovery_url, ) AuthConfig().set_oidc_client_config(backend=API_URL, provider_id="oi", client_id=client_id, client_secret=client_secret) # With all this set up, kick off the openid connect flow refresh_token_store = mock.Mock() conn = Connection(API_URL, refresh_token_store=refresh_token_store) assert isinstance(conn.auth, NullAuth) conn.authenticate_oidc_resource_owner_password_credentials( username=username, password=password) assert isinstance(conn.auth, BearerAuth) assert conn.auth.bearer == 'oidc/oi/' + oidc_mock.state["access_token"] assert refresh_token_store.mock_calls == []
def test_authenticate_basic_from_config(requests_mock, api_version): user, pwd = "john281", "J0hndo3" requests_mock.get(API_URL, json={"api_version": api_version}) def text_callback(request, context): assert request.headers[ "Authorization"] == requests.auth._basic_auth_str(username=user, password=pwd) return '{"access_token":"w3lc0m3"}' requests_mock.get(API_URL + 'credentials/basic', text=text_callback) AuthConfig().set_basic_auth(backend=API_URL, username=user, password=pwd) conn = Connection(API_URL) assert isinstance(conn.auth, NullAuth) conn.authenticate_basic() assert isinstance(conn.auth, BearerAuth) if ComparableVersion(api_version).at_least("1.0.0"): assert conn.auth.bearer == "basic//w3lc0m3" else: assert conn.auth.bearer == "w3lc0m3"
def test_authenticate_oidc_device_flow_client_from_config(requests_mock): requests_mock.get(API_URL, json={"api_version": "1.0.0"}) client_id = "myclient" client_secret = "$3cr3t" issuer = "https://oidc.test" oidc_discovery_url = "https://oidc.test/.well-known/openid-configuration" requests_mock.get(API_URL + 'credentials/oidc', json={ "providers": [{ "id": "oi", "issuer": issuer, "title": "example", "scopes": ["openid"] }] }) oidc_mock = OidcMock( requests_mock=requests_mock, expected_grant_type="urn:ietf:params:oauth:grant-type:device_code", expected_client_id=client_id, expected_fields={ "scope": "openid", "client_secret": client_secret }, oidc_discovery_url=oidc_discovery_url, ) AuthConfig().set_oidc_client_config(backend=API_URL, provider_id="oi", client_id=client_id, client_secret=client_secret) # With all this set up, kick off the openid connect flow refresh_token_store = mock.Mock() conn = Connection(API_URL, refresh_token_store=refresh_token_store) assert isinstance(conn.auth, NullAuth) oidc_mock.state["device_code_callback_timeline"] = ["great success"] conn.authenticate_oidc_device() assert isinstance(conn.auth, BearerAuth) assert conn.auth.bearer == 'oidc/oi/' + oidc_mock.state["access_token"] assert refresh_token_store.mock_calls == []