DatabaseDownload no longer uses .inspect(), refs #420

pull/426/head
Simon Willison 2019-03-31 19:04:17 -07:00
rodzic 29a3896fe1
commit 468c6fd953
3 zmienionych plików z 29 dodań i 5 usunięć

Wyświetl plik

@ -43,7 +43,14 @@ class DatabaseDownload(BaseView):
async def view_get(self, request, database, hash, correct_hash_present, **kwargs):
if not self.ds.config("allow_download"):
raise DatasetteError("Database download is forbidden", status=403)
filepath = self.ds.inspect()[database]["file"]
if database not in self.ds.databases:
raise DatasetteError("Invalid database", status=404)
db = self.ds.databases[database]
if db.is_memory:
raise DatasetteError("Cannot download :memory: database", status=404)
if not db.path:
raise DatasetteError("Cannot download database", status=404)
filepath = db.path
return await response.file_stream(
filepath,
filename=os.path.basename(filepath),

Wyświetl plik

@ -27,6 +27,7 @@ def make_app_client(
sql_time_limit_ms=None,
max_returned_rows=None,
cors=False,
memory=False,
config=None,
filename="fixtures.db",
is_immutable=False,
@ -51,6 +52,7 @@ def make_app_client(
ds = Datasette(
[] if is_immutable else [filepath],
immutables=[filepath] if is_immutable else [],
memory=memory,
cors=cors,
metadata=METADATA,
plugins_dir=plugins_dir,
@ -74,6 +76,9 @@ def app_client_no_files():
client.ds = ds
yield client
@pytest.fixture(scope="session")
def app_client_with_memory():
yield from make_app_client(memory=True)
@pytest.fixture(scope="session")
def app_client_with_hash():

Wyświetl plik

@ -3,6 +3,7 @@ from .fixtures import ( # noqa
app_client,
app_client_shorter_time_limit,
app_client_with_hash,
app_client_with_memory,
make_app_client,
)
import pytest
@ -676,12 +677,25 @@ def test_table_metadata(app_client):
assert_footer_links(soup)
def test_allow_download_on(app_client):
response = app_client.get(
def test_database_download(app_client_with_memory):
# Regular page should have a download link
response = app_client_with_memory.get(
"/fixtures"
)
soup = Soup(response.body, 'html.parser')
assert len(soup.findAll('a', {'href': re.compile(r'\.db$')}))
# Check we can actually download it
assert 200 == app_client_with_memory.get(
"/fixtures.db",
).status
# Memory page should NOT have a download link
response2 = app_client_with_memory.get("/:memory:")
soup2 = Soup(response2.body, 'html.parser')
assert 0 == len(soup2.findAll('a', {'href': re.compile(r'\.db$')}))
# The URL itself should 404
assert 404 == app_client_with_memory.get(
"/:memory:.db",
).status
def test_allow_download_off():
@ -690,14 +704,12 @@ def test_allow_download_off():
}):
response = client.get(
"/fixtures",
)
soup = Soup(response.body, 'html.parser')
assert not len(soup.findAll('a', {'href': re.compile(r'\.db$')}))
# Accessing URL directly should 403
response = client.get(
"/fixtures.db",
)
assert 403 == response.status