| # Licensed to the Apache Software Foundation (ASF) under one | |
| # or more contributor license agreements. See the NOTICE file | |
| # distributed with this work for additional information | |
| # regarding copyright ownership. The ASF licenses this file | |
| # to you under the Apache License, Version 2.0 (the | |
| # "License"); you may not use this file except in compliance | |
| # with the License. You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, | |
| # software distributed under the License is distributed on an | |
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| # KIND, either express or implied. See the License for the | |
| # specific language governing permissions and limitations | |
| # under the License. | |
| import re | |
| import pytest | |
| import sqlalchemy | |
| from app import db | |
| async def test_the_database_is_migrated_to_head_at_startup(running_app): | |
| async with db.session() as session: | |
| revision = (await session.execute(sqlalchemy.text("SELECT version_num FROM alembic_version"))).scalar_one() | |
| assert revision == "0001" | |
| async def test_home_offers_a_sign_in_to_somebody_signed_out(running_app): | |
| response = await running_app.test_client().get("/") | |
| assert response.status_code == 200 | |
| page = await response.get_data(as_text=True) | |
| assert "/auth?login=/" in page | |
| assert 'id="role-summary"' not in page | |
| async def test_the_test_login_does_not_exist_outside_the_e2e_harness(running_app, monkeypatch): | |
| monkeypatch.delenv("DIRECTORY_E2E") | |
| response = await running_app.test_client().get("/e2e/login/ada") | |
| assert response.status_code == 404 | |
| async def test_home_shows_the_directory_to_a_secretary(authenticated_client): | |
| client = await authenticated_client("ada") | |
| page = await (await client.get("/")).get_data(as_text=True) | |
| assert "data-roles='[\"secretary\"]'" in page | |
| assert 'data-testid="directory-placeholder"' in page | |
| async def test_home_tells_somebody_without_a_role_why_there_is_no_directory(authenticated_client): | |
| client = await authenticated_client("zed") | |
| page = await (await client.get("/")).get_data(as_text=True) | |
| assert 'data-testid="no-access"' in page | |
| assert 'data-testid="directory-placeholder"' not in page | |
| async def test_me_says_who_is_asking_and_nothing_more(authenticated_client): | |
| client = await authenticated_client("ada") | |
| assert await (await client.get("/api/me")).get_json() == {"uid": "ada", "roles": ["secretary"]} | |
| async def test_a_login_grants_its_roles_to_that_session_alone(authenticated_client): | |
| granted = await authenticated_client("zed", "secretary") | |
| plain = await authenticated_client("zed") | |
| assert (await (await granted.get("/api/me")).get_json())["roles"] == ["secretary"] | |
| assert (await (await plain.get("/api/me")).get_json())["roles"] == [] | |
| async def test_the_documentation_is_refused_to_somebody_without_a_role(authenticated_client): | |
| client = await authenticated_client("zed") | |
| response = await client.get("/documentation") | |
| assert response.status_code == 403 | |
| assert "can read the documentation" in await response.get_data(as_text=True) | |
| async def test_an_admin_can_read_the_documentation_and_holds_only_the_admin_section(authenticated_client): | |
| client = await authenticated_client("zed", roles="admin") | |
| response = await client.get("/documentation") | |
| assert response.status_code == 200 | |
| page = await response.get_data(as_text=True) | |
| assert page.count('data-doc-mine="true"') == 1 | |
| assert re.search(r'data-doc-section="admin"\s+data-doc-mine="true"', page), "the one marked is the admin's" | |
| async def test_the_documentation_marks_the_sections_the_reader_holds(authenticated_client): | |
| client = await authenticated_client("ada") | |
| response = await client.get("/documentation") | |
| assert response.status_code == 200 | |
| page = await response.get_data(as_text=True) | |
| assert 'data-doc-section="secretary"' in page | |
| assert page.count('data-doc-mine="true"') == 1 | |
| assert re.search(r'data-doc-section="secretary"\s+data-doc-mine="true"', page), "the one marked is theirs" | |
| assert 'data-doc-section="admin"' in page | |
| async def test_the_admin_pages_are_refused_to_a_secretary(authenticated_client): | |
| client = await authenticated_client("ada") | |
| assert (await client.get("/admin/server-status")).status_code == 403 | |
| assert (await client.get("/admin/data")).status_code == 403 | |
| async def test_an_admin_sees_the_server_status_and_the_data_browser(authenticated_client): | |
| client = await authenticated_client("zed", "admin") | |
| status = await client.get("/admin/server-status") | |
| assert status.status_code == 200 | |
| text = await status.get_data(as_text=True) | |
| assert "0001" in text | |
| # Admin is told how many Secretaries there are, never who: the uid is the Secretary's own | |
| assert "1 configured" in text | |
| assert re.search(r"\bada\b", text) is None | |
| browser = await client.get("/admin/data") | |
| assert browser.status_code == 200 | |
| assert 'data-testid="no-tables"' in await browser.get_data(as_text=True) | |
| assert (await client.get("/admin/data/nothing_by_this_name")).status_code == 404 | |
| async def test_the_api_answers_401_rather_than_sending_a_fetch_to_oauth(running_app): | |
| response = await running_app.test_client().get("/api/me") | |
| assert response.status_code == 401 | |
| async def test_every_response_carries_the_security_headers(running_app): | |
| response = await running_app.test_client().get("/") | |
| policy = response.headers["Content-Security-Policy"] | |
| assert "script-src 'self' 'nonce-" in policy | |
| assert "frame-ancestors 'none'" in policy | |
| assert response.headers["X-Frame-Options"] == "DENY" | |
| assert response.headers["X-Content-Type-Options"] == "nosniff" | |
| async def test_an_unknown_page_is_a_404_page(running_app): | |
| response = await running_app.test_client().get("/no/such/page") | |
| assert response.status_code == 404 | |
| assert 'data-testid="error-message"' in await response.get_data(as_text=True) | |
| async def test_an_admin_does_not_see_the_directory(authenticated_client): | |
| """Admin is the Tooling PMC and infra root, who keep the tool running and do not read the directory.""" | |
| client = await authenticated_client("zed", roles="admin") | |
| page = await (await client.get("/")).get_data(as_text=True) | |
| assert 'data-testid="no-access"' in page | |
| @pytest.mark.parametrize("path", ["/admin/server-status", "/admin/data"]) | |
| async def test_the_admin_pages_are_not_served_to_somebody_signed_out(running_app, path): | |
| response = await running_app.test_client().get(path) | |
| assert response.status_code != 200 | |
| assert response.status_code in (302, 401, 403) | |
| @pytest.mark.parametrize("value", ["0", "true", "", "yes"]) | |
| async def test_the_test_login_is_on_only_for_one(running_app, monkeypatch, value): | |
| monkeypatch.setenv("DIRECTORY_E2E", value) | |
| assert (await running_app.test_client().get("/e2e/login/ada")).status_code == 404 | |
| async def test_hsts_is_sent_on_a_real_host_and_not_on_loopback(running_app): | |
| client = running_app.test_client() | |
| real = await client.get("/", headers={"Host": "directory.apache.org"}) | |
| loopback = await client.get("/", headers={"Host": "localhost"}) | |
| assert "max-age=" in real.headers["Strict-Transport-Security"] | |
| assert "Strict-Transport-Security" not in loopback.headers | |
| async def test_the_session_cookie_is_sent_same_site(running_app): | |
| response = await running_app.test_client().get("/e2e/login/ada?roles=secretary") | |
| cookies = response.headers.getlist("Set-Cookie") | |
| assert cookies, "the test login sets a session" | |
| assert all("SameSite=Lax" in cookie for cookie in cookies) | |
| @pytest.fixture | |
| def ldap_supported(monkeypatch): | |
| """As asfquart has it once an LDAP library imports, which the directory's LDAP reader will bring.""" | |
| import asfquart.ldap | |
| monkeypatch.setattr(asfquart.ldap, "LDAP_SUPPORTED", True) | |
| def test_the_app_turns_asfquart_basic_login_off(ldap_supported, app): | |
| """asfquart opens a session from a Basic header where LDAP is supported; create_app turns that off | |
| (app/basic_auth.py), so an LDAP library does not bring a password check with it.""" | |
| import asfquart.ldap | |
| assert asfquart.ldap.LDAP_SUPPORTED is False | |
| async def test_a_bundle_that_is_not_there_is_never_cached(running_app): | |
| """A hashed bundle is immutable once it exists; the 404 before the next build must not be kept.""" | |
| response = await running_app.test_client().get("/assets/bundled/not-built-yet.js") | |
| assert response.status_code == 404 | |
| assert response.headers["Cache-Control"] == "no-store" | |
| async def test_a_bundle_that_is_there_is_cached_for_good(running_app, tmp_path, monkeypatch): | |
| """Its name changes with what it holds, so a bundle that exists never changes.""" | |
| from app.client import CLIENT | |
| (tmp_path / "bundled").mkdir() | |
| (tmp_path / "bundled" / "page-abc123.js").write_text("export {};\n") | |
| monkeypatch.setattr(CLIENT, "static_folder", str(tmp_path)) | |
| response = await running_app.test_client().get("/assets/bundled/page-abc123.js") | |
| assert response.status_code == 200 | |
| assert response.headers["Cache-Control"] == "public, max-age=31536000, immutable" | |
| async def test_a_vendor_file_that_is_not_there_is_never_cached(running_app): | |
| """Every error under /assets/ is no-store, not only a bundle's.""" | |
| response = await running_app.test_client().get("/assets/vendor/css/not-there.css") | |
| assert response.status_code == 404 | |
| assert response.headers["Cache-Control"] == "no-store" |