diff --git a/datasette_saved_queries/__init__.py b/datasette_saved_queries/__init__.py index 14926fc..2d0e13b 100644 --- a/datasette_saved_queries/__init__.py +++ b/datasette_saved_queries/__init__.py @@ -6,7 +6,12 @@ def create_tables(conn): db = sqlite_utils.Database(conn) if not db["saved_queries"].exists(): db["saved_queries"].create( - {"name": str, "sql": str, "author_id": str,}, pk="name" + { + "name": str, + "sql": str, + "author_id": str, + }, + pk="name", ) diff --git a/tests/test_saved_queries.py b/tests/test_saved_queries.py index 0034689..3235727 100644 --- a/tests/test_saved_queries.py +++ b/tests/test_saved_queries.py @@ -1,7 +1,6 @@ from datasette.app import Datasette import pytest import sqlite3 -import httpx @pytest.fixture @@ -12,81 +11,79 @@ def db_path(tmpdir): @pytest.fixture -@pytest.mark.asyncio -async def ds(db_path): - ds = Datasette([db_path]) - await ds.invoke_startup() - yield ds +def ds(db_path): + return Datasette([db_path]) @pytest.mark.asyncio async def test_plugin_is_installed(ds): - async with httpx.AsyncClient(app=ds.app()) as client: - response = await client.get("http://localhost/-/plugins.json") - assert 200 == response.status_code - installed_plugins = {p["name"] for p in response.json()} - assert "datasette-saved-queries" in installed_plugins + response = await ds.client.get("/-/plugins.json") + assert 200 == response.status_code + installed_plugins = {p["name"] for p in response.json()} + assert "datasette-saved-queries" in installed_plugins @pytest.mark.asyncio async def test_table_created(ds): - async with httpx.AsyncClient(app=ds.app()) as client: - response = await client.get("http://localhost/data/saved_queries.json") - assert 200 == response.status_code - assert ["name", "sql", "author_id"] == response.json()["columns"] + response = await ds.client.get("/data/saved_queries.json") + assert 200 == response.status_code + assert ["name", "sql", "author_id"] == response.json()["columns"] @pytest.mark.asyncio async def test_save_query(ds): - async with httpx.AsyncClient(app=ds.app()) as client: - # Get the csrftoken cookie - response1 = await client.get("http://localhost/data/save_query") - # Now save a new query - response2 = await client.post( - "http://localhost/data/save_query", - data={ - "name": "new_query", - "sql": "select 1 + 1", - "csrftoken": response1.cookies["ds_csrftoken"], - }, - allow_redirects=False, - ) - assert 302 == response2.status_code - # Check the query was saved - response3 = await client.get( - "http://localhost/data/saved_queries.json?_shape=array" - ) - assert [ - {"name": "new_query", "sql": "select 1 + 1", "author_id": None,} - ] == response3.json() - # ... and that we can run the query - response3 = await client.get( - "http://localhost/data/new_query.json?_shape=array" - ) - assert [{"1 + 1": 2}] == response3.json() + # Get the csrftoken cookie + response1 = await ds.client.get("/data/save_query") + # Now save a new query + response2 = await ds.client.post( + "/data/save_query", + data={ + "name": "new_query", + "sql": "select 1 + 1", + "csrftoken": response1.cookies["ds_csrftoken"], + }, + ) + assert 302 == response2.status_code + # Check the query was saved + response3 = await ds.client.get("/data/saved_queries.json?_shape=array") + assert [ + { + "name": "new_query", + "sql": "select 1 + 1", + "author_id": None, + } + ] == response3.json() + # ... and that we can run the query + response3 = await ds.client.get("/data/new_query.json?_shape=array") + assert [{"1 + 1": 2}] == response3.json() @pytest.mark.asyncio async def test_save_query_authenticated_actor(ds): - async with httpx.AsyncClient(app=ds.app()) as client: - response1 = await client.get("http://localhost/data/save_query") - response2 = await client.post( - "http://localhost/data/save_query", - data={ - "name": "new_query", - "sql": "select 1 + 1", - "csrftoken": response1.cookies["ds_csrftoken"], - }, - cookies={"ds_actor": ds.sign({"a": {"id": "root"}}, "actor")}, - allow_redirects=False, - ) - assert 302 == response2.status_code - response3 = await client.get( - "http://localhost/data/saved_queries.json?_shape=array" - ) - assert [ - {"name": "new_query", "sql": "select 1 + 1", "author_id": "root",} - ] == response3.json() + ds_actor = ds.sign({"a": {"id": "root"}}, "actor") + response1 = await ds.client.get( + "/data/save_query", + cookies={"ds_actor": ds_actor}, + ) + csrftoken = response1.cookies["ds_csrftoken"] + response2 = await ds.client.post( + "/data/save_query", + data={ + "name": "new_query", + "sql": "select 1 + 1", + "csrftoken": csrftoken, + }, + cookies={"ds_actor": ds_actor, "ds_csrftoken": csrftoken}, + ) + assert 302 == response2.status_code + response3 = await ds.client.get("/data/saved_queries.json?_shape=array") + assert [ + { + "name": "new_query", + "sql": "select 1 + 1", + "author_id": "root", + } + ] == response3.json() @pytest.mark.asyncio