-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest_oauth_requirer.py
275 lines (212 loc) · 9.44 KB
/
test_oauth_requirer.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# Copyright 2023 Canonical Ltd.
# See LICENSE file for licensing details.
import json
import logging
from typing import Any, Dict, Generator, List
from unittest.mock import MagicMock
import pytest
from charms.hydra.v0.oauth import (
CLIENT_SECRET_FIELD,
ClientConfig,
ClientConfigError,
InvalidClientConfigEvent,
OAuthInfoChangedEvent,
OAuthRequirer,
)
from ops.charm import CharmBase
from ops.framework import EventBase
from ops.testing import Harness
from pytest_mock import MockerFixture
METADATA = """
name: requirer-tester
requires:
oauth:
interface: oauth
"""
@pytest.fixture()
def harness() -> Generator:
harness = Harness(OAuthRequirerCharm, meta=METADATA)
harness.set_leader(True)
harness.begin_with_initial_hooks()
yield harness
harness.cleanup()
@pytest.fixture()
def provider_info() -> Dict:
return {
"authorization_endpoint": "https://example.oidc.com/oauth2/auth",
"introspection_endpoint": "https://example.oidc.com/admin/oauth2/introspect",
"issuer_url": "https://example.oidc.com",
"jwks_endpoint": "https://example.oidc.com/.well-known/jwks.json",
"scope": "openid profile email phone",
"token_endpoint": "https://example.oidc.com/oauth2/token",
"userinfo_endpoint": "https://example.oidc.com/userinfo",
"jwt_access_token": "False",
}
@pytest.fixture()
def mocked_client_is_created(mocker: MockerFixture) -> MagicMock:
mocked_client_created = mocker.patch(
"charms.hydra.v0.oauth.OAuthRequirer.is_client_created", return_value=True
)
return mocked_client_created
CLIENT_CONFIG = {
"redirect_uri": "https://example.oidc.client/callback",
"scope": "openid email offline_access",
"grant_types": [
"authorization_code",
"refresh_token",
"client_credentials",
"urn:ietf:params:oauth:grant-type:device_code",
],
"audience": [],
"token_endpoint_auth_method": "client_secret_basic",
}
def dict_to_relation_data(dic: Dict) -> Dict:
return {k: json.dumps(v) if isinstance(v, (list, dict)) else v for k, v in dic.items()}
class OAuthRequirerCharm(CharmBase):
def __init__(self, *args: Any) -> None:
super().__init__(*args)
client_config = ClientConfig(**CLIENT_CONFIG)
self.oauth = OAuthRequirer(self, client_config=client_config)
self.events: List = []
self.framework.observe(self.oauth.on.oauth_info_changed, self._record_event)
self.framework.observe(self.oauth.on.invalid_client_config, self._record_event)
def _record_event(self, event: EventBase) -> None:
self.events.append(event)
def test_data_in_relation_bag(harness: Harness) -> None:
relation_id = harness.add_relation("oauth", "provider")
relation_data = harness.get_relation_data(relation_id, harness.model.app.name)
assert relation_data == dict_to_relation_data(CLIENT_CONFIG)
def test_no_event_emitted_when_provider_info_available_but_no_client_id(
harness: Harness, provider_info: Dict
) -> None:
relation_id = harness.add_relation("oauth", "provider")
harness.add_relation_unit(relation_id, "provider/0")
harness.update_relation_data(
relation_id,
"provider",
provider_info,
)
relation_data = harness.get_relation_data(relation_id, harness.model.app.name)
events = harness.charm.events
assert relation_data == dict_to_relation_data(CLIENT_CONFIG)
assert len(events) == 0
def test_oauth_info_changed_event_emitted_when_client_created(
harness: Harness, provider_info: Dict
) -> None:
client_secret = "s3cR#T"
relation_id = harness.add_relation("oauth", "provider")
harness.add_relation_unit(relation_id, "provider/0")
harness.update_relation_data(
relation_id,
"provider",
provider_info,
)
secret_id = harness.add_model_secret("provider", {CLIENT_SECRET_FIELD: client_secret})
harness.grant_secret(secret_id, "requirer-tester")
harness.update_relation_data(
relation_id,
"provider",
{
"client_id": "client_id",
"client_secret_id": secret_id,
},
)
assert any(isinstance(event := e, OAuthInfoChangedEvent) for e in harness.charm.events)
assert event.client_id == "client_id"
assert event.client_secret_id == secret_id
secret = harness.charm.oauth.get_client_secret(event.client_secret_id)
assert secret.get_content() == {"secret": client_secret}
def test_get_provider_info_when_data_available(harness: Harness, provider_info: Dict) -> None:
relation_id = harness.add_relation("oauth", "provider")
harness.add_relation_unit(relation_id, "provider/0")
harness.update_relation_data(
relation_id,
"provider",
provider_info,
)
expected_provider_info = harness.charm.oauth.get_provider_info()
assert expected_provider_info.authorization_endpoint == provider_info["authorization_endpoint"]
assert expected_provider_info.introspection_endpoint == provider_info["introspection_endpoint"]
assert expected_provider_info.issuer_url == provider_info["issuer_url"]
assert expected_provider_info.jwks_endpoint == provider_info["jwks_endpoint"]
assert expected_provider_info.scope == provider_info["scope"]
assert expected_provider_info.token_endpoint == provider_info["token_endpoint"]
assert expected_provider_info.userinfo_endpoint == provider_info["userinfo_endpoint"]
assert expected_provider_info.jwt_access_token == (provider_info["jwt_access_token"] == "True")
def test_get_client_credentials_when_data_available(harness: Harness, provider_info: Dict) -> None:
client_id = "client_id"
client_secret = "s3cR#T"
relation_id = harness.add_relation("oauth", "provider")
harness.add_relation_unit(relation_id, "provider/0")
secret_id = harness.add_model_secret("provider", {CLIENT_SECRET_FIELD: client_secret})
harness.grant_secret(secret_id, "requirer-tester")
harness.update_relation_data(
relation_id,
"provider",
dict(client_id=client_id, client_secret_id=secret_id, **provider_info),
)
expected_client_details = harness.charm.oauth.get_provider_info()
assert expected_client_details.client_id == client_id
assert expected_client_details.client_secret == client_secret
def test_exception_raised_when_malformed_redirect_url(harness: Harness) -> None:
client_config = ClientConfig(**CLIENT_CONFIG)
client_config.redirect_uri = "malformed-url"
with pytest.raises(ClientConfigError, match=f"Invalid URL {client_config.redirect_uri}"):
harness.charm.oauth.update_client_config(client_config=client_config)
def test_warning_when_http_redirect_url(
harness: Harness, caplog: pytest.LogCaptureFixture
) -> None:
caplog.set_level(logging.WARNING)
client_config = ClientConfig(**CLIENT_CONFIG)
client_config.redirect_uri = "http://some.callback"
harness.charm.oauth.update_client_config(client_config=client_config)
assert "Provided Redirect URL uses http scheme. Don't do this in production" in caplog.text
def test_exception_raised_when_invalid_grant_type(harness: Harness) -> None:
client_config = ClientConfig(**CLIENT_CONFIG)
client_config.grant_types = ["authorization_code", "token_exchange"]
with pytest.raises(ClientConfigError, match="Invalid grant_type"):
harness.charm.oauth.update_client_config(client_config=client_config)
def test_exception_raised_when_invalid_client_authn_method(harness: Harness) -> None:
client_config = ClientConfig(**CLIENT_CONFIG)
client_config.token_endpoint_auth_method = "private_key_jwt"
with pytest.raises(ClientConfigError, match="Invalid client auth method"):
harness.charm.oauth.update_client_config(client_config=client_config)
class InvalidConfigOAuthRequirerCharm(CharmBase):
def __init__(self, *args: Any) -> None:
super().__init__(*args)
client_config = ClientConfig(**CLIENT_CONFIG)
client_config.grant_types = ["invalid_grant_type"]
self.oauth = OAuthRequirer(self, client_config=client_config)
self.events: List = []
self.framework.observe(self.oauth.on.oauth_info_changed, self._record_event)
self.framework.observe(self.oauth.on.invalid_client_config, self._record_event)
def _record_event(self, event: EventBase) -> None:
self.events.append(event)
@pytest.fixture()
def harness_invalid_config() -> Generator:
harness = Harness(InvalidConfigOAuthRequirerCharm, meta=METADATA)
harness.set_leader(True)
harness.begin_with_initial_hooks()
yield harness
harness.cleanup()
def test_event_emitted_when_invalid_client_config(harness_invalid_config: Harness) -> None:
harness_invalid_config.add_relation("oauth", "provider")
assert any(
isinstance(e, InvalidClientConfigEvent) for e in harness_invalid_config.charm.events
)
def test_event_deferred_on_relation_broken_when_relation_data_available(
harness: Harness,
provider_info: Dict,
mocked_client_is_created: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None:
caplog.set_level(logging.INFO)
relation_id = harness.add_relation("oauth", "provider")
harness.add_relation_unit(relation_id, "provider/0")
harness.update_relation_data(
relation_id,
"provider",
dict(client_id="client_id", client_secret_id="s3cR#T", **provider_info),
)
harness.remove_relation(relation_id)
assert caplog.record_tuples[0][2] == "Relation data still available. Deferring the event"