mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Implemented datasette.permission_allowed(), refs #699
This commit is contained in:
parent
461c82838d
commit
9315bacf6f
5 changed files with 75 additions and 4 deletions
|
|
@ -134,3 +134,11 @@ def actor_from_request(datasette, request):
|
|||
return {"id": "bot"}
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
@hookimpl
|
||||
def permission_allowed(actor, action):
|
||||
if action == "this_is_allowed":
|
||||
return True
|
||||
elif action == "this_is_denied":
|
||||
return False
|
||||
|
|
|
|||
|
|
@ -107,3 +107,16 @@ def actor_from_request(datasette, request):
|
|||
return None
|
||||
|
||||
return inner
|
||||
|
||||
|
||||
@hookimpl
|
||||
def permission_allowed(datasette, actor, action):
|
||||
# Testing asyncio version of permission_allowed
|
||||
async def inner():
|
||||
assert 2 == (await datasette.get_database().execute("select 1 + 1")).first()[0]
|
||||
if action == "this_is_allowed_async":
|
||||
return True
|
||||
elif action == "this_is_denied_async":
|
||||
return False
|
||||
|
||||
return inner
|
||||
|
|
|
|||
|
|
@ -523,7 +523,19 @@ def test_actor_from_request_async(app_client):
|
|||
assert {"id": "bot2", "1+1": 2} == app_client.ds._last_request.scope["actor"]
|
||||
|
||||
|
||||
@pytest.mark.xfail
|
||||
def test_permission_allowed(app_client):
|
||||
# TODO
|
||||
assert False
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
"action,expected",
|
||||
[
|
||||
("this_is_allowed", True),
|
||||
("this_is_denied", False),
|
||||
("this_is_allowed_async", True),
|
||||
("this_is_denied_async", False),
|
||||
("no_match", None),
|
||||
],
|
||||
)
|
||||
async def test_permission_allowed(app_client, action, expected):
|
||||
actual = await app_client.ds.permission_allowed(
|
||||
{"id": "actor"}, action, default=None
|
||||
)
|
||||
assert expected == actual
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue