khalidmammadov commented on a change in pull request #18690: URL: https://github.com/apache/airflow/pull/18690#discussion_r724519943
########## File path: tests/test_utils/api_connexion_utils.py ########## @@ -14,26 +14,43 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. +from contextlib import contextmanager + from airflow.api_connexion.exceptions import EXCEPTIONS_LINK_MAP from airflow.www.security import EXISTING_ROLES -def create_user(app, *, username, role_name, email=None, permissions=None): +@contextmanager +def create_user_scope(app, **kwargs) -> None: + """ + Helper function designed to be used with pytest fixture mainly. + It will create a user and provide it for the fixture via YIELD (generator) + then will tidy up once test is complete + """ + test_user = create_user(app, **kwargs) + if kwargs.get('no_roles', None): + test_user.roles = [] + try: + yield test_user + finally: + delete_user(app, kwargs.get('username')) + + +def create_user(app, **kwargs): appbuilder = app.appbuilder + username = kwargs.get('username') + role_name = kwargs.get('role_name') Review comment: Fixed some of it. This is test utility function and hence should not have (IMHO) additional logic to apply role implicitly. Tests should have freedom to supply values they want so they can check behaviors for different scenarios. Also, this function can't fully replace `create_user` as of yet. There are some other tests that uses unittest module that cant make use of it as it's. I will look into refactoring them once I have this new function. We touched this subject here: #18667 -- 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: commits-unsubscr...@airflow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org