mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Flash messages mechanism, closes #790
This commit is contained in:
parent
1d0bea157a
commit
4fa7cf6853
14 changed files with 217 additions and 7 deletions
|
|
@ -29,6 +29,12 @@ class TestResponse:
|
|||
self.headers = headers
|
||||
self.body = body
|
||||
|
||||
@property
|
||||
def cookies(self):
|
||||
cookie = SimpleCookie()
|
||||
cookie.load(self.headers.get("set-cookie") or "")
|
||||
return {key: value.value for key, value in cookie.items()}
|
||||
|
||||
@property
|
||||
def json(self):
|
||||
return json.loads(self.text)
|
||||
|
|
|
|||
21
tests/plugins/messages_output_renderer.py
Normal file
21
tests/plugins/messages_output_renderer.py
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
from datasette import hookimpl
|
||||
|
||||
|
||||
def render_message_debug(datasette, request):
|
||||
if request.args.get("add_msg"):
|
||||
msg_type = request.args.get("type", "INFO")
|
||||
datasette.add_message(
|
||||
request, request.args["add_msg"], getattr(datasette, msg_type)
|
||||
)
|
||||
return {"body": "Hello from message debug"}
|
||||
|
||||
|
||||
@hookimpl
|
||||
def register_output_renderer(datasette):
|
||||
return [
|
||||
{
|
||||
"extension": "message",
|
||||
"render": render_message_debug,
|
||||
"can_render": lambda: False,
|
||||
}
|
||||
]
|
||||
|
|
@ -1262,6 +1262,7 @@ def test_plugins_json(app_client):
|
|||
expected = [
|
||||
{"name": name, "static": False, "templates": False, "version": None}
|
||||
for name in (
|
||||
"messages_output_renderer.py",
|
||||
"my_plugin.py",
|
||||
"my_plugin_2.py",
|
||||
"register_output_renderer.py",
|
||||
|
|
|
|||
|
|
@ -9,11 +9,7 @@ def test_auth_token(app_client):
|
|||
response = app_client.get(path, allow_redirects=False,)
|
||||
assert 302 == response.status
|
||||
assert "/" == response.headers["Location"]
|
||||
set_cookie = response.headers["set-cookie"]
|
||||
assert set_cookie.endswith("; Path=/")
|
||||
assert set_cookie.startswith("ds_actor=")
|
||||
cookie_value = set_cookie.split("ds_actor=")[1].split("; Path=/")[0]
|
||||
assert {"id": "root"} == app_client.ds.unsign(cookie_value, "actor")
|
||||
assert {"id": "root"} == app_client.ds.unsign(response.cookies["ds_actor"], "actor")
|
||||
# Check that a second with same token fails
|
||||
assert app_client.ds._root_token is None
|
||||
assert 403 == app_client.get(path, allow_redirects=False,).status
|
||||
|
|
|
|||
28
tests/test_messages.py
Normal file
28
tests/test_messages.py
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
from .fixtures import app_client
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"qs,expected",
|
||||
[
|
||||
("add_msg=added-message", [["added-message", 1]]),
|
||||
("add_msg=added-warning&type=WARNING", [["added-warning", 2]]),
|
||||
("add_msg=added-error&type=ERROR", [["added-error", 3]]),
|
||||
],
|
||||
)
|
||||
def test_add_message_sets_cookie(app_client, qs, expected):
|
||||
response = app_client.get("/fixtures.message?{}".format(qs))
|
||||
signed = response.cookies["ds_messages"]
|
||||
decoded = app_client.ds.unsign(signed, "messages")
|
||||
assert expected == decoded
|
||||
|
||||
|
||||
def test_messages_are_displayed_and_cleared(app_client):
|
||||
# First set the message cookie
|
||||
set_msg_response = app_client.get("/fixtures.message?add_msg=xmessagex")
|
||||
# Now access a page that displays messages
|
||||
response = app_client.get("/", cookies=set_msg_response.cookies)
|
||||
# Messages should be in that HTML
|
||||
assert "xmessagex" in response.text
|
||||
# Cookie should have been set that clears messages
|
||||
assert "" == response.cookies["ds_messages"]
|
||||
Loading…
Add table
Add a link
Reference in a new issue