コード例 #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 = CloudSecretsManagerBackend(
            connections_prefix=connections_prefix)
        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, f"{connections_prefix}/{CONN_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 = CloudSecretsManagerBackend(
            connections_prefix=CONNECTIONS_PREFIX)
        secret_id = secrets_manager_backend.build_path(CONNECTIONS_PREFIX,
                                                       CONN_ID, SEP)
        with self.assertLogs(secrets_manager_backend.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"GCP API Call Error \\(NotFound\\): Secret ID {secret_id} not found"
            )