Best Python code snippet using keyboard
test_vault.py
Source:test_vault.py  
1# pylint: disable=no-member2# Licensed to the Apache Software Foundation (ASF) under one3# or more contributor license agreements.  See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership.  The ASF licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License.  You may obtain a copy of the License at9#10#   http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing,13# software distributed under the License is distributed on an14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15# KIND, either express or implied.  See the License for the16# specific language governing permissions and limitations17# under the License.18from unittest import mock19from unittest.case import TestCase20from hvac.exceptions import VaultError21from mock import PropertyMock, mock_open, patch22from airflow.providers.hashicorp.hooks.vault import VaultHook23# noinspection DuplicatedCode,PyUnresolvedReferences24class TestVaultHook(TestCase):25    @staticmethod26    def get_mock_connection(conn_type="vault",27                            schema="secret",28                            host="localhost",29                            port=8180,30                            user="user",31                            password="pass"):32        mock_connection = mock.MagicMock()33        type(mock_connection).conn_type = PropertyMock(return_value=conn_type)34        type(mock_connection).host = PropertyMock(return_value=host)35        type(mock_connection).port = PropertyMock(return_value=port)36        type(mock_connection).login = PropertyMock(return_value=user)37        type(mock_connection).password = PropertyMock(return_value=password)38        type(mock_connection).schema = PropertyMock(return_value=schema)39        return mock_connection40    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")41    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")42    def test_version_not_int(self, mock_hvac, mock_get_connection):43        mock_client = mock.MagicMock()44        mock_hvac.Client.return_value = mock_client45        mock_connection = self.get_mock_connection()46        mock_get_connection.return_value = mock_connection47        connection_dict = {48            "auth_type": "userpass",49            "kv_engine_version": "text"50        }51        mock_connection.extra_dejson.get.side_effect = connection_dict.get52        kwargs = {53            "vault_conn_id": "vault_conn_id",54        }55        with self.assertRaisesRegex(VaultError, 'The version is not an int: text'):56            VaultHook(**kwargs)57    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")58    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")59    def test_version_as_string(self, mock_hvac, mock_get_connection):60        mock_client = mock.MagicMock()61        mock_hvac.Client.return_value = mock_client62        mock_connection = self.get_mock_connection()63        mock_get_connection.return_value = mock_connection64        connection_dict = {65            "auth_type": "userpass",66            "kv_engine_version": "2"67        }68        mock_connection.extra_dejson.get.side_effect = connection_dict.get69        kwargs = {70            "vault_conn_id": "vault_conn_id",71        }72        test_hook = VaultHook(**kwargs)73        self.assertEqual(2, test_hook.vault_client.kv_engine_version)74    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")75    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")76    def test_custom_mount_point_dejson(self, mock_hvac, mock_get_connection):77        mock_client = mock.MagicMock()78        mock_hvac.Client.return_value = mock_client79        mock_connection = self.get_mock_connection(schema='custom')80        mock_get_connection.return_value = mock_connection81        connection_dict = {82            "auth_type": "userpass",83        }84        mock_connection.extra_dejson.get.side_effect = connection_dict.get85        kwargs = {86            "vault_conn_id": "vault_conn_id",87        }88        test_hook = VaultHook(**kwargs)89        self.assertEqual("custom", test_hook.vault_client.mount_point)90    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")91    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")92    def test_custom_auth_mount_point_init_params(self, mock_hvac, mock_get_connection):93        mock_client = mock.MagicMock()94        mock_hvac.Client.return_value = mock_client95        mock_connection = self.get_mock_connection()96        mock_get_connection.return_value = mock_connection97        connection_dict = {98            "auth_type": "userpass",99        }100        mock_connection.extra_dejson.get.side_effect = connection_dict.get101        kwargs = {102            "vault_conn_id": "vault_conn_id",103            "auth_mount_point": "custom"104        }105        test_hook = VaultHook(**kwargs)106        self.assertEqual("secret", test_hook.vault_client.mount_point)107        self.assertEqual("custom", test_hook.vault_client.auth_mount_point)108    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")109    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")110    def test_version_one_init(self, mock_hvac, mock_get_connection):111        mock_client = mock.MagicMock()112        mock_hvac.Client.return_value = mock_client113        mock_connection = self.get_mock_connection()114        mock_get_connection.return_value = mock_connection115        connection_dict = {116            "auth_type": "userpass",117            "kv_engine_version": 1118        }119        mock_connection.extra_dejson.get.side_effect = connection_dict.get120        kwargs = {121            "vault_conn_id": "vault_conn_id",122        }123        test_hook = VaultHook(**kwargs)124        self.assertEqual(1, test_hook.vault_client.kv_engine_version)125    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")126    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")127    def test_custom_auth_mount_point_dejson(self, mock_hvac, mock_get_connection):128        mock_client = mock.MagicMock()129        mock_hvac.Client.return_value = mock_client130        mock_connection = self.get_mock_connection()131        mock_get_connection.return_value = mock_connection132        connection_dict = {133            "auth_type": "userpass",134            "auth_mount_point": "custom"135        }136        mock_connection.extra_dejson.get.side_effect = connection_dict.get137        kwargs = {138            "vault_conn_id": "vault_conn_id",139        }140        test_hook = VaultHook(**kwargs)141        self.assertEqual("secret", test_hook.vault_client.mount_point)142        self.assertEqual("custom", test_hook.vault_client.auth_mount_point)143    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")144    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")145    def test_version_one_dejson(self, mock_hvac, mock_get_connection):146        mock_client = mock.MagicMock()147        mock_hvac.Client.return_value = mock_client148        mock_connection = self.get_mock_connection()149        mock_get_connection.return_value = mock_connection150        connection_dict = {151            "auth_type": "userpass",152        }153        mock_connection.extra_dejson.get.side_effect = connection_dict.get154        kwargs = {155            "kv_engine_version": 1,156            "vault_conn_id": "vault_conn_id",157        }158        test_hook = VaultHook(**kwargs)159        self.assertEqual(1, test_hook.vault_client.kv_engine_version)160    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")161    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")162    def test_vaults_protocol(self, mock_hvac, mock_get_connection):163        mock_client = mock.MagicMock()164        mock_hvac.Client.return_value = mock_client165        mock_connection = self.get_mock_connection(conn_type='vaults')166        mock_get_connection.return_value = mock_connection167        connection_dict = {}168        mock_connection.extra_dejson.get.side_effect = connection_dict.get169        kwargs = {170            "vault_conn_id": "vault_conn_id",171            "auth_type": "approle",172            "role_id": "role",173            "kv_engine_version": 2174        }175        test_hook = VaultHook(**kwargs)176        mock_get_connection.assert_called_with("vault_conn_id")177        test_client = test_hook.get_conn()178        mock_hvac.Client.assert_called_with(url='https://localhost:8180')179        test_client.auth_approle.assert_called_with(role_id="role", secret_id="pass")180        test_client.is_authenticated.assert_called_with()181        self.assertEqual(2, test_hook.vault_client.kv_engine_version)182    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")183    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")184    def test_http_protocol(self, mock_hvac, mock_get_connection):185        mock_client = mock.MagicMock()186        mock_hvac.Client.return_value = mock_client187        mock_connection = self.get_mock_connection(conn_type='http')188        mock_get_connection.return_value = mock_connection189        connection_dict = {}190        mock_connection.extra_dejson.get.side_effect = connection_dict.get191        kwargs = {192            "vault_conn_id": "vault_conn_id",193            "auth_type": "approle",194            "role_id": "role",195            "kv_engine_version": 2196        }197        test_hook = VaultHook(**kwargs)198        mock_get_connection.assert_called_with("vault_conn_id")199        test_client = test_hook.get_conn()200        mock_hvac.Client.assert_called_with(url='http://localhost:8180')201        test_client.auth_approle.assert_called_with(role_id="role", secret_id="pass")202        test_client.is_authenticated.assert_called_with()203        self.assertEqual(2, test_hook.vault_client.kv_engine_version)204    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")205    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")206    def test_https_protocol(self, mock_hvac, mock_get_connection):207        mock_client = mock.MagicMock()208        mock_hvac.Client.return_value = mock_client209        mock_connection = self.get_mock_connection(conn_type='https')210        mock_get_connection.return_value = mock_connection211        connection_dict = {}212        mock_connection.extra_dejson.get.side_effect = connection_dict.get213        kwargs = {214            "vault_conn_id": "vault_conn_id",215            "auth_type": "approle",216            "role_id": "role",217            "kv_engine_version": 2218        }219        test_hook = VaultHook(**kwargs)220        mock_get_connection.assert_called_with("vault_conn_id")221        test_client = test_hook.get_conn()222        mock_hvac.Client.assert_called_with(url='https://localhost:8180')223        test_client.auth_approle.assert_called_with(role_id="role", secret_id="pass")224        test_client.is_authenticated.assert_called_with()225        self.assertEqual(2, test_hook.vault_client.kv_engine_version)226    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")227    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")228    def test_approle_init_params(self, mock_hvac, mock_get_connection):229        mock_client = mock.MagicMock()230        mock_hvac.Client.return_value = mock_client231        mock_connection = self.get_mock_connection()232        mock_get_connection.return_value = mock_connection233        connection_dict = {}234        mock_connection.extra_dejson.get.side_effect = connection_dict.get235        kwargs = {236            "vault_conn_id": "vault_conn_id",237            "auth_type": "approle",238            "role_id": "role",239            "kv_engine_version": 2240        }241        test_hook = VaultHook(**kwargs)242        mock_get_connection.assert_called_with("vault_conn_id")243        test_client = test_hook.get_conn()244        mock_hvac.Client.assert_called_with(url='http://localhost:8180')245        test_client.auth_approle.assert_called_with(role_id="role", secret_id="pass")246        test_client.is_authenticated.assert_called_with()247        self.assertEqual(2, test_hook.vault_client.kv_engine_version)248    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")249    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")250    def test_approle_dejson(self, mock_hvac, mock_get_connection):251        mock_client = mock.MagicMock()252        mock_hvac.Client.return_value = mock_client253        mock_connection = self.get_mock_connection()254        mock_get_connection.return_value = mock_connection255        connection_dict = {256            "auth_type": "approle",257            'role_id': "role",258        }259        mock_connection.extra_dejson.get.side_effect = connection_dict.get260        kwargs = {261            "vault_conn_id": "vault_conn_id",262        }263        test_hook = VaultHook(**kwargs)264        mock_get_connection.assert_called_with("vault_conn_id")265        test_client = test_hook.get_conn()266        mock_hvac.Client.assert_called_with(url='http://localhost:8180')267        test_client.auth_approle.assert_called_with(role_id="role", secret_id="pass")268        test_client.is_authenticated.assert_called_with()269        self.assertEqual(2, test_hook.vault_client.kv_engine_version)270    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")271    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")272    def test_aws_iam_init_params(self, mock_hvac, mock_get_connection):273        mock_client = mock.MagicMock()274        mock_hvac.Client.return_value = mock_client275        mock_connection = self.get_mock_connection()276        mock_get_connection.return_value = mock_connection277        connection_dict = {}278        mock_connection.extra_dejson.get.side_effect = connection_dict.get279        kwargs = {280            "vault_conn_id": "vault_conn_id",281            "auth_type": "aws_iam",282            "role_id": "role"283        }284        test_hook = VaultHook(**kwargs)285        mock_get_connection.assert_called_with("vault_conn_id")286        test_client = test_hook.get_conn()287        mock_hvac.Client.assert_called_with(url='http://localhost:8180')288        test_client.auth_aws_iam.assert_called_with(289            access_key='user',290            secret_key='pass',291            role="role",292        )293        test_client.is_authenticated.assert_called_with()294        self.assertEqual(2, test_hook.vault_client.kv_engine_version)295    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")296    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")297    def test_aws_iam_dejson(self, mock_hvac, mock_get_connection):298        mock_client = mock.MagicMock()299        mock_hvac.Client.return_value = mock_client300        mock_connection = self.get_mock_connection()301        mock_get_connection.return_value = mock_connection302        connection_dict = {303            "auth_type": "aws_iam",304            "role_id": "role"305        }306        mock_connection.extra_dejson.get.side_effect = connection_dict.get307        kwargs = {308            "vault_conn_id": "vault_conn_id",309        }310        test_hook = VaultHook(**kwargs)311        mock_get_connection.assert_called_with("vault_conn_id")312        test_client = test_hook.get_conn()313        mock_hvac.Client.assert_called_with(url='http://localhost:8180')314        test_client.auth_aws_iam.assert_called_with(315            access_key='user',316            secret_key='pass',317            role="role",318        )319    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")320    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")321    def test_azure_init_params(self, mock_hvac, mock_get_connection):322        mock_client = mock.MagicMock()323        mock_hvac.Client.return_value = mock_client324        mock_connection = self.get_mock_connection()325        mock_get_connection.return_value = mock_connection326        connection_dict = {}327        mock_connection.extra_dejson.get.side_effect = connection_dict.get328        kwargs = {329            "vault_conn_id": "vault_conn_id",330            "auth_type": "azure",331            "azure_tenant_id": "tenant_id",332            "azure_resource": "resource",333        }334        test_hook = VaultHook(**kwargs)335        mock_get_connection.assert_called_with("vault_conn_id")336        test_client = test_hook.get_conn()337        mock_hvac.Client.assert_called_with(url='http://localhost:8180')338        test_client.auth.azure.configure.assert_called_with(339            tenant_id="tenant_id",340            resource="resource",341            client_id="user",342            client_secret="pass",343        )344        test_client.is_authenticated.assert_called_with()345        self.assertEqual(2, test_hook.vault_client.kv_engine_version)346    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")347    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")348    def test_azure_dejson(self, mock_hvac, mock_get_connection):349        mock_client = mock.MagicMock()350        mock_hvac.Client.return_value = mock_client351        mock_connection = self.get_mock_connection()352        mock_get_connection.return_value = mock_connection353        connection_dict = {354            "auth_type": "azure",355            "azure_tenant_id": "tenant_id",356            "azure_resource": "resource",357        }358        mock_connection.extra_dejson.get.side_effect = connection_dict.get359        kwargs = {360            "vault_conn_id": "vault_conn_id",361        }362        test_hook = VaultHook(**kwargs)363        mock_get_connection.assert_called_with("vault_conn_id")364        test_client = test_hook.get_conn()365        mock_hvac.Client.assert_called_with(url='http://localhost:8180')366        test_client.auth.azure.configure.assert_called_with(367            tenant_id="tenant_id",368            resource="resource",369            client_id="user",370            client_secret="pass",371        )372        test_client.is_authenticated.assert_called_with()373        self.assertEqual(2, test_hook.vault_client.kv_engine_version)374    @mock.patch("airflow.providers.google.cloud.utils.credentials_provider._get_scopes")375    @mock.patch("airflow.providers.google.cloud.utils.credentials_provider.get_credentials_and_project_id")376    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")377    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")378    def test_gcp_init_params(self, mock_hvac, mock_get_connection,379                             mock_get_credentials, mock_get_scopes):380        mock_client = mock.MagicMock()381        mock_hvac.Client.return_value = mock_client382        mock_connection = self.get_mock_connection()383        mock_get_connection.return_value = mock_connection384        mock_get_scopes.return_value = ['scope1', 'scope2']385        mock_get_credentials.return_value = ("credentials", "project_id")386        connection_dict = {}387        mock_connection.extra_dejson.get.side_effect = connection_dict.get388        kwargs = {389            "vault_conn_id": "vault_conn_id",390            "auth_type": "gcp",391            "gcp_key_path": "path.json",392            "gcp_scopes": "scope1,scope2",393        }394        test_hook = VaultHook(**kwargs)395        test_client = test_hook.get_conn()396        mock_get_connection.assert_called_with("vault_conn_id")397        mock_get_scopes.assert_called_with("scope1,scope2")398        mock_get_credentials.assert_called_with(399            key_path="path.json",400            keyfile_dict=None,401            scopes=['scope1', 'scope2']402        )403        mock_hvac.Client.assert_called_with(url='http://localhost:8180')404        test_client.auth.gcp.configure.assert_called_with(405            credentials="credentials",406        )407        test_client.is_authenticated.assert_called_with()408        self.assertEqual(2, test_hook.vault_client.kv_engine_version)409    @mock.patch("airflow.providers.google.cloud.utils.credentials_provider._get_scopes")410    @mock.patch("airflow.providers.google.cloud.utils.credentials_provider.get_credentials_and_project_id")411    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")412    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")413    def test_gcp_dejson(self, mock_hvac, mock_get_connection,414                        mock_get_credentials, mock_get_scopes):415        mock_client = mock.MagicMock()416        mock_hvac.Client.return_value = mock_client417        mock_connection = self.get_mock_connection()418        mock_get_connection.return_value = mock_connection419        mock_get_scopes.return_value = ['scope1', 'scope2']420        mock_get_credentials.return_value = ("credentials", "project_id")421        connection_dict = {422            "auth_type": "gcp",423            "gcp_key_path": "path.json",424            "gcp_scopes": "scope1,scope2",425        }426        mock_connection.extra_dejson.get.side_effect = connection_dict.get427        kwargs = {428            "vault_conn_id": "vault_conn_id",429        }430        test_hook = VaultHook(**kwargs)431        test_client = test_hook.get_conn()432        mock_get_connection.assert_called_with("vault_conn_id")433        mock_get_scopes.assert_called_with("scope1,scope2")434        mock_get_credentials.assert_called_with(435            key_path="path.json",436            keyfile_dict=None,437            scopes=['scope1', 'scope2']438        )439        mock_hvac.Client.assert_called_with(url='http://localhost:8180')440        test_client.auth.gcp.configure.assert_called_with(441            credentials="credentials",442        )443        test_client.is_authenticated.assert_called_with()444        self.assertEqual(2, test_hook.vault_client.kv_engine_version)445    @mock.patch("airflow.providers.google.cloud.utils.credentials_provider._get_scopes")446    @mock.patch("airflow.providers.google.cloud.utils.credentials_provider.get_credentials_and_project_id")447    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")448    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")449    def test_gcp_dict_dejson(self, mock_hvac, mock_get_connection,450                             mock_get_credentials, mock_get_scopes):451        mock_client = mock.MagicMock()452        mock_hvac.Client.return_value = mock_client453        mock_connection = self.get_mock_connection()454        mock_get_connection.return_value = mock_connection455        mock_get_scopes.return_value = ['scope1', 'scope2']456        mock_get_credentials.return_value = ("credentials", "project_id")457        connection_dict = {458            "auth_type": "gcp",459            "gcp_keyfile_dict": '{"key": "value"}',460            "gcp_scopes": "scope1,scope2",461        }462        mock_connection.extra_dejson.get.side_effect = connection_dict.get463        kwargs = {464            "vault_conn_id": "vault_conn_id",465        }466        test_hook = VaultHook(**kwargs)467        test_client = test_hook.get_conn()468        mock_get_connection.assert_called_with("vault_conn_id")469        mock_get_scopes.assert_called_with("scope1,scope2")470        mock_get_credentials.assert_called_with(471            key_path=None,472            keyfile_dict={'key': 'value'},473            scopes=['scope1', 'scope2']474        )475        mock_hvac.Client.assert_called_with(url='http://localhost:8180')476        test_client.auth.gcp.configure.assert_called_with(477            credentials="credentials",478        )479        test_client.is_authenticated.assert_called_with()480        self.assertEqual(2, test_hook.vault_client.kv_engine_version)481    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")482    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")483    def test_github_init_params(self, mock_hvac, mock_get_connection):484        mock_client = mock.MagicMock()485        mock_hvac.Client.return_value = mock_client486        mock_connection = self.get_mock_connection()487        mock_get_connection.return_value = mock_connection488        connection_dict = {}489        mock_connection.extra_dejson.get.side_effect = connection_dict.get490        kwargs = {491            "auth_type": "github",492            "vault_conn_id": "vault_conn_id",493        }494        test_hook = VaultHook(**kwargs)495        mock_get_connection.assert_called_with("vault_conn_id")496        test_client = test_hook.get_conn()497        mock_hvac.Client.assert_called_with(url='http://localhost:8180')498        test_client.auth.github.login.assert_called_with(499            token="pass")500        test_client.is_authenticated.assert_called_with()501        self.assertEqual(2, test_hook.vault_client.kv_engine_version)502    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")503    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")504    def test_github_dejson(self, mock_hvac, mock_get_connection):505        mock_client = mock.MagicMock()506        mock_hvac.Client.return_value = mock_client507        mock_connection = self.get_mock_connection()508        mock_get_connection.return_value = mock_connection509        connection_dict = {510            "auth_type": "github",511        }512        mock_connection.extra_dejson.get.side_effect = connection_dict.get513        kwargs = {514            "vault_conn_id": "vault_conn_id",515        }516        test_hook = VaultHook(**kwargs)517        mock_get_connection.assert_called_with("vault_conn_id")518        test_client = test_hook.get_conn()519        mock_hvac.Client.assert_called_with(url='http://localhost:8180')520        test_client.auth.github.login.assert_called_with(521            token="pass")522        test_client.is_authenticated.assert_called_with()523        self.assertEqual(2, test_hook.vault_client.kv_engine_version)524    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")525    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")526    def test_kubernetes_default_path(self, mock_hvac, mock_get_connection):527        mock_client = mock.MagicMock()528        mock_hvac.Client.return_value = mock_client529        mock_connection = self.get_mock_connection()530        mock_get_connection.return_value = mock_connection531        connection_dict = {}532        mock_connection.extra_dejson.get.side_effect = connection_dict.get533        kwargs = {534            "auth_type": "kubernetes",535            "kubernetes_role": "kube_role",536            "vault_conn_id": "vault_conn_id",537        }538        with patch("builtins.open", mock_open(read_data="data")) as mock_file:539            test_hook = VaultHook(**kwargs)540            test_client = test_hook.get_conn()541        mock_get_connection.assert_called_with("vault_conn_id")542        mock_file.assert_called_with("/var/run/secrets/kubernetes.io/serviceaccount/token")543        mock_hvac.Client.assert_called_with(url='http://localhost:8180')544        test_client.auth_kubernetes.assert_called_with(545            role="kube_role", jwt="data")546        test_client.is_authenticated.assert_called_with()547        self.assertEqual(2, test_hook.vault_client.kv_engine_version)548    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")549    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")550    def test_kubernetes_init_params(self, mock_hvac, mock_get_connection):551        mock_client = mock.MagicMock()552        mock_hvac.Client.return_value = mock_client553        mock_connection = self.get_mock_connection()554        mock_get_connection.return_value = mock_connection555        connection_dict = {556            "kubernetes_role": "kube_role",557            "kubernetes_jwt_path": "path",558        }559        mock_connection.extra_dejson.get.side_effect = connection_dict.get560        kwargs = {561            "auth_type": "kubernetes",562            "vault_conn_id": "vault_conn_id",563        }564        with patch("builtins.open", mock_open(read_data="data")) as mock_file:565            test_hook = VaultHook(**kwargs)566            test_client = test_hook.get_conn()567        mock_get_connection.assert_called_with("vault_conn_id")568        mock_file.assert_called_with("path")569        mock_hvac.Client.assert_called_with(url='http://localhost:8180')570        test_client.auth_kubernetes.assert_called_with(571            role="kube_role", jwt="data")572        test_client.is_authenticated.assert_called_with()573        self.assertEqual(2, test_hook.vault_client.kv_engine_version)574    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")575    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")576    def test_kubernetes_dejson(self, mock_hvac, mock_get_connection):577        mock_client = mock.MagicMock()578        mock_hvac.Client.return_value = mock_client579        mock_connection = self.get_mock_connection()580        mock_get_connection.return_value = mock_connection581        connection_dict = {}582        mock_connection.extra_dejson.get.side_effect = connection_dict.get583        kwargs = {584            "kubernetes_role": "kube_role",585            "kubernetes_jwt_path": "path",586            "auth_type": "kubernetes",587            "vault_conn_id": "vault_conn_id",588        }589        with patch("builtins.open", mock_open(read_data="data")) as mock_file:590            test_hook = VaultHook(**kwargs)591            test_client = test_hook.get_conn()592        mock_get_connection.assert_called_with("vault_conn_id")593        mock_file.assert_called_with("path")594        mock_hvac.Client.assert_called_with(url='http://localhost:8180')595        test_client.auth_kubernetes.assert_called_with(596            role="kube_role", jwt="data")597        test_client.is_authenticated.assert_called_with()598        self.assertEqual(2, test_hook.vault_client.kv_engine_version)599    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")600    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")601    def test_ldap_init_params(self, mock_hvac, mock_get_connection):602        mock_client = mock.MagicMock()603        mock_hvac.Client.return_value = mock_client604        mock_connection = self.get_mock_connection()605        mock_get_connection.return_value = mock_connection606        connection_dict = {}607        mock_connection.extra_dejson.get.side_effect = connection_dict.get608        kwargs = {609            "auth_type": "ldap",610            "vault_conn_id": "vault_conn_id",611        }612        test_hook = VaultHook(**kwargs)613        mock_get_connection.assert_called_with("vault_conn_id")614        test_client = test_hook.get_conn()615        mock_hvac.Client.assert_called_with(url='http://localhost:8180')616        test_client.auth.ldap.login.assert_called_with(617            username="user", password="pass")618        test_client.is_authenticated.assert_called_with()619        self.assertEqual(2, test_hook.vault_client.kv_engine_version)620    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")621    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")622    def test_ldap_dejson(self, mock_hvac, mock_get_connection):623        mock_client = mock.MagicMock()624        mock_hvac.Client.return_value = mock_client625        mock_connection = self.get_mock_connection()626        mock_get_connection.return_value = mock_connection627        connection_dict = {628            "auth_type": "ldap",629        }630        mock_connection.extra_dejson.get.side_effect = connection_dict.get631        kwargs = {632            "vault_conn_id": "vault_conn_id",633        }634        test_hook = VaultHook(**kwargs)635        mock_get_connection.assert_called_with("vault_conn_id")636        test_client = test_hook.get_conn()637        mock_hvac.Client.assert_called_with(url='http://localhost:8180')638        test_client.auth.ldap.login.assert_called_with(639            username="user", password="pass")640        test_client.is_authenticated.assert_called_with()641        self.assertEqual(2, test_hook.vault_client.kv_engine_version)642    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")643    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")644    def test_radius_init_params(self, mock_hvac, mock_get_connection):645        mock_client = mock.MagicMock()646        mock_hvac.Client.return_value = mock_client647        mock_connection = self.get_mock_connection()648        mock_get_connection.return_value = mock_connection649        connection_dict = {}650        mock_connection.extra_dejson.get.side_effect = connection_dict.get651        kwargs = {652            "auth_type": "radius",653            "radius_host": "radhost",654            "vault_conn_id": "vault_conn_id",655        }656        test_hook = VaultHook(**kwargs)657        mock_get_connection.assert_called_with("vault_conn_id")658        test_client = test_hook.get_conn()659        mock_hvac.Client.assert_called_with(url='http://localhost:8180')660        test_client.auth.radius.configure.assert_called_with(661            host="radhost",662            secret="pass",663            port=None)664        test_client.is_authenticated.assert_called_with()665        self.assertEqual(2, test_hook.vault_client.kv_engine_version)666    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")667    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")668    def test_radius_init_params_port(self, mock_hvac, mock_get_connection):669        mock_client = mock.MagicMock()670        mock_hvac.Client.return_value = mock_client671        mock_connection = self.get_mock_connection()672        mock_get_connection.return_value = mock_connection673        connection_dict = {}674        mock_connection.extra_dejson.get.side_effect = connection_dict.get675        kwargs = {676            "auth_type": "radius",677            "radius_host": "radhost",678            "radius_port": 8123,679            "vault_conn_id": "vault_conn_id",680        }681        test_hook = VaultHook(**kwargs)682        mock_get_connection.assert_called_with("vault_conn_id")683        test_client = test_hook.get_conn()684        mock_hvac.Client.assert_called_with(url='http://localhost:8180')685        test_client.auth.radius.configure.assert_called_with(686            host="radhost",687            secret="pass",688            port=8123)689        test_client.is_authenticated.assert_called_with()690        self.assertEqual(2, test_hook.vault_client.kv_engine_version)691    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")692    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")693    def test_radius_dejson(self, mock_hvac, mock_get_connection):694        mock_client = mock.MagicMock()695        mock_hvac.Client.return_value = mock_client696        mock_connection = self.get_mock_connection()697        mock_get_connection.return_value = mock_connection698        connection_dict = {699            "auth_type": "radius",700            "radius_host": "radhost",701            "radius_port": "8123",702        }703        mock_connection.extra_dejson.get.side_effect = connection_dict.get704        kwargs = {705            "vault_conn_id": "vault_conn_id",706        }707        test_hook = VaultHook(**kwargs)708        mock_get_connection.assert_called_with("vault_conn_id")709        test_client = test_hook.get_conn()710        mock_hvac.Client.assert_called_with(url='http://localhost:8180')711        test_client.auth.radius.configure.assert_called_with(712            host="radhost",713            secret="pass",714            port=8123)715        test_client.is_authenticated.assert_called_with()716        self.assertEqual(2, test_hook.vault_client.kv_engine_version)717    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")718    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")719    def test_radius_dejson_wrong_port(self, mock_hvac, mock_get_connection):720        mock_client = mock.MagicMock()721        mock_hvac.Client.return_value = mock_client722        mock_connection = self.get_mock_connection()723        mock_get_connection.return_value = mock_connection724        connection_dict = {725            "auth_type": "radius",726            "radius_host": "radhost",727            "radius_port": "wrong",728        }729        mock_connection.extra_dejson.get.side_effect = connection_dict.get730        kwargs = {731            "vault_conn_id": "vault_conn_id",732        }733        with self.assertRaisesRegex(VaultError, "Radius port was wrong: wrong"):734            VaultHook(**kwargs)735    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")736    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")737    def test_token_init_params(self, mock_hvac, mock_get_connection):738        mock_client = mock.MagicMock()739        mock_hvac.Client.return_value = mock_client740        mock_connection = self.get_mock_connection()741        mock_get_connection.return_value = mock_connection742        connection_dict = {}743        mock_connection.extra_dejson.get.side_effect = connection_dict.get744        kwargs = {745            "vault_conn_id": "vault_conn_id",746            "auth_type": "token",747            "kv_engine_version": 2748        }749        test_hook = VaultHook(**kwargs)750        mock_get_connection.assert_called_with("vault_conn_id")751        test_client = test_hook.get_conn()752        mock_hvac.Client.assert_called_with(url='http://localhost:8180')753        test_client.is_authenticated.assert_called_with()754        self.assertEqual("pass", test_client.token)755        self.assertEqual(2, test_hook.vault_client.kv_engine_version)756        self.assertEqual("secret", test_hook.vault_client.mount_point)757    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")758    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")759    def test_token_dejson(self, mock_hvac, mock_get_connection):760        mock_client = mock.MagicMock()761        mock_hvac.Client.return_value = mock_client762        mock_connection = self.get_mock_connection()763        mock_get_connection.return_value = mock_connection764        connection_dict = {765            "auth_type": "token",766        }767        mock_connection.extra_dejson.get.side_effect = connection_dict.get768        kwargs = {769            "vault_conn_id": "vault_conn_id",770        }771        test_hook = VaultHook(**kwargs)772        mock_get_connection.assert_called_with("vault_conn_id")773        test_client = test_hook.get_conn()774        mock_hvac.Client.assert_called_with(url='http://localhost:8180')775        test_client.is_authenticated.assert_called_with()776        self.assertEqual("pass", test_client.token)777        self.assertEqual(2, test_hook.vault_client.kv_engine_version)778    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")779    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")780    def test_userpass_init_params(self, mock_hvac, mock_get_connection):781        mock_client = mock.MagicMock()782        mock_hvac.Client.return_value = mock_client783        mock_connection = self.get_mock_connection()784        mock_get_connection.return_value = mock_connection785        connection_dict = {}786        mock_connection.extra_dejson.get.side_effect = connection_dict.get787        kwargs = {788            "vault_conn_id": "vault_conn_id",789            "auth_type": "userpass",790            "kv_engine_version": 2791        }792        test_hook = VaultHook(**kwargs)793        mock_get_connection.assert_called_with("vault_conn_id")794        test_client = test_hook.get_conn()795        mock_hvac.Client.assert_called_with(url='http://localhost:8180')796        test_client.auth_userpass.assert_called_with(797            username="user", password="pass")798        test_client.is_authenticated.assert_called_with()799        self.assertEqual(2, test_hook.vault_client.kv_engine_version)800    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")801    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")802    def test_userpass_dejson(self, mock_hvac, mock_get_connection):803        mock_client = mock.MagicMock()804        mock_hvac.Client.return_value = mock_client805        mock_connection = self.get_mock_connection()806        mock_get_connection.return_value = mock_connection807        connection_dict = {808            "auth_type": "userpass",809        }810        mock_connection.extra_dejson.get.side_effect = connection_dict.get811        kwargs = {812            "vault_conn_id": "vault_conn_id",813        }814        test_hook = VaultHook(**kwargs)815        mock_get_connection.assert_called_with("vault_conn_id")816        test_client = test_hook.get_conn()817        mock_hvac.Client.assert_called_with(url='http://localhost:8180')818        test_client.auth_userpass.assert_called_with(819            username="user", password="pass")820        test_client.is_authenticated.assert_called_with()821        self.assertEqual(2, test_hook.vault_client.kv_engine_version)822    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")823    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")824    def test_get_existing_key_v2(self, mock_hvac, mock_get_connection):825        mock_connection = self.get_mock_connection()826        mock_get_connection.return_value = mock_connection827        mock_client = mock.MagicMock()828        mock_hvac.Client.return_value = mock_client829        connection_dict = {}830        mock_client.secrets.kv.v2.read_secret_version.return_value = {831            'request_id': '94011e25-f8dc-ec29-221b-1f9c1d9ad2ae',832            'lease_id': '',833            'renewable': False,834            'lease_duration': 0,835            'data': {836                'data': {'secret_key': 'secret_value'},837                'metadata': {'created_time': '2020-03-16T21:01:43.331126Z',838                             'deletion_time': '',839                             'destroyed': False,840                             'version': 1}},841            'wrap_info': None,842            'warnings': None,843            'auth': None844        }845        mock_connection.extra_dejson.get.side_effect = connection_dict.get846        kwargs = {847            "vault_conn_id": "vault_conn_id",848            "auth_type": "token",849            "kv_engine_version": 2850        }851        test_hook = VaultHook(**kwargs)852        secret = test_hook.get_secret(secret_path="missing")853        self.assertEqual({'secret_key': 'secret_value'}, secret)854        mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(855            mount_point='secret', path='missing', version=None)856    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")857    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")858    def test_get_existing_key_v2_version(self, mock_hvac, mock_get_connection):859        mock_connection = self.get_mock_connection()860        mock_get_connection.return_value = mock_connection861        mock_client = mock.MagicMock()862        mock_hvac.Client.return_value = mock_client863        connection_dict = {}864        mock_client.secrets.kv.v2.read_secret_version.return_value = {865            'request_id': '94011e25-f8dc-ec29-221b-1f9c1d9ad2ae',866            'lease_id': '',867            'renewable': False,868            'lease_duration': 0,869            'data': {870                'data': {'secret_key': 'secret_value'},871                'metadata': {'created_time': '2020-03-16T21:01:43.331126Z',872                             'deletion_time': '',873                             'destroyed': False,874                             'version': 1}},875            'wrap_info': None,876            'warnings': None,877            'auth': None878        }879        mock_connection.extra_dejson.get.side_effect = connection_dict.get880        kwargs = {881            "vault_conn_id": "vault_conn_id",882            "auth_type": "token",883            "kv_engine_version": 2884        }885        test_hook = VaultHook(**kwargs)886        secret = test_hook.get_secret(secret_path="missing", secret_version=1)887        self.assertEqual({'secret_key': 'secret_value'}, secret)888        mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(889            mount_point='secret', path='missing', version=1)890    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")891    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")892    def test_get_existing_key_v1(self, mock_hvac, mock_get_connection):893        mock_connection = self.get_mock_connection()894        mock_get_connection.return_value = mock_connection895        mock_client = mock.MagicMock()896        mock_hvac.Client.return_value = mock_client897        connection_dict = {}898        mock_client.secrets.kv.v1.read_secret.return_value = {899            'request_id': '182d0673-618c-9889-4cba-4e1f4cfe4b4b',900            'lease_id': '',901            'renewable': False,902            'lease_duration': 2764800,903            'data': {'value': 'world'},904            'wrap_info': None,905            'warnings': None,906            'auth': None}907        mock_connection.extra_dejson.get.side_effect = connection_dict.get908        kwargs = {909            "vault_conn_id": "vault_conn_id",910            "auth_type": "token",911            "kv_engine_version": 1912        }913        test_hook = VaultHook(**kwargs)914        secret = test_hook.get_secret(secret_path="missing")915        self.assertEqual({'value': 'world'}, secret)916        mock_client.secrets.kv.v1.read_secret.assert_called_once_with(917            mount_point='secret', path='missing')918    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")919    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")920    def test_get_secret_metadata_v2(self, mock_hvac, mock_get_connection):921        mock_connection = self.get_mock_connection()922        mock_get_connection.return_value = mock_connection923        mock_client = mock.MagicMock()924        mock_hvac.Client.return_value = mock_client925        connection_dict = {}926        mock_client.secrets.kv.v2.read_secret_metadata.return_value = {927            'request_id': '94011e25-f8dc-ec29-221b-1f9c1d9ad2ae',928            'lease_id': '',929            'renewable': False,930            'lease_duration': 0,931            'metadata': [932                {'created_time': '2020-03-16T21:01:43.331126Z',933                 'deletion_time': '',934                 'destroyed': False,935                 'version': 1},936                {'created_time': '2020-03-16T21:01:43.331126Z',937                 'deletion_time': '',938                 'destroyed': False,939                 'version': 2},940            ]941        }942        mock_connection.extra_dejson.get.side_effect = connection_dict.get943        kwargs = {944            "vault_conn_id": "vault_conn_id",945            "auth_type": "token",946            "kv_engine_version": 2947        }948        test_hook = VaultHook(**kwargs)949        metadata = test_hook.get_secret_metadata(secret_path="missing")950        self.assertEqual(951            {952                'request_id': '94011e25-f8dc-ec29-221b-1f9c1d9ad2ae',953                'lease_id': '',954                'renewable': False,955                'lease_duration': 0,956                'metadata': [957                    {'created_time': '2020-03-16T21:01:43.331126Z',958                     'deletion_time': '',959                     'destroyed': False,960                     'version': 1},961                    {'created_time': '2020-03-16T21:01:43.331126Z',962                     'deletion_time': '',963                     'destroyed': False,964                     'version': 2},965                ]966            }, metadata)967        mock_client.secrets.kv.v2.read_secret_metadata.assert_called_once_with(968            mount_point='secret', path='missing')969    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")970    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")971    def test_get_secret_including_metadata_v2(self, mock_hvac, mock_get_connection):972        mock_connection = self.get_mock_connection()973        mock_get_connection.return_value = mock_connection974        mock_client = mock.MagicMock()975        mock_hvac.Client.return_value = mock_client976        connection_dict = {}977        mock_client.secrets.kv.v2.read_secret_version.return_value = {978            'request_id': '94011e25-f8dc-ec29-221b-1f9c1d9ad2ae',979            'lease_id': '',980            'renewable': False,981            'lease_duration': 0,982            'data': {983                'data': {'secret_key': 'secret_value'},984                'metadata': {'created_time': '2020-03-16T21:01:43.331126Z',985                             'deletion_time': '',986                             'destroyed': False,987                             'version': 1}},988            'wrap_info': None,989            'warnings': None,990            'auth': None991        }992        mock_connection.extra_dejson.get.side_effect = connection_dict.get993        kwargs = {994            "vault_conn_id": "vault_conn_id",995            "auth_type": "token",996            "kv_engine_version": 2997        }998        test_hook = VaultHook(**kwargs)999        metadata = test_hook.get_secret_including_metadata(secret_path="missing")1000        self.assertEqual(1001            {1002                'request_id': '94011e25-f8dc-ec29-221b-1f9c1d9ad2ae',1003                'lease_id': '',1004                'renewable': False,1005                'lease_duration': 0,1006                'data': {1007                    'data': {'secret_key': 'secret_value'},1008                    'metadata': {'created_time': '2020-03-16T21:01:43.331126Z',1009                                 'deletion_time': '',1010                                 'destroyed': False,1011                                 'version': 1}},1012                'wrap_info': None,1013                'warnings': None,1014                'auth': None1015            }, metadata)1016        mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(1017            mount_point='secret', path='missing', version=None)1018    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")1019    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")1020    def test_create_or_update_secret_v2(self, mock_hvac, mock_get_connection):1021        mock_connection = self.get_mock_connection()1022        mock_get_connection.return_value = mock_connection1023        mock_client = mock.MagicMock()1024        mock_hvac.Client.return_value = mock_client1025        connection_dict = {}1026        mock_connection.extra_dejson.get.side_effect = connection_dict.get1027        kwargs = {1028            "vault_conn_id": "vault_conn_id",1029            "auth_type": "token",1030            "kv_engine_version": 21031        }1032        test_hook = VaultHook(**kwargs)1033        test_hook.create_or_update_secret(1034            secret_path="path",1035            secret={'key': 'value'}1036        )1037        mock_client.secrets.kv.v2.create_or_update_secret.assert_called_once_with(1038            mount_point='secret', secret_path='path', secret={'key': 'value'}, cas=None)1039    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")1040    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")1041    def test_create_or_update_secret_v2_cas(self, mock_hvac, mock_get_connection):1042        mock_connection = self.get_mock_connection()1043        mock_get_connection.return_value = mock_connection1044        mock_client = mock.MagicMock()1045        mock_hvac.Client.return_value = mock_client1046        connection_dict = {}1047        mock_connection.extra_dejson.get.side_effect = connection_dict.get1048        kwargs = {1049            "vault_conn_id": "vault_conn_id",1050            "auth_type": "token",1051            "kv_engine_version": 21052        }1053        test_hook = VaultHook(**kwargs)1054        test_hook.create_or_update_secret(1055            secret_path="path",1056            secret={'key': 'value'},1057            cas=101058        )1059        mock_client.secrets.kv.v2.create_or_update_secret.assert_called_once_with(1060            mount_point='secret', secret_path='path', secret={'key': 'value'}, cas=10)1061    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")1062    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")1063    def test_create_or_update_secret_v1(self, mock_hvac, mock_get_connection):1064        mock_connection = self.get_mock_connection()1065        mock_get_connection.return_value = mock_connection1066        mock_client = mock.MagicMock()1067        mock_hvac.Client.return_value = mock_client1068        connection_dict = {}1069        mock_connection.extra_dejson.get.side_effect = connection_dict.get1070        kwargs = {1071            "vault_conn_id": "vault_conn_id",1072            "auth_type": "token",1073            "kv_engine_version": 11074        }1075        test_hook = VaultHook(**kwargs)1076        test_hook.create_or_update_secret(1077            secret_path="path",1078            secret={'key': 'value'}1079        )1080        mock_client.secrets.kv.v1.create_or_update_secret.assert_called_once_with(1081            mount_point='secret', secret_path='path', secret={'key': 'value'}, method=None)1082    @mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")1083    @mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")1084    def test_create_or_update_secret_v1_post(self, mock_hvac, mock_get_connection):1085        mock_connection = self.get_mock_connection()1086        mock_get_connection.return_value = mock_connection1087        mock_client = mock.MagicMock()1088        mock_hvac.Client.return_value = mock_client1089        connection_dict = {}1090        mock_connection.extra_dejson.get.side_effect = connection_dict.get1091        kwargs = {1092            "vault_conn_id": "vault_conn_id",1093            "auth_type": "token",1094            "kv_engine_version": 11095        }1096        test_hook = VaultHook(**kwargs)1097        test_hook.create_or_update_secret(1098            secret_path="path",1099            secret={'key': 'value'},1100            method="post"1101        )1102        mock_client.secrets.kv.v1.create_or_update_secret.assert_called_once_with(...test_hook.py
Source:test_hook.py  
...158        """159        Test that when a hook throws an exception that it is caught and logged160        as a warning.161        """162        def test_hook(module):163            raise Exception('test_hook_failed')164        register_post_import_hook('tests.utils.test_module', test_hook)165        with mock.patch('oteltrace.utils.hook.log') as log_mock:166            import tests.utils.test_module  # noqa167            calls = [168                mock.call('hook "{}" for module "tests.utils.test_module" failed: test_hook_failed'.format(test_hook))169            ]170            log_mock.warning.assert_has_calls(calls)171    def test_hook_called_with_module(self):172        """173        Test that a hook is called with the module that it is hooked on.174        """175        def test_hook(module):176            self.assertTrue(hasattr(module, 'A'))177        register_post_import_hook('tests.utils.test_module', test_hook)...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
