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