def test_variables_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: secrets_manager_backend = CloudSecretManagerBackend( variables_prefix=None) self.assertIsNone(secrets_manager_backend.get_variable(VAR_KEY)) mock_get_secret.assert_not_called()
def test_get_variable(self, variables_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 = VAR_VALUE.encode("UTF-8") mock_client.access_secret_version.return_value = test_response secrets_manager_backend = CloudSecretManagerBackend(variables_prefix=variables_prefix) secret_id = secrets_manager_backend.build_path(variables_prefix, VAR_KEY, SEP) returned_uri = secrets_manager_backend.get_variable(VAR_KEY) self.assertEqual(VAR_VALUE, returned_uri) mock_client.secret_version_path.assert_called_once_with(PROJECT_ID, secret_id, "latest")
def test_get_variable_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(variables_prefix=VARIABLES_PREFIX) secret_id = secrets_manager_backend.build_path(VARIABLES_PREFIX, VAR_KEY, SEP) with self.assertLogs(secrets_manager_backend.client.log, level="ERROR") as log_output: self.assertIsNone(secrets_manager_backend.get_variable(VAR_KEY)) self.assertRegex( log_output.output[0], f"Google Cloud API Call Error \\(NotFound\\): Secret ID {secret_id} not found", )