Remove hashed URL mode

Also simplified how view class routing works.

Refs #1661
This commit is contained in:
Simon Willison 2022-03-18 17:12:03 -07:00 committed by GitHub
commit d4f60c2388
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 79 additions and 266 deletions

View file

@ -214,12 +214,6 @@ def app_client_two_attached_databases_one_immutable():
yield client
@pytest.fixture(scope="session")
def app_client_with_hash():
with make_app_client(settings={"hash_urls": True}, is_immutable=True) as client:
yield client
@pytest.fixture(scope="session")
def app_client_with_trace():
with make_app_client(settings={"trace_debug": True}, is_immutable=True) as client:

View file

@ -825,35 +825,6 @@ def test_config_redirects_to_settings(app_client, path, expected_redirect):
assert response.headers["Location"] == expected_redirect
@pytest.mark.parametrize(
"path,expected_redirect",
[
("/fixtures/facetable.json?_hash=1", "/fixtures-HASH/facetable.json"),
(
"/fixtures/facetable.json?city_id=1&_hash=1",
"/fixtures-HASH/facetable.json?city_id=1",
),
],
)
def test_hash_parameter(
app_client_two_attached_databases_one_immutable, path, expected_redirect
):
# First get the current hash for the fixtures database
current_hash = app_client_two_attached_databases_one_immutable.ds.databases[
"fixtures"
].hash[:7]
response = app_client_two_attached_databases_one_immutable.get(path)
assert response.status == 302
location = response.headers["Location"]
assert expected_redirect.replace("HASH", current_hash) == location
def test_hash_parameter_ignored_for_mutable_databases(app_client):
path = "/fixtures/facetable.json?_hash=1"
response = app_client.get(path)
assert response.status == 200
test_json_columns_default_expected = [
{"intval": 1, "strval": "s", "floatval": 0.5, "jsonval": '{"foo": "bar"}'}
]

View file

