-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_authn.py
169 lines (157 loc) · 5.01 KB
/
test_authn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"""Test authentication cases for the proxy app."""
import pytest
from fastapi.testclient import TestClient
from utils import AppFactory
app_factory = AppFactory(
oidc_discovery_url="https://example-stac-api.com/.well-known/openid-configuration",
default_public=False,
public_endpoints={},
private_endpoints={},
)
@pytest.mark.parametrize(
"path,method",
[
("/", "GET"),
("/conformance", "GET"),
("/queryables", "GET"),
("/search", "GET"),
("/search", "POST"),
("/collections", "GET"),
("/collections", "POST"),
("/collections/example-collection", "GET"),
("/collections/example-collection", "PUT"),
("/collections/example-collection", "DELETE"),
("/collections/example-collection/items", "GET"),
("/collections/example-collection/items", "POST"),
("/collections/example-collection/items/example-item", "GET"),
("/collections/example-collection/items/example-item", "PUT"),
("/collections/example-collection/items/example-item", "DELETE"),
("/collections/example-collection/bulk_items", "POST"),
("/api.html", "GET"),
("/api", "GET"),
],
)
def test_default_public_false(source_api_server, path, method, token_builder):
"""Private endpoints permit access with a valid token."""
test_app = app_factory(upstream_url=source_api_server)
valid_auth_token = token_builder({})
client = TestClient(test_app)
response = client.request(method=method, url=path, headers={})
assert response.status_code == 403
response = client.request(
method=method, url=path, headers={"Authorization": f"Bearer {valid_auth_token}"}
)
assert response.status_code == 200
@pytest.mark.parametrize(
"rules,token,permitted",
[
[
[("POST", "collection:create")],
{"scope": "collection:create"},
True,
],
[
[("POST", "collection:create")],
{"scope": ""},
False,
],
[
[("POST", "collection:create")],
{"scope": "openid"},
False,
],
[
[("POST", "collection:create")],
{"scope": "openid collection:create"},
True,
],
[
[("POST", "foo collection:create")],
{"scope": "openid collection:create foo"},
True,
],
[
[("GET", "collection:read"), ("POST", "collection:create")],
{"scope": "openid collection:read"},
False,
],
],
)
def test_default_public_false_with_scopes(
source_api_server, rules, token, permitted, token_builder
):
"""Private endpoints permit access with a valid token."""
test_app = app_factory(
upstream_url=source_api_server,
default_public=False,
private_endpoints={r"^/collections$": rules},
)
valid_auth_token = token_builder(token)
client = TestClient(test_app)
response = client.request(
method="POST",
url="/collections",
headers={"Authorization": f"Bearer {valid_auth_token}"},
)
assert response.status_code == (200 if permitted else 401)
@pytest.mark.parametrize(
"token_scopes, private_endpoints, path, method, expected_permitted",
[
pytest.param(
"",
{r"^/*": [("POST", "collection:create")]},
"/collections",
"POST",
False,
id="empty scopes + private endpoint",
),
pytest.param(
"openid profile collection:createbutnotcreate",
{r"^/*": [("POST", "collection:create")]},
"/collections",
"POST",
False,
id="invalid scopes + private endpoint",
),
pytest.param(
"openid profile collection:create somethingelse",
{r"^/*": [("POST", "")]},
"/collections",
"POST",
True,
id="valid scopes + private endpoint without required scopes",
),
pytest.param(
"openid",
{r"^/collections/.*/items$": [("POST", "collection:create")]},
"/collections",
"GET",
True,
id="accessing public endpoint with private endpoint required scopes",
),
],
)
def test_scopes(
source_api_server,
token_builder,
token_scopes,
private_endpoints,
path,
method,
expected_permitted,
):
"""Private endpoints permit access with a valid token."""
test_app = app_factory(
upstream_url=source_api_server,
default_public=True,
private_endpoints=private_endpoints,
)
valid_auth_token = token_builder({"scope": token_scopes})
client = TestClient(test_app)
response = client.request(
method=method,
url=path,
headers={"Authorization": f"Bearer {valid_auth_token}"},
)
expected_status_code = 200 if expected_permitted else 401
assert response.status_code == expected_status_code