Skip to content

Commit

Permalink
Respect existing scope["actor"] if set, closes #854
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jun 18, 2020
1 parent d2aef9f commit 6151c25
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,7 @@ async def route_path(self, scope, receive, send, path):
):
scope_modifications["scheme"] = "https"
# Handle authentication
default_actor = scope.get("actor") or None
actor = None
for actor in pm.hook.actor_from_request(
datasette=self.ds, request=Request(scope, receive)
Expand All @@ -918,7 +919,7 @@ async def route_path(self, scope, receive, send, path):
actor = await actor
if actor:
break
scope_modifications["actor"] = actor
scope_modifications["actor"] = actor or default_actor
return await super().route_path(
dict(scope, **scope_modifications), receive, send, path
)
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"version": None,
"hooks": [
"actor_from_request",
"asgi_wrapper",
"extra_body_script",
"extra_css_urls",
"extra_js_urls",
Expand Down
14 changes: 14 additions & 0 deletions tests/plugins/my_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ def actor_from_request(datasette, request):
return None


@hookimpl
def asgi_wrapper():
def wrap(app):
async def maybe_set_actor_in_scope(scope, recieve, send):
if b"_actor_in_scope" in scope["query_string"]:
scope = dict(scope, actor={"id": "from-scope"})
print(scope)
await app(scope, recieve, send)

return maybe_set_actor_in_scope

return wrap


@hookimpl
def permission_allowed(actor, action):
if action == "this_is_allowed":
Expand Down
5 changes: 5 additions & 0 deletions tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,11 @@ def test_actor_from_request_async(app_client):
assert {"id": "bot2", "1+1": 2} == app_client.ds._last_request.scope["actor"]


def test_existing_scope_actor_respected(app_client):
app_client.get("/?_actor_in_scope=1")
assert {"id": "from-scope"} == app_client.ds._last_request.scope["actor"]


@pytest.mark.asyncio
@pytest.mark.parametrize(
"action,expected",
Expand Down

0 comments on commit 6151c25

Please sign in to comment.