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