Skip to content

Commit 897957e

Browse files
author
Will Yang
committed
Fix: Update unit tests to handle new DAG authorization behavior
1 parent f582722 commit 897957e

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

providers/fab/src/airflow/providers/fab/auth_manager/fab_auth_manager.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -564,9 +564,8 @@ def _is_authorized_dag(
564564
# Check whether the user has permissions to access a specific DAG
565565
resource_dag_name = permissions.resource_name(details.id, RESOURCE_DAG)
566566
return self._is_authorized(method=method, resource_type=resource_dag_name, user=user)
567-
else:
568-
authorized_dags = self.get_authorized_dag_ids(user=user, method=method)
569-
return len(authorized_dags) > 0
567+
authorized_dags = self.get_authorized_dag_ids(user=user, method=method)
568+
return len(authorized_dags) > 0
570569

571570
def _is_authorized_dag_run(
572571
self,
@@ -591,9 +590,8 @@ def _is_authorized_dag_run(
591590
# Check whether the user has permissions to access a specific DAG Run permission on a DAG Level
592591
resource_dag_name = permissions.resource_name(details.id, RESOURCE_DAG_RUN)
593592
return self._is_authorized(method=method, resource_type=resource_dag_name, user=user)
594-
else:
595-
authorized_dags = self.get_authorized_dag_ids(user=user, method=method)
596-
return len(authorized_dags) > 0
593+
authorized_dags = self.get_authorized_dag_ids(user=user, method=method)
594+
return len(authorized_dags) > 0
597595

598596
@staticmethod
599597
def _get_fab_action(method: ResourceMethod) -> str:

providers/fab/tests/unit/fab/auth_manager/test_fab_auth_manager.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ def test_is_authorized(self, api_name, method, user_permissions, expected_result
279279
[(ACTION_CAN_READ, "resource_test")],
280280
False,
281281
),
282-
# With specific DAG permissions but no specific DAG requested
282+
# With specific DAG permissions but no specific DAG requested
283283
(
284284
"GET",
285285
None,
@@ -301,7 +301,7 @@ def test_is_authorized(self, api_name, method, user_permissions, expected_result
301301
None,
302302
None,
303303
[(ACTION_CAN_READ, "DAG:test_dag_id")],
304-
False,
304+
True,
305305
),
306306
# With correct method permissions for specific DAG
307307
(
@@ -418,7 +418,7 @@ def test_is_authorized(self, api_name, method, user_permissions, expected_result
418418
DagAccessEntity.RUN,
419419
None,
420420
[(ACTION_CAN_READ, RESOURCE_DAG_RUN)],
421-
False,
421+
True,
422422
),
423423
# DAG sub-entity with specific DAG permissions but missing sub-entity permission
424424
(
@@ -442,21 +442,28 @@ def test_is_authorized(self, api_name, method, user_permissions, expected_result
442442
DagAccessEntity.RUN,
443443
None,
444444
[(ACTION_CAN_READ, "DAG:test_dag_id"), (ACTION_CAN_READ, RESOURCE_DAG_RUN)],
445-
False,
445+
True,
446446
),
447447
],
448448
)
449+
@mock.patch.object(FabAuthManager, "get_authorized_dag_ids")
449450
def test_is_authorized_dag(
450451
self,
452+
mock_get_authorized_dag_ids,
451453
method,
452454
dag_access_entity,
453455
dag_details,
454456
user_permissions,
455457
expected_result,
456458
auth_manager_with_appbuilder,
457459
):
460+
dag_permissions = [perm[1] for perm in user_permissions if perm[1].startswith("DAG:")]
461+
dag_ids = {perm.replace("DAG:", "") for perm in dag_permissions}
462+
mock_get_authorized_dag_ids.return_value = dag_ids
463+
458464
user = Mock()
459465
user.perms = user_permissions
466+
user.id = 1
460467
result = auth_manager_with_appbuilder.is_authorized_dag(
461468
method=method, access_entity=dag_access_entity, details=dag_details, user=user
462469
)

0 commit comments

Comments
 (0)