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,5 +1,4 @@
from .fixtures import app_client
from bs4 import BeautifulSoup as Soup
def test_auth_token(app_client):
@ -20,26 +19,3 @@ def test_actor_cookie(app_client):
cookie = app_client.ds.sign({"id": "test"}, "actor")
response = app_client.get("/", cookies={"ds_actor": cookie})
assert {"id": "test"} == app_client.ds._last_request.scope["actor"]
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

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

View file

@ -464,12 +464,16 @@ def test_multi_params(data, should_raise):
@pytest.mark.parametrize(
"actor,allow,expected",
[
({"id": "root"}, None, True),
({"id": "root"}, {}, False),
({"anonymous": True}, {"anonymous": True}, True),
(None, None, True),
(None, {}, False),
(None, {"id": "root"}, False),
({"id": "root"}, None, True),
({"id": "root"}, {}, False),
({"id": "simon", "staff": True}, {"staff": True}, True),
({"id": "simon", "staff": False}, {"staff": True}, False),
# Special case for "unauthenticated": true
(None, {"unauthenticated": True}, True),
(None, {"unauthenticated": False}, False),
# Special "*" value for any key:
({"id": "root"}, {"id": "*"}, True),
({}, {"id": "*"}, False),