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

     1  package ccv3_test
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"strings"
     7  
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     9  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
    11  
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  	. "github.com/onsi/gomega/ghttp"
    15  )
    16  
    17  var _ = Describe("Info", func() {
    18  	var (
    19  		client          *Client
    20  		rootRespondWith http.HandlerFunc
    21  		v3RespondWith   http.HandlerFunc
    22  	)
    23  
    24  	JustBeforeEach(func() {
    25  		client = NewTestClient()
    26  
    27  		server.AppendHandlers(
    28  			CombineHandlers(
    29  				VerifyRequest(http.MethodGet, "/"),
    30  				rootRespondWith,
    31  			),
    32  			CombineHandlers(
    33  				VerifyRequest(http.MethodGet, "/v3"),
    34  				v3RespondWith,
    35  			))
    36  	})
    37  
    38  	Describe("when all requests are successful", func() {
    39  		BeforeEach(func() {
    40  			rootResponse := strings.Replace(`{
    41  				"links": {
    42  					"self": {
    43  						"href": "SERVER_URL"
    44  					},
    45  					"cloud_controller_v2": {
    46  						"href": "SERVER_URL/v2",
    47  						"meta": {
    48  							"version": "2.64.0"
    49  						}
    50  					},
    51  					"cloud_controller_v3": {
    52  						"href": "SERVER_URL/v3",
    53  						"meta": {
    54  							"version": "3.0.0-alpha.5"
    55  						}
    56  					},
    57  					"network_policy_v1": {
    58  						"href": "SERVER_URL/networking/v1/external"
    59  					},
    60  					"uaa": {
    61  						"href": "https://uaa.bosh-lite.com"
    62  					},
    63  					"logging": {
    64  						"href": "wss://doppler.bosh-lite.com:443"
    65  					},
    66  					"app_ssh": {
    67  						"href": "ssh.bosh-lite.com:2222",
    68  						"meta": {
    69  							"host_key_fingerprint": "some-fingerprint",
    70  							"oath_client": "some-client"
    71  						}
    72  					}
    73  				}
    74  			}`, "SERVER_URL", server.URL(), -1)
    75  
    76  			rootRespondWith = RespondWith(
    77  				http.StatusOK,
    78  				rootResponse,
    79  				http.Header{"X-Cf-Warnings": {"warning 1"}})
    80  
    81  			v3Response := strings.Replace(`{
    82  				"links": {
    83  					"self": {
    84  						"href": "SERVER_URL/v3"
    85  					},
    86  					"apps": {
    87  						"href": "SERVER_URL/v3/apps"
    88  					},
    89  					"tasks": {
    90  						"href": "SERVER_URL/v3/tasks"
    91  					}
    92  				}
    93  			}`, "SERVER_URL", server.URL(), -1)
    94  
    95  			v3RespondWith = RespondWith(
    96  				http.StatusOK,
    97  				v3Response,
    98  				http.Header{"X-Cf-Warnings": {"warning 2"}})
    99  		})
   100  
   101  		It("returns the CC Information", func() {
   102  			apis, _, _, err := client.Info()
   103  			Expect(err).NotTo(HaveOccurred())
   104  			Expect(apis.UAA()).To(Equal("https://uaa.bosh-lite.com"))
   105  			Expect(apis.Logging()).To(Equal("wss://doppler.bosh-lite.com:443"))
   106  			Expect(apis.NetworkPolicyV1()).To(Equal(fmt.Sprintf("%s/networking/v1/external", server.URL())))
   107  			Expect(apis.AppSSHHostKeyFingerprint()).To(Equal("some-fingerprint"))
   108  			Expect(apis.AppSSHEndpoint()).To(Equal("ssh.bosh-lite.com:2222"))
   109  			Expect(apis.OAuthClient()).To(Equal("some-client"))
   110  		})
   111  
   112  		It("returns back the resource links", func() {
   113  			_, resources, _, err := client.Info()
   114  			Expect(err).NotTo(HaveOccurred())
   115  			Expect(resources[internal.AppsResource].HREF).To(Equal(server.URL() + "/v3/apps"))
   116  			Expect(resources[internal.TasksResource].HREF).To(Equal(server.URL() + "/v3/tasks"))
   117  		})
   118  
   119  		It("returns all warnings", func() {
   120  			_, _, warnings, err := client.Info()
   121  			Expect(err).NotTo(HaveOccurred())
   122  			Expect(warnings).To(ConsistOf("warning 1", "warning 2"))
   123  		})
   124  	})
   125  
   126  	Context("when the cloud controller encounters an error", func() {
   127  		Context("when the root response is invalid", func() {
   128  			BeforeEach(func() {
   129  				rootRespondWith = RespondWith(
   130  					http.StatusNotFound,
   131  					`i am google, bow down`,
   132  				)
   133  			})
   134  
   135  			It("returns an APINotFoundError", func() {
   136  				_, _, _, err := client.Info()
   137  				Expect(err).To(MatchError(ccerror.APINotFoundError{URL: server.URL()}))
   138  			})
   139  		})
   140  
   141  		Context("when the error occurs making a request to '/'", func() {
   142  			BeforeEach(func() {
   143  				rootRespondWith = RespondWith(
   144  					http.StatusNotFound,
   145  					`{"errors": [{}]}`,
   146  					http.Header{"X-Cf-Warnings": {"this is a warning"}})
   147  			})
   148  
   149  			It("returns the same error", func() {
   150  				_, _, warnings, err := client.Info()
   151  				Expect(err).To(MatchError(ccerror.ResourceNotFoundError{}))
   152  				Expect(warnings).To(ConsistOf("this is a warning"))
   153  			})
   154  		})
   155  
   156  		Context("when the error occurs making a request to '/v3'", func() {
   157  			BeforeEach(func() {
   158  				rootResponse := fmt.Sprintf(`{
   159  					"links": {
   160  						"self": {
   161  							"href": "%s"
   162  						},
   163  						"cloud_controller_v2": {
   164  							"href": "%s/v2",
   165  							"meta": {
   166  								"version": "2.64.0"
   167  							}
   168  						},
   169  						"cloud_controller_v3": {
   170  							"href": "%s/v3",
   171  							"meta": {
   172  								"version": "3.0.0-alpha.5"
   173  							}
   174  						},
   175  						"uaa": {
   176  							"href": "https://uaa.bosh-lite.com"
   177  						},
   178  						"logging": {
   179  							"href": "wss://doppler.bosh-lite.com:443"
   180  						}
   181  					}
   182  				}
   183  				`, server.URL(), server.URL(), server.URL())
   184  
   185  				rootRespondWith = RespondWith(
   186  					http.StatusOK,
   187  					rootResponse,
   188  					http.Header{"X-Cf-Warnings": {"warning 1"}})
   189  				v3RespondWith = RespondWith(
   190  					http.StatusNotFound,
   191  					`{"errors": [{
   192  							"code": 10010,
   193  							"title": "CF-ResourceNotFound",
   194  							"detail": "Not found, lol"
   195  						}]
   196  					}`,
   197  					http.Header{"X-Cf-Warnings": {"this is a warning"}})
   198  			})
   199  
   200  			It("returns the same error", func() {
   201  				_, _, warnings, err := client.Info()
   202  				Expect(err).To(MatchError(ccerror.ResourceNotFoundError{Message: "Not found, lol"}))
   203  				Expect(warnings).To(ConsistOf("warning 1", "this is a warning"))
   204  			})
   205  		})
   206  	})
   207  })