unauthenticated: true method plus allow block docs, closes #825

This commit is contained in:
Simon Willison 2020-06-09 10:01:03 -07:00
commit 7633b9ab24
6 changed files with 134 additions and 55 deletions

View file

@ -1,4 +1,5 @@
from .fixtures import app_client, assert_permissions_checked, make_app_client
from bs4 import BeautifulSoup as Soup
import pytest
@ -283,3 +284,39 @@ def test_permissions_checked(app_client, path, permissions):
response = app_client.get(path)
assert response.status in (200, 403)
assert_permissions_checked(app_client.ds, permissions)
def test_permissions_debug(app_client):
app_client.ds._permission_checks.clear()
assert 403 == app_client.get("/-/permissions").status
# With the cookie it should work
cookie = app_client.ds.sign({"id": "root"}, "actor")
response = app_client.get("/-/permissions", cookies={"ds_actor": cookie})
# Should show one failure and one success
soup = Soup(response.body, "html.parser")
check_divs = soup.findAll("div", {"class": "check"})
checks = [
{
"action": div.select_one(".check-action").text,
"result": bool(div.select(".check-result-true")),
"used_default": bool(div.select(".check-used-default")),
}
for div in check_divs
]
assert [
{"action": "permissions-debug", "result": True, "used_default": False},
{"action": "permissions-debug", "result": False, "used_default": True},
] == checks
@pytest.mark.parametrize("allow,expected", [
({"id": "root"}, 403),
({"id": "root", "unauthenticated": True}, 200),
])
def test_allow_unauthenticated(allow, expected):
with make_app_client(
metadata={
"allow": allow
}
) as client:
assert expected == client.get("/").status