Skip to content

Commit 3a86ae9

Browse files
committed
feat: TestSCIMClient environ parameter.
1 parent 86c033a commit 3a86ae9

File tree

3 files changed

+55
-26
lines changed

3 files changed

+55
-26
lines changed

doc/changelog.rst

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
Changelog
22
=========
33

4-
[0.4.4] - Unreleased
4+
[0.5.0] - Unreleased
55
--------------------
66

7-
Added
8-
^^^^^
9-
- :class:`~scim2_client.engines.werkzeug.TestSCIMClient` can take a `client` parameter.
7+
.. warning::
8+
9+
This version comes with breaking changes:
10+
11+
- :class:`~scim2_client.engines.werkzeug.TestSCIMClient` ``app`` is dropped in favor
12+
of ``client`` and ``environ``. Have a look at the reference for examples.
1013

1114
[0.4.3] - 2024-12-06
1215
--------------------

scim2_client/engines/werkzeug.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ class TestSCIMClient(BaseSyncSCIMClient):
3737
This client avoids to perform real HTTP requests and directly execute the server code instead.
3838
This allows to dynamically catch the exceptions if something gets wrong.
3939
40-
:param app: A WSGI application instance that will be used to send requests.
4140
:param client: An optional custom :class:`Werkzeug test Client <werkzeug.test.Client>`.
4241
If :data:`None` a default client is initialized.
4342
:param scim_prefix: The scim root endpoint in the application.
43+
:param environ: Additional parameters that will be passed to every request.
4444
:param resource_models: A tuple of :class:`~scim2_models.Resource` types expected to be handled by the SCIM client.
4545
If a request payload describe a resource that is not in this list, an exception will be raised.
4646
:param check_request_payload: If :data:`False`,
@@ -56,9 +56,14 @@ class TestSCIMClient(BaseSyncSCIMClient):
5656
5757
from scim2_client.engines.werkzeug import TestSCIMClient
5858
from scim2_models import User, Group
59+
from werkzeug.test import Client
5960
6061
scim_provider = myapp.create_app()
61-
testclient = TestSCIMClient(app=scim_provider, resource_models=(User, Group))
62+
testclient = TestSCIMClient(
63+
app=Client(scim_provider),
64+
environ={"base_url": "/scim/v2"},
65+
resource_models=(User, Group),
66+
)
6267
6368
request_user = User(user_name="foo", display_name="bar")
6469
response_user = scim_client.create(request_user)
@@ -70,15 +75,16 @@ class TestSCIMClient(BaseSyncSCIMClient):
7075

7176
def __init__(
7277
self,
73-
app,
74-
client: Optional[Client] = None,
75-
*args,
78+
client: Client,
79+
environ: Optional[dict] = None,
7680
scim_prefix: str = "",
81+
*args,
7782
**kwargs,
7883
):
7984
super().__init__(*args, **kwargs)
80-
self.client = client or Client(app)
85+
self.client = client
8186
self.scim_prefix = scim_prefix
87+
self.environ = environ or {}
8288

8389
def make_url(self, url: Optional[str]) -> str:
8490
url = url or ""
@@ -112,9 +118,8 @@ def create(
112118
**kwargs,
113119
)
114120

115-
response = self.client.post(
116-
self.make_url(req.url), json=req.payload, **req.request_kwargs
117-
)
121+
environ = {**self.environ, **req.request_kwargs}
122+
response = self.client.post(self.make_url(req.url), json=req.payload, **environ)
118123

119124
with handle_response_error(req.payload):
120125
return self.check_response(
@@ -152,8 +157,9 @@ def query(
152157
)
153158

154159
query_string = urlencode(req.payload, doseq=False) if req.payload else None
160+
environ = {**self.environ, **req.request_kwargs}
155161
response = self.client.get(
156-
self.make_url(req.url), query_string=query_string, **req.request_kwargs
162+
self.make_url(req.url), query_string=query_string, **environ
157163
)
158164

159165
with handle_response_error(req.payload):
@@ -187,9 +193,8 @@ def search(
187193
**kwargs,
188194
)
189195

190-
response = self.client.post(
191-
self.make_url(req.url), json=req.payload, **req.request_kwargs
192-
)
196+
environ = {**self.environ, **req.request_kwargs}
197+
response = self.client.post(self.make_url(req.url), json=req.payload, **environ)
193198

194199
with handle_response_error(response):
195200
return self.check_response(
@@ -222,7 +227,8 @@ def delete(
222227
**kwargs,
223228
)
224229

225-
response = self.client.delete(self.make_url(req.url), **req.request_kwargs)
230+
environ = {**self.environ, **req.request_kwargs}
231+
response = self.client.delete(self.make_url(req.url), **environ)
226232

227233
with handle_response_error(response):
228234
return self.check_response(
@@ -253,9 +259,8 @@ def replace(
253259
**kwargs,
254260
)
255261

256-
response = self.client.put(
257-
self.make_url(req.url), json=req.payload, **req.request_kwargs
258-
)
262+
environ = {**self.environ, **req.request_kwargs}
263+
response = self.client.put(self.make_url(req.url), json=req.payload, **environ)
259264

260265
with handle_response_error(response):
261266
return self.check_response(

tests/engines/test_werkzeug.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22
from scim2_models import SearchRequest
33
from scim2_models import User
4+
from werkzeug.test import Client
45
from werkzeug.wrappers import Request
56
from werkzeug.wrappers import Response
67

@@ -27,9 +28,10 @@ def scim_provider():
2728

2829
@pytest.fixture
2930
def scim_client(scim_provider):
30-
client = TestSCIMClient(app=scim_provider)
31-
client.discover()
32-
return client
31+
werkzeug_client = Client(scim_provider)
32+
scim_client = TestSCIMClient(werkzeug_client)
33+
scim_client.discover()
34+
return scim_client
3335

3436

3537
def test_werkzeug_engine(scim_client):
@@ -69,6 +71,25 @@ def test_no_json():
6971
def application(request):
7072
return Response("Hello, World!", content_type="application/scim+json")
7173

72-
client = TestSCIMClient(app=application, resource_models=(User,))
74+
werkzeug_client = Client(application)
75+
scim_client = TestSCIMClient(client=werkzeug_client, resource_models=(User,))
76+
scim_client.register_naive_resource_types()
7377
with pytest.raises(UnexpectedContentFormat):
74-
client.query(url="/")
78+
scim_client.query(url="/")
79+
80+
81+
def test_environ(scim_client):
82+
@Request.application
83+
def application(request):
84+
assert request.headers["content-type"] == "foobar"
85+
user = User(user_name="foobar", id="foobar")
86+
return Response(user.model_dump_json(), content_type="application/scim+json")
87+
88+
werkzeug_client = Client(application)
89+
scim_client = TestSCIMClient(
90+
client=werkzeug_client,
91+
environ={"headers": {"content-type": "foobar"}},
92+
resource_models=(User,),
93+
)
94+
scim_client.register_naive_resource_types()
95+
scim_client.query(url="/Users")

0 commit comments

Comments
 (0)