github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+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.FakeEnableFeatureFlagActor
    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.FakeEnableFeatureFlagActor)
    36  
    37  		cmd = EnableFeatureFlagCommand{
    38  			UI:          testUI,
    39  			Config:      fakeConfig,
    40  			SharedActor: fakeSharedActor,
    41  			Actor:       fakeActor,
    42  		}
    43  
    44  		binaryName = "faceman"
    45  		fakeConfig.BinaryNameReturns(binaryName)
    46  
    47  		cmd.RequiredArgs.Feature = featureFlagName
    48  	})
    49  
    50  	JustBeforeEach(func() {
    51  		executeErr = cmd.Execute(nil)
    52  	})
    53  
    54  	When("the environment is not set up correctly", func() {
    55  		BeforeEach(func() {
    56  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    57  		})
    58  
    59  		It("returns an error", func() {
    60  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    61  
    62  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    63  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    64  			Expect(checkTargetedOrg).To(BeFalse())
    65  			Expect(checkTargetedSpace).To(BeFalse())
    66  		})
    67  	})
    68  
    69  	When("the environment is setup correctly", func() {
    70  		BeforeEach(func() {
    71  			fakeConfig.CurrentUserReturns(configv3.User{Name: "apple"}, nil)
    72  		})
    73  
    74  		It("should print text indicating its running", func() {
    75  			Expect(executeErr).NotTo(HaveOccurred())
    76  			Expect(testUI.Out).To(Say(`Enabling feature flag flag1 as apple\.\.\.`))
    77  		})
    78  
    79  		When("updating featureFlag fails", func() {
    80  			BeforeEach(func() {
    81  				fakeActor.EnableFeatureFlagReturns(v7action.Warnings{"this is a warning"},
    82  					errors.New("some-error"))
    83  			})
    84  
    85  			It("prints warnings and returns error", func() {
    86  				Expect(executeErr).To(MatchError("some-error"))
    87  				Expect(testUI.Err).To(Say("this is a warning"))
    88  			})
    89  		})
    90  
    91  		When("updating featureFlag succeeds", func() {
    92  			When("featureFlag exist", func() {
    93  				BeforeEach(func() {
    94  					fakeActor.EnableFeatureFlagReturns(v7action.Warnings{"this is a warning"}, nil)
    95  				})
    96  
    97  				It("diaplays the feature flag was enabled", func() {
    98  					featureFlagArgs := fakeActor.EnableFeatureFlagArgsForCall(0)
    99  					Expect(featureFlagArgs).To(Equal(featureFlagName))
   100  					Expect(executeErr).NotTo(HaveOccurred())
   101  					Expect(testUI.Err).To(Say("this is a warning"))
   102  					Expect(testUI.Out).To(Say(`OK`))
   103  				})
   104  			})
   105  			When("there is no featureFlag", func() {
   106  				BeforeEach(func() {
   107  					fakeActor.EnableFeatureFlagReturns(v7action.Warnings{"this is a warning"}, actionerror.FeatureFlagNotFoundError{FeatureFlagName: featureFlagName})
   108  				})
   109  
   110  				It("Fails and returns a FeatureFlagNotFoundError", func() {
   111  					Expect(executeErr).To(Equal(actionerror.FeatureFlagNotFoundError{FeatureFlagName: featureFlagName}))
   112  					Expect(testUI.Err).To(Say("this is a warning"))
   113  				})
   114  			})
   115  		})
   116  	})
   117  })