github.com/sleungcy-sap/cli@v7.1.0+incompatible/api/cloudcontroller/ccv2/target_test.go (about)

     1  package ccv2_test
     2  
     3  import (
     4  	"net/http"
     5  	"strings"
     6  
     7  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/ccv2fakes"
     9  
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  	. "github.com/onsi/gomega/ghttp"
    13  )
    14  
    15  var _ = Describe("Target", func() {
    16  	var (
    17  		serverAPIURL string
    18  
    19  		client *Client
    20  	)
    21  
    22  	BeforeEach(func() {
    23  		serverAPIURL = server.URL()[8:]
    24  	})
    25  
    26  	Describe("TargetCF", func() {
    27  		var response string
    28  		var fakeWrapper1 *ccv2fakes.FakeConnectionWrapper
    29  		var fakeWrapper2 *ccv2fakes.FakeConnectionWrapper
    30  
    31  		BeforeEach(func() {
    32  			response = `{
    33  					"name":"",
    34  					"build":"",
    35  					"support":"http://support.cloudfoundry.com",
    36  					"version":0,
    37  					"description":"",
    38  					"authorization_endpoint":"https://login.APISERVER",
    39  					"min_cli_version":null,
    40  					"min_recommended_cli_version":null,
    41  					"api_version":"2.59.0",
    42  					"app_ssh_endpoint":"ssh.APISERVER",
    43  					"app_ssh_host_key_fingerprint":"a6:d1:08:0b:b0:cb:9b:5f:c4:ba:44:2a:97:26:19:8a",
    44  					"routing_endpoint": "https://APISERVER/routing",
    45  					"app_ssh_oauth_client":"ssh-proxy",
    46  					"logging_endpoint":"wss://loggregator.APISERVER",
    47  					"doppler_logging_endpoint":"wss://doppler.APISERVER"
    48  				}`
    49  			response = strings.Replace(response, "APISERVER", serverAPIURL, -1)
    50  			fakeWrapper1 = new(ccv2fakes.FakeConnectionWrapper)
    51  			fakeWrapper1.WrapReturns(fakeWrapper1)
    52  			fakeWrapper2 = new(ccv2fakes.FakeConnectionWrapper)
    53  			fakeWrapper2.WrapReturns(fakeWrapper2)
    54  
    55  			client = NewClient(Config{
    56  				AppName:    "CF CLI API Target Test",
    57  				AppVersion: "Unknown",
    58  				Wrappers:   []ConnectionWrapper{fakeWrapper1, fakeWrapper2},
    59  			})
    60  
    61  		})
    62  		When("using a older API that does not have the log cache url", func() {
    63  			BeforeEach(func() {
    64  				server.AppendHandlers(
    65  					CombineHandlers(
    66  						VerifyRequest(http.MethodGet, "/v2/info"),
    67  						RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    68  					),
    69  					CombineHandlers(
    70  						VerifyRequest(http.MethodGet, "/"),
    71  						RespondWith(http.StatusOK, `{ "links": "someurl" : "cool beans"}}`),
    72  					),
    73  				)
    74  
    75  			})
    76  			It("should string parse the API url to add the log cache url", func() {
    77  				_, err := client.TargetCF(TargetSettings{
    78  					SkipSSLValidation: true,
    79  					URL:               "api.fun.com",
    80  				})
    81  				Expect(err).NotTo(HaveOccurred())
    82  
    83  				Expect(client.LogCacheEndpoint()).To(Equal("log-cache.fun.com"))
    84  			})
    85  		})
    86  		BeforeEach(func() {
    87  			server.AppendHandlers(
    88  				CombineHandlers(
    89  					VerifyRequest(http.MethodGet, "/v2/info"),
    90  					RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    91  				),
    92  				CombineHandlers(
    93  					VerifyRequest(http.MethodGet, "/"),
    94  					RespondWith(http.StatusOK, `{ "links": {"log_cache": {"href": "api.coolbeans.log-cache"}}}`),
    95  				),
    96  			)
    97  		})
    98  
    99  		When("client has wrappers", func() {
   100  
   101  			BeforeEach(func() {
   102  				fakeWrapper1 = new(ccv2fakes.FakeConnectionWrapper)
   103  				fakeWrapper1.WrapReturns(fakeWrapper1)
   104  				fakeWrapper2 = new(ccv2fakes.FakeConnectionWrapper)
   105  				fakeWrapper2.WrapReturns(fakeWrapper2)
   106  
   107  				client = NewClient(Config{
   108  					AppName:    "CF CLI API Target Test",
   109  					AppVersion: "Unknown",
   110  					Wrappers:   []ConnectionWrapper{fakeWrapper1, fakeWrapper2},
   111  				})
   112  			})
   113  
   114  			It("calls wrap on all the wrappers", func() {
   115  				_, err := client.TargetCF(TargetSettings{
   116  					SkipSSLValidation: true,
   117  					URL:               server.URL(),
   118  				})
   119  				Expect(err).NotTo(HaveOccurred())
   120  
   121  				Expect(fakeWrapper1.WrapCallCount()).To(Equal(1))
   122  				Expect(fakeWrapper2.WrapCallCount()).To(Equal(1))
   123  				Expect(fakeWrapper2.WrapArgsForCall(0)).To(Equal(fakeWrapper1))
   124  			})
   125  		})
   126  
   127  		When("passed a valid API URL", func() {
   128  			BeforeEach(func() {
   129  				client = NewClient(Config{AppName: "CF CLI API Target Test", AppVersion: "Unknown"})
   130  			})
   131  
   132  			When("the api has unverified SSL", func() {
   133  				When("setting the skip ssl flat", func() {
   134  					It("sets all the endpoints on the client", func() {
   135  						_, err := client.TargetCF(TargetSettings{
   136  							SkipSSLValidation: true,
   137  							URL:               server.URL(),
   138  						})
   139  						Expect(err).NotTo(HaveOccurred())
   140  
   141  						Expect(client.API()).To(MatchRegexp("https://%s", serverAPIURL))
   142  						Expect(client.APIVersion()).To(Equal("2.59.0"))
   143  						Expect(client.AuthorizationEndpoint()).To(MatchRegexp("https://login.%s", serverAPIURL))
   144  						Expect(client.DopplerEndpoint()).To(MatchRegexp("wss://doppler.%s", serverAPIURL))
   145  						Expect(client.RoutingEndpoint()).To(MatchRegexp("https://%s/routing", serverAPIURL))
   146  					})
   147  				})
   148  
   149  				It("sets the http endpoint and warns user", func() {
   150  					warnings, err := client.TargetCF(TargetSettings{
   151  						SkipSSLValidation: true,
   152  						URL:               server.URL(),
   153  					})
   154  					Expect(err).NotTo(HaveOccurred())
   155  					Expect(warnings).To(ContainElement("this is a warning"))
   156  				})
   157  			})
   158  		})
   159  	})
   160  })