kopia lustrzana https://github.com/simonw/datasette
CSV export now respects --cors, fixes #326
rodzic
e7566cc59d
commit
64c2fea8df
|
@ -228,6 +228,8 @@ class BaseView(RenderMixin):
|
||||||
|
|
||||||
content_type = "text/plain; charset=utf-8"
|
content_type = "text/plain; charset=utf-8"
|
||||||
headers = {}
|
headers = {}
|
||||||
|
if self.ds.cors:
|
||||||
|
headers["Access-Control-Allow-Origin"] = "*"
|
||||||
if request.args.get("_dl", None):
|
if request.args.get("_dl", None):
|
||||||
content_type = "text/csv; charset=utf-8"
|
content_type = "text/csv; charset=utf-8"
|
||||||
disposition = 'attachment; filename="{}.csv"'.format(
|
disposition = 'attachment; filename="{}.csv"'.format(
|
||||||
|
|
|
@ -23,32 +23,39 @@ class TestClient:
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope='session')
|
@pytest.fixture(scope="session")
|
||||||
def app_client(sql_time_limit_ms=None, max_returned_rows=None, config=None, filename="fixtures.db"):
|
def app_client(
|
||||||
|
sql_time_limit_ms=None,
|
||||||
|
max_returned_rows=None,
|
||||||
|
cors=False,
|
||||||
|
config=None,
|
||||||
|
filename="fixtures.db",
|
||||||
|
):
|
||||||
with tempfile.TemporaryDirectory() as tmpdir:
|
with tempfile.TemporaryDirectory() as tmpdir:
|
||||||
filepath = os.path.join(tmpdir, filename)
|
filepath = os.path.join(tmpdir, filename)
|
||||||
conn = sqlite3.connect(filepath)
|
conn = sqlite3.connect(filepath)
|
||||||
conn.executescript(TABLES)
|
conn.executescript(TABLES)
|
||||||
os.chdir(os.path.dirname(filepath))
|
os.chdir(os.path.dirname(filepath))
|
||||||
plugins_dir = os.path.join(tmpdir, 'plugins')
|
plugins_dir = os.path.join(tmpdir, "plugins")
|
||||||
os.mkdir(plugins_dir)
|
os.mkdir(plugins_dir)
|
||||||
open(os.path.join(plugins_dir, 'my_plugin.py'), 'w').write(PLUGIN1)
|
open(os.path.join(plugins_dir, "my_plugin.py"), "w").write(PLUGIN1)
|
||||||
open(os.path.join(plugins_dir, 'my_plugin_2.py'), 'w').write(PLUGIN2)
|
open(os.path.join(plugins_dir, "my_plugin_2.py"), "w").write(PLUGIN2)
|
||||||
config = config or {}
|
config = config or {}
|
||||||
config.update({
|
config.update(
|
||||||
'default_page_size': 50,
|
{
|
||||||
'max_returned_rows': max_returned_rows or 100,
|
"default_page_size": 50,
|
||||||
'sql_time_limit_ms': sql_time_limit_ms or 200,
|
"max_returned_rows": max_returned_rows or 100,
|
||||||
})
|
"sql_time_limit_ms": sql_time_limit_ms or 200,
|
||||||
|
}
|
||||||
|
)
|
||||||
ds = Datasette(
|
ds = Datasette(
|
||||||
[filepath],
|
[filepath],
|
||||||
|
cors=cors,
|
||||||
metadata=METADATA,
|
metadata=METADATA,
|
||||||
plugins_dir=plugins_dir,
|
plugins_dir=plugins_dir,
|
||||||
config=config,
|
config=config,
|
||||||
)
|
)
|
||||||
ds.sqlite_functions.append(
|
ds.sqlite_functions.append(("sleep", 1, lambda n: time.sleep(float(n))))
|
||||||
('sleep', 1, lambda n: time.sleep(float(n))),
|
|
||||||
)
|
|
||||||
client = TestClient(ds.app().test_client)
|
client = TestClient(ds.app().test_client)
|
||||||
client.ds = ds
|
client.ds = ds
|
||||||
yield client
|
yield client
|
||||||
|
@ -83,6 +90,11 @@ def app_client_with_dot():
|
||||||
yield from app_client(filename="fixtures.dot.db")
|
yield from app_client(filename="fixtures.dot.db")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope='session')
|
||||||
|
def app_client_with_cors():
|
||||||
|
yield from app_client(cors=True)
|
||||||
|
|
||||||
|
|
||||||
def generate_compound_rows(num):
|
def generate_compound_rows(num):
|
||||||
for a, b, c in itertools.islice(
|
for a, b, c in itertools.islice(
|
||||||
itertools.product(string.ascii_lowercase, repeat=3), num
|
itertools.product(string.ascii_lowercase, repeat=3), num
|
||||||
|
|
|
@ -1,4 +1,8 @@
|
||||||
from .fixtures import app_client, app_client_csv_max_mb_one # noqa
|
from .fixtures import ( # noqa
|
||||||
|
app_client,
|
||||||
|
app_client_csv_max_mb_one,
|
||||||
|
app_client_with_cors
|
||||||
|
)
|
||||||
|
|
||||||
EXPECTED_TABLE_CSV = '''id,content
|
EXPECTED_TABLE_CSV = '''id,content
|
||||||
1,hello
|
1,hello
|
||||||
|
@ -30,13 +34,21 @@ pk,planet_int,on_earth,state,city_id,city_id_label,neighborhood
|
||||||
15,2,0,MC,4,Memnonia,Arcadia Planitia
|
15,2,0,MC,4,Memnonia,Arcadia Planitia
|
||||||
'''.lstrip().replace('\n', '\r\n')
|
'''.lstrip().replace('\n', '\r\n')
|
||||||
|
|
||||||
|
|
||||||
def test_table_csv(app_client):
|
def test_table_csv(app_client):
|
||||||
response = app_client.get('/fixtures/simple_primary_key.csv')
|
response = app_client.get('/fixtures/simple_primary_key.csv')
|
||||||
assert response.status == 200
|
assert response.status == 200
|
||||||
|
assert not response.headers.get("Access-Control-Allow-Origin")
|
||||||
assert 'text/plain; charset=utf-8' == response.headers['Content-Type']
|
assert 'text/plain; charset=utf-8' == response.headers['Content-Type']
|
||||||
assert EXPECTED_TABLE_CSV == response.text
|
assert EXPECTED_TABLE_CSV == response.text
|
||||||
|
|
||||||
|
|
||||||
|
def test_table_csv_cors_headers(app_client_with_cors):
|
||||||
|
response = app_client_with_cors.get('/fixtures/simple_primary_key.csv')
|
||||||
|
assert response.status == 200
|
||||||
|
assert "*" == response.headers["Access-Control-Allow-Origin"]
|
||||||
|
|
||||||
|
|
||||||
def test_table_csv_with_labels(app_client):
|
def test_table_csv_with_labels(app_client):
|
||||||
response = app_client.get('/fixtures/facetable.csv?_labels=1')
|
response = app_client.get('/fixtures/facetable.csv?_labels=1')
|
||||||
assert response.status == 200
|
assert response.status == 200
|
||||||
|
|
Ładowanie…
Reference in New Issue