github.com/sleungcy-sap/cli@v7.1.0+incompatible/command/v7/enable_feature_flag_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v7action"
     8  	"code.cloudfoundry.org/cli/command/commandfakes"
     9  	. "code.cloudfoundry.org/cli/command/v7"
    10  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    11  	"code.cloudfoundry.org/cli/util/configv3"
    12  	"code.cloudfoundry.org/cli/util/ui"
    13  
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  	. "github.com/onsi/gomega/gbytes"
    17  )
    18  
    19  var _ = Describe("Enable Feature Flag Command", func() {
    20  	var (
    21  		cmd             EnableFeatureFlagCommand
    22  		testUI          *ui.UI
    23  		fakeConfig      *commandfakes.FakeConfig
    24  		fakeSharedActor *commandfakes.FakeSharedActor
    25  		fakeActor       *v7fakes.FakeActor
    26  		executeErr      error
    27  		binaryName      string
    28  		featureFlagName = "flag1"
    29  	)
    30  
    31  	BeforeEach(func() {
    32  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    33  		fakeConfig = new(commandfakes.FakeConfig)
    34  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    35  		fakeActor = new(v7fakes.FakeActor)
    36  
    37  		cmd = EnableFeatureFlagCommand{
    38  			BaseCommand: BaseCommand{
    39  				UI:          testUI,
    40  				Config:      fakeConfig,
    41  				SharedActor: fakeSharedActor,
    42  				Actor:       fakeActor,
    43  			},
    44  		}
    45  
    46  		binaryName = "faceman"
    47  		fakeConfig.BinaryNameReturns(binaryName)
    48  
    49  		cmd.RequiredArgs.Feature = featureFlagName
    50  	})
    51  
    52  	JustBeforeEach(func() {
    53  		executeErr = cmd.Execute(nil)
    54  	})
    55  
    56  	When("the environment is not set up correctly", func() {
    57  		BeforeEach(func() {
    58  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    59  		})
    60  
    61  		It("returns an error", func() {
    62  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    63  
    64  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    65  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    66  			Expect(checkTargetedOrg).To(BeFalse())
    67  			Expect(checkTargetedSpace).To(BeFalse())
    68  		})
    69  	})
    70  
    71  	When("the environment is setup correctly", func() {
    72  		BeforeEach(func() {
    73  			fakeConfig.CurrentUserReturns(configv3.User{Name: "apple"}, nil)
    74  		})
    75  
    76  		It("should print text indicating its running", func() {
    77  			Expect(executeErr).NotTo(HaveOccurred())
    78  			Expect(testUI.Out).To(Say(`Enabling feature flag flag1 as apple\.\.\.`))
    79  		})
    80  
    81  		When("updating featureFlag fails", func() {
    82  			BeforeEach(func() {
    83  				fakeActor.EnableFeatureFlagReturns(v7action.Warnings{"this is a warning"},
    84  					errors.New("some-error"))
    85  			})
    86  
    87  			It("prints warnings and returns error", func() {
    88  				Expect(executeErr).To(MatchError("some-error"))
    89  				Expect(testUI.Err).To(Say("this is a warning"))
    90  			})
    91  		})
    92  
    93  		When("updating featureFlag succeeds", func() {
    94  			When("featureFlag exist", func() {
    95  				BeforeEach(func() {
    96  					fakeActor.EnableFeatureFlagReturns(v7action.Warnings{"this is a warning"}, nil)
    97  				})
    98  
    99  				It("diaplays the feature flag was enabled", func() {
   100  					featureFlagArgs := fakeActor.EnableFeatureFlagArgsForCall(0)
   101  					Expect(featureFlagArgs).To(Equal(featureFlagName))
   102  					Expect(executeErr).NotTo(HaveOccurred())
   103  					Expect(testUI.Err).To(Say("this is a warning"))
   104  					Expect(testUI.Out).To(Say(`OK`))
   105  				})
   106  			})
   107  			When("there is no featureFlag", func() {
   108  				BeforeEach(func() {
   109  					fakeActor.EnableFeatureFlagReturns(v7action.Warnings{"this is a warning"}, actionerror.FeatureFlagNotFoundError{FeatureFlagName: featureFlagName})
   110  				})
   111  
   112  				It("Fails and returns a FeatureFlagNotFoundError", func() {
   113  					Expect(executeErr).To(Equal(actionerror.FeatureFlagNotFoundError{FeatureFlagName: featureFlagName}))
   114  					Expect(testUI.Err).To(Say("this is a warning"))
   115  				})
   116  			})
   117  		})
   118  	})
   119  })