Skip to content

Commit

Permalink
Do not show table header if no tables, closes #32
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Feb 20, 2024
1 parent 6959131 commit f2a629a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
4 changes: 2 additions & 2 deletions datasette_auth_tokens/templates/create_api_token.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ <h2>All tables in "{{ database.name }}"</h2>
{% endfor %}
</ul>
{% endfor %}
{% if database_with_tables %}
{% if databases_with_at_least_one_table %}
<h2>Specific tables in specific databases</h2>
{% for database in database_with_tables %}
{% for database in databases_with_at_least_one_table %}
{% for table in database.tables %}
<h3>{{ database.name }}: {{ table.name }}</h3>
<ul>
Expand Down
18 changes: 11 additions & 7 deletions datasette_auth_tokens/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ async def _shared(datasette, request):
)
# Build list of databases and tables the user has permission to view
database_with_tables = []
databases_with_at_least_one_table = []
for database in datasette.databases.values():
if database.name in ("_internal", "_memory"):
continue
Expand All @@ -148,13 +149,15 @@ async def _shared(datasette, request):
):
continue
tables.append({"name": table, "encoded": tilde_encode(table)})
database_with_tables.append(
{
"name": database.name,
"encoded": tilde_encode(database.name),
"tables": tables,
}
)

db_info = {
"name": database.name,
"encoded": tilde_encode(database.name),
"tables": tables,
}
database_with_tables.append(db_info)
if tables:
databases_with_at_least_one_table.append(db_info)
return {
"actor": request.actor,
"all_permissions": [
Expand All @@ -179,6 +182,7 @@ async def _shared(datasette, request):
if value.takes_resource
],
"database_with_tables": database_with_tables,
"databases_with_at_least_one_table": databases_with_at_least_one_table,
"tokens_exist": tokens_exist,
}

Expand Down
30 changes: 30 additions & 0 deletions tests/test_managed_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,3 +410,33 @@ async def test_tokens_cannot_be_restricted_to_auth_tokens_revoke_all(ds_managed)
"/-/api/tokens/create", cookies={"ds_actor": root_cookie}
)
assert "auth-tokens-revoke-all" not in create_page.text


@pytest.mark.asyncio
@pytest.mark.parametrize("has_a_table", (True, False))
async def test_no_table_heading_if_no_tables(tmpdir, has_a_table):
# https://github.com/simonw/datasette-auth-tokens/issues/32
db_path = str(tmpdir / "empty.db")
db = sqlite_utils.Database(db_path)
db.vacuum()
if has_a_table:
db["foo"].insert({"bar": 1})
ds = Datasette(
[db_path],
plugin_config={"datasette-auth-tokens": {"manage_tokens": True}},
config={
"permissions": {
"auth-tokens-create": {"id": "*"},
},
},
)
response = await ds.client.get(
"/-/api/tokens/create",
cookies={"ds_actor": ds.client.actor_cookie({"id": "admin"})},
)
assert response.status_code == 200
fragment = ">Specific tables in specific databases<"
if has_a_table:
assert fragment in response.text
else:
assert fragment not in response.text

0 comments on commit f2a629a

Please sign in to comment.