github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/cf/commands/featureflag/enable_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/requirements"
    10  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    11  	testcmd "code.cloudfoundry.org/cli/util/testhelpers/commands"
    12  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    13  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    14  	testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  )
    18  
    19  var _ = Describe("enable-feature-flag command", func() {
    20  	var (
    21  		ui                  *testterm.FakeUI
    22  		requirementsFactory *requirementsfakes.FakeFactory
    23  		configRepo          coreconfig.Repository
    24  		flagRepo            *featureflagsfakes.FakeFeatureFlagRepository
    25  		deps                commandregistry.Dependency
    26  	)
    27  
    28  	updateCommandDependency := func(pluginCall bool) {
    29  		deps.UI = ui
    30  		deps.RepoLocator = deps.RepoLocator.SetFeatureFlagRepository(flagRepo)
    31  		deps.Config = configRepo
    32  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("enable-feature-flag").SetDependency(deps, pluginCall))
    33  	}
    34  
    35  	BeforeEach(func() {
    36  		ui = &testterm.FakeUI{}
    37  		configRepo = testconfig.NewRepositoryWithDefaults()
    38  		requirementsFactory = new(requirementsfakes.FakeFactory)
    39  		requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    40  		flagRepo = new(featureflagsfakes.FakeFeatureFlagRepository)
    41  	})
    42  
    43  	runCommand := func(args ...string) bool {
    44  		return testcmd.RunCLICommand("enable-feature-flag", args, requirementsFactory, updateCommandDependency, false, ui)
    45  	}
    46  
    47  	Describe("requirements", func() {
    48  		It("requires the user to be logged in", func() {
    49  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    50  			Expect(runCommand()).ToNot(HavePassedRequirements())
    51  		})
    52  
    53  		It("fails with usage if a single feature is not specified", func() {
    54  			runCommand()
    55  			Expect(ui.Outputs()).To(ContainSubstrings(
    56  				[]string{"Incorrect Usage", "Requires an argument"},
    57  			))
    58  		})
    59  	})
    60  
    61  	Describe("when logged in", func() {
    62  		BeforeEach(func() {
    63  			flagRepo.UpdateReturns(nil)
    64  		})
    65  
    66  		It("Sets the flag", func() {
    67  			runCommand("user_org_creation")
    68  
    69  			flag, set := flagRepo.UpdateArgsForCall(0)
    70  			Expect(flag).To(Equal("user_org_creation"))
    71  			Expect(set).To(BeTrue())
    72  
    73  			Expect(ui.Outputs()).To(ContainSubstrings(
    74  				[]string{"Setting status of user_org_creation as my-user..."},
    75  				[]string{"OK"},
    76  				[]string{"Feature user_org_creation Enabled."},
    77  			))
    78  		})
    79  
    80  		Context("when an error occurs", func() {
    81  			BeforeEach(func() {
    82  				flagRepo.UpdateReturns(errors.New("An error occurred."))
    83  			})
    84  
    85  			It("fails with an error", func() {
    86  				runCommand("i-dont-exist")
    87  				Expect(ui.Outputs()).To(ContainSubstrings(
    88  					[]string{"FAILED"},
    89  					[]string{"An error occurred."},
    90  				))
    91  			})
    92  		})
    93  	})
    94  })