github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/cf/commands/space/allow_space_ssh_test.go (about)

     1  package space_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/cf/api/spaces/spacesfakes"
     7  	"code.cloudfoundry.org/cli/cf/commandregistry"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/models"
    10  	"code.cloudfoundry.org/cli/cf/requirements"
    11  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    12  	testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands"
    13  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    14  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    15  
    16  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    17  	. "github.com/onsi/ginkgo"
    18  	. "github.com/onsi/gomega"
    19  )
    20  
    21  var _ = Describe("allow-space-ssh command", func() {
    22  	var (
    23  		ui                  *testterm.FakeUI
    24  		requirementsFactory *requirementsfakes.FakeFactory
    25  		spaceRepo           *spacesfakes.FakeSpaceRepository
    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  		spaceRepo = new(spacesfakes.FakeSpaceRepository)
    35  	})
    36  
    37  	updateCommandDependency := func(pluginCall bool) {
    38  		deps.UI = ui
    39  		deps.Config = configRepo
    40  		deps.RepoLocator = deps.RepoLocator.SetSpaceRepository(spaceRepo)
    41  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("allow-space-ssh").SetDependency(deps, pluginCall))
    42  	}
    43  
    44  	runCommand := func(args ...string) bool {
    45  		return testcmd.RunCLICommand("allow-space-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  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    61  			Expect(runCommand("my-space")).To(BeFalse())
    62  		})
    63  
    64  		It("does not pass requirements if org is not targeted", func() {
    65  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    66  			targetedOrgReq := new(requirementsfakes.FakeTargetedOrgRequirement)
    67  			targetedOrgReq.ExecuteReturns(errors.New("no org targeted"))
    68  			requirementsFactory.NewTargetedOrgRequirementReturns(targetedOrgReq)
    69  
    70  			Expect(runCommand("my-space")).To(BeFalse())
    71  		})
    72  
    73  		It("does not pass requirements if space does not exist", func() {
    74  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    75  			requirementsFactory.NewTargetedOrgRequirementReturns(new(requirementsfakes.FakeTargetedOrgRequirement))
    76  			spaceReq := new(requirementsfakes.FakeSpaceRequirement)
    77  			spaceReq.ExecuteReturns(errors.New("no space"))
    78  			requirementsFactory.NewSpaceRequirementReturns(spaceReq)
    79  
    80  			Expect(runCommand("my-space")).To(BeFalse())
    81  		})
    82  	})
    83  
    84  	Describe("allow-space-ssh", func() {
    85  		var space models.Space
    86  
    87  		BeforeEach(func() {
    88  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    89  			requirementsFactory.NewTargetedOrgRequirementReturns(new(requirementsfakes.FakeTargetedOrgRequirement))
    90  
    91  			space = models.Space{}
    92  			space.Name = "the-space-name"
    93  			space.GUID = "the-space-guid"
    94  			spaceReq := new(requirementsfakes.FakeSpaceRequirement)
    95  			spaceReq.GetSpaceReturns(space)
    96  			requirementsFactory.NewSpaceRequirementReturns(spaceReq)
    97  		})
    98  
    99  		Context("when allow_ssh is already set to the true", func() {
   100  			BeforeEach(func() {
   101  				space.AllowSSH = true
   102  				spaceReq := new(requirementsfakes.FakeSpaceRequirement)
   103  				spaceReq.GetSpaceReturns(space)
   104  				requirementsFactory.NewSpaceRequirementReturns(spaceReq)
   105  			})
   106  
   107  			It("notifies the user", func() {
   108  				runCommand("the-space-name")
   109  
   110  				Expect(ui.Outputs()).To(ContainSubstrings([]string{"ssh support is already enabled in space 'the-space-name'"}))
   111  			})
   112  		})
   113  
   114  		Context("Updating allow_ssh when not already set to true", func() {
   115  			Context("Update successfully", func() {
   116  				BeforeEach(func() {
   117  					space.AllowSSH = false
   118  					spaceReq := new(requirementsfakes.FakeSpaceRequirement)
   119  					spaceReq.GetSpaceReturns(space)
   120  					requirementsFactory.NewSpaceRequirementReturns(spaceReq)
   121  				})
   122  
   123  				It("updates the space's allow_ssh", func() {
   124  					runCommand("the-space-name")
   125  
   126  					Expect(spaceRepo.SetAllowSSHCallCount()).To(Equal(1))
   127  					spaceGUID, allow := spaceRepo.SetAllowSSHArgsForCall(0)
   128  					Expect(spaceGUID).To(Equal("the-space-guid"))
   129  					Expect(allow).To(Equal(true))
   130  					Expect(ui.Outputs()).To(ContainSubstrings([]string{"Enabling ssh support for space 'the-space-name'"}))
   131  					Expect(ui.Outputs()).To(ContainSubstrings([]string{"OK"}))
   132  				})
   133  			})
   134  
   135  			Context("Update fails", func() {
   136  				It("notifies user of any api error", func() {
   137  					spaceRepo.SetAllowSSHReturns(errors.New("api error"))
   138  					runCommand("the-space-name")
   139  
   140  					Expect(ui.Outputs()).To(ContainSubstrings(
   141  						[]string{"FAILED"},
   142  						[]string{"Error", "api error"},
   143  					))
   144  
   145  				})
   146  			})
   147  
   148  		})
   149  	})
   150  
   151  })