github.com/jasonkeene/cli@v6.14.1-0.20160816203908-ca5715166dfb+incompatible/cf/commands/application/disable_ssh_test.go (about)

     1  package application_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/cloudfoundry/cli/cf/api/applications/applicationsfakes"
     7  	"github.com/cloudfoundry/cli/cf/commandregistry"
     8  	"github.com/cloudfoundry/cli/cf/configuration/coreconfig"
     9  	"github.com/cloudfoundry/cli/cf/models"
    10  	"github.com/cloudfoundry/cli/cf/requirements"
    11  	"github.com/cloudfoundry/cli/cf/requirements/requirementsfakes"
    12  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
    13  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    14  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    15  
    16  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    17  	. "github.com/onsi/ginkgo"
    18  	. "github.com/onsi/gomega"
    19  )
    20  
    21  var _ = Describe("disable-ssh command", func() {
    22  	var (
    23  		ui                  *testterm.FakeUI
    24  		requirementsFactory *requirementsfakes.FakeFactory
    25  		appRepo             *applicationsfakes.FakeRepository
    26  		configRepo          coreconfig.Repository
    27  		deps                commandregistry.Dependency
    28  	)
    29  
    30  	BeforeEach(func() {
    31  		ui = &testterm.FakeUI{}
    32  		configRepo = testconfig.NewRepositoryWithDefaults()
    33  		requirementsFactory = new(requirementsfakes.FakeFactory)
    34  		appRepo = new(applicationsfakes.FakeRepository)
    35  	})
    36  
    37  	updateCommandDependency := func(pluginCall bool) {
    38  		deps.UI = ui
    39  		deps.Config = configRepo
    40  		deps.RepoLocator = deps.RepoLocator.SetApplicationRepository(appRepo)
    41  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("disable-ssh").SetDependency(deps, pluginCall))
    42  	}
    43  
    44  	runCommand := func(args ...string) bool {
    45  		return testcmd.RunCLICommand("disable-ssh", args, requirementsFactory, updateCommandDependency, false, ui)
    46  	}
    47  
    48  	Describe("requirements", func() {
    49  		It("fails with usage when called without enough arguments", func() {
    50  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    51  
    52  			runCommand()
    53  			Expect(ui.Outputs()).To(ContainSubstrings(
    54  				[]string{"Incorrect Usage", "Requires", "argument"},
    55  			))
    56  
    57  		})
    58  
    59  		It("fails requirements when not logged in", func() {
    60  			Expect(runCommand("my-app", "none")).To(BeFalse())
    61  		})
    62  
    63  		It("fails if a space is not targeted", func() {
    64  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    65  			requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Failing{Message: "not targeting space"})
    66  			Expect(runCommand("my-app", "none")).To(BeFalse())
    67  		})
    68  	})
    69  
    70  	Describe("disable-ssh", func() {
    71  		var (
    72  			app models.Application
    73  		)
    74  
    75  		BeforeEach(func() {
    76  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    77  			requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{})
    78  
    79  			app = models.Application{}
    80  			app.Name = "my-app"
    81  			app.GUID = "my-app-guid"
    82  			app.EnableSSH = true
    83  
    84  			applicationReq := new(requirementsfakes.FakeApplicationRequirement)
    85  			applicationReq.GetApplicationReturns(app)
    86  			requirementsFactory.NewApplicationRequirementReturns(applicationReq)
    87  		})
    88  
    89  		Context("when enable_ssh is already set to the false", func() {
    90  			BeforeEach(func() {
    91  				app.EnableSSH = false
    92  				applicationReq := new(requirementsfakes.FakeApplicationRequirement)
    93  				applicationReq.GetApplicationReturns(app)
    94  				requirementsFactory.NewApplicationRequirementReturns(applicationReq)
    95  			})
    96  
    97  			It("notifies the user", func() {
    98  				runCommand("my-app")
    99  
   100  				Expect(ui.Outputs()).To(ContainSubstrings([]string{"ssh support is already disabled for 'my-app'"}))
   101  			})
   102  		})
   103  
   104  		Context("Updating enable_ssh when not already set to false", func() {
   105  			Context("Update successfully", func() {
   106  				BeforeEach(func() {
   107  					app = models.Application{}
   108  					app.Name = "my-app"
   109  					app.GUID = "my-app-guid"
   110  					app.EnableSSH = false
   111  
   112  					appRepo.UpdateReturns(app, nil)
   113  				})
   114  
   115  				It("updates the app's enable_ssh", func() {
   116  					runCommand("my-app")
   117  
   118  					Expect(appRepo.UpdateCallCount()).To(Equal(1))
   119  					appGUID, params := appRepo.UpdateArgsForCall(0)
   120  					Expect(appGUID).To(Equal("my-app-guid"))
   121  					Expect(*params.EnableSSH).To(BeFalse())
   122  					Expect(ui.Outputs()).To(ContainSubstrings([]string{"Disabling ssh support for 'my-app'"}))
   123  					Expect(ui.Outputs()).To(ContainSubstrings([]string{"OK"}))
   124  				})
   125  			})
   126  
   127  			Context("Update fails", func() {
   128  				It("notifies user of any api error", func() {
   129  					appRepo.UpdateReturns(models.Application{}, errors.New("Error updating app."))
   130  					runCommand("my-app")
   131  
   132  					Expect(appRepo.UpdateCallCount()).To(Equal(1))
   133  					Expect(ui.Outputs()).To(ContainSubstrings(
   134  						[]string{"FAILED"},
   135  						[]string{"Error disabling ssh support"},
   136  					))
   137  
   138  				})
   139  
   140  				It("notifies user when updated result is not in the desired state", func() {
   141  					app = models.Application{}
   142  					app.Name = "my-app"
   143  					app.GUID = "my-app-guid"
   144  					app.EnableSSH = true
   145  					appRepo.UpdateReturns(app, nil)
   146  
   147  					runCommand("my-app")
   148  
   149  					Expect(appRepo.UpdateCallCount()).To(Equal(1))
   150  					Expect(ui.Outputs()).To(ContainSubstrings(
   151  						[]string{"FAILED"},
   152  						[]string{"ssh support is not disabled for my-app"},
   153  					))
   154  
   155  				})
   156  			})
   157  		})
   158  	})
   159  })