Esempio n. 1
0
 def test_get_existing_key(self, mock_client_info, mock_secrets_client):
     mock_client = mock.MagicMock()
     mock_client_info.return_value = mock.MagicMock()
     mock_secrets_client.return_value = mock_client
     mock_client.secret_version_path.return_value = "full-path"
     test_response = AccessSecretVersionResponse()
     test_response.payload.data = b"result"
     mock_client.access_secret_version.return_value = test_response
     secrets_client = _SecretManagerClient(credentials="credentials")
     secret = secrets_client.get_secret(secret_id="existing", project_id="project_id")
     mock_client.secret_version_path.assert_called_once_with("project_id", 'existing', 'latest')
     assert "result" == secret
     mock_client.access_secret_version.assert_called_once_with('full-path')
Esempio n. 2
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")
Esempio n. 3
0
    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_existing_key_with_version(self, mock_client_info,
                                        mock_secrets_client):
     mock_client = mock.MagicMock()
     mock_client_info.return_value = mock.MagicMock()
     mock_secrets_client.return_value = mock_client
     mock_client.secret_version_path.return_value = "full-path"
     test_response = AccessSecretVersionResponse()
     test_response.payload.data = "result".encode("UTF-8")
     mock_client.access_secret_version.return_value = test_response
     secrets_client = _SecretManagerClient(credentials="credentials")
     secret = secrets_client.get_secret(secret_id="existing",
                                        project_id="project_id",
                                        secret_version="test-version")
     mock_client.secret_version_path.assert_called_once_with(
         "project_id", 'existing', 'test-version')
     self.assertEqual("result", secret)
     mock_client.access_secret_version.assert_called_once_with('full-path')
Esempio n. 5
0
    def test_get_config(self, config_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 = CONFIG_VALUE.encode("UTF-8")
        mock_client.access_secret_version.return_value = test_response

        secrets_manager_backend = CloudSecretManagerBackend(
            config_prefix=config_prefix)
        secret_id = secrets_manager_backend.build_path(config_prefix,
                                                       CONFIG_KEY, SEP)
        returned_val = secrets_manager_backend.get_config(CONFIG_KEY)
        assert CONFIG_VALUE == returned_val
        mock_client.secret_version_path.assert_called_once_with(
            PROJECT_ID, secret_id, "latest")
def test_send_email_google_cloud_secret_manager(mock_request):
    secret_payload = SecretPayload(
        data=os.environ["NOTIFY_API_KEY"].encode("UTF-8"))
    access_secret_version_response = AccessSecretVersionResponse(
        payload=secret_payload)
    del os.environ["NOTIFY_API_KEY"]
    with mock.patch("google.auth.default",
                    return_value=("", "project_id"),
                    autospec=True):
        with mock.patch(
                "google.cloud.secretmanager.SecretManagerServiceClient.access_secret_version",
                return_value=access_secret_version_response,
                autospec=True,
        ):
            reload(main)
            responses.add(responses.POST,
                          url,
                          json={"content": "ok"},
                          status=200)
            response = send_email(mock_request)
            assert response == ("notify request successful", 200)