github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/api/cloudcontroller/ccv3/ccv3_suite_test.go (about)

     1  package ccv3_test
     2  
     3  import (
     4  	"bytes"
     5  	"log"
     6  	"net/http"
     7  	"strings"
     8  
     9  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/ccv3fakes"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  	. "github.com/onsi/gomega/ghttp"
    14  
    15  	"testing"
    16  )
    17  
    18  func TestCcv3(t *testing.T) {
    19  	RegisterFailHandler(Fail)
    20  	RunSpecs(t, "Cloud Controller V3 Suite")
    21  }
    22  
    23  var server *Server
    24  
    25  var _ = BeforeEach(func() {
    26  	server = NewTLSServer()
    27  
    28  	// Suppresses ginkgo server logs
    29  	server.HTTPTestServer.Config.ErrorLog = log.New(&bytes.Buffer{}, "", 0)
    30  })
    31  
    32  var _ = AfterEach(func() {
    33  	server.Close()
    34  })
    35  
    36  func NewTestClient(config ...Config) (*Client, *ccv3fakes.FakeClock) {
    37  	SetupV3Response()
    38  	var client *Client
    39  	fakeClock := new(ccv3fakes.FakeClock)
    40  
    41  	if config != nil {
    42  		client = TestClient(config[0], fakeClock)
    43  	} else {
    44  		client = TestClient(Config{AppName: "CF CLI API V3 Test", AppVersion: "Unknown"}, fakeClock)
    45  	}
    46  	warnings, err := client.TargetCF(TargetSettings{
    47  		SkipSSLValidation: true,
    48  		URL:               server.URL(),
    49  	})
    50  	Expect(err).ToNot(HaveOccurred())
    51  	Expect(warnings).To(BeEmpty())
    52  
    53  	return client, fakeClock
    54  }
    55  
    56  func SetupV3Response() {
    57  	serverURL := server.URL()
    58  	rootResponse := strings.Replace(`{
    59  		"links": {
    60  			"self": {
    61  				"href": "SERVER_URL"
    62  			},
    63  			"cloud_controller_v2": {
    64  				"href": "SERVER_URL/v2",
    65  				"meta": {
    66  					"version": "2.64.0"
    67  				}
    68  			},
    69  			"cloud_controller_v3": {
    70  				"href": "SERVER_URL/v3",
    71  				"meta": {
    72  					"version": "3.0.0-alpha.5"
    73  				}
    74  			},
    75  			"uaa": {
    76  				"href": "https://uaa.bosh-lite.com"
    77  			}
    78  		}
    79  	}`, "SERVER_URL", serverURL, -1)
    80  
    81  	server.AppendHandlers(
    82  		CombineHandlers(
    83  			VerifyRequest(http.MethodGet, "/"),
    84  			RespondWith(http.StatusOK, rootResponse),
    85  		),
    86  	)
    87  
    88  	v3Response := strings.Replace(`{
    89  		"links": {
    90  			"self": {
    91  				"href": "SERVER_URL/v3"
    92  			},
    93  			"apps": {
    94  				"href": "SERVER_URL/v3/apps"
    95  			},
    96  			"tasks": {
    97  				"href": "SERVER_URL/v3/tasks"
    98  			},
    99  			"isolation_segments": {
   100  				"href": "SERVER_URL/v3/isolation_segments"
   101  			},
   102  			"builds": {
   103  				"href": "SERVER_URL/v3/builds"
   104  			},
   105  			"organizations": {
   106  				"href": "SERVER_URL/v3/organizations"
   107  			},
   108  			"service_brokers": {
   109  				"href": "SERVER_URL/v3/service_brokers"
   110  			},
   111  			"service_instances": {
   112  				"href": "SERVER_URL/v3/service_instances"
   113  			},
   114  			"spaces": {
   115  				"href": "SERVER_URL/v3/spaces"
   116  			},
   117  			"packages": {
   118  				"href": "SERVER_URL/v3/packages"
   119  			},
   120  			"processes": {
   121  				"href": "SERVER_URL/v3/processes"
   122  			},
   123  			"droplets": {
   124  				"href": "SERVER_URL/v3/droplets"
   125  			},
   126  			"audit_events": {
   127  				"href": "SERVER_URL/v3/audit_events"
   128  			},
   129              "domains": {
   130                "href": "SERVER_URL/v3/domains"
   131              },
   132  			"deployments": {
   133  				"href": "SERVER_URL/v3/deployments"
   134  			},
   135  			"stacks": {
   136  				"href": "SERVER_URL/v3/stacks"
   137  			},
   138  			"buildpacks": {
   139  				"href": "SERVER_URL/v3/buildpacks"
   140  			},
   141  			"feature_flags": {
   142  				"href": "SERVER_URL/v3/feature_flags"
   143  			},
   144  			"resource_matches": {
   145  				"href": "SERVER_URL/v3/resource_matches"
   146  			},
   147              "roles": {
   148                  "href": "SERVER_URL/v3/roles"
   149              },
   150              "routes": {
   151                  "href": "SERVER_URL/v3/routes"
   152              },
   153              "users": {
   154                  "href": "SERVER_URL/v3/users"
   155              },
   156              "environment_variable_groups": {
   157                  "href": "SERVER_URL/v3/environment_variable_groups"
   158              }
   159  		}
   160  	}`, "SERVER_URL", serverURL, -1)
   161  
   162  	server.AppendHandlers(
   163  		CombineHandlers(
   164  			VerifyRequest(http.MethodGet, "/v3"),
   165  			RespondWith(http.StatusOK, v3Response),
   166  		),
   167  	)
   168  }