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

     1  package space_test
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  
     7  	"code.cloudfoundry.org/cli/cf/api/spaces/spacesfakes"
     8  	"code.cloudfoundry.org/cli/cf/commandregistry"
     9  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    10  	"code.cloudfoundry.org/cli/cf/flags"
    11  	"code.cloudfoundry.org/cli/cf/models"
    12  	"code.cloudfoundry.org/cli/cf/requirements"
    13  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    14  	"code.cloudfoundry.org/cli/cf/trace/tracefakes"
    15  	testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands"
    16  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    17  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    18  	"code.cloudfoundry.org/cli/plugin/models"
    19  
    20  	"code.cloudfoundry.org/cli/cf/commands/space"
    21  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    22  	. "github.com/onsi/ginkgo"
    23  	. "github.com/onsi/gomega"
    24  )
    25  
    26  var _ = Describe("spaces command", func() {
    27  	var (
    28  		ui                  *testterm.FakeUI
    29  		requirementsFactory *requirementsfakes.FakeFactory
    30  		configRepo          coreconfig.Repository
    31  		spaceRepo           *spacesfakes.FakeSpaceRepository
    32  
    33  		deps commandregistry.Dependency
    34  	)
    35  
    36  	updateCommandDependency := func(pluginCall bool) {
    37  		deps.UI = ui
    38  		deps.Config = configRepo
    39  		deps.RepoLocator = deps.RepoLocator.SetSpaceRepository(spaceRepo)
    40  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("spaces").SetDependency(deps, pluginCall))
    41  	}
    42  
    43  	BeforeEach(func() {
    44  		deps = commandregistry.NewDependency(os.Stdout, new(tracefakes.FakePrinter), "")
    45  		ui = &testterm.FakeUI{}
    46  		spaceRepo = new(spacesfakes.FakeSpaceRepository)
    47  		requirementsFactory = new(requirementsfakes.FakeFactory)
    48  		configRepo = testconfig.NewRepositoryWithDefaults()
    49  	})
    50  
    51  	runCommand := func(args ...string) bool {
    52  		return testcmd.RunCLICommand("spaces", args, requirementsFactory, updateCommandDependency, false, ui)
    53  	}
    54  
    55  	Describe("requirements", func() {
    56  		It("fails when not logged in", func() {
    57  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    58  
    59  			Expect(runCommand()).To(BeFalse())
    60  		})
    61  
    62  		It("fails when an org is not targeted", func() {
    63  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    64  			targetedOrgReq := new(requirementsfakes.FakeTargetedOrgRequirement)
    65  			targetedOrgReq.ExecuteReturns(errors.New("no org targeted"))
    66  			requirementsFactory.NewTargetedOrgRequirementReturns(targetedOrgReq)
    67  
    68  			Expect(runCommand()).To(BeFalse())
    69  		})
    70  
    71  		Context("when arguments are provided", func() {
    72  			var cmd commandregistry.Command
    73  			var flagContext flags.FlagContext
    74  
    75  			BeforeEach(func() {
    76  				cmd = &space.ListSpaces{}
    77  				cmd.SetDependency(deps, false)
    78  				flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
    79  			})
    80  
    81  			It("should fail with usage", func() {
    82  				flagContext.Parse("blahblah")
    83  
    84  				reqs, err := cmd.Requirements(requirementsFactory, flagContext)
    85  				Expect(err).NotTo(HaveOccurred())
    86  
    87  				err = testcmd.RunRequirements(reqs)
    88  				Expect(err).To(HaveOccurred())
    89  				Expect(err.Error()).To(ContainSubstring("Incorrect Usage"))
    90  				Expect(err.Error()).To(ContainSubstring("No argument required"))
    91  			})
    92  		})
    93  	})
    94  
    95  	listSpacesStub := func(spaces []models.Space) func(func(models.Space) bool) error {
    96  		return func(cb func(models.Space) bool) error {
    97  			var keepGoing bool
    98  			for _, s := range spaces {
    99  				keepGoing = cb(s)
   100  				if !keepGoing {
   101  					return nil
   102  				}
   103  			}
   104  			return nil
   105  		}
   106  	}
   107  
   108  	Describe("when invoked by a plugin", func() {
   109  		var (
   110  			pluginModels []plugin_models.GetSpaces_Model
   111  		)
   112  
   113  		BeforeEach(func() {
   114  			pluginModels = []plugin_models.GetSpaces_Model{}
   115  			deps.PluginModels.Spaces = &pluginModels
   116  
   117  			space := models.Space{}
   118  			space.Name = "space1"
   119  			space.GUID = "123"
   120  			space2 := models.Space{}
   121  			space2.Name = "space2"
   122  			space2.GUID = "456"
   123  			spaceRepo.ListSpacesStub = listSpacesStub([]models.Space{space, space2})
   124  
   125  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
   126  			requirementsFactory.NewTargetedOrgRequirementReturns(new(requirementsfakes.FakeTargetedOrgRequirement))
   127  		})
   128  
   129  		It("populates the plugin models upon execution", func() {
   130  			testcmd.RunCLICommand("spaces", []string{}, requirementsFactory, updateCommandDependency, true, ui)
   131  			runCommand()
   132  			Expect(pluginModels[0].Name).To(Equal("space1"))
   133  			Expect(pluginModels[0].Guid).To(Equal("123"))
   134  			Expect(pluginModels[1].Name).To(Equal("space2"))
   135  			Expect(pluginModels[1].Guid).To(Equal("456"))
   136  		})
   137  	})
   138  
   139  	Context("when logged in and an org is targeted", func() {
   140  		BeforeEach(func() {
   141  			space := models.Space{}
   142  			space.Name = "space1"
   143  			space2 := models.Space{}
   144  			space2.Name = "space2"
   145  			space3 := models.Space{}
   146  			space3.Name = "space3"
   147  			spaceRepo.ListSpacesStub = listSpacesStub([]models.Space{space, space2, space3})
   148  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
   149  			requirementsFactory.NewTargetedOrgRequirementReturns(new(requirementsfakes.FakeTargetedOrgRequirement))
   150  		})
   151  
   152  		It("lists all of the spaces", func() {
   153  			runCommand()
   154  
   155  			Expect(ui.Outputs()).To(ContainSubstrings(
   156  				[]string{"Getting spaces in org", "my-org", "my-user"},
   157  				[]string{"space1"},
   158  				[]string{"space2"},
   159  				[]string{"space3"},
   160  			))
   161  		})
   162  
   163  		Context("when there are no spaces", func() {
   164  			BeforeEach(func() {
   165  				spaceRepo.ListSpacesStub = listSpacesStub([]models.Space{})
   166  			})
   167  
   168  			It("politely tells the user", func() {
   169  				runCommand()
   170  				Expect(ui.Outputs()).To(ContainSubstrings(
   171  					[]string{"Getting spaces in org", "my-org", "my-user"},
   172  					[]string{"No spaces found"},
   173  				))
   174  			})
   175  		})
   176  	})
   177  })