github.com/loafoe/cli@v7.1.0+incompatible/command/v7/allow_space_ssh_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/ui" 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 . "github.com/onsi/gomega/gbytes" 15 ) 16 17 var _ = Describe("allow-space-ssh Command", func() { 18 var ( 19 cmd AllowSpaceSSHCommand 20 testUI *ui.UI 21 fakeConfig *commandfakes.FakeConfig 22 fakeSharedActor *commandfakes.FakeSharedActor 23 fakeActor *v7fakes.FakeActor 24 25 binaryName string 26 currentUserName string 27 executeErr error 28 ) 29 30 BeforeEach(func() { 31 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 32 fakeConfig = new(commandfakes.FakeConfig) 33 fakeSharedActor = new(commandfakes.FakeSharedActor) 34 fakeActor = new(v7fakes.FakeActor) 35 36 cmd = AllowSpaceSSHCommand{ 37 BaseCommand: BaseCommand{ 38 UI: testUI, 39 Config: fakeConfig, 40 SharedActor: fakeSharedActor, 41 Actor: fakeActor, 42 }, 43 } 44 45 cmd.RequiredArgs.Space = "some-space" 46 47 binaryName = "faceman" 48 fakeConfig.BinaryNameReturns(binaryName) 49 currentUserName = "some-user" 50 fakeConfig.CurrentUserNameReturns(currentUserName, nil) 51 }) 52 53 JustBeforeEach(func() { 54 executeErr = cmd.Execute(nil) 55 }) 56 57 When("checking target fails", func() { 58 BeforeEach(func() { 59 fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}) 60 }) 61 62 It("returns an error", func() { 63 Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})) 64 65 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 66 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 67 Expect(checkTargetedOrg).To(BeTrue()) 68 Expect(checkTargetedSpace).To(BeFalse()) 69 }) 70 }) 71 72 When("checking the current user fails", func() { 73 BeforeEach(func() { 74 fakeConfig.CurrentUserNameReturns("", errors.New("uh oh")) 75 }) 76 77 It("returns the error", func() { 78 Expect(executeErr).To(MatchError("uh oh")) 79 }) 80 }) 81 82 When("the user is logged in", func() { 83 When("no errors occur", func() { 84 BeforeEach(func() { 85 fakeActor.UpdateSpaceFeatureReturns( 86 v7action.Warnings{"some-warning"}, 87 nil, 88 ) 89 }) 90 91 It("allows ssh for the space", func() { 92 Expect(executeErr).ToNot(HaveOccurred()) 93 94 Expect(fakeActor.UpdateSpaceFeatureCallCount()).To(Equal(1)) 95 96 Expect(testUI.Out).To(Say("Enabling ssh support for space %s as %s...", cmd.RequiredArgs.Space, currentUserName)) 97 Expect(testUI.Out).To(Say("OK")) 98 Expect(testUI.Err).To(Say("some-warning")) 99 }) 100 }) 101 102 When("ssh is already allowed", func() { 103 BeforeEach(func() { 104 fakeActor.UpdateSpaceFeatureReturns( 105 v7action.Warnings{"some-warning"}, 106 actionerror.SpaceSSHAlreadyEnabledError{Space: "some-space"}, 107 ) 108 }) 109 110 It("allows ssh for the space", func() { 111 Expect(executeErr).ToNot(HaveOccurred()) 112 113 Expect(fakeActor.UpdateSpaceFeatureCallCount()).To(Equal(1)) 114 115 Expect(testUI.Out).To(Say("Enabling ssh support for space %s as %s...", cmd.RequiredArgs.Space, currentUserName)) 116 Expect(testUI.Out).To(Say("ssh support for space '%s' is already enabled.", cmd.RequiredArgs.Space)) 117 Expect(testUI.Out).To(Say("OK")) 118 Expect(testUI.Err).To(Say("some-warning")) 119 }) 120 }) 121 122 When("an error occurs while enabling SSH", func() { 123 BeforeEach(func() { 124 fakeActor.UpdateSpaceFeatureReturns( 125 v7action.Warnings{"some-warning"}, 126 errors.New("allow-ssh-error"), 127 ) 128 }) 129 130 It("does not display OK and returns the error", func() { 131 Expect(testUI.Out).NotTo(Say("OK")) 132 Expect(testUI.Err).To(Say("some-warning")) 133 Expect(executeErr).To(MatchError("allow-ssh-error")) 134 }) 135 }) 136 }) 137 })