github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/api/feature_flags/feature_flags_test.go (about)

     1  package feature_flags_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/net"
    11  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    12  	testnet "github.com/cloudfoundry/cli/testhelpers/net"
    13  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    14  
    15  	. "github.com/cloudfoundry/cli/cf/api/feature_flags"
    16  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    17  	. "github.com/onsi/ginkgo"
    18  	. "github.com/onsi/gomega"
    19  )
    20  
    21  var _ = Describe("Feature Flags Repository", func() {
    22  	var (
    23  		testServer  *httptest.Server
    24  		testHandler *testnet.TestHandler
    25  		configRepo  core_config.ReadWriter
    26  		repo        CloudControllerFeatureFlagRepository
    27  	)
    28  
    29  	BeforeEach(func() {
    30  		configRepo = testconfig.NewRepositoryWithDefaults()
    31  		gateway := net.NewCloudControllerGateway(configRepo, time.Now, &testterm.FakeUI{})
    32  		repo = NewCloudControllerFeatureFlagRepository(configRepo, gateway)
    33  	})
    34  
    35  	AfterEach(func() {
    36  		testServer.Close()
    37  	})
    38  
    39  	setupTestServer := func(reqs ...testnet.TestRequest) {
    40  		testServer, testHandler = testnet.NewServer(reqs)
    41  		configRepo.SetApiEndpoint(testServer.URL)
    42  	}
    43  
    44  	Describe(".List", func() {
    45  		BeforeEach(func() {
    46  			setupTestServer(featureFlagsGetAllRequest)
    47  		})
    48  
    49  		It("returns all of the feature flags", func() {
    50  			featureFlagModels, err := repo.List()
    51  
    52  			Expect(err).NotTo(HaveOccurred())
    53  			Expect(testHandler).To(HaveAllRequestsCalled())
    54  			Expect(len(featureFlagModels)).To(Equal(5))
    55  			Expect(featureFlagModels[0].Name).To(Equal("user_org_creation"))
    56  			Expect(featureFlagModels[0].Enabled).To(BeFalse())
    57  			Expect(featureFlagModels[1].Name).To(Equal("private_domain_creation"))
    58  			Expect(featureFlagModels[1].Enabled).To(BeFalse())
    59  			Expect(featureFlagModels[2].Name).To(Equal("app_bits_upload"))
    60  			Expect(featureFlagModels[2].Enabled).To(BeTrue())
    61  			Expect(featureFlagModels[3].Name).To(Equal("app_scaling"))
    62  			Expect(featureFlagModels[3].Enabled).To(BeTrue())
    63  			Expect(featureFlagModels[4].Name).To(Equal("route_creation"))
    64  			Expect(featureFlagModels[4].Enabled).To(BeTrue())
    65  		})
    66  	})
    67  
    68  	Describe(".FindByName", func() {
    69  		BeforeEach(func() {
    70  			setupTestServer(featureFlagRequest)
    71  		})
    72  
    73  		It("returns the requested", func() {
    74  			featureFlagModel, err := repo.FindByName("user_org_creation")
    75  
    76  			Expect(err).NotTo(HaveOccurred())
    77  			Expect(testHandler).To(HaveAllRequestsCalled())
    78  
    79  			Expect(featureFlagModel.Name).To(Equal("user_org_creation"))
    80  			Expect(featureFlagModel.Enabled).To(BeFalse())
    81  		})
    82  	})
    83  
    84  	Describe(".Update", func() {
    85  		BeforeEach(func() {
    86  			setupTestServer(featureFlagsUpdateRequest)
    87  		})
    88  
    89  		It("updates the given feature flag with the specified value", func() {
    90  			err := repo.Update("app_scaling", true)
    91  			Expect(err).ToNot(HaveOccurred())
    92  		})
    93  
    94  		Context("when given a non-existent feature flag", func() {
    95  			BeforeEach(func() {
    96  				setupTestServer(featureFlagsUpdateErrorRequest)
    97  			})
    98  
    99  			It("returns an error", func() {
   100  				err := repo.Update("i_dont_exist", true)
   101  				Expect(err).To(HaveOccurred())
   102  			})
   103  		})
   104  	})
   105  })
   106  
   107  var featureFlagsGetAllRequest = testapi.NewCloudControllerTestRequest(testnet.TestRequest{
   108  	Method: "GET",
   109  	Path:   "/v2/config/feature_flags",
   110  	Response: testnet.TestResponse{
   111  		Status: http.StatusOK,
   112  		Body: `[
   113      { 
   114        "name": "user_org_creation",
   115        "enabled": false,
   116        "error_message": null,
   117        "url": "/v2/config/feature_flags/user_org_creation"
   118      },
   119      { 
   120        "name": "private_domain_creation",
   121        "enabled": false,
   122        "error_message": "foobar",
   123        "url": "/v2/config/feature_flags/private_domain_creation"
   124      },
   125      { 
   126        "name": "app_bits_upload",
   127        "enabled": true,
   128        "error_message": null,
   129        "url": "/v2/config/feature_flags/app_bits_upload"
   130      },
   131      { 
   132        "name": "app_scaling",
   133        "enabled": true,
   134        "error_message": null,
   135        "url": "/v2/config/feature_flags/app_scaling"
   136      },
   137      { 
   138        "name": "route_creation",
   139        "enabled": true,
   140        "error_message": null,
   141        "url": "/v2/config/feature_flags/route_creation"
   142      }
   143  ]`,
   144  	},
   145  })
   146  
   147  var featureFlagRequest = testapi.NewCloudControllerTestRequest(testnet.TestRequest{
   148  	Method: "GET",
   149  	Path:   "/v2/config/feature_flags/user_org_creation",
   150  	Response: testnet.TestResponse{
   151  		Status: http.StatusOK,
   152  		Body: `{
   153    "name": "user_org_creation",
   154    "enabled": false,
   155    "error_message": null,
   156    "url": "/v2/config/feature_flags/user_org_creation"
   157  }`,
   158  	},
   159  })
   160  
   161  var featureFlagsUpdateErrorRequest = testapi.NewCloudControllerTestRequest(testnet.TestRequest{
   162  	Method: "PUT",
   163  	Path:   "/v2/config/feature_flags/i_dont_exist",
   164  	Response: testnet.TestResponse{
   165  		Status: http.StatusNotFound,
   166  		Body: `{
   167           "code": 330000,
   168           "description": "The feature flag could not be found: i_dont_exist",
   169           "error_code": "CF-FeatureFlagNotFound"
   170           }`,
   171  	},
   172  })
   173  
   174  var featureFlagsUpdateRequest = testapi.NewCloudControllerTestRequest(testnet.TestRequest{
   175  	Method: "PUT",
   176  	Path:   "/v2/config/feature_flags/app_scaling",
   177  	Response: testnet.TestResponse{
   178  		Status: http.StatusOK,
   179  		Body: `{ 
   180        "name": "app_scaling",
   181        "enabled": true,
   182        "error_message": null,
   183        "url": "/v2/config/feature_flags/app_scaling"
   184      }`,
   185  	},
   186  })