Skip to content

Commit 9897167

Browse files
committed
feat(context_url): add context url to caller
1 parent 1ab6be4 commit 9897167

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

src/agent_toolkit/forestadmin/agent_toolkit/resources/collections/decorators.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import Any, Awaitable, Callable, TypeVar, Union
2+
from urllib.parse import unquote
23

34
from forestadmin.agent_toolkit.resources.collections.base_collection_resource import BaseCollectionResource
45
from forestadmin.agent_toolkit.resources.collections.filter import parse_timezone
@@ -42,6 +43,10 @@ async def _authenticate(
4243
except JWTError:
4344
return Response(status=401)
4445

46+
context_url = None
47+
if "Forest-Context-Url" in request.headers:
48+
context_url = unquote(request.headers["Forest-Context-Url"])
49+
4550
request.user = User(
4651
rendering_id=int(user["rendering_id"]),
4752
user_id=int(user["id"]),
@@ -51,6 +56,7 @@ async def _authenticate(
5156
last_name=user["last_name"],
5257
team=user["team"],
5358
timezone=parse_timezone(request),
59+
context_url=context_url,
5460
)
5561
return await decorated_fn(self, request)
5662

src/agent_toolkit/forestadmin/agent_toolkit/utils/context.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class User:
3232
last_name: str
3333
team: str
3434
timezone: ZoneInfo
35+
context_url: Optional[str]
3536
# permission_level
3637
# role
3738

src/agent_toolkit/tests/resources/collections/test_decorators.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,39 @@ async def _decorated_fn(resource, request):
183183

184184
self.assertEqual(response, True)
185185

186+
def test_should_parse_forest_context_url_if_present(self):
187+
user = {
188+
"rendering_id": "1",
189+
"id": "1",
190+
"tags": {"test": "tag"},
191+
"email": "user@company.com",
192+
"first_name": "first_name",
193+
"last_name": "last_name",
194+
"team": "best_team",
195+
}
196+
encoded_user = jwt.encode(user, "auth_secret")
197+
request = RequestCollection(
198+
RequestMethod.GET,
199+
self.book_collection,
200+
body=None,
201+
query={"timezone": "Europe/Paris"},
202+
headers={
203+
"Authorization": f"Bearer {encoded_user}",
204+
"Forest-Context-Url": "http://localhost/?param%3D%2Ftest%2F",
205+
},
206+
)
207+
208+
async def _decorated_fn(resource, request):
209+
self.assertEqual(request.user.context_url, "http://localhost/?param=/test/")
210+
211+
return True
212+
213+
decorated_fn = AsyncMock(wraps=_decorated_fn)
214+
response = self.loop.run_until_complete(_authenticate(self.collection_resource, request, decorated_fn))
215+
decorated_fn.assert_awaited_once_with(self.collection_resource, request)
216+
217+
self.assertEqual(response, True)
218+
186219

187220
class TestAuthorizeDecorators(TestDecorators):
188221
@classmethod

0 commit comments

Comments
 (0)