Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_get_products_invalid_json(test_wkt):
api = SentinelAPI("mock_user", "mock_password")
with requests_mock.mock() as rqst:
rqst.post(
'https://scihub.copernicus.eu/apihub/search?format=json',
text="{Invalid JSON response", status_code=200
)
with pytest.raises(SentinelAPIError) as excinfo:
api.query(
area=test_wkt,
date=("20151219", "20151228"),
platformname="Sentinel-2"
)
assert excinfo.value.msg == "Invalid API response."
assert format_query_date(date_str) == date_str
api.query(raw="ingestiondate:[{} TO *]".format(date_str), limit=0)
for date_str in (
"NOW - 1HOUR",
"NOW - 1HOURS",
"NOW-1 HOURS",
"NOW-1",
"NOW-",
"**",
"+",
"-",
):
with pytest.raises(ValueError):
format_query_date(date_str)
with pytest.raises(SentinelAPIError):
api.query(raw="ingestiondate:[{} TO *]".format(date_str), limit=0)
def test_unicode_support(api):
test_str = "٩(●̮̮̃•̃)۶:"
with pytest.raises(SentinelAPIError) as excinfo:
api.count(raw=test_str)
assert test_str == excinfo.value.response.json()["feed"]["opensearch:Query"]["searchTerms"]
with pytest.raises(SentinelAPIError) as excinfo:
api.get_product_odata(test_str)
assert test_str in excinfo.value.response.json()["error"]["message"]["value"]
def test_get_product_info_bad_key(api):
with pytest.raises(SentinelAPIError) as excinfo:
api.get_product_odata("invalid-xyz")
assert excinfo.value.msg == "InvalidKeyException : Invalid key (invalid-xyz) to access Products"
)
with pytest.raises(SentinelAPIError) as excinfo:
api.get_product_odata("8df46c9e-a20c-43db-a19a-4240c2ed3b8b")
assert (
excinfo.value.msg
== "No Products found with key '8df46c9e-a20c-43db-a19a-4240c2ed3b8b' "
)
rqst.get(request_url, text="Mock SciHub is Down", status_code=200)
with pytest.raises(SentinelAPIError) as excinfo:
api.get_product_odata("8df46c9e-a20c-43db-a19a-4240c2ed3b8b")
assert excinfo.value.msg == "Mock SciHub is Down"
# Test with a real "server under maintenance" response
rqst.get(request_url, text=read_fixture_file("server_maintenance.html"), status_code=502)
with pytest.raises(SentinelAPIError) as excinfo:
api.get_product_odata("8df46c9e-a20c-43db-a19a-4240c2ed3b8b")
assert "The Sentinels Scientific Data Hub will be back soon!" in excinfo.value.msg
def test_SentinelAPI_wrong_credentials(small_query):
api = SentinelAPI("wrong_user", "wrong_password")
with pytest.raises(SentinelAPIError) as excinfo:
api.query(**small_query)
assert excinfo.value.response.status_code == 401
with pytest.raises(SentinelAPIError) as excinfo:
api.get_product_odata("8df46c9e-a20c-43db-a19a-4240c2ed3b8b")
assert excinfo.value.response.status_code == 401
with pytest.raises(SentinelAPIError) as excinfo:
api.download("8df46c9e-a20c-43db-a19a-4240c2ed3b8b")
assert excinfo.value.response.status_code == 401
with pytest.raises(SentinelAPIError) as excinfo:
api.download_all(["8df46c9e-a20c-43db-a19a-4240c2ed3b8b"])
assert excinfo.value.response.status_code == 401
def test_unicode_support(api):
test_str = "٩(●̮̮̃•̃)۶:"
with pytest.raises(SentinelAPIError) as excinfo:
api.count(raw=test_str)
assert test_str == excinfo.value.response.json()["feed"]["opensearch:Query"]["searchTerms"]
with pytest.raises(SentinelAPIError) as excinfo:
api.get_product_odata(test_str)
assert test_str in excinfo.value.response.json()["error"]["message"]["value"]
def test_name_search_empty(run_cli):
run_cli("--name", "", must_raise=SentinelAPIError)
assert format_query_date('2015-01-01T00:00:00Z') == '2015-01-01T00:00:00Z'
assert format_query_date('20150101') == '2015-01-01T00:00:00Z'
assert format_query_date(' NOW ') == 'NOW'
assert format_query_date(None) == '*'
for date_str in ("NOW", "NOW-1DAY", "NOW-1DAYS", "NOW-500DAY", "NOW-500DAYS",
"NOW-2MONTH", "NOW-2MONTHS", "NOW-20MINUTE", "NOW-20MINUTES",
"NOW+10HOUR", "2015-01-01T00:00:00Z+1DAY", "NOW+3MONTHS-7DAYS/DAYS",
"*"):
assert format_query_date(date_str) == date_str
api.query(raw='ingestiondate:[{} TO *]'.format(date_str), limit=0)
for date_str in ("NOW - 1HOUR", "NOW - 1HOURS", "NOW-1 HOURS", "NOW-1", "NOW-", "**", "+", "-"):
with pytest.raises(ValueError):
format_query_date(date_str)
with pytest.raises(SentinelAPIError):
api.query(raw='ingestiondate:[{} TO *]'.format(date_str), limit=0)
def test_download_invalid_id(api):
uuid = "1f62a176-c980-41dc-xxxx-c735d660c910"
with pytest.raises(SentinelAPIError) as excinfo:
api.download(uuid)
assert "Invalid key" in excinfo.value.msg