github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/feature_flag_command_test.go (about)

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