Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make compatible with <1.0 and >1.0a #16

Merged
merged 4 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ permissions:

jobs:
test:
runs-on: ubicloud
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
Expand All @@ -28,7 +28,7 @@ jobs:
run: |
pytest
deploy:
runs-on: ubicloud
runs-on: ubuntu-latest
needs: [test]
environment: release
permissions:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ jobs:
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
datasette-version: ["<1.0", ">=1.0a13"]
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
Expand All @@ -22,6 +23,7 @@ jobs:
- name: Install dependencies
run: |
pip install '.[test]'
pip install "datasette${{ matrix.datasette-version }}"
- name: Run tests
run: |
if [ -d tests/ ]; then
Expand Down
31 changes: 20 additions & 11 deletions datasette_secrets/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import click
from cryptography.fernet import Fernet
import dataclasses
from datasette import hookimpl, Forbidden, Permission, Response
from datasette import hookimpl, Forbidden, Response
from datasette.plugins import pm
from datasette.utils import await_me_maybe, sqlite3
import os
Expand Down Expand Up @@ -89,7 +89,7 @@ class Secret:
def get_database(datasette):
plugin_config = datasette.plugin_config("datasette-secrets") or {}
database = plugin_config.get("database") or "_internal"
if database == "_internal":
if database == "_internal" and hasattr(datasette, "get_internal_database"):
return datasette.get_internal_database()
return datasette.get_database(database)

Expand All @@ -108,6 +108,8 @@ def get_config(datasette):

@hookimpl
def register_permissions(datasette):
from datasette import Permission

