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

     1  package ccv3_test
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     7  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  	. "github.com/onsi/gomega/ghttp"
    11  )
    12  
    13  var _ = Describe("RelationshipList", func() {
    14  	var (
    15  		client *Client
    16  	)
    17  
    18  	BeforeEach(func() {
    19  		client = NewTestClient()
    20  	})
    21  
    22  	Describe("EntitleIsolationSegmentToOrganizations", func() {
    23  		Context("when the delete is successful", func() {
    24  			BeforeEach(func() {
    25  				response := `{
    26  					"data": [
    27  						{
    28  							"guid": "some-relationship-guid-1"
    29  						},
    30  						{
    31  							"guid": "some-relationship-guid-2"
    32  						}
    33  					]
    34  				}`
    35  
    36  				requestBody := map[string][]map[string]string{
    37  					"data": {{"guid": "org-guid-1"}, {"guid": "org-guid-2"}},
    38  				}
    39  				server.AppendHandlers(
    40  					CombineHandlers(
    41  						VerifyRequest(http.MethodPost, "/v3/isolation_segments/some-iso-guid/relationships/organizations"),
    42  						VerifyJSONRepresenting(requestBody),
    43  						RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    44  					),
    45  				)
    46  			})
    47  
    48  			It("returns all relationships and warnings", func() {
    49  				relationships, warnings, err := client.EntitleIsolationSegmentToOrganizations("some-iso-guid", []string{"org-guid-1", "org-guid-2"})
    50  				Expect(err).NotTo(HaveOccurred())
    51  				Expect(warnings).To(ConsistOf("this is a warning"))
    52  				Expect(relationships).To(Equal(RelationshipList{
    53  					GUIDs: []string{"some-relationship-guid-1", "some-relationship-guid-2"},
    54  				}))
    55  			})
    56  		})
    57  
    58  		Context("when the cloud controller returns errors and warnings", func() {
    59  			BeforeEach(func() {
    60  				response := `{
    61  					"errors": [
    62  						{
    63  							"code": 10008,
    64  							"detail": "The request is semantically invalid: command presence",
    65  							"title": "CF-UnprocessableEntity"
    66  						}
    67  					]
    68  				}`
    69  				server.AppendHandlers(
    70  					CombineHandlers(
    71  						VerifyRequest(http.MethodPost, "/v3/isolation_segments/some-iso-guid/relationships/organizations"),
    72  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    73  					),
    74  				)
    75  			})
    76  
    77  			It("returns the error and all warnings", func() {
    78  				_, warnings, err := client.EntitleIsolationSegmentToOrganizations("some-iso-guid", []string{"org-guid-1", "org-guid-2"})
    79  				Expect(err).To(MatchError(ccerror.V3UnexpectedResponseError{
    80  					ResponseCode: http.StatusTeapot,
    81  					V3ErrorResponse: ccerror.V3ErrorResponse{
    82  						Errors: []ccerror.V3Error{
    83  							{
    84  								Code:   10008,
    85  								Detail: "The request is semantically invalid: command presence",
    86  								Title:  "CF-UnprocessableEntity",
    87  							},
    88  						},
    89  					},
    90  				}))
    91  				Expect(warnings).To(ConsistOf("this is a warning"))
    92  			})
    93  		})
    94  	})
    95  
    96  	Describe("ShareServiceInstanceToSpaces", func() {
    97  		var (
    98  			serviceInstanceGUID string
    99  			spaceGUIDs          []string
   100  
   101  			relationshipList RelationshipList
   102  			warnings         Warnings
   103  			executeErr       error
   104  		)
   105  
   106  		BeforeEach(func() {
   107  			serviceInstanceGUID = "some-service-instance-guid"
   108  			spaceGUIDs = []string{"some-space-guid", "some-other-space-guid"}
   109  		})
   110  
   111  		JustBeforeEach(func() {
   112  			relationshipList, warnings, executeErr = client.ShareServiceInstanceToSpaces(serviceInstanceGUID, spaceGUIDs)
   113  		})
   114  
   115  		Context("when no errors are encountered", func() {
   116  			BeforeEach(func() {
   117  				response := `{
   118  					"data": [
   119  						{
   120  							"guid": "some-space-guid"
   121  						},
   122  						{
   123  							"guid": "some-other-space-guid"
   124  						}
   125  					]
   126  				}`
   127  
   128  				requestBody := map[string][]map[string]string{
   129  					"data": {{"guid": "some-space-guid"}, {"guid": "some-other-space-guid"}},
   130  				}
   131  
   132  				server.AppendHandlers(
   133  					CombineHandlers(
   134  						VerifyRequest(http.MethodPost, "/v3/service_instances/some-service-instance-guid/relationships/shared_spaces"),
   135  						VerifyJSONRepresenting(requestBody),
   136  						RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   137  					),
   138  				)
   139  			})
   140  
   141  			It("returns all relationships and warnings", func() {
   142  				Expect(executeErr).NotTo(HaveOccurred())
   143  				Expect(warnings).To(ConsistOf("this is a warning"))
   144  				Expect(relationshipList).To(Equal(RelationshipList{
   145  					GUIDs: []string{"some-space-guid", "some-other-space-guid"},
   146  				}))
   147  			})
   148  		})
   149  
   150  		Context("when the cloud controller returns errors and warnings", func() {
   151  			BeforeEach(func() {
   152  				response := `{
   153  					"errors": [
   154  						{
   155  							"code": 10008,
   156  							"detail": "The request is semantically invalid: command presence",
   157  							"title": "CF-UnprocessableEntity"
   158  						}
   159  					]
   160  				}`
   161  				server.AppendHandlers(
   162  					CombineHandlers(
   163  						VerifyRequest(http.MethodPost, "/v3/service_instances/some-service-instance-guid/relationships/shared_spaces"),
   164  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   165  					),
   166  				)
   167  			})
   168  
   169  			It("returns the error and all warnings", func() {
   170  				Expect(executeErr).To(MatchError(ccerror.V3UnexpectedResponseError{
   171  					ResponseCode: http.StatusTeapot,
   172  					V3ErrorResponse: ccerror.V3ErrorResponse{
   173  						Errors: []ccerror.V3Error{
   174  							{
   175  								Code:   10008,
   176  								Detail: "The request is semantically invalid: command presence",
   177  								Title:  "CF-UnprocessableEntity",
   178  							},
   179  						},
   180  					},
   181  				}))
   182  				Expect(warnings).To(ConsistOf("this is a warning"))
   183  			})
   184  		})
   185  	})
   186  })