Skip to content

Commit

Permalink
Implemented view-instance permission, refs #811
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jun 7, 2020
1 parent ece0ba6 commit 8571ce3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions datasette/default_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ def permission_allowed(datasette, actor, action, resource_type, resource_identif
if action == "permissions-debug":
if actor and actor.get("id") == "root":
return True
elif action == "view-instance":
allow = datasette.metadata("allow")
if allow is not None:
return actor_matches_allow(actor, allow)
elif action == "view-query":
# Check if this query has a "allow" block in metadata
assert resource_type == "query"
Expand Down
20 changes: 20 additions & 0 deletions tests/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,23 @@ def test_execute_sql(allow, expected_anon, expected_auth):
"/fixtures/q", cookies={"ds_actor": client.ds.sign({"id": "root"}, "actor")}
)
assert expected_auth == auth_response.status


@pytest.mark.parametrize(
"allow,expected_anon,expected_auth",
[(None, 200, 200), ({}, 403, 403), ({"id": "root"}, 403, 200),],
)
def test_view_instance(allow, expected_anon, expected_auth):
with make_app_client(metadata={"allow": allow}) as client:
for path in (
"/",
"/fixtures",
"/fixtures/compound_three_primary_keys",
"/fixtures/compound_three_primary_keys/a,a,a",
):
anon_response = client.get(path)
assert expected_anon == anon_response.status
auth_response = client.get(
path, cookies={"ds_actor": client.ds.sign({"id": "root"}, "actor")},
)
assert expected_auth == auth_response.status

0 comments on commit 8571ce3

Please sign in to comment.