return [
Permission(
name="manage-secrets",
Expand Down Expand Up @@ -187,15 +189,22 @@ async def secrets_index(datasette, request):
)
existing_secrets = {row["name"]: dict(row) for row in existing_secrets_result.rows}
# Try to turn updated_by into actors
actors = await datasette.actors_from_ids(
{row["updated_by"] for row in existing_secrets.values() if row["updated_by"]}
)
for secret in existing_secrets.values():
if secret["updated_by"]:
actor = actors.get(secret["updated_by"])
if actor:
display = actor.get("username") or actor.get("name") or actor.get("id")
secret["updated_by"] = display
if hasattr(datasette, "actors_from_ids"):
actors = await datasette.actors_from_ids(
{
Comment on lines +192 to +194
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To compensate for datasette.actors_from_ids() not being in Datasette <1.0

row["updated_by"]
for row in existing_secrets.values()
if row["updated_by"]
}
)
for secret in existing_secrets.values():
if secret["updated_by"]:
actor = actors.get(secret["updated_by"])
if actor:
display = (
actor.get("username") or actor.get("name") or actor.get("id")
)
secret["updated_by"] = display
unset_secrets = [
secret
for secret in all_secrets
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ classifiers=[
]
requires-python = ">=3.8"
dependencies = [
"datasette>=1.0a13",
"datasette",
"cryptography"
]

Expand All @@ -25,7 +25,7 @@ CI = "https://github.com/datasette/datasette-secrets/actions"
secrets = "datasette_secrets"

[project.optional-dependencies]
test = ["pytest", "pytest-asyncio"]
test = ["pytest", "pytest-asyncio", "datasette-test>=0.3"]

[tool.pytest.ini_options]
asyncio_mode = "strict"
Expand Down
44 changes: 27 additions & 17 deletions tests/test_secrets.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
from click.testing import CliRunner
from cryptography.fernet import Fernet
from datasette import hookimpl
from datasette.app import Datasette
from datasette.cli import cli
from datasette.plugins import pm
from datasette_test import Datasette, actor_cookie
from datasette_secrets import get_secret, Secret, startup, get_config
import pytest
from unittest.mock import ANY

TEST_ENCRYPTION_KEY = "-LujHtwFWGaBpznrV1zduoZBmCnMOW7J0H5hmeXgAVo="


def get_internal_database(ds):
if hasattr(ds, "get_internal_database"):
return ds.get_internal_database()
else:
return ds.get_database("_internal")


Comment on lines +14 to +20
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bit of a gross hack here to get the internal database working in tests.

def test_generate_command():
runner = CliRunner()
result = runner.invoke(cli, ["secrets", "generate-encryption-key"])
Expand Down Expand Up @@ -89,15 +96,13 @@ def register_secrets(self):
@pytest.fixture
def ds():
return Datasette(
config={
"plugins": {
"datasette-secrets": {
"database": "_internal",
"encryption-key": TEST_ENCRYPTION_KEY,
}
},
"permissions": {"manage-secrets": {"id": "admin"}},
}
plugin_config={
"datasette-secrets": {
"database": "_internal",
"encryption-key": TEST_ENCRYPTION_KEY,
}
},
permissions={"manage-secrets": {"id": "admin"}},
Comment on lines +99 to +105
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

)


Expand All @@ -115,7 +120,7 @@ async def test_permissions(ds, path, verb, data, user):
kwargs = {}
if user:
kwargs["cookies"] = {
"ds_actor": ds.client.actor_cookie({"id": user}),
"ds_actor": actor_cookie(ds, {"id": user}),
}
if data:
kwargs["data"] = data
Expand All @@ -131,7 +136,7 @@ async def test_permissions(ds, path, verb, data, user):

@pytest.mark.asyncio
async def test_set_secret(ds, use_actors_plugin):
cookies = {"ds_actor": ds.client.actor_cookie({"id": "admin"})}
cookies = {"ds_actor": actor_cookie(ds, {"id": "admin"})}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using this new feature in datasette-test:

get_response = await ds.client.get("/-/secrets/EXAMPLE_SECRET", cookies=cookies)
csrftoken = get_response.cookies["ds_csrftoken"]
cookies["ds_csrftoken"] = csrftoken
Expand All @@ -142,7 +147,7 @@ async def test_set_secret(ds, use_actors_plugin):
)
assert post_response.status_code == 302
assert post_response.headers["Location"] == "/-/secrets"
internal_db = ds.get_internal_database()
internal_db = get_internal_database(ds)
secrets = await internal_db.execute("select * from datasette_secrets")
rows = [dict(r) for r in secrets.rows]
assert rows == [
Expand Down Expand Up @@ -174,7 +179,12 @@ async def test_set_secret(ds, use_actors_plugin):
assert response.status_code == 200
assert "EXAMPLE_SECRET" in response.text
assert "new-note" in response.text
assert "<td>ADMIN</td>" in response.text

if hasattr(ds, "actors_from_ids"):
assert "<td>ADMIN</td>" in response.text
else:
# Pre 1.0, so can't use that mechanism
assert "<td>admin</td>" in response.text

# Now let's edit it
post_response2 = await ds.client.post(
Expand Down Expand Up @@ -213,11 +223,11 @@ async def test_set_secret(ds, use_actors_plugin):
@pytest.mark.asyncio
async def test_get_secret(ds, monkeypatch):
# First set it manually
cookies = {"ds_actor": ds.client.actor_cookie({"id": "admin"})}
cookies = {"ds_actor": actor_cookie(ds, {"id": "admin"})}
get_response = await ds.client.get("/-/secrets/EXAMPLE_SECRET", cookies=cookies)
csrftoken = get_response.cookies["ds_csrftoken"]
cookies["ds_csrftoken"] = csrftoken
db = ds.get_internal_database()
db = get_internal_database(ds)
# Reset state
await db.execute_write(
"update datasette_secrets set last_used_at = null, last_used_by = null"
Expand Down Expand Up @@ -288,7 +298,7 @@ async def test_secret_index_page(ds, register_multiple_secrets):
response = await ds.client.get(
"/-/secrets",
cookies={
"ds_actor": ds.client.actor_cookie({"id": "admin"}),
"ds_actor": actor_cookie(ds, {"id": "admin"}),
},
)
assert response.status_code == 200
Expand Down
Loading