github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/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  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
     9  	"code.cloudfoundry.org/cli/resources"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  	. "github.com/onsi/gomega/ghttp"
    13  )
    14  
    15  var _ = Describe("EnvironmentVariables", func() {
    16  	var client *Client
    17  
    18  	BeforeEach(func() {
    19  		client, _ = NewTestClient()
    20  	})
    21  
    22  	Describe("GetEnvironmentVariableGroup", func() {
    23  		var (
    24  			envVars    resources.EnvironmentVariables
    25  			warnings   Warnings
    26  			executeErr error
    27  		)
    28  
    29  		JustBeforeEach(func() {
    30  			envVars, warnings, executeErr = client.GetEnvironmentVariableGroup(constant.StagingEnvironmentVariableGroup)
    31  		})
    32  
    33  		When("the request errors", func() {
    34  			BeforeEach(func() {
    35  				server.AppendHandlers(
    36  					CombineHandlers(
    37  						VerifyRequest(http.MethodGet, "/v3/environment_variable_groups/staging"),
    38  						RespondWith(http.StatusTeapot, "{}", http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    39  					),
    40  				)
    41  			})
    42  
    43  			It("returns the error and warnings", func() {
    44  				Expect(executeErr).To(MatchError(ccerror.V3UnexpectedResponseError{ResponseCode: http.StatusTeapot}))
    45  				Expect(warnings).To(ConsistOf("this is a warning"))
    46  			})
    47  		})
    48  
    49  		When("the request succeeds", func() {
    50  			BeforeEach(func() {
    51  				responseBody := `{
    52  						"var": {
    53  							"DEBUG": "false",
    54  							"my-var": "my-val",
    55  							"number": 6,
    56  							"json": {"is": "fun"}
    57  						}
    58  					}`
    59  
    60  				server.AppendHandlers(
    61  					CombineHandlers(
    62  						VerifyRequest(http.MethodGet, "/v3/environment_variable_groups/staging"),
    63  						RespondWith(http.StatusOK, responseBody, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    64  					),
    65  				)
    66  			})
    67  
    68  			It("returns the error and warnings", func() {
    69  				Expect(executeErr).ToNot(HaveOccurred())
    70  				Expect(warnings).To(ConsistOf("this is a warning"))
    71  				Expect(envVars).To(Equal(resources.EnvironmentVariables{
    72  					"DEBUG":  {Value: "false", IsSet: true},
    73  					"my-var": {Value: "my-val", IsSet: true},
    74  					"number": {Value: "6", IsSet: true},
    75  					"json":   {Value: "{\"is\":\"fun\"}", IsSet: true},
    76  				}))
    77  			})
    78  		})
    79  	})
    80  
    81  	Describe("UpdateApplicationEnvironmentVariables", func() {
    82  		var (
    83  			envVars        resources.EnvironmentVariables
    84  			patchedEnvVars resources.EnvironmentVariables
    85  
    86  			warnings   Warnings
    87  			executeErr error
    88  		)
    89  
    90  		JustBeforeEach(func() {
    91  			patchedEnvVars, warnings, executeErr = client.UpdateApplicationEnvironmentVariables("some-app-guid", envVars)
    92  		})
    93  
    94  		When("the request errors", func() {
    95  			BeforeEach(func() {
    96  				envVars = resources.EnvironmentVariables{"my-var": {Value: "my-val", IsSet: true}}
    97  
    98  				expectedBody := map[string]interface{}{
    99  					"var": map[string]string{
   100  						"my-var": "my-val",
   101  					},
   102  				}
   103  
   104  				server.AppendHandlers(
   105  					CombineHandlers(
   106  						VerifyRequest(http.MethodPatch, "/v3/apps/some-app-guid/environment_variables"),
   107  						VerifyJSONRepresenting(expectedBody),
   108  						RespondWith(http.StatusTeapot, "{}", http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   109  					),
   110  				)
   111  			})
   112  
   113  			It("returns the error and warnings", func() {
   114  				Expect(executeErr).To(MatchError(ccerror.V3UnexpectedResponseError{ResponseCode: http.StatusTeapot}))
   115  				Expect(warnings).To(ConsistOf("this is a warning"))
   116  			})
   117  		})
   118  
   119  		When("the request succeeds", func() {
   120  			When("env variable is being set", func() {
   121  				BeforeEach(func() {
   122  					envVars = resources.EnvironmentVariables{
   123  						"my-var":    {Value: "my-val", IsSet: true},
   124  						"delete-me": {},
   125  					}
   126  
   127  					expectedBody := map[string]interface{}{
   128  						"var": map[string]interface{}{
   129  							"my-var":    "my-val",
   130  							"delete-me": nil,
   131  						},
   132  					}
   133  
   134  					responseBody := `{
   135  						"var": {
   136  							"DEBUG": "false",
   137  							"my-var": "my-val"
   138  						}
   139  					}`
   140  
   141  					server.AppendHandlers(
   142  						CombineHandlers(
   143  							VerifyRequest(http.MethodPatch, "/v3/apps/some-app-guid/environment_variables"),
   144  							VerifyJSONRepresenting(expectedBody),
   145  							RespondWith(http.StatusOK, responseBody, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   146  						),
   147  					)
   148  				})
   149  
   150  				It("returns the error and warnings", func() {
   151  					Expect(executeErr).ToNot(HaveOccurred())
   152  					Expect(warnings).To(ConsistOf("this is a warning"))
   153  					Expect(patchedEnvVars).To(Equal(resources.EnvironmentVariables{
   154  						"DEBUG":  {Value: "false", IsSet: true},
   155  						"my-var": {Value: "my-val", IsSet: true},
   156  					}))
   157  				})
   158  			})
   159  
   160  			When("env variable is being unset", func() {
   161  				BeforeEach(func() {
   162  					envVars = resources.EnvironmentVariables{
   163  						"my-var": {Value: "", IsSet: false},
   164  					}
   165  
   166  					expectedBody := map[string]interface{}{
   167  						"var": map[string]interface{}{
   168  							"my-var": nil,
   169  						},
   170  					}
   171  
   172  					responseBody := `{
   173  						"var": {
   174  							"DEBUG": "false"
   175  						}
   176  					}`
   177  
   178  					server.AppendHandlers(
   179  						CombineHandlers(
   180  							VerifyRequest(http.MethodPatch, "/v3/apps/some-app-guid/environment_variables"),
   181  							VerifyJSONRepresenting(expectedBody),
   182  							RespondWith(http.StatusOK, responseBody, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   183  						),
   184  					)
   185  				})
   186  
   187  				It("returns the patchedEnvVars and warnings", func() {
   188  					Expect(executeErr).ToNot(HaveOccurred())
   189  					Expect(warnings).To(ConsistOf("this is a warning"))
   190  					Expect(patchedEnvVars).To(Equal(resources.EnvironmentVariables{
   191  						"DEBUG": {Value: "false", IsSet: true},
   192  					}))
   193  				})
   194  			})
   195  		})
   196  	})
   197  
   198  	Describe("UpdateEnvironmentVariableGroup", func() {
   199  		var (
   200  			warnings       Warnings
   201  			executeErr     error
   202  			envVars        resources.EnvironmentVariables
   203  			patchedEnvVars resources.EnvironmentVariables
   204  		)
   205  
   206  		JustBeforeEach(func() {
   207  			patchedEnvVars, warnings, executeErr = client.UpdateEnvironmentVariableGroup(constant.StagingEnvironmentVariableGroup, envVars)
   208  		})
   209  
   210  		When("the request errors", func() {
   211  			BeforeEach(func() {
   212  				envVars = resources.EnvironmentVariables{"my-var": {Value: "my-val", IsSet: true}}
   213  
   214  				expectedBody := map[string]interface{}{
   215  					"var": map[string]string{
   216  						"my-var": "my-val",
   217  					},
   218  				}
   219  
   220  				server.AppendHandlers(
   221  					CombineHandlers(
   222  						VerifyRequest(http.MethodPatch, "/v3/environment_variable_groups/staging"),
   223  						VerifyJSONRepresenting(expectedBody),
   224  						RespondWith(http.StatusTeapot, "{}", http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   225  					),
   226  				)
   227  			})
   228  
   229  			It("returns the error and warnings", func() {
   230  				Expect(executeErr).To(MatchError(ccerror.V3UnexpectedResponseError{ResponseCode: http.StatusTeapot}))
   231  				Expect(warnings).To(ConsistOf("this is a warning"))
   232  			})
   233  		})
   234  
   235  		When("the request succeeds", func() {
   236  			When("env variable is being set", func() {
   237  				BeforeEach(func() {
   238  					envVars = resources.EnvironmentVariables{
   239  						"my-var":    {Value: "my-val", IsSet: true},
   240  						"delete-me": {},
   241  					}
   242  
   243  					expectedBody := map[string]interface{}{
   244  						"var": map[string]interface{}{
   245  							"my-var":    "my-val",
   246  							"delete-me": nil,
   247  						},
   248  					}
   249  
   250  					responseBody := `{
   251  						"var": {
   252  							"my-var": "my-val"
   253  						}
   254  					}`
   255  
   256  					server.AppendHandlers(
   257  						CombineHandlers(
   258  							VerifyRequest(http.MethodPatch, "/v3/environment_variable_groups/staging"),
   259  							VerifyJSONRepresenting(expectedBody),
   260  							RespondWith(http.StatusOK, responseBody, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   261  						),
   262  					)
   263  				})
   264  
   265  				It("returns the patchedEnvVars and warnings", func() {
   266  					Expect(executeErr).ToNot(HaveOccurred())
   267  					Expect(warnings).To(ConsistOf("this is a warning"))
   268  					Expect(patchedEnvVars).To(Equal(resources.EnvironmentVariables{
   269  						"my-var": {Value: "my-val", IsSet: true},
   270  					}))
   271  				})
   272  			})
   273  		})
   274  	})
   275  })