@ -21,61 +21,61 @@ def custom_pages_client_with_base_url():
def test_custom_pages_view_name(custom_pages_client):
response = custom_pages_client.get("/about")
assert 200 == response.status
assert "ABOUT! view_name:page" == response.text
assert response.status == 200
assert response.text == "ABOUT! view_name:page"
def test_request_is_available(custom_pages_client):
response = custom_pages_client.get("/request")
assert 200 == response.status
assert "path:/request" == response.text
assert response.status == 200
assert response.text == "path:/request"
def test_custom_pages_with_base_url(custom_pages_client_with_base_url):
response = custom_pages_client_with_base_url.get("/prefix/request")
assert 200 == response.status
assert "path:/prefix/request" == response.text
assert response.status == 200
assert response.text == "path:/prefix/request"
def test_custom_pages_nested(custom_pages_client):
response = custom_pages_client.get("/nested/nest")
assert 200 == response.status
assert "Nest!" == response.text
assert response.status == 200
assert response.text == "Nest!"
response = custom_pages_client.get("/nested/nest2")
assert 404 == response.status
assert response.status == 404
def test_custom_status(custom_pages_client):
response = custom_pages_client.get("/202")
assert 202 == response.status
assert "202!" == response.text
assert response.status == 202
assert response.text == "202!"
def test_custom_headers(custom_pages_client):
response = custom_pages_client.get("/headers")
assert 200 == response.status
assert "foo" == response.headers["x-this-is-foo"]
assert "bar" == response.headers["x-this-is-bar"]
assert "FOOBAR" == response.text
assert response.status == 200
assert response.headers["x-this-is-foo"] == "foo"
assert response.headers["x-this-is-bar"] == "bar"
assert response.text == "FOOBAR"
def test_custom_content_type(custom_pages_client):
response = custom_pages_client.get("/atom")
assert 200 == response.status
assert response.status == 200
assert response.headers["content-type"] == "application/xml"
assert "<?xml ...>" == response.text
assert response.text == "<?xml ...>"
def test_redirect(custom_pages_client):
response = custom_pages_client.get("/redirect")
assert 302 == response.status
assert "/example" == response.headers["Location"]
assert response.status == 302
assert response.headers["Location"] == "/example"
def test_redirect2(custom_pages_client):
response = custom_pages_client.get("/redirect2")
assert 301 == response.status
assert "/example" == response.headers["Location"]
assert response.status == 301
assert response.headers["Location"] == "/example"
@pytest.mark.parametrize(

View file

@ -5,7 +5,6 @@ from .fixtures import ( # noqa
app_client_base_url_prefix,
app_client_shorter_time_limit,
app_client_two_attached_databases,
app_client_with_hash,
make_app_client,
METADATA,
)
@ -101,13 +100,6 @@ def test_not_allowed_methods():
assert response.status == 405
def test_database_page_redirects_with_url_hash(app_client_with_hash):
response = app_client_with_hash.get("/fixtures")
assert response.status == 302
response = app_client_with_hash.get("/fixtures", follow_redirects=True)
assert "fixtures" in response.text
def test_database_page(app_client):
response = app_client.get("/fixtures")
soup = Soup(response.body, "html.parser")
@ -182,26 +174,6 @@ def test_sql_time_limit(app_client_shorter_time_limit):
assert expected_html_fragment in response.text
def test_row_redirects_with_url_hash(app_client_with_hash):
response = app_client_with_hash.get("/fixtures/simple_primary_key/1")
assert response.status == 302
assert response.headers["Location"].endswith("/1")
response = app_client_with_hash.get(
"/fixtures/simple_primary_key/1", follow_redirects=True
)
assert response.status == 200
def test_row_strange_table_name_with_url_hash(app_client_with_hash):
response = app_client_with_hash.get("/fixtures/table~2Fwith~2Fslashes~2Ecsv/3")
assert response.status == 302
assert response.headers["Location"].endswith("/table~2Fwith~2Fslashes~2Ecsv/3")
response = app_client_with_hash.get(
"/fixtures/table~2Fwith~2Fslashes~2Ecsv/3", follow_redirects=True
)
assert response.status == 200
def test_row_page_does_not_truncate():
with make_app_client(settings={"truncate_cells_html": 5}) as client:
response = client.get("/fixtures/facetable/1")

View file

@ -1,6 +1,5 @@
from datasette.app import Datasette
from datasette.utils import PrefixedUrlString
from .fixtures import app_client_with_hash
import pytest
@ -147,20 +146,3 @@ def test_row(ds, base_url, format, expected):
actual = ds.urls.row("_memory", "facetable", "1", format=format)
assert actual == expected
assert isinstance(actual, PrefixedUrlString)
@pytest.mark.parametrize("base_url", ["/", "/prefix/"])
def test_database_hashed(app_client_with_hash, base_url):
ds = app_client_with_hash.ds
original_base_url = ds._settings["base_url"]
try:
ds._settings["base_url"] = base_url
db_hash = ds.get_database("fixtures").hash
assert len(db_hash) == 64
expected = f"{base_url}fixtures-{db_hash[:7]}"
assert ds.urls.database("fixtures") == expected
assert ds.urls.table("fixtures", "name") == expected + "/name"
assert ds.urls.query("fixtures", "name") == expected + "/name"
finally:
# Reset this since fixture is shared with other tests
ds._settings["base_url"] = original_base_url

View file

@ -2,7 +2,6 @@ from datasette.utils import detect_json1
from datasette.utils.sqlite import sqlite_version
from .fixtures import ( # noqa
app_client,
app_client_with_hash,
app_client_with_trace,
app_client_returned_rows_matches_page_size,
generate_compound_rows,
@ -41,13 +40,6 @@ def test_table_not_exists_json(app_client):
} == app_client.get("/fixtures/blah.json").json
def test_jsono_redirects_to_shape_objects(app_client_with_hash):
response_1 = app_client_with_hash.get("/fixtures/simple_primary_key.jsono")
response = app_client_with_hash.get(response_1.headers["Location"])
assert response.status == 302
assert response.headers["Location"].endswith("?_shape=objects")
def test_table_shape_arrays(app_client):
response = app_client.get("/fixtures/simple_primary_key.json?_shape=arrays")
assert [