github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/api/environmentvariablegroups/environment_variable_groups_test.go (about)

     1  package environmentvariablegroups_test
     2  
     3  import (
     4  	"net/http"
     5  	"time"
     6  
     7  	"code.cloudfoundry.org/cli/cf/api/environmentvariablegroups"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/models"
    10  	"code.cloudfoundry.org/cli/cf/net"
    11  
    12  	"code.cloudfoundry.org/cli/cf/terminal/terminalfakes"
    13  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    14  
    15  	"github.com/onsi/gomega/ghttp"
    16  
    17  	"code.cloudfoundry.org/cli/cf/trace/tracefakes"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  )
    21  
    22  var _ = Describe("CloudControllerRepository", func() {
    23  	var (
    24  		ccServer   *ghttp.Server
    25  		configRepo coreconfig.ReadWriter
    26  		repo       environmentvariablegroups.CloudControllerRepository
    27  	)
    28  
    29  	BeforeEach(func() {
    30  		ccServer = ghttp.NewServer()
    31  		configRepo = testconfig.NewRepositoryWithDefaults()
    32  		configRepo.SetAPIEndpoint(ccServer.URL())
    33  		gateway := net.NewCloudControllerGateway(configRepo, time.Now, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), "")
    34  		repo = environmentvariablegroups.NewCloudControllerRepository(configRepo, gateway)
    35  	})
    36  
    37  	AfterEach(func() {
    38  		ccServer.Close()
    39  	})
    40  
    41  	Describe("ListRunning", func() {
    42  		BeforeEach(func() {
    43  			ccServer.AppendHandlers(
    44  				ghttp.CombineHandlers(
    45  					ghttp.VerifyRequest("GET", "/v2/config/environment_variable_groups/running"),
    46  					ghttp.RespondWith(http.StatusOK, `{ "abc": 123, "do-re-mi": "fa-sol-la-ti" }`),
    47  				),
    48  			)
    49  		})
    50  
    51  		It("lists the environment variables in the running group", func() {
    52  			envVars, err := repo.ListRunning()
    53  			Expect(err).NotTo(HaveOccurred())
    54  
    55  			Expect(envVars).To(ConsistOf([]models.EnvironmentVariable{
    56  				{Name: "abc", Value: "123"},
    57  				{Name: "do-re-mi", Value: "fa-sol-la-ti"},
    58  			}))
    59  		})
    60  	})
    61  
    62  	Describe("ListStaging", func() {
    63  		BeforeEach(func() {
    64  			ccServer.AppendHandlers(
    65  				ghttp.CombineHandlers(
    66  					ghttp.VerifyRequest("GET", "/v2/config/environment_variable_groups/staging"),
    67  					ghttp.RespondWith(http.StatusOK, `{ "abc": 123, "do-re-mi": "fa-sol-la-ti" }`),
    68  				),
    69  			)
    70  		})
    71  
    72  		It("lists the environment variables in the staging group", func() {
    73  			envVars, err := repo.ListStaging()
    74  			Expect(err).NotTo(HaveOccurred())
    75  			Expect(envVars).To(ConsistOf([]models.EnvironmentVariable{
    76  				{Name: "abc", Value: "123"},
    77  				{Name: "do-re-mi", Value: "fa-sol-la-ti"},
    78  			}))
    79  		})
    80  	})
    81  
    82  	Describe("SetStaging", func() {
    83  		BeforeEach(func() {
    84  			ccServer.AppendHandlers(
    85  				ghttp.CombineHandlers(
    86  					ghttp.VerifyRequest("PUT", "/v2/config/environment_variable_groups/staging"),
    87  					ghttp.VerifyJSON(`{ "abc": "one-two-three", "def": 456 }`),
    88  					ghttp.RespondWith(http.StatusOK, nil),
    89  				),
    90  			)
    91  		})
    92  
    93  		It("sets the environment variables in the staging group", func() {
    94  			err := repo.SetStaging(`{"abc": "one-two-three", "def": 456}`)
    95  			Expect(err).NotTo(HaveOccurred())
    96  			Expect(ccServer.ReceivedRequests()).To(HaveLen(1))
    97  		})
    98  	})
    99  
   100  	Describe("SetRunning", func() {
   101  		BeforeEach(func() {
   102  			ccServer.AppendHandlers(
   103  				ghttp.CombineHandlers(
   104  					ghttp.VerifyRequest("PUT", "/v2/config/environment_variable_groups/running"),
   105  					ghttp.VerifyJSON(`{ "abc": "one-two-three", "def": 456 }`),
   106  					ghttp.RespondWith(http.StatusOK, nil),
   107  				),
   108  			)
   109  		})
   110  
   111  		It("sets the environment variables in the running group", func() {
   112  			err := repo.SetRunning(`{"abc": "one-two-three", "def": 456}`)
   113  			Expect(err).NotTo(HaveOccurred())
   114  			Expect(ccServer.ReceivedRequests()).To(HaveLen(1))
   115  		})
   116  	})
   117  })