github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/space/disallow_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/util/testhelpers/commands"
    13  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    14  	testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal"
    15  
    16  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    17  	. "github.com/onsi/ginkgo"
    18  	. "github.com/onsi/gomega"
    19  )
    20  
    21  var _ = Describe("disallow-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("disallow-space-ssh").SetDependency(deps, pluginCall))
    42  	}
    43  
    44  	runCommand := func(args ...string) bool {
    45  		return testcmd.RunCLICommand("disallow-space-ssh", args, requirementsFactory, updateCommandDependency, false, ui)
    46  	}
    47  
    48  	Describe("requirements", func() {
    49  		BeforeEach(func() {
    50  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    51  		})
    52  
    53  		It("fails with usage when called without enough arguments", func() {
    54  			runCommand()
    55  			Expect(ui.Outputs()).To(ContainSubstrings(
    56  				[]string{"Incorrect Usage", "Requires", "argument"},
    57  			))
    58  
    59  		})
    60  
    61  		It("fails requirements when not logged in", func() {
    62  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    63  			Expect(runCommand("my-space")).To(BeFalse())
    64  		})
    65  
    66  		It("does not pass requirements if org is not targeted", func() {
    67  			targetedOrgReq := new(requirementsfakes.FakeTargetedOrgRequirement)
    68  			targetedOrgReq.ExecuteReturns(errors.New("no org targeted"))
    69  			requirementsFactory.NewTargetedOrgRequirementReturns(targetedOrgReq)
    70  
    71  			Expect(runCommand("my-space")).To(BeFalse())
    72  		})
    73  
    74  		It("does not pass requirements if space does not exist", func() {
    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("disallow-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  		})
    95  
    96  		Context("when allow_ssh is already set to the false", func() {
    97  			BeforeEach(func() {
    98  				space.AllowSSH = false
    99  				spaceReq := new(requirementsfakes.FakeSpaceRequirement)
   100  				spaceReq.GetSpaceReturns(space)
   101  				requirementsFactory.NewSpaceRequirementReturns(spaceReq)
   102  			})
   103  
   104  			It("notifies the user", func() {
   105  				runCommand("the-space-name")
   106  
   107  				Expect(ui.Outputs()).To(ContainSubstrings([]string{"ssh support is already disabled in space 'the-space-name'"}))
   108  			})
   109  		})
   110  
   111  		Context("Updating allow_ssh when not already set to false", func() {
   112  			Context("Update successfully", func() {
   113  				BeforeEach(func() {
   114  					space.AllowSSH = true
   115  					spaceReq := new(requirementsfakes.FakeSpaceRequirement)
   116  					spaceReq.GetSpaceReturns(space)
   117  					requirementsFactory.NewSpaceRequirementReturns(spaceReq)
   118  				})
   119  
   120  				It("updates the space's allow_ssh", func() {
   121  					runCommand("the-space-name")
   122  
   123  					Expect(spaceRepo.SetAllowSSHCallCount()).To(Equal(1))
   124  					spaceGUID, allow := spaceRepo.SetAllowSSHArgsForCall(0)
   125  					Expect(spaceGUID).To(Equal("the-space-guid"))
   126  					Expect(allow).To(Equal(false))
   127  					Expect(ui.Outputs()).To(ContainSubstrings([]string{"Disabling ssh support for space 'the-space-name'"}))
   128  					Expect(ui.Outputs()).To(ContainSubstrings([]string{"OK"}))
   129  				})
   130  			})
   131  
   132  			Context("Update fails", func() {
   133  				BeforeEach(func() {
   134  					space.AllowSSH = true
   135  					spaceReq := new(requirementsfakes.FakeSpaceRequirement)
   136  					spaceReq.GetSpaceReturns(space)
   137  					requirementsFactory.NewSpaceRequirementReturns(spaceReq)
   138  				})
   139  
   140  				It("notifies user of any api error", func() {
   141  					spaceRepo.SetAllowSSHReturns(errors.New("api error"))
   142  					runCommand("the-space-name")
   143  
   144  					Expect(ui.Outputs()).To(ContainSubstrings(
   145  						[]string{"FAILED"},
   146  						[]string{"Error", "api error"},
   147  					))
   148  
   149  				})
   150  			})
   151  
   152  		})
   153  	})
   154  
   155  })