Skip to content

Commit

Permalink
Use the new db.route mechanism, closes #8, closes #9
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Mar 20, 2022
1 parent bac7775 commit b7ec370
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 24 deletions.
17 changes: 6 additions & 11 deletions datasette_hashed_urls/__init__.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import collections
from datasette import hookimpl
from functools import wraps


@hookimpl
def startup(datasette):
new_databases = collections.OrderedDict()
datasette._hashed_url_databases = {}
for name, database in datasette.databases.items():
if database.hash:
datasette._hashed_url_databases[name] = database.hash[:7]
new_name = "{}-{}".format(name, database.hash[:7])
new_databases[new_name] = database
database.name = new_name
else:
new_databases[name] = database
datasette.databases.clear()
datasette.databases.update(new_databases)
hash = database.hash[:7]
datasette._hashed_url_databases[name] = hash
route = "{}-{}".format(name, hash)
database.route = route
datasette._hashed_url_databases[name] = hash


@hookimpl
Expand All @@ -28,7 +23,7 @@ async def hashed_urls(scope, receive, send):
await app(scope, receive, send)
return
# Only trigger on pages with a path that starts with /xxx
# or /xxx_yyy where xxx is the name of an immutable database
# or /xxx-yyy where xxx is the name of an immutable database
# and where the first page component matches a database name
path = scope["path"].lstrip("/")
first_component = path.split("/")[0]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def get_long_description():
version=VERSION,
packages=["datasette_hashed_urls"],
entry_points={"datasette": ["hashed_urls = datasette_hashed_urls"]},
install_requires=["datasette"],
install_requires=["datasette>=0.61a0"],
extras_require={"test": ["pytest", "pytest-asyncio", "sqlite-utils"]},
python_requires=">=3.7",
)
43 changes: 31 additions & 12 deletions tests/test_hashed_urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datasette.app import Datasette
import pytest
import pytest_asyncio
import sqlite_utils


Expand All @@ -13,20 +14,18 @@ def db_files(tmpdir):
return mutable, immutable


@pytest.fixture
def ds(db_files):
return Datasette(files=[db_files[0]], immutables=[db_files[1]])
@pytest_asyncio.fixture
async def ds(db_files):
ds = Datasette(files=[db_files[0]], immutables=[db_files[1]])
await ds.invoke_startup()
return ds


@pytest.mark.asyncio
async def test_immutable_database_renamed_on_startup(ds):
await ds.invoke_startup()
databases = (await ds.client.get("/-/databases.json")).json()
names = [db["name"] for db in databases]
assert len(names) == 2
assert "this-is-mutable" in names
other_name = [name for name in names if name != "this-is-mutable"][0]
assert other_name.startswith("this-is-immutable-")
async def test_immutable_database_has_new_route_on_startup(ds):

route = ds.databases["this-is-immutable"].route
assert route.startswith("this-is-immutable-")


@pytest.mark.asyncio
Expand All @@ -44,7 +43,6 @@ async def test_immutable_database_renamed_on_startup(ds):
),
)
async def test_paths_with_no_hash_redirect(ds, path, should_redirect):
await ds.invoke_startup()
immutable_hash = ds._hashed_url_databases["this-is-immutable"]
response = await ds.client.get(path)
assert (
Expand Down Expand Up @@ -77,3 +75,24 @@ async def test_paths_with_hash_have_cache_header(db_files, path_suffix, max_age)
cache_control = response.headers["cache-control"]
expected = "max-age={}, public".format(max_age or 31536000)
assert cache_control == expected


@pytest.mark.asyncio
async def test_index_page(ds):
hash = ds.get_database("this-is-immutable").hash[:7]
response = await ds.client.get("/")
assert (
'<a href="/this-is-immutable-{}">this-is-immutable</a>'.format(hash)
in response.text
)


@pytest.mark.asyncio
@pytest.mark.parametrize(
"path",
("/", "/this-is-immutable", "/this-is-immutable/t", "/this-is-immutable/t/1"),
)
async def test_links_on_pages(ds, path):
hash = ds.get_database("this-is-immutable").hash[:7]
response = await ds.client.get(path, follow_redirects=True)
assert "/this-is-immutable-{}".format(hash) in response.text

0 comments on commit b7ec370

Please sign in to comment.