github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v7/disable_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/api/cloudcontroller/ccv3"
     9  	"code.cloudfoundry.org/cli/command/commandfakes"
    10  	. "code.cloudfoundry.org/cli/command/v7"
    11  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    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("disable-ssh Command", func() {
    19  	var (
    20  		cmd                 DisableSSHCommand
    21  		testUI              *ui.UI
    22  		fakeConfig          *commandfakes.FakeConfig
    23  		fakeSharedActor     *commandfakes.FakeSharedActor
    24  		fakeDisableSSHActor *v7fakes.FakeDisableSSHActor
    25  
    26  		binaryName      string
    27  		currentUserName string
    28  		executeErr      error
    29  	)
    30  
    31  	BeforeEach(func() {
    32  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    33  		fakeConfig = new(commandfakes.FakeConfig)
    34  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    35  		fakeDisableSSHActor = new(v7fakes.FakeDisableSSHActor)
    36  
    37  		cmd = DisableSSHCommand{
    38  			UI:          testUI,
    39  			Config:      fakeConfig,
    40  			SharedActor: fakeSharedActor,
    41  			Actor:       fakeDisableSSHActor,
    42  		}
    43  
    44  		cmd.RequiredArgs.AppName = "some-app"
    45  
    46  		binaryName = "faceman"
    47  		fakeConfig.BinaryNameReturns(binaryName)
    48  		currentUserName = "some-user"
    49  		fakeConfig.CurrentUserNameReturns(currentUserName, nil)
    50  	})
    51  
    52  	JustBeforeEach(func() {
    53  		executeErr = cmd.Execute(nil)
    54  	})
    55  
    56  	When("checking target fails", 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: "faceman"}))
    63  
    64  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    65  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    66  			Expect(checkTargetedOrg).To(BeTrue())
    67  			Expect(checkTargetedSpace).To(BeTrue())
    68  		})
    69  	})
    70  
    71  	When("the user is logged in", func() {
    72  		When("no errors occur", func() {
    73  			BeforeEach(func() {
    74  				fakeDisableSSHActor.GetApplicationByNameAndSpaceReturns(
    75  					v7action.Application{Name: "some-app", GUID: "some-app-guid"},
    76  					v7action.Warnings{"some-get-app-warnings"},
    77  					nil,
    78  				)
    79  				fakeDisableSSHActor.GetAppFeatureReturns(
    80  					ccv3.ApplicationFeature{Enabled: true, Name: "ssh"},
    81  					v7action.Warnings{"some-feature-warnings"},
    82  					nil,
    83  				)
    84  				fakeDisableSSHActor.UpdateAppFeatureReturns(
    85  					v7action.Warnings{"some-update-ssh-warnings"},
    86  					nil,
    87  				)
    88  			})
    89  
    90  			It("disables ssh on the app", func() {
    91  				Expect(executeErr).ToNot(HaveOccurred())
    92  
    93  				Expect(fakeDisableSSHActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1))
    94  
    95  				appName, spaceGUID := fakeDisableSSHActor.GetApplicationByNameAndSpaceArgsForCall(0)
    96  				Expect(appName).To(Equal(cmd.RequiredArgs.AppName))
    97  				Expect(spaceGUID).To(Equal(cmd.Config.TargetedSpace().GUID))
    98  
    99  				Expect(fakeDisableSSHActor.GetAppFeatureCallCount()).To(Equal(1))
   100  
   101  				appGUID, featureName := fakeDisableSSHActor.GetAppFeatureArgsForCall(0)
   102  				Expect(appGUID).To(Equal("some-app-guid"))
   103  				Expect(featureName).To(Equal("ssh"))
   104  
   105  				Expect(fakeDisableSSHActor.UpdateAppFeatureCallCount()).To(Equal(1))
   106  				app, enabled, featureName := fakeDisableSSHActor.UpdateAppFeatureArgsForCall(0)
   107  				Expect(app.Name).To(Equal("some-app"))
   108  				Expect(enabled).To(Equal(false))
   109  				Expect(featureName).To(Equal("ssh"))
   110  
   111  				Expect(testUI.Err).To(Say("some-get-app-warnings"))
   112  				Expect(testUI.Err).To(Say("some-feature-warnings"))
   113  				Expect(testUI.Err).To(Say("some-update-ssh-warnings"))
   114  				Expect(testUI.Out).To(Say(`Disabling ssh support for app %s as %s\.\.\.`, appName, currentUserName))
   115  				Expect(testUI.Out).To(Say("OK"))
   116  			})
   117  		})
   118  
   119  		When("app ssh is already disabled", func() {
   120  			BeforeEach(func() {
   121  				fakeDisableSSHActor.UpdateAppFeatureReturns(
   122  					v7action.Warnings{"ssh support for app 'some-app' is already disabled.", "some-other-warnings"},
   123  					nil,
   124  				)
   125  				fakeDisableSSHActor.GetAppFeatureReturns(
   126  					ccv3.ApplicationFeature{Enabled: false, Name: "ssh"},
   127  					v7action.Warnings{},
   128  					nil,
   129  				)
   130  			})
   131  
   132  			It("shows the app ssh is already disabled", func() {
   133  				Expect(testUI.Out).To(Say("ssh support for app 'some-app' is already disabled."))
   134  				Expect(testUI.Out).To(Say("OK"))
   135  			})
   136  		})
   137  
   138  		When("an error occurs", func() {
   139  			When("GetApp action errors", func() {
   140  				When("no user is found", func() {
   141  					var returnedErr error
   142  
   143  					BeforeEach(func() {
   144  						returnedErr = actionerror.ApplicationNotFoundError{Name: "some-app"}
   145  						fakeDisableSSHActor.GetApplicationByNameAndSpaceReturns(
   146  							v7action.Application{},
   147  							nil,
   148  							returnedErr)
   149  					})
   150  
   151  					It("returns the same error", func() {
   152  						Expect(executeErr).To(HaveOccurred())
   153  						Expect(executeErr).To(MatchError(returnedErr))
   154  					})
   155  				})
   156  			})
   157  
   158  			When("GetAppFeature action errors", func() {
   159  				returnedErr := errors.New("some-error")
   160  				BeforeEach(func() {
   161  
   162  					fakeDisableSSHActor.GetAppFeatureReturns(
   163  						ccv3.ApplicationFeature{},
   164  						nil,
   165  						returnedErr,
   166  					)
   167  				})
   168  
   169  				It("returns the same error", func() {
   170  					Expect(executeErr).To(HaveOccurred())
   171  					Expect(executeErr).To(MatchError(returnedErr))
   172  				})
   173  			})
   174  
   175  			When("Disable ssh action errors", func() {
   176  				var returnedErr error
   177  
   178  				BeforeEach(func() {
   179  					returnedErr = errors.New("some-error")
   180  					fakeDisableSSHActor.GetApplicationByNameAndSpaceReturns(
   181  						v7action.Application{Name: "some-app"},
   182  						v7action.Warnings{"some-warning"},
   183  						nil,
   184  					)
   185  					fakeDisableSSHActor.UpdateAppFeatureReturns(nil, returnedErr)
   186  				})
   187  
   188  				It("returns the same error", func() {
   189  					Expect(executeErr).To(MatchError(returnedErr))
   190  				})
   191  			})
   192  		})
   193  	})
   194  })