You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by bi...@apache.org on 2021/04/09 07:19:07 UTC

[airflow] branch master updated: Remove duplicate test utils (#15300)

This is an automated email from the ASF dual-hosted git repository.

binh pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/master by this push:
     new 3bd84cc3 Remove duplicate test utils (#15300)
3bd84cc3 is described below

commit 3bd84cc3eeae2aacaab957b012038ec24aa50b25
Author: Xinbin Huang <bi...@gmail.com>
AuthorDate: Fri Apr 9 00:18:55 2021 -0700

    Remove duplicate test utils (#15300)
    
    * Remove duplicate test utils
    
    * fixup! Remove duplicate test utils
---
 tests/test_utils/api_connexion_utils.py |  8 +++-
 tests/test_utils/fab_utils.py           | 82 ---------------------------------
 tests/www/test_security.py              | 18 ++++----
 tests/www/test_views.py                 |  4 +-
 4 files changed, 18 insertions(+), 94 deletions(-)

diff --git a/tests/test_utils/api_connexion_utils.py b/tests/test_utils/api_connexion_utils.py
index 1a95e37..46a428b 100644
--- a/tests/test_utils/api_connexion_utils.py
+++ b/tests/test_utils/api_connexion_utils.py
@@ -26,7 +26,7 @@ def create_user(app, username, role_name, permissions=None):
     delete_role(app, role_name)
     role = create_role(app, role_name, permissions)
 
-    appbuilder.sm.add_user(
+    return appbuilder.sm.add_user(
         username=username,
         first_name=username,
         last_name=username,
@@ -54,6 +54,12 @@ def delete_role(app, name):
         app.appbuilder.sm.delete_role(name)
 
 
+def delete_roles(app):
+    for role in app.appbuilder.sm.get_all_roles():
+        if role.name not in EXISTING_ROLES:
+            app.appbuilder.sm.delete_role(role.name)
+
+
 def delete_user(app, username):
     appbuilder = app.appbuilder
     for user in appbuilder.sm.get_all_users():
diff --git a/tests/test_utils/fab_utils.py b/tests/test_utils/fab_utils.py
deleted file mode 100644
index 4f2d52e..0000000
--- a/tests/test_utils/fab_utils.py
+++ /dev/null
@@ -1,82 +0,0 @@
-# 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 airflow.api_connexion.exceptions import EXCEPTIONS_LINK_MAP
-from airflow.www import security
-from airflow.www.security import EXISTING_ROLES
-
-
-def create_user(app, username, role_name, permissions=None):
-    appbuilder = app.appbuilder
-
-    # Removes user and role so each test has isolated test data.
-    delete_user(app, username)
-    delete_role(app, role_name)
-    role = create_role(app, role_name, permissions)
-
-    return appbuilder.sm.add_user(
-        username=username,
-        first_name=username,
-        last_name=username,
-        email=f"{username}@fab.org",
-        role=role,
-        password=username,
-    )
-
-
-def create_role(app, name, permissions=None):
-    appbuilder = app.appbuilder
-    role = appbuilder.sm.find_role(name)
-    if not role:
-        role = appbuilder.sm.add_role(name)
-    if not permissions:
-        permissions = []
-    for permission in permissions:
-        perm_object = appbuilder.sm.find_permission_view_menu(*permission)
-        appbuilder.sm.add_permission_role(role, perm_object)
-    return role
-
-
-def delete_role(app, name):
-    if app.appbuilder.sm.find_role(name):
-        app.appbuilder.sm.delete_role(name)
-
-
-def delete_roles(app):
-    for role in app.appbuilder.sm.get_all_roles():
-        if role.name not in security.EXISTING_ROLES:
-            app.appbuilder.sm.delete_role(role.name)
-
-
-def delete_user(app, username):
-    appbuilder = app.appbuilder
-    for user in appbuilder.sm.get_all_users():
-        if user.username == username:
-            _ = [
-                delete_role(app, role.name) for role in user.roles if role and role.name not in EXISTING_ROLES
-            ]
-            appbuilder.sm.del_register_user(user)
-            break
-
-
-def assert_401(response):
-    assert response.status_code == 401
-    assert response.json == {
-        'detail': None,
-        'status': 401,
-        'title': 'Unauthorized',
-        'type': EXCEPTIONS_LINK_MAP[401],
-    }
diff --git a/tests/www/test_security.py b/tests/www/test_security.py
index 90b3585..00d8d17 100644
--- a/tests/www/test_security.py
+++ b/tests/www/test_security.py
@@ -32,7 +32,7 @@ from airflow.models import DagModel
 from airflow.security import permissions
 from airflow.www import app as application
 from airflow.www.utils import CustomSQLAInterface
-from tests.test_utils import fab_utils
+from tests.test_utils import api_connexion_utils
 from tests.test_utils.asserts import assert_queries_count
 from tests.test_utils.db import clear_db_dags, clear_db_runs
 from tests.test_utils.mock_security_manager import MockSecurityManager
@@ -100,7 +100,7 @@ class TestSecurity(unittest.TestCase):
     @classmethod
     def delete_roles(cls):
         for role_name in ['team-a', 'MyRole1', 'MyRole5', 'Test_Role', 'MyRole3', 'MyRole2']:
-            fab_utils.delete_role(cls.app, role_name)
+            api_connexion_utils.delete_role(cls.app, role_name)
 
     def expect_user_is_in_role(self, user, rolename):
         self.security_manager.bulk_sync_roles([{'role': rolename, 'perms': []}])
@@ -332,7 +332,7 @@ class TestSecurity(unittest.TestCase):
         username = 'get_current_user_permissions'
 
         with self.app.app_context():
-            user = fab_utils.create_user(
+            user = api_connexion_utils.create_user(
                 self.app,
                 username,
                 role_name,
@@ -354,7 +354,7 @@ class TestSecurity(unittest.TestCase):
         dag_id = 'dag_id'
         username = "ElUser"
 
-        user = fab_utils.create_user(
+        user = api_connexion_utils.create_user(
             self.app,
             username,
             role_name,
@@ -382,7 +382,7 @@ class TestSecurity(unittest.TestCase):
         permission_action = [permissions.ACTION_CAN_EDIT]
         dag_id = "dag_id"
 
-        user = fab_utils.create_user(
+        user = api_connexion_utils.create_user(
             self.app,
             username,
             role_name,
@@ -448,7 +448,7 @@ class TestSecurity(unittest.TestCase):
         username = 'dag_access_user'
         role_name = 'dag_access_role'
         with self.app.app_context():
-            user = fab_utils.create_user(
+            user = api_connexion_utils.create_user(
                 self.app,
                 username,
                 role_name,
@@ -470,7 +470,7 @@ class TestSecurity(unittest.TestCase):
             'can_eat_pudding',  # clearly not a real permission
         ]
         username = "LaUser"
-        user = fab_utils.create_user(
+        user = api_connexion_utils.create_user(
             self.app,
             username=username,
             role_name='team-a',
@@ -487,7 +487,7 @@ class TestSecurity(unittest.TestCase):
         username = 'access_control_is_set_on_init'
         role_name = 'team-a'
         with self.app.app_context():
-            user = fab_utils.create_user(
+            user = api_connexion_utils.create_user(
                 self.app,
                 username,
                 role_name,
@@ -515,7 +515,7 @@ class TestSecurity(unittest.TestCase):
         username = 'access_control_stale_perms_are_revoked'
         role_name = 'team-a'
         with self.app.app_context():
-            user = fab_utils.create_user(
+            user = api_connexion_utils.create_user(
                 self.app,
                 username,
                 role_name,
diff --git a/tests/www/test_views.py b/tests/www/test_views.py
index a8c5fe1..fb47d65 100644
--- a/tests/www/test_views.py
+++ b/tests/www/test_views.py
@@ -65,7 +65,7 @@ from airflow.www import app as application
 from airflow.www.extensions import init_views
 from airflow.www.extensions.init_appbuilder_links import init_appbuilder_links
 from airflow.www.views import ConnectionModelView, get_safe_url, truncate_task_duration
-from tests.test_utils import fab_utils
+from tests.test_utils import api_connexion_utils
 from tests.test_utils.asserts import assert_queries_count
 from tests.test_utils.config import conf_vars
 from tests.test_utils.db import clear_db_runs
@@ -229,7 +229,7 @@ class TestBase(unittest.TestCase):
 
     def create_user_and_login(self, username, role_name, perms):
         self.logout()
-        fab_utils.create_user(
+        api_connexion_utils.create_user(
             self.app,
             username=username,
             role_name=role_name,