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