github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+incompatible/api/cloudcontroller/ccv3/environment_variables_test.go (about)

     1  package ccv3_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/resources"
     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("EnvironmentVariables", func() {
    15  	var client *Client
    16  
    17  	BeforeEach(func() {
    18  		client, _ = NewTestClient()
    19  	})
    20  
    21  	Describe("UpdateApplicationEnvironmentVariables", func() {
    22  		var (
    23  			envVars        resources.EnvironmentVariables
    24  			patchedEnvVars resources.EnvironmentVariables
    25  
    26  			warnings   Warnings
    27  			executeErr error
    28  		)
    29  
    30  		JustBeforeEach(func() {
    31  			patchedEnvVars, warnings, executeErr = client.UpdateApplicationEnvironmentVariables("some-app-guid", envVars)
    32  		})
    33  
    34  		When("the request errors", func() {
    35  			BeforeEach(func() {
    36  				envVars = resources.EnvironmentVariables{"my-var": {Value: "my-val", IsSet: true}}
    37  
    38  				expectedBody := map[string]interface{}{
    39  					"var": map[string]string{
    40  						"my-var": "my-val",
    41  					},
    42  				}
    43  
    44  				server.AppendHandlers(
    45  					CombineHandlers(
    46  						VerifyRequest(http.MethodPatch, "/v3/apps/some-app-guid/environment_variables"),
    47  						VerifyJSONRepresenting(expectedBody),
    48  						RespondWith(http.StatusTeapot, "{}", http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    49  					),
    50  				)
    51  			})
    52  
    53  			It("returns the error and warnings", func() {
    54  				Expect(executeErr).To(MatchError(ccerror.V3UnexpectedResponseError{ResponseCode: http.StatusTeapot}))
    55  				Expect(warnings).To(ConsistOf("this is a warning"))
    56  			})
    57  		})
    58  
    59  		When("the request succeeds", func() {
    60  			When("env variable is being set", func() {
    61  				BeforeEach(func() {
    62  					envVars = resources.EnvironmentVariables{
    63  						"my-var":    {Value: "my-val", IsSet: true},
    64  						"delete-me": {},
    65  					}
    66  
    67  					expectedBody := map[string]interface{}{
    68  						"var": map[string]interface{}{
    69  							"my-var":    "my-val",
    70  							"delete-me": nil,
    71  						},
    72  					}
    73  
    74  					responseBody := `{
    75  						"var": {
    76  							"DEBUG": "false",
    77  							"my-var": "my-val"
    78  						}
    79  					}`
    80  
    81  					server.AppendHandlers(
    82  						CombineHandlers(
    83  							VerifyRequest(http.MethodPatch, "/v3/apps/some-app-guid/environment_variables"),
    84  							VerifyJSONRepresenting(expectedBody),
    85  							RespondWith(http.StatusOK, responseBody, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    86  						),
    87  					)
    88  				})
    89  
    90  				It("returns the error and warnings", func() {
    91  					Expect(executeErr).ToNot(HaveOccurred())
    92  					Expect(warnings).To(ConsistOf("this is a warning"))
    93  					Expect(patchedEnvVars).To(Equal(resources.EnvironmentVariables{
    94  						"DEBUG":  {Value: "false", IsSet: true},
    95  						"my-var": {Value: "my-val", IsSet: true},
    96  					}))
    97  				})
    98  			})
    99  
   100  			When("env variable is being unset", func() {
   101  				BeforeEach(func() {
   102  					envVars = resources.EnvironmentVariables{
   103  						"my-var": {Value: "", IsSet: false},
   104  					}
   105  
   106  					expectedBody := map[string]interface{}{
   107  						"var": map[string]interface{}{
   108  							"my-var": nil,
   109  						},
   110  					}
   111  
   112  					responseBody := `{
   113  						"var": {
   114  							"DEBUG": "false"
   115  						}
   116  					}`
   117  
   118  					server.AppendHandlers(
   119  						CombineHandlers(
   120  							VerifyRequest(http.MethodPatch, "/v3/apps/some-app-guid/environment_variables"),
   121  							VerifyJSONRepresenting(expectedBody),
   122  							RespondWith(http.StatusOK, responseBody, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   123  						),
   124  					)
   125  				})
   126  
   127  				It("returns the error and warnings", func() {
   128  					Expect(executeErr).ToNot(HaveOccurred())
   129  					Expect(warnings).To(ConsistOf("this is a warning"))
   130  					Expect(patchedEnvVars).To(Equal(resources.EnvironmentVariables{
   131  						"DEBUG": {Value: "false", IsSet: true},
   132  					}))
   133  				})
   134  			})
   135  		})
   136  	})
   137  })