github.com/LukasHeimann/cloudfoundrycli@v7.1.0+incompatible/api/cloudcontroller/ccv3/space_feature_test.go (about)

     1  package ccv3_test
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     8  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/ghttp"
    12  )
    13  
    14  var _ = Describe("Space Features", func() {
    15  	var client *Client
    16  
    17  	BeforeEach(func() {
    18  		client, _ = NewTestClient()
    19  	})
    20  
    21  	Describe("GetSpaceFeature", func() {
    22  		var (
    23  			warnings   Warnings
    24  			executeErr error
    25  			spaceGUID  = "some-space-guid"
    26  			enabled    bool
    27  		)
    28  
    29  		Context("Getting the SSH feature", func() {
    30  			JustBeforeEach(func() {
    31  				enabled, warnings, executeErr = client.GetSpaceFeature(spaceGUID, "ssh")
    32  			})
    33  
    34  			When("there are no API errors", func() {
    35  				BeforeEach(func() {
    36  					response := fmt.Sprintf(`{
    37     "name": "ssh",
    38     "description": "Enable SSHing into apps in the space.",
    39     "enabled": true
    40  }`)
    41  
    42  					server.AppendHandlers(
    43  						CombineHandlers(
    44  							VerifyRequest(http.MethodGet, fmt.Sprintf("/v3/spaces/%s/features/ssh", spaceGUID)),
    45  							RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    46  						),
    47  					)
    48  				})
    49  
    50  				It("gets the SSH feature value", func() {
    51  					Expect(executeErr).NotTo(HaveOccurred())
    52  					Expect(warnings).To(ConsistOf("this is a warning"))
    53  					Expect(enabled).To(BeTrue())
    54  				})
    55  
    56  			})
    57  
    58  			When("the cloud controller returns errors and warnings", func() {
    59  				BeforeEach(func() {
    60  					response := `{
    61  		 "errors": [
    62  		   {
    63  		     "code": 10010,
    64  		     "detail": "Org not found",
    65  		     "title": "CF-ResourceNotFound"
    66  		   }
    67  		 ]
    68  		}`
    69  					server.AppendHandlers(
    70  						CombineHandlers(
    71  							VerifyRequest(http.MethodGet, fmt.Sprintf("/v3/spaces/%s/features/ssh", spaceGUID)),
    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  					Expect(executeErr).To(MatchError(ccerror.V3UnexpectedResponseError{
    79  						ResponseCode: http.StatusTeapot,
    80  						V3ErrorResponse: ccerror.V3ErrorResponse{
    81  							Errors: []ccerror.V3Error{
    82  								{
    83  									Code:   10010,
    84  									Detail: "Org not found",
    85  									Title:  "CF-ResourceNotFound",
    86  								},
    87  							},
    88  						},
    89  					}))
    90  					Expect(warnings).To(ConsistOf("this is a warning"))
    91  				})
    92  			})
    93  		})
    94  	})
    95  
    96  	Describe("UpdateSpaceFeature", func() {
    97  		var (
    98  			warnings   Warnings
    99  			executeErr error
   100  			spaceGUID  = "some-space-guid"
   101  			enabled    = false
   102  		)
   103  
   104  		Context("Updating SSH", func() {
   105  			JustBeforeEach(func() {
   106  				warnings, executeErr = client.UpdateSpaceFeature(spaceGUID, enabled, "ssh")
   107  			})
   108  
   109  			When("the space exists", func() {
   110  				BeforeEach(func() {
   111  					response := fmt.Sprintf(`{
   112     "name": "ssh",
   113     "description": "Enable SSHing into apps in the space.",
   114     "enabled": false
   115  }`)
   116  
   117  					server.AppendHandlers(
   118  						CombineHandlers(
   119  							VerifyRequest(http.MethodPatch, fmt.Sprintf("/v3/spaces/%s/features/ssh", spaceGUID)),
   120  							VerifyBody([]byte(fmt.Sprintf(`{"enabled":%t}`, enabled))),
   121  							RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   122  						),
   123  					)
   124  				})
   125  
   126  				It("updates the space feature to the desired value", func() {
   127  					Expect(executeErr).NotTo(HaveOccurred())
   128  					Expect(warnings).To(ConsistOf("this is a warning"))
   129  				})
   130  			})
   131  
   132  			When("the cloud controller returns errors and warnings", func() {
   133  				BeforeEach(func() {
   134  					updateResponse := `{
   135  		 "errors": [
   136  		   {
   137  		     "code": 10010,
   138  		     "detail": "Org not found",
   139  		     "title": "CF-ResourceNotFound"
   140  		   }
   141  		 ]
   142  		}`
   143  					server.AppendHandlers(
   144  						CombineHandlers(
   145  							VerifyRequest(http.MethodPatch, fmt.Sprintf("/v3/spaces/%s/features/ssh", spaceGUID)),
   146  							RespondWith(http.StatusTeapot, updateResponse, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   147  						),
   148  					)
   149  				})
   150  
   151  				It("returns the error and all warnings", func() {
   152  					Expect(executeErr).To(MatchError(ccerror.V3UnexpectedResponseError{
   153  						ResponseCode: http.StatusTeapot,
   154  						V3ErrorResponse: ccerror.V3ErrorResponse{
   155  							Errors: []ccerror.V3Error{
   156  								{
   157  									Code:   10010,
   158  									Detail: "Org not found",
   159  									Title:  "CF-ResourceNotFound",
   160  								},
   161  							},
   162  						},
   163  					}))
   164  					Expect(warnings).To(ConsistOf("this is a warning"))
   165  				})
   166  			})
   167  		})
   168  	})
   169  })