1010)
1111
1212
13- import pytest
14- from unittest .mock import patch , MagicMock
15-
16-
17- # Fixture to patch OpenIdConnectAuth and mock valid_token_dependency
18- @pytest .fixture
19- def skip_auth ():
20- with patch ("eoapi.auth_utils.OpenIdConnectAuth" ) as MockClass :
21- # Create a mock instance
22- mock_instance = MagicMock ()
23- # Set the return value of `valid_token_dependency`
24- mock_instance .valid_token_dependency .return_value = "constant"
25- # Assign the mock instance to the patched class's return value
26- MockClass .return_value = mock_instance
27-
28- # Yield the mock instance for use in tests
29- yield mock_instance
30-
31-
3213@pytest .mark .parametrize (
3314 "endpoint, expected_status_code" ,
3415 [
3516 ("/" , 403 ),
3617 ("/?foo=xyz" , 403 ),
18+ ("/?bar=foo" , 403 ),
3719 ("/?foo=bar" , 200 ),
20+ ("/?foo=xyz&foo=bar" , 200 ), # Only the last value is checked
21+ ("/?foo=bar&foo=xyz" , 403 ), # Only the last value is checked
3822 ],
3923)
4024def test_guard_query_params (
@@ -43,7 +27,6 @@ def test_guard_query_params(
4327 endpoint ,
4428 expected_status_code ,
4529):
46- """When no OpenAPI spec endpoint is set, the proxied OpenAPI spec is unaltered."""
4730 app = app_factory (
4831 upstream_url = source_api_server ,
4932 guard = {
@@ -56,3 +39,39 @@ def test_guard_query_params(
5639 client = TestClient (app , headers = {"Authorization" : f"Bearer { token_builder ({})} " })
5740 response = client .get (endpoint )
5841 assert response .status_code == expected_status_code
42+
43+
44+ @pytest .mark .parametrize (
45+ "token, expected_status_code" ,
46+ [
47+ ({"foo" : "bar" }, 403 ),
48+ ({"collections" : []}, 403 ),
49+ ({"collections" : ["foo" , "bar" ]}, 403 ),
50+ ({"collections" : ["xyz" ]}, 200 ),
51+ ({"collections" : ["foo" , "xyz" ]}, 200 ),
52+ ],
53+ )
54+ def test_guard_auth_token (
55+ source_api_server ,
56+ token_builder ,
57+ token ,
58+ expected_status_code ,
59+ ):
60+ app = app_factory (
61+ upstream_url = source_api_server ,
62+ guard = {
63+ "cls" : "stac_auth_proxy.guards.cel.Cel" ,
64+ "kwargs" : {
65+ "expression" : """
66+ ("collections" in token)
67+ && ("collection_id" in req.path_params)
68+ && (req.path_params.collection_id in token.collections)
69+ """
70+ },
71+ },
72+ )
73+ client = TestClient (
74+ app , headers = {"Authorization" : f"Bearer { token_builder (token )} " }
75+ )
76+ response = client .get ("/collections/xyz" )
77+ assert response .status_code == expected_status_code
0 commit comments