Skip to content

Commit 6e0cefc

Browse files
authored
Revert "Allow for generic auth headers with pystac + validators (#316)"
This reverts commit 6a38a74.
1 parent 6a38a74 commit 6e0cefc

File tree

6 files changed

+9
-281
lines changed

6 files changed

+9
-281
lines changed

noxfile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def mypy(session: Session) -> None:
151151
"""Type-check using mypy."""
152152
args = session.posargs or ["src", "tests", "docs/conf.py"]
153153
session.install(".")
154-
session.install("mypy", "pytest", "types-requests", "types-PyYAML", "typeguard")
154+
session.install("mypy", "pytest", "types-requests", "types-PyYAML")
155155
session.run("mypy", *args)
156156
if not session.posargs:
157157
session.run("mypy", f"--python-executable={sys.executable}", "noxfile.py")
@@ -161,7 +161,7 @@ def mypy(session: Session) -> None:
161161
def tests(session: Session) -> None:
162162
"""Run the test suite."""
163163
session.install(".")
164-
session.install("coverage[toml]", "pytest", "pygments", "typeguard")
164+
session.install("coverage[toml]", "pytest", "pygments")
165165
try:
166166
session.run("coverage", "run", "--parallel", "-m", "pytest", *session.posargs)
167167
finally:

