github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/space/spaces_test.go (about)

     1  package space_test
     2  
     3  import (
     4  	testapi "github.com/cloudfoundry/cli/cf/api/fakes"
     5  	"github.com/cloudfoundry/cli/cf/command_registry"
     6  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     7  	"github.com/cloudfoundry/cli/cf/models"
     8  	"github.com/cloudfoundry/cli/plugin/models"
     9  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
    10  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    11  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    12  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    13  
    14  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  )
    18  
    19  var _ = Describe("spaces command", func() {
    20  	var (
    21  		ui                  *testterm.FakeUI
    22  		requirementsFactory *testreq.FakeReqFactory
    23  		configRepo          core_config.Repository
    24  		spaceRepo           *testapi.FakeSpaceRepository
    25  
    26  		deps command_registry.Dependency
    27  	)
    28  
    29  	updateCommandDependency := func(pluginCall bool) {
    30  		deps.Ui = ui
    31  		deps.Config = configRepo
    32  		deps.RepoLocator = deps.RepoLocator.SetSpaceRepository(spaceRepo)
    33  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("spaces").SetDependency(deps, pluginCall))
    34  	}
    35  
    36  	BeforeEach(func() {
    37  		deps = command_registry.NewDependency()
    38  		ui = &testterm.FakeUI{}
    39  		spaceRepo = &testapi.FakeSpaceRepository{}
    40  		requirementsFactory = &testreq.FakeReqFactory{}
    41  		configRepo = testconfig.NewRepositoryWithDefaults()
    42  	})
    43  
    44  	runCommand := func(args ...string) bool {
    45  		return testcmd.RunCliCommand("spaces", args, requirementsFactory, updateCommandDependency, false)
    46  	}
    47  
    48  	Describe("requirements", func() {
    49  		It("fails when not logged in", func() {
    50  			requirementsFactory.TargetedOrgSuccess = true
    51  
    52  			Expect(runCommand()).To(BeFalse())
    53  		})
    54  
    55  		It("fails when an org is not targeted", func() {
    56  			requirementsFactory.LoginSuccess = true
    57  
    58  			Expect(runCommand()).To(BeFalse())
    59  		})
    60  		It("should fail with usage when provided any arguments", func() {
    61  			requirementsFactory.LoginSuccess = true
    62  
    63  			Expect(runCommand("blahblah")).To(BeFalse())
    64  			Expect(ui.Outputs).To(ContainSubstrings(
    65  				[]string{"Incorrect Usage", "No argument required"},
    66  			))
    67  		})
    68  	})
    69  
    70  	Describe("when invoked by a plugin", func() {
    71  		var (
    72  			pluginModels []plugin_models.GetSpaces_Model
    73  		)
    74  
    75  		BeforeEach(func() {
    76  			pluginModels = []plugin_models.GetSpaces_Model{}
    77  			deps.PluginModels.Spaces = &pluginModels
    78  
    79  			space := models.Space{}
    80  			space.Name = "space1"
    81  			space.Guid = "123"
    82  			space2 := models.Space{}
    83  			space2.Name = "space2"
    84  			space2.Guid = "456"
    85  			spaceRepo.Spaces = []models.Space{space, space2}
    86  
    87  			requirementsFactory.TargetedOrgSuccess = true
    88  			requirementsFactory.LoginSuccess = true
    89  
    90  		})
    91  
    92  		It("populates the plugin models upon execution", func() {
    93  			testcmd.RunCliCommand("spaces", []string{}, requirementsFactory, updateCommandDependency, true)
    94  			runCommand()
    95  			Ω(pluginModels[0].Name).To(Equal("space1"))
    96  			Ω(pluginModels[0].Guid).To(Equal("123"))
    97  			Ω(pluginModels[1].Name).To(Equal("space2"))
    98  			Ω(pluginModels[1].Guid).To(Equal("456"))
    99  		})
   100  	})
   101  
   102  	Context("when logged in and an org is targeted", func() {
   103  		BeforeEach(func() {
   104  			space := models.Space{}
   105  			space.Name = "space1"
   106  			space2 := models.Space{}
   107  			space2.Name = "space2"
   108  			space3 := models.Space{}
   109  			space3.Name = "space3"
   110  			spaceRepo.Spaces = []models.Space{space, space2, space3}
   111  			requirementsFactory.LoginSuccess = true
   112  			requirementsFactory.TargetedOrgSuccess = true
   113  		})
   114  
   115  		It("lists all of the spaces", func() {
   116  			runCommand()
   117  
   118  			Expect(ui.Outputs).To(ContainSubstrings(
   119  				[]string{"Getting spaces in org", "my-org", "my-user"},
   120  				[]string{"space1"},
   121  				[]string{"space2"},
   122  				[]string{"space3"},
   123  			))
   124  		})
   125  
   126  		Context("when there are no spaces", func() {
   127  			BeforeEach(func() {
   128  				spaceRepo.Spaces = []models.Space{}
   129  			})
   130  
   131  			It("politely tells the user", func() {
   132  				runCommand()
   133  				Expect(ui.Outputs).To(ContainSubstrings(
   134  					[]string{"Getting spaces in org", "my-org", "my-user"},
   135  					[]string{"No spaces found"},
   136  				))
   137  			})
   138  		})
   139  	})
   140  })