github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/api/cloudcontroller/ccv2/resource_test.go (about)

     1  package ccv2_test
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     7  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  	. "github.com/onsi/gomega/ghttp"
    11  )
    12  
    13  var _ = Describe("Resource", func() {
    14  	var client *Client
    15  
    16  	BeforeEach(func() {
    17  		client = NewTestClient()
    18  	})
    19  
    20  	Describe("ResourceMatch", func() {
    21  		Context("when route binding is successful", func() {
    22  			BeforeEach(func() {
    23  				responseBody := `[
    24  						{
    25  							"fn":   "some-file-1",
    26  							"mode": "744",
    27  							"sha1": "some-sha-1",
    28  							"size": 1
    29  						},
    30  						{
    31  							"fn":   "some-file-3",
    32  							"mode": "744",
    33  							"sha1": "some-sha-3",
    34  							"size": 3
    35  						}
    36  					]`
    37  
    38  				// Note: ordering matters with VerifyBody
    39  				expectedRequestBody := []map[string]interface{}{
    40  					map[string]interface{}{
    41  						"fn":   "some-file-1",
    42  						"mode": "744",
    43  						"sha1": "some-sha-1",
    44  						"size": 1,
    45  					},
    46  					map[string]interface{}{
    47  						"fn":   "some-file-2",
    48  						"mode": "744",
    49  						"sha1": "some-sha-2",
    50  						"size": 2,
    51  					},
    52  					map[string]interface{}{
    53  						"fn":   "some-file-3",
    54  						"mode": "744",
    55  						"sha1": "some-sha-3",
    56  						"size": 3,
    57  					},
    58  				}
    59  
    60  				server.AppendHandlers(
    61  					CombineHandlers(
    62  						VerifyRequest(http.MethodPut, "/v2/resource_match"),
    63  						VerifyJSONRepresenting(expectedRequestBody),
    64  						RespondWith(http.StatusCreated, responseBody, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    65  					),
    66  				)
    67  			})
    68  
    69  			It("returns the route and warnings", func() {
    70  				resourcesToMatch := []Resource{
    71  					{
    72  						Filename: "some-file-1",
    73  						Mode:     0744,
    74  						SHA1:     "some-sha-1",
    75  						Size:     1,
    76  					},
    77  					{
    78  						Filename: "some-file-2",
    79  						Mode:     0744,
    80  						SHA1:     "some-sha-2",
    81  						Size:     2,
    82  					},
    83  					{
    84  						Filename: "some-file-3",
    85  						Mode:     0744,
    86  						SHA1:     "some-sha-3",
    87  						Size:     3,
    88  					},
    89  				}
    90  				matchedResources, warnings, err := client.ResourceMatch(resourcesToMatch)
    91  
    92  				Expect(err).ToNot(HaveOccurred())
    93  				Expect(warnings).To(ConsistOf("this is a warning"))
    94  
    95  				Expect(matchedResources).To(ConsistOf(
    96  					Resource{
    97  						Filename: "some-file-1",
    98  						Mode:     0744,
    99  						SHA1:     "some-sha-1",
   100  						Size:     1,
   101  					},
   102  					Resource{
   103  						Filename: "some-file-3",
   104  						Mode:     0744,
   105  						SHA1:     "some-sha-3",
   106  						Size:     3,
   107  					}))
   108  			})
   109  		})
   110  
   111  		Context("when the cc returns an error", func() {
   112  			BeforeEach(func() {
   113  				response := `{
   114  					"code": 10001,
   115  					"description": "Some Error",
   116  					"error_code": "CF-SomeError"
   117  				}`
   118  				server.AppendHandlers(
   119  					CombineHandlers(
   120  						VerifyRequest(http.MethodPut, "/v2/resource_match"),
   121  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   122  					),
   123  				)
   124  			})
   125  
   126  			It("returns an error", func() {
   127  				_, warnings, err := client.ResourceMatch(nil)
   128  				Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{
   129  					ResponseCode: http.StatusTeapot,
   130  					V2ErrorResponse: ccerror.V2ErrorResponse{
   131  						Code:        10001,
   132  						Description: "Some Error",
   133  						ErrorCode:   "CF-SomeError",
   134  					},
   135  				}))
   136  				Expect(warnings).To(ConsistOf("this is a warning"))
   137  			})
   138  		})
   139  	})
   140  })