github.com/LukasHeimann/cloudfoundrycli@v7.1.0+incompatible/api/cloudcontroller/ccv3/environment_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("Environment", func() {
    14  	var client *Client
    15  
    16  	BeforeEach(func() {
    17  		client, _ = NewTestClient()
    18  	})
    19  
    20  	Describe("GetApplicationEnvironment", func() {
    21  		var (
    22  			fetchedEnvVars Environment
    23  			warnings       Warnings
    24  			executeErr     error
    25  		)
    26  
    27  		JustBeforeEach(func() {
    28  			fetchedEnvVars, warnings, executeErr = client.GetApplicationEnvironment("some-app-guid")
    29  		})
    30  
    31  		When("the request errors", func() {
    32  			BeforeEach(func() {
    33  				server.AppendHandlers(
    34  					CombineHandlers(
    35  						VerifyRequest(http.MethodGet, "/v3/apps/some-app-guid/env"),
    36  						RespondWith(http.StatusTeapot, "{}", http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    37  					),
    38  				)
    39  			})
    40  
    41  			It("returns the error and warnings", func() {
    42  				Expect(executeErr).To(MatchError(ccerror.V3UnexpectedResponseError{ResponseCode: http.StatusTeapot}))
    43  				Expect(warnings).To(ConsistOf("this is a warning"))
    44  			})
    45  		})
    46  
    47  		When("the request succeeds", func() {
    48  			BeforeEach(func() {
    49  				responseBody := `{
    50  					"staging_env_json": {
    51  						"staging-name": "staging-value"
    52  					},
    53  					"running_env_json": {
    54  						"running-name": "running-value"
    55  					},
    56  					"environment_variables": {
    57  						"user-name": "user-value"
    58  					},
    59  					"system_env_json": {
    60  						"VCAP_SERVICES": {
    61  							"mysql": [
    62  								{
    63  									"name": "db-for-my-app"
    64  								}
    65  							]
    66  						}
    67  					},
    68  					"application_env_json": {
    69  						"VCAP_APPLICATION": {
    70  							"application_name": "my_app"
    71  						}
    72  					}
    73  				}`
    74  				server.AppendHandlers(
    75  					CombineHandlers(
    76  						VerifyRequest(http.MethodGet, "/v3/apps/some-app-guid/env"),
    77  						RespondWith(http.StatusOK, responseBody, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    78  					),
    79  				)
    80  			})
    81  
    82  			It("returns env variable groups", func() {
    83  				Expect(executeErr).ToNot(HaveOccurred())
    84  				Expect(warnings).To(ConsistOf("this is a warning"))
    85  				Expect(fetchedEnvVars).To(Equal(Environment{
    86  					System: map[string]interface{}{
    87  						"VCAP_SERVICES": map[string]interface{}{
    88  							"mysql": []interface{}{map[string]interface{}{"name": "db-for-my-app"}},
    89  						},
    90  					},
    91  					Application: map[string]interface{}{
    92  						"VCAP_APPLICATION": map[string]interface{}{
    93  							"application_name": "my_app",
    94  						},
    95  					},
    96  					EnvironmentVariables: map[string]interface{}{
    97  						"user-name": "user-value",
    98  					},
    99  					Running: map[string]interface{}{
   100  						"running-name": "running-value",
   101  					},
   102  					Staging: map[string]interface{}{
   103  						"staging-name": "staging-value",
   104  					},
   105  				}))
   106  			})
   107  		})
   108  	})
   109  })