github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/cf/commands/stacks_test.go (about)

     1  package commands_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/api/stacks/stacksfakes"
     5  	"code.cloudfoundry.org/cli/cf/commandregistry"
     6  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     7  	"code.cloudfoundry.org/cli/cf/flags"
     8  	"code.cloudfoundry.org/cli/cf/models"
     9  	"code.cloudfoundry.org/cli/cf/requirements"
    10  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    11  	testcmd "code.cloudfoundry.org/cli/util/testhelpers/commands"
    12  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    13  	testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  
    17  	"code.cloudfoundry.org/cli/cf/commands"
    18  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    19  )
    20  
    21  var _ = Describe("stacks command", func() {
    22  	var (
    23  		ui                  *testterm.FakeUI
    24  		repo                *stacksfakes.FakeStackRepository
    25  		config              coreconfig.Repository
    26  		requirementsFactory *requirementsfakes.FakeFactory
    27  		deps                commandregistry.Dependency
    28  	)
    29  
    30  	updateCommandDependency := func(pluginCall bool) {
    31  		deps.UI = ui
    32  		deps.Config = config
    33  		deps.RepoLocator = deps.RepoLocator.SetStackRepository(repo)
    34  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("stacks").SetDependency(deps, pluginCall))
    35  	}
    36  
    37  	BeforeEach(func() {
    38  		ui = &testterm.FakeUI{}
    39  		config = testconfig.NewRepositoryWithDefaults()
    40  		requirementsFactory = new(requirementsfakes.FakeFactory)
    41  		requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    42  		repo = new(stacksfakes.FakeStackRepository)
    43  	})
    44  
    45  	Describe("login requirements", func() {
    46  		It("fails if the user is not logged in", func() {
    47  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    48  
    49  			Expect(testcmd.RunCLICommand("stacks", []string{}, requirementsFactory, updateCommandDependency, false, ui)).To(BeFalse())
    50  		})
    51  
    52  		Context("when arguments are provided", func() {
    53  			var cmd commandregistry.Command
    54  			var flagContext flags.FlagContext
    55  
    56  			BeforeEach(func() {
    57  				cmd = &commands.ListStacks{}
    58  				cmd.SetDependency(deps, false)
    59  				flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
    60  			})
    61  
    62  			It("should fail with usage", func() {
    63  				flagContext.Parse("blahblah")
    64  
    65  				reqs, err := cmd.Requirements(requirementsFactory, flagContext)
    66  				Expect(err).NotTo(HaveOccurred())
    67  
    68  				err = testcmd.RunRequirements(reqs)
    69  				Expect(err).To(HaveOccurred())
    70  				Expect(err.Error()).To(ContainSubstring("Incorrect Usage"))
    71  				Expect(err.Error()).To(ContainSubstring("No argument required"))
    72  			})
    73  		})
    74  	})
    75  
    76  	It("lists the stacks", func() {
    77  		stack1 := models.Stack{
    78  			Name:        "Stack-1",
    79  			Description: "Stack 1 Description",
    80  		}
    81  		stack2 := models.Stack{
    82  			Name:        "Stack-2",
    83  			Description: "Stack 2 Description",
    84  		}
    85  
    86  		repo.FindAllReturns([]models.Stack{stack1, stack2}, nil)
    87  		testcmd.RunCLICommand("stacks", []string{}, requirementsFactory, updateCommandDependency, false, ui)
    88  
    89  		Expect(ui.Outputs()).To(ContainSubstrings(
    90  			[]string{"Getting stacks in org", "my-org", "my-space", "my-user"},
    91  			[]string{"OK"},
    92  			[]string{"Stack-1", "Stack 1 Description"},
    93  			[]string{"Stack-2", "Stack 2 Description"},
    94  		))
    95  	})
    96  })