| 1 |
from bs4 import BeautifulSoup as Soup |
| 2 |
from datasette.utils import allowed_pragmas |
| 3 |
from .fixtures import ( # noqa |
| 4 |
app_client, |
| 5 |
app_client_base_url_prefix, |
| 6 |
app_client_shorter_time_limit, |
| 7 |
app_client_two_attached_databases, |
| 8 |
make_app_client, |
| 9 |
METADATA, |
| 10 |
) |
| 11 |
from .utils import assert_footer_links, inner_html |
| 12 |
import json |
| 13 |
import pathlib |
| 14 |
import pytest |
| 15 |
import re |
| 16 |
import urllib.parse |
| 17 |
|
| 18 |
|
| 19 |
def test_homepage(app_client_two_attached_databases): |
| 20 |
response = app_client_two_attached_databases.get("/") |
| 21 |
assert response.status == 200 |
| 22 |
assert "text/html; charset=utf-8" == response.headers["content-type"] |
| 23 |
soup = Soup(response.body, "html.parser") |
| 24 |
assert "Datasette Fixtures" == soup.find("h1").text |
| 25 |
assert ( |
| 26 |
"An example SQLite database demonstrating Datasette. Sign in as root user" |
| 27 |
== soup.select(".metadata-description")[0].text.strip() |
| 28 |
) |
| 29 |
# Should be two attached databases |
| 30 |
assert [ |
| 31 |
{"href": "/extra+database", "text": "extra database"}, |
| 32 |
{"href": "/fixtures", "text": "fixtures"}, |
| 33 |
] == [{"href": a["href"], "text": a.text.strip()} for a in soup.select("h2 a")] |
| 34 |
# Database should show count text and attached tables |
| 35 |
h2 = soup.select("h2")[0] |
| 36 |
assert "extra database" == h2.text.strip() |
| 37 |
counts_p, links_p = h2.find_all_next("p")[:2] |
| 38 |
assert ( |
| 39 |
"2 rows in 1 table, 5 rows in 4 hidden tables, 1 view" == counts_p.text.strip() |
| 40 |
) |
| 41 |
# We should only show visible, not hidden tables here: |
| 42 |
table_links = [ |
| 43 |
{"href": a["href"], "text": a.text.strip()} for a in links_p.find_all("a") |
| 44 |
] |
| 45 |
assert [ |
| 46 |
{"href": r"/extra+database/searchable", "text": "searchable"}, |
| 47 |
{"href": r"/extra+database/searchable_view", "text": "searchable_view"}, |
| 48 |
] == table_links |
| 49 |
|
| 50 |
|
| 51 |
def test_http_head(app_client): |
| 52 |
response = app_client.get("/", method="HEAD") |
| 53 |
assert response.status == 200 |
| 54 |
|
| 55 |
|
| 56 |
def test_homepage_options(app_client): |
| 57 |
response = app_client.get("/", method="OPTIONS") |
| 58 |
assert response.status == 405 |
| 59 |
assert response.text == "Method not allowed" |
| 60 |
|
| 61 |
|
| 62 |
def test_favicon(app_client): |
| 63 |
response = app_client.get("/favicon.ico") |
| 64 |
assert response.status == 200 |
| 65 |
assert response.headers["cache-control"] == "max-age=3600, immutable, public" |
| 66 |
assert int(response.headers["content-length"]) > 100 |
| 67 |
assert response.headers["content-type"] == "image/png" |
| 68 |
|
| 69 |
|
| 70 |
def test_static(app_client): |
| 71 |
response = app_client.get("/-/static/app2.css") |
| 72 |
assert response.status == 404 |
| 73 |
response = app_client.get("/-/static/app.css") |
| 74 |
assert response.status == 200 |
| 75 |
assert "text/css" == response.headers["content-type"] |
| 76 |
|
| 77 |
|
| 78 |
def test_static_mounts(): |
| 79 |
with make_app_client( |
| 80 |
static_mounts=[("custom-static", str(pathlib.Path(__file__).parent))] |
| 81 |
) as client: |
| 82 |
response = client.get("/custom-static/test_html.py") |
| 83 |
assert response.status == 200 |
| 84 |
response = client.get("/custom-static/not_exists.py") |
| 85 |
assert response.status == 404 |
| 86 |
response = client.get("/custom-static/../LICENSE") |
| 87 |
assert response.status == 404 |
| 88 |
|
| 89 |
|
| 90 |
def test_memory_database_page(): |
| 91 |
with make_app_client(memory=True) as client: |
| 92 |
response = client.get("/_memory") |
| 93 |
assert response.status == 200 |
| 94 |
|
| 95 |
|
| 96 |
def test_not_allowed_methods(): |
| 97 |
with make_app_client(memory=True) as client: |
| 98 |
for method in ("post", "put", "patch", "delete"): |
| 99 |
response = client.request(path="/_memory", method=method.upper()) |
| 100 |
assert response.status == 405 |
| 101 |
|
| 102 |
|
| 103 |
def test_database_page(app_client): |
| 104 |
response = app_client.get("/fixtures") |
| 105 |
soup = Soup(response.body, "html.parser") |
| 106 |
# Should have a <textarea> for executing SQL |
| 107 |
assert "<textarea" in response.text |
| 108 |
|
| 109 |
# And a list of tables |
| 110 |
for fragment in ( |
| 111 |
'<h2 id="tables">Tables</h2>', |
| 112 |
'<h3><a href="/fixtures/sortable">sortable</a></h3>', |
| 113 |
"<p><em>pk, foreign_key_with_label, foreign_key_with_blank_label, ", |
| 114 |
): |
| 115 |
assert fragment in response.text |
| 116 |
|
| 117 |
# And views |
| 118 |
views_ul = soup.find("h2", string="Views").find_next_sibling("ul") |
| 119 |
assert views_ul is not None |
| 120 |
assert [ |
| 121 |
("/fixtures/paginated_view", "paginated_view"), |
| 122 |
("/fixtures/searchable_view", "searchable_view"), |
| 123 |
( |
| 124 |
"/fixtures/searchable_view_configured_by_metadata", |
| 125 |
"searchable_view_configured_by_metadata", |
| 126 |
), |
| 127 |
("/fixtures/simple_view", "simple_view"), |
| 128 |
] == sorted([(a["href"], a.text) for a in views_ul.find_all("a")]) |
| 129 |
|
| 130 |
# And a list of canned queries |
| 131 |
queries_ul = soup.find("h2", string="Queries").find_next_sibling("ul") |
| 132 |
assert queries_ul is not None |
| 133 |
assert [ |
| 134 |
("/fixtures/from_async_hook", "from_async_hook"), |
| 135 |
("/fixtures/from_hook", "from_hook"), |
| 136 |
("/fixtures/magic_parameters", "magic_parameters"), |
| 137 |
("/fixtures/neighborhood_search#fragment-goes-here", "Search neighborhoods"), |
| 138 |
("/fixtures/pragma_cache_size", "pragma_cache_size"), |
| 139 |
( |
| 140 |
"/fixtures/~F0~9D~90~9C~F0~9D~90~A2~F0~9D~90~AD~F0~9D~90~A2~F0~9D~90~9E~F0~9D~90~AC", |
| 141 |
"𝐜𝐢𝐭𝐢𝐞𝐬", |
| 142 |
), |
| 143 |
] == sorted( |
| 144 |
[(a["href"], a.text) for a in queries_ul.find_all("a")], key=lambda p: p[0] |
| 145 |
) |
| 146 |
|
| 147 |
|
| 148 |
def test_invalid_custom_sql(app_client): |
| 149 |
response = app_client.get("/fixtures?sql=.schema") |
| 150 |
assert response.status == 400 |
| 151 |
assert "Statement must be a SELECT" in response.text |
| 152 |
|
| 153 |
|
| 154 |
def test_disallowed_custom_sql_pragma(app_client): |
| 155 |
response = app_client.get( |
| 156 |
"/fixtures?sql=SELECT+*+FROM+pragma_not_on_allow_list('idx52')" |
| 157 |
) |
| 158 |
assert response.status == 400 |
| 159 |
pragmas = ", ".join("pragma_{}()".format(pragma) for pragma in allowed_pragmas) |
| 160 |
assert ( |
| 161 |
"Statement contained a disallowed PRAGMA. Allowed pragma functions are {}".format( |
| 162 |
pragmas |
| 163 |
) |
| 164 |
in response.text |
| 165 |
) |
| 166 |
|
| 167 |
|
| 168 |
def test_sql_time_limit(app_client_shorter_time_limit): |
| 169 |
response = app_client_shorter_time_limit.get("/fixtures?sql=select+sleep(0.5)") |
| 170 |
assert 400 == response.status |
| 171 |
expected_html_fragments = [ |
| 172 |
""" |
| 173 |
<a href="https://docs.datasette.io/en/stable/settings.html#sql-time-limit-ms">sql_time_limit_ms</a> |
| 174 |
""".strip(), |
| 175 |
'<textarea style="width: 90%">select sleep(0.5)</textarea>', |
| 176 |
] |
| 177 |
for expected_html_fragment in expected_html_fragments: |
| 178 |
assert expected_html_fragment in response.text |
| 179 |
|
| 180 |
|
| 181 |
def test_row_page_does_not_truncate(): |
| 182 |
with make_app_client(settings={"truncate_cells_html": 5}) as client: |
| 183 |
response = client.get("/fixtures/facetable/1") |
| 184 |
assert response.status == 200 |
| 185 |
table = Soup(response.body, "html.parser").find("table") |
| 186 |
assert table["class"] == ["rows-and-columns"] |
| 187 |
assert ["Mission"] == [ |
| 188 |
td.string |
| 189 |
for td in table.find_all("td", {"class": "col-neighborhood-b352a7"}) |
| 190 |
] |
| 191 |
|
| 192 |
|
| 193 |
def test_query_page_truncates(): |
| 194 |
with make_app_client(settings={"truncate_cells_html": 5}) as client: |
| 195 |
response = client.get( |
| 196 |
"/fixtures?" |
| 197 |
+ urllib.parse.urlencode( |
| 198 |
{ |
| 199 |
"sql": "select 'this is longer than 5' as a, 'https://example.com/' as b" |
| 200 |
} |
| 201 |
) |
| 202 |
) |
| 203 |
assert response.status == 200 |
| 204 |
table = Soup(response.body, "html.parser").find("table") |
| 205 |
tds = table.find_all("td") |
| 206 |
assert [str(td) for td in tds] == [ |
| 207 |
'<td class="col-a">this …</td>', |
| 208 |
'<td class="col-b"><a href="https://example.com/">http…</a></td>', |
| 209 |
] |
| 210 |
|
| 211 |
|
| 212 |
@pytest.mark.parametrize( |
| 213 |
"path,expected_classes", |
| 214 |
[ |
| 215 |
("/", ["index"]), |
| 216 |
("/fixtures", ["db", "db-fixtures"]), |
| 217 |
("/fixtures?sql=select+1", ["query", "db-fixtures"]), |
| 218 |
( |
| 219 |
"/fixtures/simple_primary_key", |
| 220 |
["table", "db-fixtures", "table-simple_primary_key"], |
| 221 |
), |
| 222 |
( |
| 223 |
"/fixtures/neighborhood_search", |
| 224 |
["query", "db-fixtures", "query-neighborhood_search"], |
| 225 |
), |
| 226 |
( |
| 227 |
"/fixtures/table~2Fwith~2Fslashes~2Ecsv", |
| 228 |
["table", "db-fixtures", "table-tablewithslashescsv-fa7563"], |
| 229 |
), |
| 230 |
( |
| 231 |
"/fixtures/simple_primary_key/1", |
| 232 |
["row", "db-fixtures", "table-simple_primary_key"], |
| 233 |
), |
| 234 |
], |
| 235 |
) |
| 236 |
def test_css_classes_on_body(app_client, path, expected_classes): |
| 237 |
response = app_client.get(path) |
| 238 |
assert response.status == 200 |
| 239 |
classes = re.search(r'<body class="(.*)">', response.text).group(1).split() |
| 240 |
assert classes == expected_classes |
| 241 |
|
| 242 |
|
| 243 |
@pytest.mark.parametrize( |
| 244 |
"path,expected_considered", |
| 245 |
[ |
| 246 |
("/", "*index.html"), |
| 247 |
("/fixtures", "database-fixtures.html, *database.html"), |
| 248 |
( |
| 249 |
"/fixtures/simple_primary_key", |
| 250 |
"table-fixtures-simple_primary_key.html, *table.html", |
| 251 |
), |
| 252 |
( |
| 253 |
"/fixtures/table~2Fwith~2Fslashes~2Ecsv", |
| 254 |
"table-fixtures-tablewithslashescsv-fa7563.html, *table.html", |
| 255 |
), |
| 256 |
( |
| 257 |
"/fixtures/simple_primary_key/1", |
| 258 |
"row-fixtures-simple_primary_key.html, *row.html", |
| 259 |
), |
| 260 |
], |
| 261 |
) |
| 262 |
def test_templates_considered(app_client, path, expected_considered): |
| 263 |
response = app_client.get(path) |
| 264 |
assert response.status == 200 |
| 265 |
assert f"<!-- Templates considered: {expected_considered} -->" in response.text |
| 266 |
|
| 267 |
|
| 268 |
def test_row_json_export_link(app_client): |
| 269 |
response = app_client.get("/fixtures/simple_primary_key/1") |
| 270 |
assert response.status == 200 |
| 271 |
assert '<a href="/fixtures/simple_primary_key/1.json">json</a>' in response.text |
| 272 |
|
| 273 |
|
| 274 |
def test_query_json_csv_export_links(app_client): |
| 275 |
response = app_client.get("/fixtures?sql=select+1") |
| 276 |
assert response.status == 200 |
| 277 |
assert '<a href="/fixtures.json?sql=select+1">json</a>' in response.text |
| 278 |
assert '<a href="/fixtures.csv?sql=select+1&_size=max">CSV</a>' in response.text |
| 279 |
|
| 280 |
|
| 281 |
def test_row_html_simple_primary_key(app_client): |
| 282 |
response = app_client.get("/fixtures/simple_primary_key/1") |
| 283 |
assert response.status == 200 |
| 284 |
table = Soup(response.body, "html.parser").find("table") |
| 285 |
assert ["id", "content"] == [th.string.strip() for th in table.select("thead th")] |
| 286 |
assert [ |
| 287 |
[ |
| 288 |
'<td class="col-id type-str">1</td>', |
| 289 |
'<td class="col-content type-str">hello</td>', |
| 290 |
] |
| 291 |
] == [[str(td) for td in tr.select("td")] for tr in table.select("tbody tr")] |
| 292 |
|
| 293 |
|
| 294 |
def test_row_html_no_primary_key(app_client): |
| 295 |
response = app_client.get("/fixtures/no_primary_key/1") |
| 296 |
assert response.status == 200 |
| 297 |
table = Soup(response.body, "html.parser").find("table") |
| 298 |
assert ["rowid", "content", "a", "b", "c"] == [ |
| 299 |
th.string.strip() for th in table.select("thead th") |
| 300 |
] |
| 301 |
expected = [ |
| 302 |
[ |
| 303 |
'<td class="col-rowid type-int">1</td>', |
| 304 |
'<td class="col-content type-str">1</td>', |
| 305 |
'<td class="col-a type-str">a1</td>', |
| 306 |
'<td class="col-b type-str">b1</td>', |
| 307 |
'<td class="col-c type-str">c1</td>', |
| 308 |
] |
| 309 |
] |
| 310 |
assert expected == [ |
| 311 |
[str(td) for td in tr.select("td")] for tr in table.select("tbody tr") |
| 312 |
] |
| 313 |
|
| 314 |
|
| 315 |
@pytest.mark.parametrize( |
| 316 |
"path,expected_text,expected_link", |
| 317 |
( |
| 318 |
( |
| 319 |
"/fixtures/facet_cities/1", |
| 320 |
"6 rows from _city_id in facetable", |
| 321 |
"/fixtures/facetable?_city_id__exact=1", |
| 322 |
), |
| 323 |
( |
| 324 |
"/fixtures/attraction_characteristic/2", |
| 325 |
"3 rows from characteristic_id in roadside_attraction_characteristics", |
| 326 |
"/fixtures/roadside_attraction_characteristics?characteristic_id=2", |
| 327 |
), |
| 328 |
), |
| 329 |
) |
| 330 |
def test_row_links_from_other_tables(app_client, path, expected_text, expected_link): |
| 331 |
response = app_client.get(path) |
| 332 |
assert response.status == 200 |
| 333 |
soup = Soup(response.body, "html.parser") |
| 334 |
h2 = soup.find("h2") |
| 335 |
assert h2.text == "Links from other tables" |
| 336 |
li = h2.find_next("ul").find("li") |
| 337 |
text = re.sub(r"\s+", " ", li.text.strip()) |
| 338 |
assert text == expected_text |
| 339 |
link = li.find("a")["href"] |
| 340 |
assert link == expected_link |
| 341 |
|
| 342 |
|
| 343 |
@pytest.mark.parametrize( |
| 344 |
"path,expected", |
| 345 |
( |
| 346 |
( |
| 347 |
"/fixtures/compound_primary_key/a,b", |
| 348 |
[ |
| 349 |
[ |
| 350 |
'<td class="col-pk1 type-str">a</td>', |
| 351 |
'<td class="col-pk2 type-str">b</td>', |
| 352 |
'<td class="col-content type-str">c</td>', |
| 353 |
] |
| 354 |
], |
| 355 |
), |
| 356 |
( |
| 357 |
"/fixtures/compound_primary_key/a~2Fb,~2Ec~2Dd", |
| 358 |
[ |
| 359 |
[ |
| 360 |
'<td class="col-pk1 type-str">a/b</td>', |
| 361 |
'<td class="col-pk2 type-str">.c-d</td>', |
| 362 |
'<td class="col-content type-str">c</td>', |
| 363 |
] |
| 364 |
], |
| 365 |
), |
| 366 |
), |
| 367 |
) |
| 368 |
def test_row_html_compound_primary_key(app_client, path, expected): |
| 369 |
response = app_client.get(path) |
| 370 |
assert response.status == 200 |
| 371 |
table = Soup(response.body, "html.parser").find("table") |
| 372 |
assert ["pk1", "pk2", "content"] == [ |
| 373 |
th.string.strip() for th in table.select("thead th") |
| 374 |
] |
| 375 |
assert expected == [ |
| 376 |
[str(td) for td in tr.select("td")] for tr in table.select("tbody tr") |
| 377 |
] |
| 378 |
|
| 379 |
|
| 380 |
def test_index_metadata(app_client): |
| 381 |
response = app_client.get("/") |
| 382 |
assert response.status == 200 |
| 383 |
soup = Soup(response.body, "html.parser") |
| 384 |
assert "Datasette Fixtures" == soup.find("h1").text |
| 385 |
assert ( |
| 386 |
'An example SQLite database demonstrating Datasette. <a href="/login-as-root">Sign in as root user</a>' |
| 387 |
== inner_html(soup.find("div", {"class": "metadata-description"})) |
| 388 |
) |
| 389 |
assert_footer_links(soup) |
| 390 |
|
| 391 |
|
| 392 |
def test_database_metadata(app_client): |
| 393 |
response = app_client.get("/fixtures") |
| 394 |
assert response.status == 200 |
| 395 |
soup = Soup(response.body, "html.parser") |
| 396 |
# Page title should be the default |
| 397 |
assert "fixtures" == soup.find("h1").text |
| 398 |
# Description should be custom |
| 399 |
assert "Test tables description" == inner_html( |
| 400 |
soup.find("div", {"class": "metadata-description"}) |
| 401 |
) |
| 402 |
# The source/license should be inherited |
| 403 |
assert_footer_links(soup) |
| 404 |
|
| 405 |
|
| 406 |
def test_database_metadata_with_custom_sql(app_client): |
| 407 |
response = app_client.get("/fixtures?sql=select+*+from+simple_primary_key") |
| 408 |
assert response.status == 200 |
| 409 |
soup = Soup(response.body, "html.parser") |
| 410 |
# Page title should be the default |
| 411 |
assert "fixtures" == soup.find("h1").text |
| 412 |
# Description should be custom |
| 413 |
assert "Custom SQL query returning" in soup.find("h3").text |
| 414 |
# The source/license should be inherited |
| 415 |
assert_footer_links(soup) |
| 416 |
|
| 417 |
|
| 418 |
def test_database_download_for_immutable(): |
| 419 |
with make_app_client(is_immutable=True) as client: |
| 420 |
assert not client.ds.databases["fixtures"].is_mutable |
| 421 |
# Regular page should have a download link |
| 422 |
response = client.get("/fixtures") |
| 423 |
soup = Soup(response.body, "html.parser") |
| 424 |
assert len(soup.find_all("a", {"href": re.compile(r"\.db$")})) |
| 425 |
# Check we can actually download it |
| 426 |
download_response = client.get("/fixtures.db") |
| 427 |
assert download_response.status == 200 |
| 428 |
# Check the content-length header exists |
| 429 |
assert "content-length" in download_response.headers |
| 430 |
content_length = download_response.headers["content-length"] |
| 431 |
assert content_length.isdigit() |
| 432 |
assert int(content_length) > 100 |
| 433 |
assert "content-disposition" in download_response.headers |
| 434 |
assert ( |
| 435 |
download_response.headers["content-disposition"] |
| 436 |
== 'attachment; filename="fixtures.db"' |
| 437 |
) |
| 438 |
assert download_response.headers["transfer-encoding"] == "chunked" |
| 439 |
# ETag header should be present and match db.hash |
| 440 |
assert "etag" in download_response.headers |
| 441 |
etag = download_response.headers["etag"] |
| 442 |
assert etag == '"{}"'.format(client.ds.databases["fixtures"].hash) |
| 443 |
# Try a second download with If-None-Match: current-etag |
| 444 |
download_response2 = client.get("/fixtures.db", if_none_match=etag) |
| 445 |
assert download_response2.body == b"" |
| 446 |
assert download_response2.status == 304 |
| 447 |
|
| 448 |
|
| 449 |
def test_database_download_disallowed_for_mutable(app_client): |
| 450 |
response = app_client.get("/fixtures") |
| 451 |
soup = Soup(response.body, "html.parser") |
| 452 |
assert 0 == len(soup.find_all("a", {"href": re.compile(r"\.db$")})) |
| 453 |
assert 403 == app_client.get("/fixtures.db").status |
| 454 |
|
| 455 |
|
| 456 |
def test_database_download_disallowed_for_memory(): |
| 457 |
with make_app_client(memory=True) as client: |
| 458 |
# Memory page should NOT have a download link |
| 459 |
response = client.get("/_memory") |
| 460 |
soup = Soup(response.body, "html.parser") |
| 461 |
assert 0 == len(soup.find_all("a", {"href": re.compile(r"\.db$")})) |
| 462 |
assert 404 == client.get("/_memory.db").status |
| 463 |
|
| 464 |
|
| 465 |
def test_allow_download_off(): |
| 466 |
with make_app_client( |
| 467 |
is_immutable=True, settings={"allow_download": False} |
| 468 |
) as client: |
| 469 |
response = client.get("/fixtures") |
| 470 |
soup = Soup(response.body, "html.parser") |
| 471 |
assert not len(soup.find_all("a", {"href": re.compile(r"\.db$")})) |
| 472 |
# Accessing URL directly should 403 |
| 473 |
response = client.get("/fixtures.db") |
| 474 |
assert 403 == response.status |
| 475 |
|
| 476 |
|
| 477 |
def test_allow_sql_off(): |
| 478 |
with make_app_client(metadata={"allow_sql": {}}) as client: |
| 479 |
response = client.get("/fixtures") |
| 480 |
soup = Soup(response.body, "html.parser") |
| 481 |
assert not len(soup.find_all("textarea", {"name": "sql"})) |
| 482 |
# The table page should no longer show "View and edit SQL" |
| 483 |
response = client.get("/fixtures/sortable") |
| 484 |
assert b"View and edit SQL" not in response.body |
| 485 |
|
| 486 |
|
| 487 |
@pytest.mark.parametrize("path", ["/404", "/fixtures/404"]) |
| 488 |
def test_404(app_client, path): |
| 489 |
response = app_client.get(path) |
| 490 |
assert 404 == response.status |
| 491 |
assert ( |
| 492 |
f'<link rel="stylesheet" href="/-/static/app.css?{app_client.ds.app_css_hash()}' |
| 493 |
in response.text |
| 494 |
) |
| 495 |
|
| 496 |
|
| 497 |
@pytest.mark.parametrize( |
| 498 |
"path,expected_redirect", |
| 499 |
[("/fixtures/", "/fixtures"), ("/fixtures/simple_view/", "/fixtures/simple_view")], |
| 500 |
) |
| 501 |
def test_404_trailing_slash_redirect(app_client, path, expected_redirect): |
| 502 |
response = app_client.get(path) |
| 503 |
assert 302 == response.status |
| 504 |
assert expected_redirect == response.headers["Location"] |
| 505 |
|
| 506 |
|
| 507 |
def test_404_content_type(app_client): |
| 508 |
response = app_client.get("/404") |
| 509 |
assert 404 == response.status |
| 510 |
assert "text/html; charset=utf-8" == response.headers["content-type"] |
| 511 |
|
| 512 |
|
| 513 |
def test_canned_query_default_title(app_client): |
| 514 |
response = app_client.get("/fixtures/magic_parameters") |
| 515 |
assert response.status == 200 |
| 516 |
soup = Soup(response.body, "html.parser") |
| 517 |
assert "fixtures: magic_parameters" == soup.find("h1").text |
| 518 |
|
| 519 |
|
| 520 |
def test_canned_query_with_custom_metadata(app_client): |
| 521 |
response = app_client.get("/fixtures/neighborhood_search?text=town") |
| 522 |
assert response.status == 200 |
| 523 |
soup = Soup(response.body, "html.parser") |
| 524 |
assert "Search neighborhoods" == soup.find("h1").text |
| 525 |
assert ( |
| 526 |
""" |
| 527 |
<div class="metadata-description"> |
| 528 |
<b> |
| 529 |
Demonstrating |
| 530 |
</b> |
| 531 |
simple like search |
| 532 |
</div>""".strip() |
| 533 |
== soup.find("div", {"class": "metadata-description"}).prettify().strip() |
| 534 |
) |
| 535 |
|
| 536 |
|
| 537 |
def test_urlify_custom_queries(app_client): |
| 538 |
path = "/fixtures?" + urllib.parse.urlencode( |
| 539 |
{"sql": "select ('https://twitter.com/' || 'simonw') as user_url;"} |
| 540 |
) |
| 541 |
response = app_client.get(path) |
| 542 |
assert response.status == 200 |
| 543 |
soup = Soup(response.body, "html.parser") |
| 544 |
assert ( |
| 545 |
"""<td class="col-user_url"> |
| 546 |
<a href="https://twitter.com/simonw"> |
| 547 |
https://twitter.com/simonw |
| 548 |
</a> |
| 549 |
</td>""" |
| 550 |
== soup.find("td", {"class": "col-user_url"}).prettify().strip() |
| 551 |
) |
| 552 |
|
| 553 |
|
| 554 |
def test_show_hide_sql_query(app_client): |
| 555 |
path = "/fixtures?" + urllib.parse.urlencode( |
| 556 |
{"sql": "select ('https://twitter.com/' || 'simonw') as user_url;"} |
| 557 |
) |
| 558 |
response = app_client.get(path) |
| 559 |
soup = Soup(response.body, "html.parser") |
| 560 |
span = soup.select(".show-hide-sql")[0] |
| 561 |
assert span.find("a")["href"].endswith("&_hide_sql=1") |
| 562 |
assert "(hide)" == span.getText() |
| 563 |
assert soup.find("textarea") is not None |
| 564 |
# Now follow the link to hide it |
| 565 |
response = app_client.get(span.find("a")["href"]) |
| 566 |
soup = Soup(response.body, "html.parser") |
| 567 |
span = soup.select(".show-hide-sql")[0] |
| 568 |
assert not span.find("a")["href"].endswith("&_hide_sql=1") |
| 569 |
assert "(show)" == span.getText() |
| 570 |
assert soup.find("textarea") is None |
| 571 |
# The SQL should still be there in a hidden form field |
| 572 |
hiddens = soup.find("form").select("input[type=hidden]") |
| 573 |
assert [ |
| 574 |
("sql", "select ('https://twitter.com/' || 'simonw') as user_url;"), |
| 575 |
("_hide_sql", "1"), |
| 576 |
] == [(hidden["name"], hidden["value"]) for hidden in hiddens] |
| 577 |
|
| 578 |
|
| 579 |
def test_canned_query_with_hide_has_no_hidden_sql(app_client): |
| 580 |
# For a canned query the show/hide should NOT have a hidden SQL field |
| 581 |
# https://github.com/simonw/datasette/issues/1411 |
| 582 |
response = app_client.get("/fixtures/pragma_cache_size?_hide_sql=1") |
| 583 |
soup = Soup(response.body, "html.parser") |
| 584 |
hiddens = soup.find("form").select("input[type=hidden]") |
| 585 |
assert [ |
| 586 |
("_hide_sql", "1"), |
| 587 |
] == [(hidden["name"], hidden["value"]) for hidden in hiddens] |
| 588 |
|
| 589 |
|
| 590 |
@pytest.mark.parametrize( |
| 591 |
"hide_sql,querystring,expected_hidden,expected_show_hide_link,expected_show_hide_text", |
| 592 |
( |
| 593 |
(False, "", None, "/_memory/one?_hide_sql=1", "hide"), |
| 594 |
(False, "?_hide_sql=1", "_hide_sql", "/_memory/one", "show"), |
| 595 |
(True, "", None, "/_memory/one?_show_sql=1", "show"), |
| 596 |
(True, "?_show_sql=1", "_show_sql", "/_memory/one", "hide"), |
| 597 |
), |
| 598 |
) |
| 599 |
def test_canned_query_show_hide_metadata_option( |
| 600 |
hide_sql, |
| 601 |
querystring, |
| 602 |
expected_hidden, |
| 603 |
expected_show_hide_link, |
| 604 |
expected_show_hide_text, |
| 605 |
): |
| 606 |
with make_app_client( |
| 607 |
metadata={ |
| 608 |
"databases": { |
| 609 |
"_memory": { |
| 610 |
"queries": { |
| 611 |
"one": { |
| 612 |
"sql": "select 1 + 1", |
| 613 |
"hide_sql": hide_sql, |
| 614 |
} |
| 615 |
} |
| 616 |
} |
| 617 |
} |
| 618 |
}, |
| 619 |
memory=True, |
| 620 |
) as client: |
| 621 |
expected_show_hide_fragment = '(<a href="{}">{}</a>)'.format( |
| 622 |
expected_show_hide_link, expected_show_hide_text |
| 623 |
) |
| 624 |
response = client.get("/_memory/one" + querystring) |
| 625 |
html = response.text |
| 626 |
show_hide_fragment = html.split('<span class="show-hide-sql">')[1].split( |
| 627 |
"</span>" |
| 628 |
)[0] |
| 629 |
assert show_hide_fragment == expected_show_hide_fragment |
| 630 |
if expected_hidden: |
| 631 |
assert ( |
| 632 |
'<input type="hidden" name="{}" value="1">'.format(expected_hidden) |
| 633 |
in html |
| 634 |
) |
| 635 |
else: |
| 636 |
assert '<input type="hidden" ' not in html |
| 637 |
|
| 638 |
|
| 639 |
def test_binary_data_display_in_query(app_client): |
| 640 |
response = app_client.get("/fixtures?sql=select+*+from+binary_data") |
| 641 |
assert response.status == 200 |
| 642 |
table = Soup(response.body, "html.parser").find("table") |
| 643 |
expected_tds = [ |
| 644 |
[ |
| 645 |
'<td class="col-data"><a class="blob-download" href="/fixtures.blob?sql=select+*+from+binary_data&_blob_column=data&_blob_hash=f3088978da8f9aea479ffc7f631370b968d2e855eeb172bea7f6c7a04262bb6d"><Binary:\xa07\xa0bytes></a></td>' |
| 646 |
], |
| 647 |
[ |
| 648 |
'<td class="col-data"><a class="blob-download" href="/fixtures.blob?sql=select+*+from+binary_data&_blob_column=data&_blob_hash=b835b0483cedb86130b9a2c280880bf5fadc5318ddf8c18d0df5204d40df1724"><Binary:\xa07\xa0bytes></a></td>' |
| 649 |
], |
| 650 |
['<td class="col-data">\xa0</td>'], |
| 651 |
] |
| 652 |
assert expected_tds == [ |
| 653 |
[str(td) for td in tr.select("td")] for tr in table.select("tbody tr") |
| 654 |
] |
| 655 |
|
| 656 |
|
| 657 |
@pytest.mark.parametrize( |
| 658 |
"path,expected_filename", |
| 659 |
[ |
| 660 |
("/fixtures/binary_data/1.blob?_blob_column=data", "binary_data-1-data.blob"), |
| 661 |
( |
| 662 |
"/fixtures.blob?sql=select+*+from+binary_data&_blob_column=data&_blob_hash=f3088978da8f9aea479ffc7f631370b968d2e855eeb172bea7f6c7a04262bb6d", |
| 663 |
"data-f30889.blob", |
| 664 |
), |
| 665 |
], |
| 666 |
) |
| 667 |
def test_blob_download(app_client, path, expected_filename): |
| 668 |
response = app_client.get(path) |
| 669 |
assert response.status == 200 |
| 670 |
assert response.body == b"\x15\x1c\x02\xc7\xad\x05\xfe" |
| 671 |
assert response.headers["x-content-type-options"] == "nosniff" |
| 672 |
assert ( |
| 673 |
response.headers["content-disposition"] |
| 674 |
== f'attachment; filename="{expected_filename}"' |
| 675 |
) |
| 676 |
assert response.headers["content-type"] == "application/binary" |
| 677 |
|
| 678 |
|
| 679 |
@pytest.mark.parametrize( |
| 680 |
"path,expected_message", |
| 681 |
[ |
| 682 |
("/fixtures/binary_data/1.blob", "?_blob_column= is required"), |
| 683 |
("/fixtures/binary_data/1.blob?_blob_column=foo", "foo is not a valid column"), |
| 684 |
( |
| 685 |
"/fixtures/binary_data/1.blob?_blob_column=data&_blob_hash=x", |
| 686 |
"Link has expired - the requested binary content has changed or could not be found.", |
| 687 |
), |
| 688 |
], |
| 689 |
) |
| 690 |
def test_blob_download_invalid_messages(app_client, path, expected_message): |
| 691 |
response = app_client.get(path) |
| 692 |
assert response.status == 400 |
| 693 |
assert expected_message in response.text |
| 694 |
|
| 695 |
|
| 696 |
def test_metadata_json_html(app_client): |
| 697 |
response = app_client.get("/-/metadata") |
| 698 |
assert response.status == 200 |
| 699 |
pre = Soup(response.body, "html.parser").find("pre") |
| 700 |
assert METADATA == json.loads(pre.text) |
| 701 |
|
| 702 |
|
| 703 |
@pytest.mark.parametrize( |
| 704 |
"path", |
| 705 |
[ |
| 706 |
"/fixtures?sql=select+*+from+[123_starts_with_digits]", |
| 707 |
"/fixtures/123_starts_with_digits", |
| 708 |
], |
| 709 |
) |
| 710 |
def test_zero_results(app_client, path): |
| 711 |
response = app_client.get(path) |
| 712 |
soup = Soup(response.text, "html.parser") |
| 713 |
assert 0 == len(soup.select("table")) |
| 714 |
assert 1 == len(soup.select("p.zero-results")) |
| 715 |
|
| 716 |
|
| 717 |
def test_query_error(app_client): |
| 718 |
response = app_client.get("/fixtures?sql=select+*+from+notatable") |
| 719 |
html = response.text |
| 720 |
assert '<p class="message-error">no such table: notatable</p>' in html |
| 721 |
assert '<textarea id="sql-editor" name="sql" style="height: 3em' in html |
| 722 |
assert ">select * from notatable</textarea>" in html |
| 723 |
assert "0 results" not in html |
| 724 |
|
| 725 |
|
| 726 |
def test_config_template_debug_on(): |
| 727 |
with make_app_client(settings={"template_debug": True}) as client: |
| 728 |
response = client.get("/fixtures/facetable?_context=1") |
| 729 |
assert response.status == 200 |
| 730 |
assert response.text.startswith("<pre>{") |
| 731 |
|
| 732 |
|
| 733 |
def test_config_template_debug_off(app_client): |
| 734 |
response = app_client.get("/fixtures/facetable?_context=1") |
| 735 |
assert response.status == 200 |
| 736 |
assert not response.text.startswith("<pre>{") |
| 737 |
|
| 738 |
|
| 739 |
def test_debug_context_includes_extra_template_vars(): |
| 740 |
# https://github.com/simonw/datasette/issues/693 |
| 741 |
with make_app_client(settings={"template_debug": True}) as client: |
| 742 |
response = client.get("/fixtures/facetable?_context=1") |
| 743 |
# scope_path is added by PLUGIN1 |
| 744 |
assert "scope_path" in response.text |
| 745 |
|
| 746 |
|
| 747 |
@pytest.mark.parametrize( |
| 748 |
"path", |
| 749 |
[ |
| 750 |
"/", |
| 751 |
"/fixtures", |
| 752 |
"/fixtures/compound_three_primary_keys", |
| 753 |
"/fixtures/compound_three_primary_keys/a,a,a", |
| 754 |
"/fixtures/paginated_view", |
| 755 |
"/fixtures/facetable", |
| 756 |
"/fixtures/facetable?_facet=state", |
| 757 |
"/fixtures?sql=select+1", |
| 758 |
], |
| 759 |
) |
| 760 |
@pytest.mark.parametrize("use_prefix", (True, False)) |
| 761 |
def test_base_url_config(app_client_base_url_prefix, path, use_prefix): |
| 762 |
client = app_client_base_url_prefix |
| 763 |
path_to_get = path |
| 764 |
if use_prefix: |
| 765 |
path_to_get = "/prefix/" + path.lstrip("/") |
| 766 |
response = client.get(path_to_get) |
| 767 |
soup = Soup(response.body, "html.parser") |
| 768 |
for form in soup.select("form"): |
| 769 |
assert form["action"].startswith("/prefix") |
| 770 |
for el in soup.find_all(["a", "link", "script"]): |
| 771 |
if "href" in el.attrs: |
| 772 |
href = el["href"] |
| 773 |
elif "src" in el.attrs: |
| 774 |
href = el["src"] |
| 775 |
else: |
| 776 |
continue # Could be a <script>...</script> |
| 777 |
if ( |
| 778 |
not href.startswith("#") |
| 779 |
and href |
| 780 |
not in { |
| 781 |
"https://datasette.io/", |
| 782 |
"https://github.com/simonw/datasette", |
| 783 |
"https://github.com/simonw/datasette/blob/main/LICENSE", |
| 784 |
"https://github.com/simonw/datasette/blob/main/tests/fixtures.py", |
| 785 |
"/login-as-root", # Only used for the latest.datasette.io demo |
| 786 |
} |
| 787 |
and not href.startswith("https://plugin-example.datasette.io/") |
| 788 |
): |
| 789 |
# If this has been made absolute it may start http://localhost/ |
| 790 |
if href.startswith("http://localhost/"): |
| 791 |
href = href[len("http://localost/") :] |
| 792 |
assert href.startswith("/prefix/"), json.dumps( |
| 793 |
{ |
| 794 |
"path": path, |
| 795 |
"path_to_get": path_to_get, |
| 796 |
"href_or_src": href, |
| 797 |
"element_parent": str(el.parent), |
| 798 |
}, |
| 799 |
indent=4, |
| 800 |
default=repr, |
| 801 |
) |
| 802 |
|
| 803 |
|
| 804 |
def test_base_url_affects_filter_redirects(app_client_base_url_prefix): |
| 805 |
path = "/fixtures/binary_data?_filter_column=rowid&_filter_op=exact&_filter_value=1&_sort=rowid" |
| 806 |
response = app_client_base_url_prefix.get(path) |
| 807 |
assert response.status == 302 |
| 808 |
assert ( |
| 809 |
response.headers["location"] |
| 810 |
== "/prefix/fixtures/binary_data?_sort=rowid&rowid__exact=1" |
| 811 |
) |
| 812 |
|
| 813 |
|
| 814 |
def test_base_url_affects_metadata_extra_css_urls(app_client_base_url_prefix): |
| 815 |
html = app_client_base_url_prefix.get("/").text |
| 816 |
assert '<link rel="stylesheet" href="/prefix/static/extra-css-urls.css">' in html |
| 817 |
|
| 818 |
|
| 819 |
@pytest.mark.parametrize( |
| 820 |
"path,expected", |
| 821 |
[ |
| 822 |
( |
| 823 |
"/fixtures/neighborhood_search", |
| 824 |
"/fixtures?sql=%0Aselect+_neighborhood%2C+facet_cities.name%2C+state%0Afrom+facetable%0A++++join+facet_cities%0A++++++++on+facetable._city_id+%3D+facet_cities.id%0Awhere+_neighborhood+like+%27%25%27+%7C%7C+%3Atext+%7C%7C+%27%25%27%0Aorder+by+_neighborhood%3B%0A&text=", |
| 825 |
), |
| 826 |
( |
| 827 |
"/fixtures/neighborhood_search?text=ber", |
| 828 |
"/fixtures?sql=%0Aselect+_neighborhood%2C+facet_cities.name%2C+state%0Afrom+facetable%0A++++join+facet_cities%0A++++++++on+facetable._city_id+%3D+facet_cities.id%0Awhere+_neighborhood+like+%27%25%27+%7C%7C+%3Atext+%7C%7C+%27%25%27%0Aorder+by+_neighborhood%3B%0A&text=ber", |
| 829 |
), |
| 830 |
("/fixtures/pragma_cache_size", None), |
| 831 |
( |
| 832 |
# /fixtures/𝐜𝐢𝐭𝐢𝐞𝐬 |
| 833 |
"/fixtures/~F0~9D~90~9C~F0~9D~90~A2~F0~9D~90~AD~F0~9D~90~A2~F0~9D~90~9E~F0~9D~90~AC", |
| 834 |
"/fixtures?sql=select+id%2C+name+from+facet_cities+order+by+id+limit+1%3B", |
| 835 |
), |
| 836 |
("/fixtures/magic_parameters", None), |
| 837 |
], |
| 838 |
) |
| 839 |
def test_edit_sql_link_on_canned_queries(app_client, path, expected): |
| 840 |
response = app_client.get(path) |
| 841 |
assert response.status == 200 |
| 842 |
expected_link = f'<a href="{expected}" class="canned-query-edit-sql">Edit SQL</a>' |
| 843 |
if expected: |
| 844 |
assert expected_link in response.text |
| 845 |
else: |
| 846 |
assert "Edit SQL" not in response.text |
| 847 |
|
| 848 |
|
| 849 |
@pytest.mark.parametrize("permission_allowed", [True, False]) |
| 850 |
def test_edit_sql_link_not_shown_if_user_lacks_permission(permission_allowed): |
| 851 |
with make_app_client( |
| 852 |
metadata={ |
| 853 |
"allow_sql": None if permission_allowed else {"id": "not-you"}, |
| 854 |
"databases": {"fixtures": {"queries": {"simple": "select 1 + 1"}}}, |
| 855 |
} |
| 856 |
) as client: |
| 857 |
response = client.get("/fixtures/simple") |
| 858 |
if permission_allowed: |
| 859 |
assert "Edit SQL" in response.text |
| 860 |
else: |
| 861 |
assert "Edit SQL" not in response.text |
| 862 |
|
| 863 |
|
| 864 |
@pytest.mark.parametrize( |
| 865 |
"actor_id,should_have_links,should_not_have_links", |
| 866 |
[ |
| 867 |
(None, None, None), |
| 868 |
("test", None, ["/-/permissions"]), |
| 869 |
("root", ["/-/permissions", "/-/allow-debug", "/-/metadata"], None), |
| 870 |
], |
| 871 |
) |
| 872 |
def test_navigation_menu_links( |
| 873 |
app_client, actor_id, should_have_links, should_not_have_links |
| 874 |
): |
| 875 |
cookies = {} |
| 876 |
if actor_id: |
| 877 |
cookies = {"ds_actor": app_client.actor_cookie({"id": actor_id})} |
| 878 |
html = app_client.get("/", cookies=cookies).text |
| 879 |
soup = Soup(html, "html.parser") |
| 880 |
details = soup.find("nav").find("details") |
| 881 |
if not actor_id: |
| 882 |
# Should not show a menu |
| 883 |
assert details is None |
| 884 |
return |
| 885 |
# They are logged in: should show a menu |
| 886 |
assert details is not None |
| 887 |
# And a rogout form |
| 888 |
assert details.find("form") is not None |
| 889 |
if should_have_links: |
| 890 |
for link in should_have_links: |
| 891 |
assert ( |
| 892 |
details.find("a", {"href": link}) is not None |
| 893 |
), f"{link} expected but missing from nav menu" |
| 894 |
|
| 895 |
if should_not_have_links: |
| 896 |
for link in should_not_have_links: |
| 897 |
assert ( |
| 898 |
details.find("a", {"href": link}) is None |
| 899 |
), f"{link} found but should not have been in nav menu" |
| 900 |
|
| 901 |
|
| 902 |
def test_trace_correctly_escaped(app_client): |
| 903 |
response = app_client.get("/fixtures?sql=select+'<h1>Hello'&_trace=1") |
| 904 |
assert "select '<h1>Hello" not in response.text |
| 905 |
assert "select '<h1>Hello" in response.text |
| 906 |
|
| 907 |
|
| 908 |
@pytest.mark.parametrize( |
| 909 |
"path,expected", |
| 910 |
( |
| 911 |
# Instance index page |
| 912 |
("/", "http://localhost/.json"), |
| 913 |
# Table page |
| 914 |
("/fixtures/facetable", "http://localhost/fixtures/facetable.json"), |
| 915 |
( |
| 916 |
"/fixtures/table~2Fwith~2Fslashes~2Ecsv", |
| 917 |
"http://localhost/fixtures/table~2Fwith~2Fslashes~2Ecsv.json", |
| 918 |
), |
| 919 |
# Row page |
| 920 |
( |
| 921 |
"/fixtures/no_primary_key/1", |
| 922 |
"http://localhost/fixtures/no_primary_key/1.json", |
| 923 |
), |
| 924 |
# Database index page |
| 925 |
( |
| 926 |
"/fixtures", |
| 927 |
"http://localhost/fixtures.json", |
| 928 |
), |
| 929 |
# Custom query page |
| 930 |
( |
| 931 |
"/fixtures?sql=select+*+from+facetable", |
| 932 |
"http://localhost/fixtures.json?sql=select+*+from+facetable", |
| 933 |
), |
| 934 |
# Canned query page |
| 935 |
( |
| 936 |
"/fixtures/neighborhood_search?text=town", |
| 937 |
"http://localhost/fixtures/neighborhood_search.json?text=town", |
| 938 |
), |
| 939 |
# /-/ pages |
| 940 |
( |
| 941 |
"/-/plugins", |
| 942 |
"http://localhost/-/plugins.json", |
| 943 |
), |
| 944 |
), |
| 945 |
) |
| 946 |
def test_alternate_url_json(app_client, path, expected): |
| 947 |
response = app_client.get(path) |
| 948 |
assert response.status == 200 |
| 949 |
link = response.headers["link"] |
| 950 |
assert link == '{}; rel="alternate"; type="application/json+datasette"'.format( |
| 951 |
expected |
| 952 |
) |
| 953 |
assert ( |
| 954 |
'<link rel="alternate" type="application/json+datasette" href="{}">'.format( |
| 955 |
expected |
| 956 |
) |
| 957 |
in response.text |
| 958 |
) |
| 959 |
|
| 960 |
|
| 961 |
@pytest.mark.parametrize( |
| 962 |
"path", |
| 963 |
("/-/patterns", "/-/messages", "/-/allow-debug", "/fixtures.db"), |
| 964 |
) |
| 965 |
def test_no_alternate_url_json(app_client, path): |
| 966 |
response = app_client.get(path) |
| 967 |
assert "link" not in response.headers |
| 968 |
assert ( |
| 969 |
'<link rel="alternate" type="application/json+datasette"' not in response.text |
| 970 |
) |
| 971 |
|
| 972 |
|
| 973 |
@pytest.mark.parametrize( |
| 974 |
"path,expected", |
| 975 |
( |
| 976 |
( |
| 977 |
"/fivethirtyeight/twitter-ratio%2Fsenators", |
| 978 |
"/fivethirtyeight/twitter-ratio~2Fsenators", |
| 979 |
), |
| 980 |
( |
| 981 |
"/fixtures/table%2Fwith%2Fslashes.csv", |
| 982 |
"/fixtures/table~2Fwith~2Fslashes~2Ecsv", |
| 983 |
), |
| 984 |
# query string should be preserved |
| 985 |
("/foo/bar%2Fbaz?id=5", "/foo/bar~2Fbaz?id=5"), |
| 986 |
), |
| 987 |
) |
| 988 |
def test_redirect_percent_encoding_to_tilde_encoding(app_client, path, expected): |
| 989 |
response = app_client.get(path) |
| 990 |
assert response.status == 302 |
| 991 |
assert response.headers["location"] == expected |
| 992 |
|
| 993 |
|
| 994 |
@pytest.mark.asyncio |
| 995 |
@pytest.mark.parametrize( |
| 996 |
"path,metadata,expected_links", |
| 997 |
( |
| 998 |
("/fixtures", {}, [("/", "home")]), |
| 999 |
("/fixtures", {"allow": False, "databases": {"fixtures": {"allow": True}}}, []), |
| 1000 |
( |
| 1001 |
"/fixtures/facetable", |
| 1002 |
{"allow": False, "databases": {"fixtures": {"allow": True}}}, |
| 1003 |
[("/fixtures", "fixtures")], |
| 1004 |
), |
| 1005 |
( |
| 1006 |
"/fixtures/facetable/1", |
| 1007 |
{}, |
| 1008 |
[ |
| 1009 |
("/", "home"), |
| 1010 |
("/fixtures", "fixtures"), |
| 1011 |
("/fixtures/facetable", "facetable"), |
| 1012 |
], |
| 1013 |
), |
| 1014 |
( |
| 1015 |
"/fixtures/facetable/1", |
| 1016 |
{"allow": False, "databases": {"fixtures": {"allow": True}}}, |
| 1017 |
[("/fixtures", "fixtures"), ("/fixtures/facetable", "facetable")], |
| 1018 |
), |
| 1019 |
( |
| 1020 |
"/fixtures/facetable/1", |
| 1021 |
{ |
| 1022 |
"allow": False, |
| 1023 |
"databases": {"fixtures": {"tables": {"facetable": {"allow": True}}}}, |
| 1024 |
}, |
| 1025 |
[("/fixtures/facetable", "facetable")], |
| 1026 |
), |
| 1027 |
), |
| 1028 |
) |
| 1029 |
async def test_breadcrumbs_respect_permissions( |
| 1030 |
app_client, path, metadata, expected_links |
| 1031 |
): |
| 1032 |
orig = app_client.ds._metadata_local |
| 1033 |
app_client.ds._metadata_local = metadata |
| 1034 |
try: |
| 1035 |
response = await app_client.ds.client.get(path) |
| 1036 |
soup = Soup(response.text, "html.parser") |
| 1037 |
breadcrumbs = soup.select("p.crumbs a") |
| 1038 |
actual = [(a["href"], a.text) for a in breadcrumbs] |
| 1039 |
assert actual == expected_links |
| 1040 |
finally: |
| 1041 |
app_client.ds._metadata_local = orig |