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