Skip to content

Commit

Permalink
Fix for 500 error if not configured, closes #14
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Apr 25, 2024
1 parent e81e9a4 commit b7eecfc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
5 changes: 4 additions & 1 deletion datasette_secrets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ async def get_secret(datasette, secret_name, actor_id=None):
return os.environ[env_var]
# Now look it up in the database
config = get_config(datasette)
if config is None:
return None
encryption_key = config["encryption_key"]
db = get_database(datasette)
try:
db_secret = (
Expand All @@ -35,7 +38,7 @@ async def get_secret(datasette, secret_name, actor_id=None):
return None
if not db_secret:
return None
key = Fernet(config["encryption_key"].encode("utf-8"))
key = Fernet(encryption_key.encode("utf-8"))
decrypted = key.decrypt(db_secret["encrypted"])
# Update the last used timestamp and actor_id
params = (actor_id, db_secret["id"])
Expand Down
10 changes: 9 additions & 1 deletion tests/test_secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from datasette.app import Datasette
from datasette.cli import cli
from datasette.plugins import pm
from datasette_secrets import get_secret, Secret, startup
from datasette_secrets import get_secret, Secret, startup, get_config
import pytest
from unittest.mock import ANY

Expand Down Expand Up @@ -275,6 +275,14 @@ async def test_get_secret(ds, monkeypatch):
assert await get_secret(ds, "EXAMPLE_SECRET") is None


@pytest.mark.asyncio
async def test_if_not_configured(register_multiple_secrets):
ds = Datasette()
config = get_config(ds)
assert config is None
assert await get_secret(ds, "OPENAI_API_KEY") is None


@pytest.mark.asyncio
async def test_secret_index_page(ds, register_multiple_secrets):
response = await ds.client.get(
Expand Down

0 comments on commit b7eecfc

Please sign in to comment.