github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/api/environment_variable_groups/environment_variable_groups_test.go (about) 1 package environment_variable_groups_test 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "time" 7 8 testapi "github.com/cloudfoundry/cli/cf/api/fakes" 9 "github.com/cloudfoundry/cli/cf/configuration/core_config" 10 "github.com/cloudfoundry/cli/cf/models" 11 "github.com/cloudfoundry/cli/cf/net" 12 testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" 13 testnet "github.com/cloudfoundry/cli/testhelpers/net" 14 testterm "github.com/cloudfoundry/cli/testhelpers/terminal" 15 16 . "github.com/cloudfoundry/cli/cf/api/environment_variable_groups" 17 . "github.com/cloudfoundry/cli/testhelpers/matchers" 18 . "github.com/onsi/ginkgo" 19 . "github.com/onsi/gomega" 20 ) 21 22 var _ = Describe("CloudControllerEnvironmentVariableGroupsRepository", func() { 23 var ( 24 testServer *httptest.Server 25 testHandler *testnet.TestHandler 26 configRepo core_config.ReadWriter 27 repo CloudControllerEnvironmentVariableGroupsRepository 28 ) 29 30 BeforeEach(func() { 31 configRepo = testconfig.NewRepositoryWithDefaults() 32 gateway := net.NewCloudControllerGateway(configRepo, time.Now, &testterm.FakeUI{}) 33 repo = NewCloudControllerEnvironmentVariableGroupsRepository(configRepo, gateway) 34 }) 35 36 AfterEach(func() { 37 testServer.Close() 38 }) 39 40 setupTestServer := func(reqs ...testnet.TestRequest) { 41 testServer, testHandler = testnet.NewServer(reqs) 42 configRepo.SetApiEndpoint(testServer.URL) 43 } 44 45 Describe("ListRunning", func() { 46 BeforeEach(func() { 47 setupTestServer(listRunningRequest) 48 }) 49 50 It("lists the environment variables in the running group", func() { 51 envVars, err := repo.ListRunning() 52 53 Expect(err).NotTo(HaveOccurred()) 54 Expect(testHandler).To(HaveAllRequestsCalled()) 55 56 Expect(envVars).To(ConsistOf([]models.EnvironmentVariable{ 57 models.EnvironmentVariable{Name: "abc", Value: "123"}, 58 models.EnvironmentVariable{Name: "do-re-mi", Value: "fa-sol-la-ti"}, 59 })) 60 }) 61 }) 62 63 Describe("ListStaging", func() { 64 BeforeEach(func() { 65 setupTestServer(listStagingRequest) 66 }) 67 68 It("lists the environment variables in the staging group", func() { 69 envVars, err := repo.ListStaging() 70 71 Expect(err).NotTo(HaveOccurred()) 72 Expect(testHandler).To(HaveAllRequestsCalled()) 73 Expect(envVars).To(ConsistOf([]models.EnvironmentVariable{ 74 models.EnvironmentVariable{Name: "abc", Value: "123"}, 75 models.EnvironmentVariable{Name: "do-re-mi", Value: "fa-sol-la-ti"}, 76 })) 77 }) 78 }) 79 80 Describe("SetStaging", func() { 81 BeforeEach(func() { 82 setupTestServer(setStagingRequest) 83 }) 84 85 It("sets the environment variables in the staging group", func() { 86 err := repo.SetStaging(`{"abc": "one-two-three", "def": 456}`) 87 88 Expect(err).NotTo(HaveOccurred()) 89 Expect(testHandler).To(HaveAllRequestsCalled()) 90 }) 91 }) 92 93 Describe("SetRunning", func() { 94 BeforeEach(func() { 95 setupTestServer(setRunningRequest) 96 }) 97 98 It("sets the environment variables in the running group", func() { 99 err := repo.SetRunning(`{"abc": "one-two-three", "def": 456}`) 100 101 Expect(err).NotTo(HaveOccurred()) 102 Expect(testHandler).To(HaveAllRequestsCalled()) 103 }) 104 }) 105 }) 106 107 var listRunningRequest = testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 108 Method: "GET", 109 Path: "/v2/config/environment_variable_groups/running", 110 Response: testnet.TestResponse{ 111 Status: http.StatusOK, 112 Body: `{ 113 "abc": 123, 114 "do-re-mi": "fa-sol-la-ti" 115 }`, 116 }, 117 }) 118 119 var listStagingRequest = testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 120 Method: "GET", 121 Path: "/v2/config/environment_variable_groups/staging", 122 Response: testnet.TestResponse{ 123 Status: http.StatusOK, 124 Body: `{ 125 "abc": 123, 126 "do-re-mi": "fa-sol-la-ti" 127 }`, 128 }, 129 }) 130 131 var setStagingRequest = testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 132 Method: "PUT", 133 Path: "/v2/config/environment_variable_groups/staging", 134 Matcher: testnet.RequestBodyMatcher(`{ 135 "abc": "one-two-three", 136 "def": 456 137 }`), 138 }) 139 140 var setRunningRequest = testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 141 Method: "PUT", 142 Path: "/v2/config/environment_variable_groups/running", 143 Matcher: testnet.RequestBodyMatcher(`{ 144 "abc": "one-two-three", 145 "def": 456 146 }`), 147 })