Пример #1
0
    def test_get_conn_uri(self, connections_prefix, mock_client_callable, mock_get_creds):
        mock_get_creds.return_value = CREDENTIALS, PROJECT_ID
        mock_client = mock.MagicMock()
        mock_client_callable.return_value = mock_client

        test_response = AccessSecretVersionResponse()
        test_response.payload.data = CONN_URI.encode("UTF-8")
        mock_client.access_secret_version.return_value = test_response

        secrets_manager_backend = CloudSecretManagerBackend(connections_prefix=connections_prefix)
        secret_id = secrets_manager_backend.build_path(connections_prefix, CONN_ID, SEP)
        returned_uri = secrets_manager_backend.get_conn_uri(conn_id=CONN_ID)
        self.assertEqual(CONN_URI, returned_uri)
        mock_client.secret_version_path.assert_called_once_with(PROJECT_ID, secret_id, "latest")
Пример #2
0
    def test_get_conn_uri_non_existent_key(self, mock_client_callable, mock_get_creds):
        mock_get_creds.return_value = CREDENTIALS, PROJECT_ID
        mock_client = mock.MagicMock()
        mock_client_callable.return_value = mock_client
        # The requested secret id or secret version does not exist
        mock_client.access_secret_version.side_effect = NotFound('test-msg')

        secrets_manager_backend = CloudSecretManagerBackend(connections_prefix=CONNECTIONS_PREFIX)
        secret_id = secrets_manager_backend.build_path(CONNECTIONS_PREFIX, CONN_ID, SEP)
        with self.assertLogs(secrets_manager_backend.client.log, level="ERROR") as log_output:
            self.assertIsNone(secrets_manager_backend.get_conn_uri(conn_id=CONN_ID))
            self.assertEqual([], secrets_manager_backend.get_connections(conn_id=CONN_ID))
            self.assertRegex(
                log_output.output[0],
                f"Google Cloud API Call Error \\(NotFound\\): Secret ID {secret_id} not found",
            )
    def test_connections_prefix_none_value(self, mock_client_callable,
                                           mock_get_creds):
        mock_get_creds.return_value = CREDENTIALS, PROJECT_ID
        mock_client = mock.MagicMock()
        mock_client_callable.return_value = mock_client

        with mock.patch(
                MODULE_NAME +
                '.CloudSecretManagerBackend._get_secret') as mock_get_secret:
            with mock.patch(
                    MODULE_NAME +
                    '.CloudSecretManagerBackend._is_valid_prefix_and_sep'
            ) as mock_is_valid_prefix_sep:
                secrets_manager_backend = CloudSecretManagerBackend(
                    connections_prefix=None)

                mock_is_valid_prefix_sep.assert_not_called()
                self.assertIsNone(
                    secrets_manager_backend.get_conn_uri(conn_id=CONN_ID))
                mock_get_secret.assert_not_called()