github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/api/cloudcontroller/ccv3/application_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("Application", func() {
    15  	var client *Client
    16  
    17  	BeforeEach(func() {
    18  		client, _ = NewTestClient()
    19  	})
    20  
    21  	Describe("UpdateAppFeature", func() {
    22  		var (
    23  			warnings   Warnings
    24  			executeErr error
    25  			appGUID    = "some-app-guid"
    26  			enabled    = false
    27  		)
    28  		Context("Updating SSH", func() {
    29  			JustBeforeEach(func() {
    30  				warnings, executeErr = client.UpdateAppFeature(appGUID, enabled, "ssh")
    31  			})
    32  
    33  			When("the app exists", func() {
    34  				BeforeEach(func() {
    35  					response1 := fmt.Sprintf(`{
    36     "name": "ssh",
    37     "description": "Enable SSHing into the app.",
    38     "enabled": false
    39  }`)
    40  
    41  					server.AppendHandlers(
    42  						CombineHandlers(
    43  							VerifyRequest(http.MethodPatch, fmt.Sprintf("/v3/apps/%s/features/ssh", appGUID)),
    44  							VerifyBody([]byte(fmt.Sprintf(`{"enabled":%t}`, enabled))),
    45  							RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    46  						),
    47  					)
    48  				})
    49  
    50  				It("returns all warnings", func() {
    51  					Expect(executeErr).NotTo(HaveOccurred())
    52  
    53  					Expect(warnings).To(ConsistOf("this is a warning"))
    54  				})
    55  			})
    56  
    57  			When("the cloud controller returns errors and warnings", func() {
    58  				BeforeEach(func() {
    59  					updateResponse := `{
    60  		 "errors": [
    61  		   {
    62  		     "code": 10008,
    63  		     "detail": "The request is semantically invalid: command presence",
    64  		     "title": "CF-UnprocessableEntity"
    65  		   },
    66  		   {
    67  		     "code": 10010,
    68  		     "detail": "Org not found",
    69  		     "title": "CF-ResourceNotFound"
    70  		   }
    71  		 ]
    72  		}`
    73  					server.AppendHandlers(
    74  						CombineHandlers(
    75  							VerifyRequest(http.MethodPatch, fmt.Sprintf("/v3/apps/%s/features/ssh", appGUID)),
    76  							RespondWith(http.StatusTeapot, updateResponse, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    77  						),
    78  					)
    79  				})
    80  
    81  				It("returns the error and all warnings", func() {
    82  					Expect(executeErr).To(MatchError(ccerror.MultiError{
    83  						ResponseCode: http.StatusTeapot,
    84  						Errors: []ccerror.V3Error{
    85  							{
    86  								Code:   10008,
    87  								Detail: "The request is semantically invalid: command presence",
    88  								Title:  "CF-UnprocessableEntity",
    89  							},
    90  							{
    91  								Code:   10010,
    92  								Detail: "Org not found",
    93  								Title:  "CF-ResourceNotFound",
    94  							},
    95  						},
    96  					}))
    97  					Expect(warnings).To(ConsistOf("this is a warning"))
    98  				})
    99  			})
   100  		})
   101  	})
   102  
   103  	Describe("GetAppFeature", func() {
   104  		var (
   105  			warnings           Warnings
   106  			executeErr         error
   107  			appGUID            = "some-app-guid"
   108  			applicationFeature ApplicationFeature
   109  		)
   110  
   111  		Context("Getting SSH", func() {
   112  			JustBeforeEach(func() {
   113  				applicationFeature, warnings, executeErr = client.GetAppFeature(appGUID, "ssh")
   114  			})
   115  
   116  			When("the app exists", func() {
   117  				BeforeEach(func() {
   118  					getResponse := fmt.Sprintf(`{
   119     "name": "ssh",
   120     "description": "Enable SSHing into the app.",
   121     "enabled": false
   122  }`)
   123  
   124  					server.AppendHandlers(
   125  						CombineHandlers(
   126  							VerifyRequest(http.MethodGet, fmt.Sprintf("/v3/apps/%s/features/ssh", appGUID)),
   127  							RespondWith(http.StatusOK, getResponse, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   128  						),
   129  					)
   130  				})
   131  
   132  				It("returns an app feature and all warnings", func() {
   133  					Expect(executeErr).NotTo(HaveOccurred())
   134  
   135  					Expect(warnings).To(ConsistOf("this is a warning"))
   136  					Expect(applicationFeature.Name).To(Equal("ssh"))
   137  					Expect(applicationFeature.Enabled).To(Equal(false))
   138  				})
   139  			})
   140  
   141  			When("the cloud controller returns errors and warnings", func() {
   142  				BeforeEach(func() {
   143  					response := `{
   144  		"errors": [
   145  		  {
   146  		    "code": 10008,
   147  		    "detail": "The request is semantically invalid: command presence",
   148  		    "title": "CF-UnprocessableEntity"
   149  		  },
   150  		  {
   151  		    "code": 10010,
   152  		    "detail": "Org not found",
   153  		    "title": "CF-ResourceNotFound"
   154  		  }
   155  		]
   156  		}`
   157  					server.AppendHandlers(
   158  						CombineHandlers(
   159  							VerifyRequest(http.MethodGet, fmt.Sprintf("/v3/apps/%s/features/ssh", appGUID)),
   160  							RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   161  						),
   162  					)
   163  				})
   164  
   165  				It("returns the error and all warnings", func() {
   166  					Expect(executeErr).To(MatchError(ccerror.MultiError{
   167  						ResponseCode: http.StatusTeapot,
   168  						Errors: []ccerror.V3Error{
   169  							{
   170  								Code:   10008,
   171  								Detail: "The request is semantically invalid: command presence",
   172  								Title:  "CF-UnprocessableEntity",
   173  							},
   174  							{
   175  								Code:   10010,
   176  								Detail: "Org not found",
   177  								Title:  "CF-ResourceNotFound",
   178  							},
   179  						},
   180  					}))
   181  					Expect(warnings).To(ConsistOf("this is a warning"))
   182  				})
   183  			})
   184  		})
   185  	})
   186  
   187  	Describe("GetSSHEnabled", func() {
   188  
   189  		var (
   190  			warnings   Warnings
   191  			executeErr error
   192  			appGUID    = "some-app-guid"
   193  			sshEnabled SSHEnabled
   194  		)
   195  
   196  		JustBeforeEach(func() {
   197  			sshEnabled, warnings, executeErr = client.GetSSHEnabled(appGUID)
   198  		})
   199  
   200  		When("no error occurs", func() {
   201  
   202  			BeforeEach(func() {
   203  				getResponse := fmt.Sprintf(`{
   204     "enabled": false,
   205     "reason": "Disabled for space some-space"
   206  }`)
   207  
   208  				server.AppendHandlers(
   209  					CombineHandlers(
   210  						VerifyRequest(http.MethodGet, fmt.Sprintf("/v3/apps/%s/ssh_enabled", appGUID)),
   211  						RespondWith(http.StatusOK, getResponse, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   212  					),
   213  				)
   214  			})
   215  
   216  			It("returns the SSHEnabled boolean", func() {
   217  
   218  				Expect(executeErr).NotTo(HaveOccurred())
   219  
   220  				Expect(warnings).To(ConsistOf("this is a warning"))
   221  				Expect(sshEnabled.Enabled).To(Equal(false))
   222  				Expect(sshEnabled.Reason).To(Equal("Disabled for space some-space"))
   223  			})
   224  		})
   225  
   226  		When("the cloud controller returns errors and warnings", func() {
   227  			BeforeEach(func() {
   228  				response := `{
   229  		"errors": [
   230  		  {
   231  		    "code": 10008,
   232  		    "detail": "The request is semantically invalid: command presence",
   233  		    "title": "CF-UnprocessableEntity"
   234  		  },
   235  		  {
   236  		    "code": 10010,
   237  		    "detail": "Org not found",
   238  		    "title": "CF-ResourceNotFound"
   239  		  }
   240  		]
   241  		}`
   242  				server.AppendHandlers(
   243  					CombineHandlers(
   244  						VerifyRequest(http.MethodGet, fmt.Sprintf("/v3/apps/%s/ssh_enabled", appGUID)),
   245  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   246  					),
   247  				)
   248  			})
   249  
   250  			It("returns the error and all warnings", func() {
   251  				Expect(executeErr).To(MatchError(ccerror.MultiError{
   252  					ResponseCode: http.StatusTeapot,
   253  					Errors: []ccerror.V3Error{
   254  						{
   255  							Code:   10008,
   256  							Detail: "The request is semantically invalid: command presence",
   257  							Title:  "CF-UnprocessableEntity",
   258  						},
   259  						{
   260  							Code:   10010,
   261  							Detail: "Org not found",
   262  							Title:  "CF-ResourceNotFound",
   263  						},
   264  					},
   265  				}))
   266  				Expect(warnings).To(ConsistOf("this is a warning"))
   267  			})
   268  		})
   269  	})
   270  })