def test_get_bind_by_name(auth, mock_get_app_by_name):
    auth.return_value = responses.auth_response
    with requests_mock.mock() as m:
        m.get(
            "https://example.com/v2/apps/" + responses.app["metadata"]["guid"] + "/service_bindings",
            status_code=200, json=responses.binds, complete_qs=True)

        get_bind_by_name("my-bind", config.config, secrets.secrets, app_name="my-app", org_name="my-org")

    mock_get_app_by_name.assert_has_calls([call("my-app", config.config,
                                                secrets.secrets, org_name="my-org", space_name=None)])
def test_get_bind_by_name_when_app_has_no_binding_with_the_specified_name(auth, mock_get_app_by_name):
    auth.return_value = responses.auth_response
    with requests_mock.mock() as m:
        m.get(
            "https://example.com/v2/apps/" + responses.app["metadata"]["guid"] + "/service_bindings",
            status_code=200, json=responses.binds, complete_qs=True)

        with pytest.raises(FailedActivity):
            get_bind_by_name("my-bind-non-existent", config.config, secrets.secrets, app_name="my-app", org_name="my-org")

    mock_get_app_by_name.assert_has_calls([call("my-app", config.config,
                                                secrets.secrets, org_name="my-org", space_name=None)])
def unbind_service_from_app(app_name: str,
                            bind_name: str,
                            configuration: Configuration,
                            secrets: Secrets,
                            org_name: str = None,
                            space_name: str = None):
    """
    Unbind the service from the given application.

    See
    https://apidocs.cloudfoundry.org/280/service_bindings/delete_a_particular_service_binding.html
    """  # noqa: E501
    service_bind = get_bind_by_name(bind_name,
                                    configuration,
                                    secrets,
                                    app_name=app_name,
                                    space_name=space_name,
                                    org_name=org_name)

    logger.debug("Ubinding service {s} from application {a}".format(
        s=bind_name, a=app_name))

    path = "/v2/service_bindings/{s}".format(
        s=service_bind["metadata"]["guid"])
    call_api(path, configuration, secrets, method="DELETE")
def test_get_bind_by_name_when_app_has_no_bindings(auth, mock_get_app_by_name):
    auth.return_value = responses.auth_response
    with requests_mock.mock() as m:
        m.get(
            "https://example.com/v2/apps/" + responses.app["metadata"]["guid"] + "/service_bindings",
            status_code=200, json={
                'total_results': 0,
                'total_pages': 0,
                'prev_url': None,
                'next_url': None,
                'resources': []
            }, complete_qs=True)

        with pytest.raises(FailedActivity):
            get_bind_by_name("my-bind", config.config, secrets.secrets, app_name="my-app", org_name="my-org")

    mock_get_app_by_name.assert_has_calls([call("my-app", config.config,
                                                secrets.secrets, org_name="my-org", space_name=None)])