Test HTTP redirection
authorJavier Sancho <jsf@jsancho.org>
Fri, 18 Sep 2020 08:31:23 +0000 (10:31 +0200)
committerJavier Sancho <jsf@jsancho.org>
Fri, 18 Sep 2020 08:31:23 +0000 (10:31 +0200)
tests/fixtures.py
tests/test_html.py

index 4cb60cef297aaf5fe12e21ff3591e94f4c9ea613..f6f695da316234e4e3113a3504cc1e049ff355a7 100644 (file)
@@ -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
index e604694bdab37278a2e7178f601c914534987b1e..9363d142014fb1e105283fa12b994f8049128cec 100644 (file)
@@ -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