github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/command/v2/feature_flags_command_test.go (about) 1 package v2_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/actor/actionerror" 7 "code.cloudfoundry.org/cli/actor/v2action" 8 "code.cloudfoundry.org/cli/command/commandfakes" 9 . "code.cloudfoundry.org/cli/command/v2" 10 "code.cloudfoundry.org/cli/command/v2/v2fakes" 11 "code.cloudfoundry.org/cli/util/configv3" 12 "code.cloudfoundry.org/cli/util/ui" 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 . "github.com/onsi/gomega/gbytes" 16 ) 17 18 var _ = Describe("feature flags Command", func() { 19 var ( 20 cmd FeatureFlagsCommand 21 testUI *ui.UI 22 fakeConfig *commandfakes.FakeConfig 23 fakeSharedActor *commandfakes.FakeSharedActor 24 fakeActor *v2fakes.FakeFeatureFlagsActor 25 binaryName string 26 executeErr error 27 ) 28 29 BeforeEach(func() { 30 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 31 fakeConfig = new(commandfakes.FakeConfig) 32 fakeSharedActor = new(commandfakes.FakeSharedActor) 33 fakeActor = new(v2fakes.FakeFeatureFlagsActor) 34 35 cmd = FeatureFlagsCommand{ 36 UI: testUI, 37 Config: fakeConfig, 38 SharedActor: fakeSharedActor, 39 Actor: fakeActor, 40 } 41 42 binaryName = "faceman" 43 fakeConfig.BinaryNameReturns(binaryName) 44 }) 45 46 JustBeforeEach(func() { 47 executeErr = cmd.Execute(nil) 48 }) 49 50 Context("when the user is not logged in", func() { 51 BeforeEach(func() { 52 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 53 }) 54 55 It("returns an error", func() { 56 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 57 58 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 59 checkTargetedOrgArg, checkTargetedSpaceArg := fakeSharedActor.CheckTargetArgsForCall(0) 60 Expect(checkTargetedOrgArg).To(BeFalse()) 61 Expect(checkTargetedSpaceArg).To(BeFalse()) 62 }) 63 }) 64 65 Context("when the user is logged in", func() { 66 Context("when getting the current user fails", func() { 67 BeforeEach(func() { 68 fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("get-user-error")) 69 }) 70 71 It("returns the error", func() { 72 Expect(executeErr).To(MatchError("get-user-error")) 73 }) 74 }) 75 76 Context("when getting the current user succeeds", func() { 77 BeforeEach(func() { 78 fakeConfig.CurrentUserReturns( 79 configv3.User{Name: "some-user"}, 80 nil) 81 }) 82 83 Context("when an error is encountered getting feature flags", func() { 84 var expectedErr error 85 86 BeforeEach(func() { 87 expectedErr = errors.New("get feature flags error") 88 fakeActor.GetFeatureFlagsReturns( 89 []v2action.FeatureFlag{}, 90 v2action.Warnings{"get-flags-warning"}, 91 expectedErr) 92 }) 93 94 It("displays an empty list and all warnings", func() { 95 Expect(executeErr).To(MatchError(expectedErr)) 96 97 Expect(testUI.Err).To(Say("get-flags-warning")) 98 99 Expect(fakeActor.GetFeatureFlagsCallCount()).To(Equal(1)) 100 }) 101 }) 102 103 Context("when there are no feature flags", func() { 104 BeforeEach(func() { 105 fakeActor.GetFeatureFlagsReturns( 106 []v2action.FeatureFlag{}, 107 v2action.Warnings{"get-flags-warning"}, 108 nil) 109 }) 110 111 It("displays an empty list and all warnings", func() { 112 Expect(executeErr).ToNot(HaveOccurred()) 113 114 Expect(testUI.Out).To(Say("Retrieving status of all flagged features as some-user\\.\\.\\.")) 115 Expect(testUI.Out).To(Say("")) 116 Expect(testUI.Out).To(Say("features\\s+state")) 117 118 Expect(testUI.Err).To(Say("get-flags-warning")) 119 120 Expect(fakeActor.GetFeatureFlagsCallCount()).To(Equal(1)) 121 }) 122 }) 123 124 Context("when there are feature flags", func() { 125 BeforeEach(func() { 126 fakeActor.GetFeatureFlagsReturns( 127 []v2action.FeatureFlag{ 128 { 129 Name: "feature-flag-1", 130 Enabled: true, 131 }, 132 { 133 Name: "feature-flag-2", 134 Enabled: false, 135 }, 136 }, 137 v2action.Warnings{"get-flags-warning"}, 138 nil) 139 }) 140 141 It("displays a list of feature flags with state and all warnings", func() { 142 Expect(executeErr).ToNot(HaveOccurred()) 143 144 Expect(testUI.Out).To(Say("Retrieving status of all flagged features as some-user\\.\\.\\.")) 145 Expect(testUI.Out).To(Say("")) 146 Expect(testUI.Out).To(Say("features\\s+state")) 147 Expect(testUI.Out).To(Say("feature-flag-1\\s+enabled")) 148 Expect(testUI.Out).To(Say("feature-flag-2\\s+disabled")) 149 150 Expect(testUI.Err).To(Say("get-flags-warning")) 151 152 Expect(fakeActor.GetFeatureFlagsCallCount()).To(Equal(1)) 153 }) 154 }) 155 }) 156 }) 157 })