Skip to content

Commit ac0c20c

Browse files
authored
Mock the simple auth manager in permission tests and guard against the base-class patch (#75138)
* Mock the simple auth manager in the dependencies graph permission tests SimpleAuthManager overrides get_authorized_assets since #72934, so patching the BaseAuthManager method left the real one in place, every asset stayed readable, and the tests added in #73008 failed on main. Generated-by: Claude Opus 5 * Reject patching BaseAuthManager methods that SimpleAuthManager overrides Twice in one day a permission test patched BaseAuthManager.get_authorized_assets, which SimpleAuthManager overrides, so the mock never applied and main broke after merge. The hook reads which methods SimpleAuthManager overrides, so it keeps up when more are added. Generated-by: Claude Opus 5
1 parent 941316b commit ac0c20c

4 files changed

Lines changed: 190 additions & 6 deletions

File tree

‎.pre-commit-config.yaml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,14 @@ repos:
975975
entry: "^\\s*from urllib\\.parse import ((\\|, )(urlparse\\|urlunparse))+$"
976976
pass_filenames: true
977977
files: \.py$
978+
- id: check-auth-manager-mock-targets
979+
name: Patch SimpleAuthManager, not BaseAuthManager, for methods it overrides
980+
description: The API test client uses SimpleAuthManager, so patching an overridden BaseAuthManager method has no effect
981+
entry: ./scripts/ci/prek/check_auth_manager_mock_targets.py
982+
language: python
983+
pass_filenames: true
984+
files: ^airflow-core/tests/unit/api_fastapi/.*\.py$
985+
exclude: ^airflow-core/tests/unit/api_fastapi/auth/managers/simple/
978986
- id: check-for-inclusive-language
979987
language: pygrep
980988
name: Check for language that we do not accept as community

‎airflow-core/tests/unit/api_fastapi/core_api/routes/ui/test_dependencies.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ def test_data_dependencies_hides_asset_when_user_has_no_dag_access(self, _, test
503503
assert response.status_code == 404
504504

505505
@mock.patch(
506-
"airflow.api_fastapi.auth.managers.base_auth_manager.BaseAuthManager.get_authorized_assets",
506+
"airflow.api_fastapi.auth.managers.simple.simple_auth_manager.SimpleAuthManager.get_authorized_assets",
507507
autospec=True,
508508
return_value=set(),
509509
)
@@ -523,7 +523,7 @@ def test_scheduling_dependencies_hides_assets_the_caller_may_not_read(self, _, t
523523
assert f"asset:{asset1_id}" not in edge_endpoints
524524

525525
@mock.patch(
526-
"airflow.api_fastapi.auth.managers.base_auth_manager.BaseAuthManager.get_authorized_assets",
526+
"airflow.api_fastapi.auth.managers.simple.simple_auth_manager.SimpleAuthManager.get_authorized_assets",
527527
autospec=True,
528528
)
529529
def test_scheduling_dependencies_hides_unreadable_gate_leaf(
@@ -554,7 +554,7 @@ def test_scheduling_dependencies_hides_unreadable_gate_leaf(
554554
assert (f"asset:{asset_b_id}", gate_node["id"]) not in edge_tuples
555555

556556
@mock.patch(
557-
"airflow.api_fastapi.auth.managers.base_auth_manager.BaseAuthManager.get_authorized_assets",
557+
"airflow.api_fastapi.auth.managers.simple.simple_auth_manager.SimpleAuthManager.get_authorized_assets",
558558
autospec=True,
559559
return_value=set(),
560560
)
@@ -595,7 +595,7 @@ def test_scheduling_dependencies_hides_gate_leaf_without_id(self, dag_maker, tes
595595
assert {"gate_asset_a", "gate_asset_b"}.isdisjoint(node["label"] for node in result["nodes"])
596596

597597
@mock.patch(
598-
"airflow.api_fastapi.auth.managers.base_auth_manager.BaseAuthManager.get_authorized_assets",
598+
"airflow.api_fastapi.auth.managers.simple.simple_auth_manager.SimpleAuthManager.get_authorized_assets",
599599
autospec=True,
600600
return_value=set(),
601601
)
@@ -609,7 +609,7 @@ def test_data_dependencies_hides_asset_the_caller_may_not_read(self, _, test_cli
609609
assert response.status_code == 404
610610

611611
@mock.patch(
612-
"airflow.api_fastapi.auth.managers.base_auth_manager.BaseAuthManager.get_authorized_assets",
612+
"airflow.api_fastapi.auth.managers.simple.simple_auth_manager.SimpleAuthManager.get_authorized_assets",
613613
autospec=True,
614614
)
615615
def test_data_dependencies_hides_unreadable_sibling_asset(
@@ -642,7 +642,7 @@ def test_data_dependencies_hides_unreadable_sibling_asset(
642642
assert not any(node_id.startswith("task:gate_upstream_b") for node_id in node_ids)
643643

644644
@mock.patch(
645-
"airflow.api_fastapi.auth.managers.base_auth_manager.BaseAuthManager.get_authorized_assets",
645+
"airflow.api_fastapi.auth.managers.simple.simple_auth_manager.SimpleAuthManager.get_authorized_assets",
646646
autospec=True,
647647
)
648648
def test_data_dependencies_hides_gate_leaf_without_id(
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env python
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
# /// script
19+
# requires-python = ">=3.11,<3.12"
20+
# ///
21+
"""Check that API tests do not patch ``BaseAuthManager`` methods that ``SimpleAuthManager`` overrides.
22+
23+
The API test client runs with ``SimpleAuthManager``. Patching a ``BaseAuthManager`` method that
24+
``SimpleAuthManager`` overrides leaves the override in place, so the mock never takes effect and a
25+
permission test passes or fails for the wrong reason. Patch ``SimpleAuthManager`` instead.
26+
27+
The set of overridden methods is read from the two classes, so it follows the code.
28+
"""
29+
30+
from __future__ import annotations
31+
32+
import ast
33+
import re
34+
import sys
35+
from pathlib import Path
36+
37+
AIRFLOW_ROOT = Path(__file__).parents[3]
38+
BASE_AUTH_MANAGER = AIRFLOW_ROOT / "airflow-core/src/airflow/api_fastapi/auth/managers/base_auth_manager.py"
39+
SIMPLE_AUTH_MANAGER = (
40+
AIRFLOW_ROOT / "airflow-core/src/airflow/api_fastapi/auth/managers/simple/simple_auth_manager.py"
41+
)
42+
43+
PATCH_TARGET = re.compile(r"BaseAuthManager\.(\w+)")
44+
45+
46+
def get_methods(path: Path, class_name: str) -> set[str]:
47+
tree = ast.parse(path.read_text(), filename=str(path))
48+
for node in tree.body:
49+
if isinstance(node, ast.ClassDef) and node.name == class_name:
50+
return {
51+
item.name
52+
for item in node.body
53+
if isinstance(item, (ast.FunctionDef, ast.AsyncFunctionDef)) and not item.name.startswith("_")
54+
}
55+
raise ValueError(f"class {class_name} not found in {path}")
56+
57+
58+
def get_overridden_methods() -> set[str]:
59+
return get_methods(BASE_AUTH_MANAGER, "BaseAuthManager") & get_methods(
60+
SIMPLE_AUTH_MANAGER, "SimpleAuthManager"
61+
)
62+
63+
64+
def find_bad_patch_targets(text: str, overridden: set[str]) -> list[tuple[int, str]]:
65+
return [
66+
(line_number, match.group(1))
67+
for line_number, line in enumerate(text.splitlines(), start=1)
68+
for match in PATCH_TARGET.finditer(line)
69+
if match.group(1) in overridden
70+
]
71+
72+
73+
def main(paths: list[str]) -> int:
74+
overridden = get_overridden_methods()
75+
errors = []
76+
for path in paths:
77+
for line_number, method in find_bad_patch_targets(Path(path).read_text(), overridden):
78+
errors.append(
79+
f"{path}:{line_number}: BaseAuthManager.{method} is overridden by SimpleAuthManager, "
80+
f"so patching it has no effect in API tests; patch SimpleAuthManager.{method} instead."
81+
)
82+
for error in errors:
83+
print(error)
84+
return 1 if errors else 0
85+
86+
87+
if __name__ == "__main__":
88+
sys.exit(main(sys.argv[1:]))
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
from __future__ import annotations
18+
19+
import pytest
20+
from check_auth_manager_mock_targets import (
21+
find_bad_patch_targets,
22+
get_methods,
23+
get_overridden_methods,
24+
main,
25+
)
26+
27+
BASE = "airflow.api_fastapi.auth.managers.base_auth_manager.BaseAuthManager"
28+
SIMPLE = "airflow.api_fastapi.auth.managers.simple.simple_auth_manager.SimpleAuthManager"
29+
30+
31+
def test_get_methods_skips_private_methods(tmp_path):
32+
module = tmp_path / "module.py"
33+
module.write_text(
34+
"class Manager:\n def public(self): ...\n async def run(self): ...\n def _private(self): ...\n"
35+
)
36+
37+
assert get_methods(module, "Manager") == {"public", "run"}
38+
39+
40+
def test_get_methods_raises_for_missing_class(tmp_path):
41+
module = tmp_path / "module.py"
42+
module.write_text("class Other: ...\n")
43+
44+
with pytest.raises(ValueError, match="class Manager not found"):
45+
get_methods(module, "Manager")
46+
47+
48+
def test_overridden_methods_follow_the_real_classes():
49+
overridden = get_overridden_methods()
50+
51+
assert "get_authorized_assets" in overridden
52+
assert "get_authorized_dag_ids" not in overridden
53+
54+
55+
@pytest.mark.parametrize(
56+
("text", "expected"),
57+
[
58+
pytest.param(
59+
f'@mock.patch("{BASE}.get_authorized_assets")', [(1, "get_authorized_assets")], id="base"
60+
),
61+
pytest.param(f'@mock.patch("{SIMPLE}.get_authorized_assets")', [], id="simple"),
62+
pytest.param(f'@mock.patch("{BASE}.get_authorized_dag_ids")', [], id="not-overridden"),
63+
pytest.param(
64+
f'x = 1\n@mock.patch(\n "{BASE}.is_authorized_dag",\n)',
65+
[(3, "is_authorized_dag")],
66+
id="multiline",
67+
),
68+
],
69+
)
70+
def test_find_bad_patch_targets(text, expected):
71+
overridden = {"get_authorized_assets", "is_authorized_dag"}
72+
73+
assert find_bad_patch_targets(text, overridden) == expected
74+
75+
76+
def test_main_reports_bad_target_and_fails(tmp_path, capsys):
77+
test_file = tmp_path / "test_routes.py"
78+
test_file.write_text(f'@mock.patch("{BASE}.get_authorized_assets")\n')
79+
80+
assert main([str(test_file)]) == 1
81+
assert "patch SimpleAuthManager.get_authorized_assets instead" in capsys.readouterr().out
82+
83+
84+
def test_main_passes_good_target(tmp_path):
85+
test_file = tmp_path / "test_routes.py"
86+
test_file.write_text(f'@mock.patch("{SIMPLE}.get_authorized_assets")\n')
87+
88+
assert main([str(test_file)]) == 0

0 commit comments

Comments
 (0)