github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+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  				}
    67  			}`, "SERVER_URL", server.URL(), -1)
    68  
    69  			rootRespondWith = RespondWith(
    70  				http.StatusOK,
    71  				rootResponse,
    72  				http.Header{"X-Cf-Warnings": {"warning 1"}})
    73  
    74  			v3Response := strings.Replace(`{
    75  				"links": {
    76  					"self": {
    77  						"href": "SERVER_URL/v3"
    78  					},
    79  					"apps": {
    80  						"href": "SERVER_URL/v3/apps"
    81  					},
    82  					"tasks": {
    83  						"href": "SERVER_URL/v3/tasks"
    84  					}
    85  				}
    86  			}`, "SERVER_URL", server.URL(), -1)
    87  
    88  			v3RespondWith = RespondWith(
    89  				http.StatusOK,
    90  				v3Response,
    91  				http.Header{"X-Cf-Warnings": {"warning 2"}})
    92  		})
    93  
    94  		It("returns the CC Information", func() {
    95  			apis, _, _, err := client.Info()
    96  			Expect(err).NotTo(HaveOccurred())
    97  			Expect(apis.UAA()).To(Equal("https://uaa.bosh-lite.com"))
    98  			Expect(apis.Logging()).To(Equal("wss://doppler.bosh-lite.com:443"))
    99  			Expect(apis.NetworkPolicyV1()).To(Equal(fmt.Sprintf("%s/networking/v1/external", server.URL())))
   100  		})
   101  
   102  		It("returns back the resource links", func() {
   103  			_, resources, _, err := client.Info()
   104  			Expect(err).NotTo(HaveOccurred())
   105  			Expect(resources[internal.AppsResource].HREF).To(Equal(server.URL() + "/v3/apps"))
   106  			Expect(resources[internal.TasksResource].HREF).To(Equal(server.URL() + "/v3/tasks"))
   107  		})
   108  
   109  		It("returns all warnings", func() {
   110  			_, _, warnings, err := client.Info()
   111  			Expect(err).NotTo(HaveOccurred())
   112  			Expect(warnings).To(ConsistOf("warning 1", "warning 2"))
   113  		})
   114  	})
   115  
   116  	Context("when the cloud controller encounters an error", func() {
   117  		Context("when the root response is invalid", func() {
   118  			BeforeEach(func() {
   119  				rootRespondWith = RespondWith(
   120  					http.StatusNotFound,
   121  					`i am google, bow down`,
   122  				)
   123  			})
   124  
   125  			It("returns an APINotFoundError", func() {
   126  				_, _, _, err := client.Info()
   127  				Expect(err).To(MatchError(ccerror.APINotFoundError{URL: server.URL()}))
   128  			})
   129  		})
   130  
   131  		Context("when the error occurs making a request to '/'", func() {
   132  			BeforeEach(func() {
   133  				rootRespondWith = RespondWith(
   134  					http.StatusNotFound,
   135  					`{"errors": [{}]}`,
   136  					http.Header{"X-Cf-Warnings": {"this is a warning"}})
   137  			})
   138  
   139  			It("returns the same error", func() {
   140  				_, _, warnings, err := client.Info()
   141  				Expect(err).To(MatchError(ccerror.ResourceNotFoundError{}))
   142  				Expect(warnings).To(ConsistOf("this is a warning"))
   143  			})
   144  		})
   145  
   146  		Context("when the error occurs making a request to '/v3'", func() {
   147  			BeforeEach(func() {
   148  				rootResponse := fmt.Sprintf(`{
   149  					"links": {
   150  						"self": {
   151  							"href": "%s"
   152  						},
   153  						"cloud_controller_v2": {
   154  							"href": "%s/v2",
   155  							"meta": {
   156  								"version": "2.64.0"
   157  							}
   158  						},
   159  						"cloud_controller_v3": {
   160  							"href": "%s/v3",
   161  							"meta": {
   162  								"version": "3.0.0-alpha.5"
   163  							}
   164  						},
   165  						"uaa": {
   166  							"href": "https://uaa.bosh-lite.com"
   167  						},
   168  						"logging": {
   169  							"href": "wss://doppler.bosh-lite.com:443"
   170  						}
   171  					}
   172  				}
   173  				`, server.URL(), server.URL(), server.URL())
   174  
   175  				rootRespondWith = RespondWith(
   176  					http.StatusOK,
   177  					rootResponse,
   178  					http.Header{"X-Cf-Warnings": {"warning 1"}})
   179  				v3RespondWith = RespondWith(
   180  					http.StatusNotFound,
   181  					`{"errors": [{
   182  							"code": 10010,
   183  							"title": "CF-ResourceNotFound",
   184  							"detail": "Not found, lol"
   185  						}]
   186  					}`,
   187  					http.Header{"X-Cf-Warnings": {"this is a warning"}})
   188  			})
   189  
   190  			It("returns the same error", func() {
   191  				_, _, warnings, err := client.Info()
   192  				Expect(err).To(MatchError(ccerror.ResourceNotFoundError{Message: "Not found, lol"}))
   193  				Expect(warnings).To(ConsistOf("warning 1", "this is a warning"))
   194  			})
   195  		})
   196  	})
   197  })