vincbeck commented on code in PR #77311:
URL: https://github.com/apache/airflow/pull/77311#discussion_r4643780522


##########
providers/fab/tests/unit/fab/auth_manager/test_fab_auth_manager.py:
##########
@@ -1237,6 +1237,80 @@ def test_get_authorized_dag_ids(
 
         delete_user(flask_app, "username")
 
+    @pytest.mark.parametrize(
+        ("method", "role_permissions", "expected_results"),
+        [
+            pytest.param("GET", [(ACTION_CAN_READ, "DAG:test_dag1")], 
{"test_dag1"}, id="read-one-dag"),
+            pytest.param(
+                "PUT",
+                [(ACTION_CAN_EDIT, "DAG:test_dag1"), (ACTION_CAN_EDIT, 
"DAG:test_dag2")],
+                {"test_dag1", "test_dag2"},
+                id="edit-two-dags",
+            ),
+        ],
+    )
+    def test_get_authorized_dag_ids_for_public_user(
+        self, method, role_permissions, expected_results, 
auth_manager_with_appbuilder, flask_app, dag_maker
+    ):
+        for dag_id in ("test_dag1", "test_dag2", "test_dag3"):
+            with dag_maker(dag_id):
+                EmptyOperator(task_id="task")
+            if AIRFLOW_V_3_1_PLUS:
+                sync_dag_to_db(dag_maker.dag)
+        dag_maker.session.commit()
+        dag_maker.session.close()
+
+        security_manager = auth_manager_with_appbuilder.security_manager
+        security_manager.sync_perm_for_dag("test_dag1")
+        security_manager.sync_perm_for_dag("test_dag2")
+        delete_role(flask_app, "public_test")
+        create_role(flask_app, "public_test", role_permissions)
+
+        try:
+            with conf_vars({("fab", "auth_role_public"): "public_test"}):
+                user = auth_manager_with_appbuilder.build_public_user()
+                results = 
auth_manager_with_appbuilder.get_authorized_dag_ids(user=user, method=method)
+        finally:
+            delete_role(flask_app, "public_test")
+
+        assert results == expected_results
+
+    def test_get_authorized_dag_ids_reads_cached_user_permissions(
+        self, auth_manager_with_appbuilder, flask_app, dag_maker
+    ):
+        for dag_id in ("test_dag1", "test_dag2"):
+            with dag_maker(dag_id):
+                EmptyOperator(task_id="task")
+            if AIRFLOW_V_3_1_PLUS:
+                sync_dag_to_db(dag_maker.dag)
+        dag_maker.session.commit()
+        dag_maker.session.close()
+
+        security_manager = auth_manager_with_appbuilder.security_manager
+        security_manager.sync_perm_for_dag("test_dag1")
+        security_manager.sync_perm_for_dag("test_dag2")
+        user = create_user(
+            flask_app,
+            username="username",
+            role_name="test",
+            permissions=[(ACTION_CAN_READ, "DAG:test_dag1")],
+        )
+        try:
+            # Load the permissions as deserialize_user's cached user would 
hold them,
+            # then grant another Dag in the DB.
+            user.perms

Review Comment:
   nit: a bare `user.perms` looks like dead code and could be removed by 
mistake. An assert makes the intent clear and checks the starting state
   
   ```suggestion
               assert user.perms == {(ACTION_CAN_READ, "DAG:test_dag1")}
   ```



##########
providers/fab/tests/unit/fab/auth_manager/test_fab_auth_manager.py:
##########
@@ -1237,6 +1237,80 @@ def test_get_authorized_dag_ids(
 
         delete_user(flask_app, "username")
 
+    @pytest.mark.parametrize(
+        ("method", "role_permissions", "expected_results"),
+        [
+            pytest.param("GET", [(ACTION_CAN_READ, "DAG:test_dag1")], 
{"test_dag1"}, id="read-one-dag"),
+            pytest.param(
+                "PUT",
+                [(ACTION_CAN_EDIT, "DAG:test_dag1"), (ACTION_CAN_EDIT, 
"DAG:test_dag2")],
+                {"test_dag1", "test_dag2"},
+                id="edit-two-dags",
+            ),
+        ],
+    )
+    def test_get_authorized_dag_ids_for_public_user(

Review Comment:
   nit: this test looks very similar to `test_get_authorized_dag_ids`, any 
chance to factorize? For example by adding a public user parameter to it



##########
providers/fab/tests/unit/fab/auth_manager/test_fab_auth_manager.py:
##########
@@ -1237,6 +1237,80 @@ def test_get_authorized_dag_ids(
 
         delete_user(flask_app, "username")
 
+    @pytest.mark.parametrize(
+        ("method", "role_permissions", "expected_results"),
+        [
+            pytest.param("GET", [(ACTION_CAN_READ, "DAG:test_dag1")], 
{"test_dag1"}, id="read-one-dag"),
+            pytest.param(
+                "PUT",
+                [(ACTION_CAN_EDIT, "DAG:test_dag1"), (ACTION_CAN_EDIT, 
"DAG:test_dag2")],
+                {"test_dag1", "test_dag2"},
+                id="edit-two-dags",
+            ),
+        ],
+    )
+    def test_get_authorized_dag_ids_for_public_user(
+        self, method, role_permissions, expected_results, 
auth_manager_with_appbuilder, flask_app, dag_maker
+    ):
+        for dag_id in ("test_dag1", "test_dag2", "test_dag3"):
+            with dag_maker(dag_id):
+                EmptyOperator(task_id="task")
+            if AIRFLOW_V_3_1_PLUS:
+                sync_dag_to_db(dag_maker.dag)
+        dag_maker.session.commit()
+        dag_maker.session.close()
+
+        security_manager = auth_manager_with_appbuilder.security_manager
+        security_manager.sync_perm_for_dag("test_dag1")
+        security_manager.sync_perm_for_dag("test_dag2")
+        delete_role(flask_app, "public_test")
+        create_role(flask_app, "public_test", role_permissions)
+
+        try:
+            with conf_vars({("fab", "auth_role_public"): "public_test"}):

Review Comment:
   nit: my bad on my previous suggestion. If you keep this test separate, the 
value does not change, so `conf_vars` should be a decorator on the test (see 
AGENTS.md)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to