github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/api/featureflags/feature_flags_test.go (about)

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