src/stac_api_validator/__main__.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,6 @@
130130
"--transaction-collection",
131131
help="The name of the collection to use for Transaction Extension tests.",
132132
)
133-
@click.option(
134-
"-H",
135-
"--headers",
136-
multiple=True,
137-
help="Headers to attach to the main request and dependent pystac requests, curl syntax",
138-
)
139133
def main(
140134
log_level: str,
141135
root_url: str,
@@ -160,21 +154,11 @@ def main(
160154
query_in_field: Optional[str] = None,
161155
query_in_values: Optional[str] = None,
162156
transaction_collection: Optional[str] = None,
163-
headers: Optional[List[str]] = None,
164157
) -> int:
165158
"""STAC API Validator."""
166159
logging.basicConfig(stream=sys.stdout, level=log_level)
167160

168161
try:
169-
processed_headers = {}
170-
if headers:
171-
processed_headers.update(
172-
{
173-
key.strip(): value.strip()
174-
for key, value in (header.split(":") for header in headers)
175-
}
176-
)
177-
178162
(warnings, errors) = validate_api(
179163
root_url=root_url,
180164
ccs_to_validate=conformance_classes,
@@ -200,7 +184,6 @@ def main(
200184
query_in_values,
201185
),
202186
transaction_collection=transaction_collection,
203-
headers=processed_headers,
204187
)
205188
except Exception as e:
206189
click.secho(

src/stac_api_validator/validations.py

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
from pystac import Collection
2727
from pystac import Item
2828
from pystac import ItemCollection
29-
from pystac import StacIO
3029
from pystac import STACValidationError
3130
from pystac_client import Client
3231
from requests import Request
@@ -298,16 +297,6 @@ def is_geojson_type(maybe_type: Optional[str]) -> bool:
298297
)
299298

300299

301-
def get_catalog(data_dict: Dict[str, Any], r_session: Session) -> Catalog:
302-
stac_io = StacIO.default()
303-
if r_session.headers and r_session.headers.get("Authorization"):
304-
stac_io.headers = r_session.headers # noqa, type: ignore
305-
stac_io.headers["Accept-Encoding"] = "*"
306-
catalog = Catalog.from_dict(data_dict)
307-
catalog._stac_io = stac_io
308-
return catalog
309-
310-
311300
# def is_json_or_geojson_type(maybe_type: Optional[str]) -> bool:
312301
# return maybe_type and (is_json_type(maybe_type) or is_geojson_type(maybe_type))
313302

@@ -391,8 +380,9 @@ def retrieve(
391380
additional: Optional[str] = "",
392381
content_type: Optional[str] = None,
393382
) -> Tuple[int, Optional[Dict[str, Any]], Optional[Mapping[str, str]]]:
394-
request = Request(method.value, url, headers=headers, params=params, json=body)
395-
resp = r_session.send(r_session.prepare_request(request))
383+
resp = r_session.send(
384+
Request(method.value, url, headers=headers, params=params, json=body).prepare()
385+
)
396386

397387
# todo: handle connection exception, etc.
398388
# todo: handle timeout
@@ -546,7 +536,6 @@ def validate_api(
546536
validate_pagination: bool,
547537
query_config: QueryConfig,
548538
transaction_collection: Optional[str],
549-
headers: Optional[Dict[str, str]],
550539
) -> Tuple[Warnings, Errors]:
551540
warnings = Warnings()
552541
errors = Errors()
@@ -558,9 +547,6 @@ def validate_api(
558547
if auth_query_parameter and (xs := auth_query_parameter.split("=", 1)):
559548
r_session.params = {xs[0]: xs[1]}
560549

561-
if headers:
562-
r_session.headers.update(headers)
563-
564550
_, landing_page_body, landing_page_headers = retrieve(
565551
Method.GET, root_url, errors, Context.CORE, r_session
566552
)
@@ -717,7 +703,7 @@ def validate_api(
717703

718704
if not errors:
719705
try:
720-
catalog = Client.open(root_url, headers=headers)
706+
catalog = Client.open(root_url)
721707
catalog.validate()
722708
for child in catalog.get_children():
723709
child.validate()
@@ -824,8 +810,7 @@ def validate_core(
824810
# this validates, among other things, that the child and item link relations reference
825811
# valid STAC Catalogs, Collections, and/or Items
826812
try:
827-
catalog = get_catalog(root_body, r_session)
828-
list(take(1000, catalog.get_all_items()))
813+
list(take(1000, Catalog.from_dict(root_body).get_all_items()))
829814
except pystac.errors.STACTypeError as e:
830815
errors += (
831816
f"[{Context.CORE}] Error while traversing Catalog child/item links to find Items: {e} "
@@ -853,15 +838,14 @@ def validate_browseable(
853838
# check that at least a few of the items that can be reached from child/item link relations
854839
# can be found through search
855840
try:
856-
catalog = get_catalog(root_body, r_session)
857-
for item in take(10, catalog.get_all_items()):
841+
for item in take(10, Catalog.from_dict(root_body).get_all_items()):
858842
if link := link_by_rel(root_body.get("links"), "search"):
859843
_, body, _ = retrieve(
860844
Method.GET,
861845
link["href"],
862846
errors,
863847
Context.BROWSEABLE,
864-
params={"ids": item.id, "collections": item.collection_id},
848+
params={"ids": item.id, "collections": item.collection},
865849
r_session=r_session,
866850
)
867851
if body and len(body.get("features", [])) != 1:

tests/resources/sample-item.json

Lines changed: 0 additions & 53 deletions
This file was deleted.

tests/test_main.py

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
"""Test cases for the __main__ module."""
2-
import unittest.mock
3-
42
import pytest
53
from click.testing import CliRunner
64

@@ -16,38 +14,3 @@ def runner() -> CliRunner:
1614
def test_main_fails(runner: CliRunner) -> None:
1715
result = runner.invoke(__main__.main)
1816
assert result.exit_code == 2
19-
20-
21-
def test_retrieve_called_with_auth_headers(
22-
request: pytest.FixtureRequest, runner: CliRunner
23-
) -> None:
24-
if request.config.getoption("typeguard_packages"):
25-
pytest.skip(
26-
"The import hook that typeguard uses seems to break the mock below."
27-
)
28-
29-
expected_headers = {
30-
"User-Agent": "python-requests/2.28.2",
31-
"Accept-Encoding": "gzip, deflate",
32-
"Accept": "*/*",
33-
"Connection": "keep-alive",
34-
"Authorization": "api-key fake-api-key-value",
35-
}
36-
37-
with unittest.mock.patch(
38-
"stac_api_validator.validations.retrieve"
39-
) as retrieve_mock:
40-
runner.invoke(
41-
__main__.main,
42-
args=[
43-
"--root-url",
44-
"https://invalid",
45-
"--conformance",
46-
"core",
47-
"-H",
48-
"Authorization: api-key fake-api-key-value",
49-
],
50-
)
51-
assert retrieve_mock.call_count == 1
52-
r_session = retrieve_mock.call_args.args[-1]
53-
assert r_session.headers == expected_headers

0 commit comments

Comments
 (0)