github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/api/cloudcontroller/ccv3/target_test.go (about)

     1  package ccv3_test
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     9  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/ccv3fakes"
    11  
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  	. "github.com/onsi/gomega/ghttp"
    15  )
    16  
    17  var _ = Describe("Target", func() {
    18  	var (
    19  		client *Client
    20  	)
    21  
    22  	BeforeEach(func() {
    23  		client = NewClient(Config{AppName: "CF CLI API V3 Target Test", AppVersion: "Unknown"})
    24  	})
    25  
    26  	Describe("TargetCF", func() {
    27  		BeforeEach(func() {
    28  			server.Reset()
    29  
    30  			serverURL := server.URL()
    31  			rootResponse := fmt.Sprintf(`{
    32  				"links": {
    33  					"self": {
    34  						"href": "%s"
    35  					},
    36  					"cloud_controller_v2": {
    37  						"href": "%s/v2",
    38  						"meta": {
    39  							"version": "2.64.0"
    40  						}
    41  					},
    42  					"cloud_controller_v3": {
    43  						"href": "%s/v3",
    44  						"meta": {
    45  							"version": "3.0.0-alpha.5"
    46  						}
    47  					},
    48  					"uaa": {
    49  						"href": "https://uaa.bosh-lite.com"
    50  					}
    51  				}
    52  			}`, serverURL, serverURL, serverURL)
    53  
    54  			server.AppendHandlers(
    55  				CombineHandlers(
    56  					VerifyRequest(http.MethodGet, "/"),
    57  					RespondWith(
    58  						http.StatusOK,
    59  						rootResponse,
    60  						http.Header{"X-Cf-Warnings": {"warning 1"}}),
    61  				),
    62  			)
    63  
    64  			v3Response := fmt.Sprintf(`{
    65  				"links": {
    66  					"self": {
    67  						"href": "%s/v3"
    68  					},
    69  					"tasks": {
    70  						"href": "%s/v3/tasks"
    71  					}
    72  				}
    73  			}`, serverURL, serverURL)
    74  
    75  			server.AppendHandlers(
    76  				CombineHandlers(
    77  					VerifyRequest(http.MethodGet, "/v3"),
    78  					RespondWith(
    79  						http.StatusOK,
    80  						v3Response,
    81  						http.Header{"X-Cf-Warnings": {"warning 2"}}),
    82  				),
    83  			)
    84  		})
    85  
    86  		Context("when client has wrappers", func() {
    87  			var fakeWrapper1 *ccv3fakes.FakeConnectionWrapper
    88  			var fakeWrapper2 *ccv3fakes.FakeConnectionWrapper
    89  
    90  			BeforeEach(func() {
    91  				fakeWrapper1 = new(ccv3fakes.FakeConnectionWrapper)
    92  				fakeWrapper1.WrapReturns(fakeWrapper1)
    93  				fakeWrapper2 = new(ccv3fakes.FakeConnectionWrapper)
    94  				fakeWrapper2.WrapReturns(fakeWrapper2)
    95  
    96  				fakeWrapper2.MakeStub = func(request *cloudcontroller.Request, passedResponse *cloudcontroller.Response) error {
    97  					apiInfo, ok := passedResponse.Result.(*APIInfo)
    98  					if ok { // Only caring about the first time Make is called, ignore all others
    99  						apiInfo.Links.CCV3.HREF = server.URL() + "/v3"
   100  					}
   101  					return nil
   102  				}
   103  
   104  				client = NewClient(Config{
   105  					AppName:    "CF CLI API Target Test",
   106  					AppVersion: "Unknown",
   107  					Wrappers:   []ConnectionWrapper{fakeWrapper1, fakeWrapper2},
   108  				})
   109  			})
   110  
   111  			It("calls wrap on all the wrappers", func() {
   112  				_, err := client.TargetCF(TargetSettings{
   113  					SkipSSLValidation: true,
   114  					URL:               server.URL(),
   115  				})
   116  				Expect(err).NotTo(HaveOccurred())
   117  
   118  				Expect(fakeWrapper1.WrapCallCount()).To(Equal(1))
   119  				Expect(fakeWrapper2.WrapCallCount()).To(Equal(1))
   120  				Expect(fakeWrapper2.WrapArgsForCall(0)).To(Equal(fakeWrapper1))
   121  			})
   122  		})
   123  
   124  		Context("when passed a valid API URL", func() {
   125  			Context("when the server has unverified SSL", func() {
   126  				Context("when setting the skip ssl flag", func() {
   127  					It("sets all the endpoints on the client and returns all warnings", func() {
   128  						warnings, err := client.TargetCF(TargetSettings{
   129  							SkipSSLValidation: true,
   130  							URL:               server.URL(),
   131  						})
   132  						Expect(err).NotTo(HaveOccurred())
   133  						Expect(warnings).To(ConsistOf("warning 1", "warning 2"))
   134  
   135  						Expect(client.UAA()).To(Equal("https://uaa.bosh-lite.com"))
   136  						Expect(client.CloudControllerAPIVersion()).To(Equal("3.0.0-alpha.5"))
   137  					})
   138  				})
   139  			})
   140  		})
   141  
   142  		Context("when the cloud controller encounters an error", func() {
   143  			BeforeEach(func() {
   144  				server.SetHandler(1,
   145  					CombineHandlers(
   146  						VerifyRequest(http.MethodGet, "/v3"),
   147  						RespondWith(
   148  							http.StatusNotFound,
   149  							`{"errors": [{}]}`,
   150  							http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   151  					),
   152  				)
   153  			})
   154  
   155  			It("returns the same error", func() {
   156  				warnings, err := client.TargetCF(TargetSettings{
   157  					SkipSSLValidation: true,
   158  					URL:               server.URL(),
   159  				})
   160  				Expect(err).To(MatchError(ccerror.ResourceNotFoundError{}))
   161  				Expect(warnings).To(ConsistOf("warning 1", "this is a warning"))
   162  			})
   163  		})
   164  	})
   165  })