From f1dd3e8c2e09f511840c1cfa8243fb83a6174705 Mon Sep 17 00:00:00 2001 From: Javier Sancho Date: Fri, 18 Sep 2020 10:31:23 +0200 Subject: [PATCH] Test HTTP redirection --- tests/fixtures.py | 48 +++++++++++++++++++++++++++++++++++----------- tests/test_html.py | 12 ++++++------ 2 files changed, 43 insertions(+), 17 deletions(-) diff --git a/tests/fixtures.py b/tests/fixtures.py index 4cb60ce..f6f695d 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -8,26 +8,52 @@ from datasette.utils.testing import TestClient import os import pytest import tempfile +import contextlib -@pytest.fixture(scope='session') -def app_client(max_returned_rows=None): +def populate_file(filepath): + dummyfile = open(filepath, "w") + dummyfile.write("This is a dummy file. We need something to force a SQLite error") + dummyfile.close() + + +@contextlib.contextmanager +def make_app_client( + max_returned_rows=None, + config=None, + is_immutable=False, +): with tempfile.TemporaryDirectory() as tmpdir: filepath = os.path.join(tmpdir, 'dummy_tables.db') populate_file(filepath) + if is_immutable: + files = [] + immutables = [filepath] + else: + files = [filepath] + immutables = [] + config = config or {} + config.update({ + 'default_page_size': 50, + 'max_returned_rows': max_returned_rows or 1000, + }) ds = Datasette( - [filepath], - config={ - 'default_page_size': 50, - 'max_returned_rows': max_returned_rows or 1000, - } + files, + immutables=immutables, + config=config, ) client = TestClient(ds.app()) client.ds = ds yield client -def populate_file(filepath): - dummyfile = open(filepath, "w") - dummyfile.write("This is a dummy file. We need something to force a SQLite error") - dummyfile.close() +@pytest.fixture(scope='session') +def app_client(): + with make_app_client() as client: + yield client + + +@pytest.fixture(scope='session') +def app_client_with_hash(): + with make_app_client(config={"hash_urls": True}, is_immutable=True) as client: + yield client diff --git a/tests/test_html.py b/tests/test_html.py index e604694..9363d14 100644 --- a/tests/test_html.py +++ b/tests/test_html.py @@ -1,16 +1,16 @@ -from .fixtures import app_client +from .fixtures import app_client, app_client_with_hash def test_homepage(app_client): - response = app_client.get('/', gather_request=False) + response = app_client.get('/') assert response.status == 200 assert 'dummy_tables' in response.text -def test_database_page(app_client): - response = app_client.get('/dummy_tables', allow_redirects=False, gather_request=False) +def test_database_page(app_client_with_hash): + response = app_client_with_hash.get('/dummy_tables', allow_redirects=False) assert response.status == 302 - response = app_client.get('/dummy_tables', gather_request=False) + response = app_client_with_hash.get('/dummy_tables') assert 'dummy_tables' in response.text def test_table(app_client): - response = app_client.get('/dummy_tables/table2', gather_request=False) + response = app_client.get('/dummy_tables/table2') assert response.status == 200 -- 2.39.2