github.com/jasonkeene/cli@v6.14.1-0.20160816203908-ca5715166dfb+incompatible/cf/commands/featureflag/disable_feature_flag_test.go (about)

     1  package featureflag_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/cloudfoundry/cli/cf/api/featureflags/featureflagsfakes"
     7  	"github.com/cloudfoundry/cli/cf/commandregistry"
     8  	"github.com/cloudfoundry/cli/cf/configuration/coreconfig"
     9  	"github.com/cloudfoundry/cli/cf/requirements"
    10  	"github.com/cloudfoundry/cli/cf/requirements/requirementsfakes"
    11  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
    12  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    13  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    14  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  )
    18  
    19  var _ = Describe("disable-feature-flag command", func() {
    20  	var (
    21  		ui                  *testterm.FakeUI
    22  		requirementsFactory *requirementsfakes.FakeFactory
    23  		flagRepo            *featureflagsfakes.FakeFeatureFlagRepository
    24  		configRepo          coreconfig.Repository
    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("disable-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("disable-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(BeFalse())
    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 Disabled."},
    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  })