-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix permission check on menus (#35781)
- Loading branch information
1 parent
c068b86
commit d35e982
Showing
2 changed files
with
126 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
# | ||
# 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. | ||
from __future__ import annotations | ||
|
||
from unittest import mock | ||
from unittest.mock import Mock | ||
|
||
import pytest | ||
|
||
from airflow.security.permissions import ( | ||
ACTION_CAN_READ, | ||
RESOURCE_ADMIN_MENU, | ||
RESOURCE_BROWSE_MENU, | ||
RESOURCE_DOCS_MENU, | ||
RESOURCE_VARIABLE, | ||
) | ||
from airflow.www import app as application | ||
|
||
|
||
@pytest.fixture() | ||
def app(): | ||
return application.create_app(testing=True) | ||
|
||
|
||
@pytest.fixture() | ||
def app_builder(app): | ||
return app.appbuilder | ||
|
||
|
||
@pytest.fixture() | ||
def security_manager(app_builder): | ||
return app_builder.sm | ||
|
||
|
||
@pytest.mark.db_test | ||
class TestAirflowSecurityManagerV2: | ||
@pytest.mark.parametrize( | ||
"resource_name, auth_manager_methods, expected", | ||
[ | ||
(RESOURCE_VARIABLE, {"is_authorized_variable": True}, True), | ||
(RESOURCE_VARIABLE, {"is_authorized_variable": False}, False), | ||
(RESOURCE_DOCS_MENU, {"is_authorized_view": True}, True), | ||
(RESOURCE_DOCS_MENU, {"is_authorized_view": False}, False), | ||
( | ||
RESOURCE_ADMIN_MENU, | ||
{ | ||
"is_authorized_view": False, | ||
"is_authorized_variable": False, | ||
"is_authorized_connection": True, | ||
"is_authorized_dag": False, | ||
"is_authorized_configuration": False, | ||
"is_authorized_pool": False, | ||
}, | ||
True, | ||
), | ||
( | ||
RESOURCE_ADMIN_MENU, | ||
{ | ||
"is_authorized_view": False, | ||
"is_authorized_variable": False, | ||
"is_authorized_connection": False, | ||
"is_authorized_dag": False, | ||
"is_authorized_configuration": False, | ||
"is_authorized_pool": False, | ||
}, | ||
False, | ||
), | ||
( | ||
RESOURCE_BROWSE_MENU, | ||
{ | ||
"is_authorized_dag": False, | ||
"is_authorized_view": False, | ||
}, | ||
False, | ||
), | ||
( | ||
RESOURCE_BROWSE_MENU, | ||
{ | ||
"is_authorized_dag": False, | ||
"is_authorized_view": True, | ||
}, | ||
True, | ||
), | ||
], | ||
) | ||
@mock.patch("airflow.www.security_manager.get_auth_manager") | ||
def test_has_access( | ||
self, mock_get_auth_manager, security_manager, resource_name, auth_manager_methods, expected | ||
): | ||
user = Mock() | ||
auth_manager = Mock() | ||
for method_name, method_return in auth_manager_methods.items(): | ||
method = Mock(return_value=method_return) | ||
getattr(auth_manager, method_name).side_effect = method | ||
mock_get_auth_manager.return_value = auth_manager | ||
result = security_manager.has_access(ACTION_CAN_READ, resource_name, user=user) | ||
assert result == expected | ||
if len(auth_manager_methods) > 1 and not expected: | ||
for method_name in auth_manager_methods: | ||
getattr(auth_manager, method_name).assert_called() |