github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/featureflag/feature_flag_test.go (about) 1 package featureflag_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/cf/api/featureflags/featureflagsfakes" 7 "code.cloudfoundry.org/cli/cf/commandregistry" 8 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 9 "code.cloudfoundry.org/cli/cf/models" 10 "code.cloudfoundry.org/cli/cf/requirements" 11 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 12 testcmd "code.cloudfoundry.org/cli/util/testhelpers/commands" 13 testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration" 14 . "code.cloudfoundry.org/cli/util/testhelpers/matchers" 15 testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal" 16 . "github.com/onsi/ginkgo" 17 . "github.com/onsi/gomega" 18 ) 19 20 var _ = Describe("feature-flag command", func() { 21 var ( 22 ui *testterm.FakeUI 23 requirementsFactory *requirementsfakes.FakeFactory 24 configRepo coreconfig.Repository 25 flagRepo *featureflagsfakes.FakeFeatureFlagRepository 26 deps commandregistry.Dependency 27 ) 28 29 updateCommandDependency := func(pluginCall bool) { 30 deps.UI = ui 31 deps.RepoLocator = deps.RepoLocator.SetFeatureFlagRepository(flagRepo) 32 deps.Config = configRepo 33 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("feature-flag").SetDependency(deps, pluginCall)) 34 } 35 36 BeforeEach(func() { 37 ui = &testterm.FakeUI{} 38 configRepo = testconfig.NewRepositoryWithDefaults() 39 requirementsFactory = new(requirementsfakes.FakeFactory) 40 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 41 flagRepo = new(featureflagsfakes.FakeFeatureFlagRepository) 42 }) 43 44 runCommand := func(args ...string) bool { 45 return testcmd.RunCLICommand("feature-flag", args, requirementsFactory, updateCommandDependency, false, ui) 46 } 47 48 Describe("requirements", func() { 49 It("requires the user to be logged in", func() { 50 requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"}) 51 Expect(runCommand("foo")).ToNot(HavePassedRequirements()) 52 }) 53 54 It("requires the user to provide a feature flag", func() { 55 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 56 Expect(runCommand()).ToNot(HavePassedRequirements()) 57 }) 58 }) 59 60 Describe("when logged in", func() { 61 BeforeEach(func() { 62 flag := models.FeatureFlag{ 63 Name: "route_creation", 64 Enabled: false, 65 } 66 flagRepo.FindByNameReturns(flag, nil) 67 }) 68 69 It("lists the state of the specified feature flag", func() { 70 runCommand("route_creation") 71 Expect(ui.Outputs()).To(ContainSubstrings( 72 []string{"Retrieving status of route_creation as my-user..."}, 73 []string{"Feature", "State"}, 74 []string{"route_creation", "disabled"}, 75 )) 76 }) 77 78 Context("when an error occurs", func() { 79 BeforeEach(func() { 80 flagRepo.FindByNameReturns(models.FeatureFlag{}, errors.New("An error occurred.")) 81 }) 82 83 It("fails with an error", func() { 84 runCommand("route_creation") 85 Expect(ui.Outputs()).To(ContainSubstrings( 86 []string{"FAILED"}, 87 []string{"An error occurred."}, 88 )) 89 }) 90 }) 91 }) 92 })