github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/disable_ssh_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  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  	. "github.com/onsi/gomega/gbytes"
    17  )
    18  
    19  var _ = Describe("disable-ssh Command", func() {
    20  	var (
    21  		cmd                 DisableSSHCommand
    22  		testUI              *ui.UI
    23  		fakeConfig          *commandfakes.FakeConfig
    24  		fakeSharedActor     *commandfakes.FakeSharedActor
    25  		fakeDisableSSHActor *v7fakes.FakeActor
    26  
    27  		binaryName      string
    28  		currentUserName string
    29  		executeErr      error
    30  	)
    31  
    32  	BeforeEach(func() {
    33  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    34  		fakeConfig = new(commandfakes.FakeConfig)
    35  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    36  		fakeDisableSSHActor = new(v7fakes.FakeActor)
    37  
    38  		cmd = DisableSSHCommand{
    39  			BaseCommand: BaseCommand{
    40  				UI:          testUI,
    41  				Config:      fakeConfig,
    42  				SharedActor: fakeSharedActor,
    43  				Actor:       fakeDisableSSHActor,
    44  			},
    45  		}
    46  
    47  		cmd.RequiredArgs.AppName = "some-app"
    48  
    49  		binaryName = "faceman"
    50  		fakeConfig.BinaryNameReturns(binaryName)
    51  		currentUserName = "some-user"
    52  		fakeDisableSSHActor.GetCurrentUserReturns(configv3.User{Name: currentUserName}, nil)
    53  	})
    54  
    55  	JustBeforeEach(func() {
    56  		executeErr = cmd.Execute(nil)
    57  	})
    58  
    59  	When("checking target fails", func() {
    60  		BeforeEach(func() {
    61  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    62  		})
    63  
    64  		It("returns an error", func() {
    65  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: "faceman"}))
    66  
    67  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    68  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    69  			Expect(checkTargetedOrg).To(BeTrue())
    70  			Expect(checkTargetedSpace).To(BeTrue())
    71  		})
    72  	})
    73  
    74  	When("the user is logged in", func() {
    75  		When("no errors occur", func() {
    76  			BeforeEach(func() {
    77  				fakeDisableSSHActor.GetApplicationByNameAndSpaceReturns(
    78  					resources.Application{Name: "some-app", GUID: "some-app-guid"},
    79  					v7action.Warnings{"some-get-app-warnings"},
    80  					nil,
    81  				)
    82  				fakeDisableSSHActor.GetAppFeatureReturns(
    83  					resources.ApplicationFeature{Enabled: true, Name: "ssh"},
    84  					v7action.Warnings{"some-feature-warnings"},
    85  					nil,
    86  				)
    87  				fakeDisableSSHActor.UpdateAppFeatureReturns(
    88  					v7action.Warnings{"some-update-ssh-warnings"},
    89  					nil,
    90  				)
    91  			})
    92  
    93  			It("disables ssh on the app", func() {
    94  				Expect(executeErr).ToNot(HaveOccurred())
    95  
    96  				Expect(fakeDisableSSHActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1))
    97  
    98  				appName, spaceGUID := fakeDisableSSHActor.GetApplicationByNameAndSpaceArgsForCall(0)
    99  				Expect(appName).To(Equal(cmd.RequiredArgs.AppName))
   100  				Expect(spaceGUID).To(Equal(cmd.Config.TargetedSpace().GUID))
   101  
   102  				Expect(fakeDisableSSHActor.GetAppFeatureCallCount()).To(Equal(1))
   103  
   104  				appGUID, featureName := fakeDisableSSHActor.GetAppFeatureArgsForCall(0)
   105  				Expect(appGUID).To(Equal("some-app-guid"))
   106  				Expect(featureName).To(Equal("ssh"))
   107  
   108  				Expect(fakeDisableSSHActor.UpdateAppFeatureCallCount()).To(Equal(1))
   109  				app, enabled, featureName := fakeDisableSSHActor.UpdateAppFeatureArgsForCall(0)
   110  				Expect(app.Name).To(Equal("some-app"))
   111  				Expect(enabled).To(Equal(false))
   112  				Expect(featureName).To(Equal("ssh"))
   113  
   114  				Expect(testUI.Err).To(Say("some-get-app-warnings"))
   115  				Expect(testUI.Err).To(Say("some-feature-warnings"))
   116  				Expect(testUI.Err).To(Say("some-update-ssh-warnings"))
   117  				Expect(testUI.Out).To(Say(`Disabling ssh support for app %s as %s\.\.\.`, appName, currentUserName))
   118  				Expect(testUI.Out).To(Say("OK"))
   119  			})
   120  		})
   121  
   122  		When("app ssh is already disabled", func() {
   123  			BeforeEach(func() {
   124  				fakeDisableSSHActor.UpdateAppFeatureReturns(
   125  					v7action.Warnings{"ssh support for app 'some-app' is already disabled.", "some-other-warnings"},
   126  					nil,
   127  				)
   128  				fakeDisableSSHActor.GetAppFeatureReturns(
   129  					resources.ApplicationFeature{Enabled: false, Name: "ssh"},
   130  					v7action.Warnings{},
   131  					nil,
   132  				)
   133  			})
   134  
   135  			It("shows the app ssh is already disabled", func() {
   136  				Expect(testUI.Out).To(Say("ssh support for app 'some-app' is already disabled."))
   137  				Expect(testUI.Out).To(Say("OK"))
   138  			})
   139  		})
   140  
   141  		When("an error occurs", func() {
   142  			When("GetApp action errors", func() {
   143  				When("no user is found", func() {
   144  					var returnedErr error
   145  
   146  					BeforeEach(func() {
   147  						returnedErr = actionerror.ApplicationNotFoundError{Name: "some-app"}
   148  						fakeDisableSSHActor.GetApplicationByNameAndSpaceReturns(
   149  							resources.Application{},
   150  							nil,
   151  							returnedErr)
   152  					})
   153  
   154  					It("returns the same error", func() {
   155  						Expect(executeErr).To(HaveOccurred())
   156  						Expect(executeErr).To(MatchError(returnedErr))
   157  					})
   158  				})
   159  			})
   160  
   161  			When("GetAppFeature action errors", func() {
   162  				returnedErr := errors.New("some-error")
   163  				BeforeEach(func() {
   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  })