| 1 |
from .fixtures import app_client |
| 2 |
from datasette.utils import baseconv |
| 3 |
import pytest |
| 4 |
import time |
| 5 |
|
| 6 |
|
| 7 |
def test_auth_token(app_client): |
| 8 |
"""The /-/auth-token endpoint sets the correct cookie""" |
| 9 |
assert app_client.ds._root_token is not None |
| 10 |
path = f"/-/auth-token?token={app_client.ds._root_token}" |
| 11 |
response = app_client.get( |
| 12 |
path, |
| 13 |
) |
| 14 |
assert 302 == response.status |
| 15 |
assert "/" == response.headers["Location"] |
| 16 |
assert {"a": {"id": "root"}} == app_client.ds.unsign( |
| 17 |
response.cookies["ds_actor"], "actor" |
| 18 |
) |
| 19 |
# Check that a second with same token fails |
| 20 |
assert app_client.ds._root_token is None |
| 21 |
assert ( |
| 22 |
403 |
| 23 |
== app_client.get( |
| 24 |
path, |
| 25 |
).status |
| 26 |
) |
| 27 |
|
| 28 |
|
| 29 |
def test_actor_cookie(app_client): |
| 30 |
"""A valid actor cookie sets request.scope['actor']""" |
| 31 |
cookie = app_client.actor_cookie({"id": "test"}) |
| 32 |
response = app_client.get("/", cookies={"ds_actor": cookie}) |
| 33 |
assert {"id": "test"} == app_client.ds._last_request.scope["actor"] |
| 34 |
|
| 35 |
|
| 36 |
def test_actor_cookie_invalid(app_client): |
| 37 |
cookie = app_client.actor_cookie({"id": "test"}) |
| 38 |
# Break the signature |
| 39 |
response = app_client.get("/", cookies={"ds_actor": cookie[:-1] + "."}) |
| 40 |
assert None == app_client.ds._last_request.scope["actor"] |
| 41 |
# Break the cookie format |
| 42 |
cookie = app_client.ds.sign({"b": {"id": "test"}}, "actor") |
| 43 |
response = app_client.get("/", cookies={"ds_actor": cookie}) |
| 44 |
assert None == app_client.ds._last_request.scope["actor"] |
| 45 |
|
| 46 |
|
| 47 |
@pytest.mark.parametrize( |
| 48 |
"offset,expected", |
| 49 |
[ |
| 50 |
((24 * 60 * 60), {"id": "test"}), |
| 51 |
(-(24 * 60 * 60), None), |
| 52 |
], |
| 53 |
) |
| 54 |
def test_actor_cookie_that_expires(app_client, offset, expected): |
| 55 |
expires_at = int(time.time()) + offset |
| 56 |
cookie = app_client.ds.sign( |
| 57 |
{"a": {"id": "test"}, "e": baseconv.base62.encode(expires_at)}, "actor" |
| 58 |
) |
| 59 |
response = app_client.get("/", cookies={"ds_actor": cookie}) |
| 60 |
assert expected == app_client.ds._last_request.scope["actor"] |
| 61 |
|
| 62 |
|
| 63 |
def test_logout(app_client): |
| 64 |
response = app_client.get( |
| 65 |
"/-/logout", cookies={"ds_actor": app_client.actor_cookie({"id": "test"})} |
| 66 |
) |
| 67 |
assert 200 == response.status |
| 68 |
assert "<p>You are logged in as <strong>test</strong></p>" in response.text |
| 69 |
# Actors without an id get full serialization |
| 70 |
response2 = app_client.get( |
| 71 |
"/-/logout", cookies={"ds_actor": app_client.actor_cookie({"name2": "bob"})} |
| 72 |
) |
| 73 |
assert 200 == response2.status |
| 74 |
assert ( |
| 75 |
"<p>You are logged in as <strong>{'name2': 'bob'}</strong></p>" |
| 76 |
in response2.text |
| 77 |
) |
| 78 |
# If logged out you get a redirect to / |
| 79 |
response3 = app_client.get("/-/logout") |
| 80 |
assert 302 == response3.status |
| 81 |
# A POST to that page should log the user out |
| 82 |
response4 = app_client.post( |
| 83 |
"/-/logout", |
| 84 |
csrftoken_from=True, |
| 85 |
cookies={"ds_actor": app_client.actor_cookie({"id": "test"})}, |
| 86 |
) |
| 87 |
# The ds_actor cookie should have been unset |
| 88 |
assert response4.cookie_was_deleted("ds_actor") |
| 89 |
# Should also have set a message |
| 90 |
messages = app_client.ds.unsign(response4.cookies["ds_messages"], "messages") |
| 91 |
assert [["You are now logged out", 2]] == messages |
| 92 |
|
| 93 |
|
| 94 |
@pytest.mark.parametrize("path", ["/", "/fixtures", "/fixtures/facetable"]) |
| 95 |
def test_logout_button_in_navigation(app_client, path): |
| 96 |
response = app_client.get( |
| 97 |
path, cookies={"ds_actor": app_client.actor_cookie({"id": "test"})} |
| 98 |
) |
| 99 |
anon_response = app_client.get(path) |
| 100 |
for fragment in ( |
| 101 |
"<strong>test</strong>", |
| 102 |
'<form action="/-/logout" method="post">', |
| 103 |
): |
| 104 |
assert fragment in response.text |
| 105 |
assert fragment not in anon_response.text |
| 106 |
|
| 107 |
|
| 108 |
@pytest.mark.parametrize("path", ["/", "/fixtures", "/fixtures/facetable"]) |
| 109 |
def test_no_logout_button_in_navigation_if_no_ds_actor_cookie(app_client, path): |
| 110 |
response = app_client.get(path + "?_bot=1") |
| 111 |
assert "<strong>bot</strong>" in response.text |
| 112 |
assert '<form action="/-/logout" method="post">' not in response.text |