github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/organization/orgs_test.go (about)

     1  package organization_test
     2  
     3  import (
     4  	"os"
     5  
     6  	"code.cloudfoundry.org/cli/cf/api/organizations/organizationsfakes"
     7  	"code.cloudfoundry.org/cli/cf/commandregistry"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/flags"
    10  	"code.cloudfoundry.org/cli/cf/models"
    11  	"code.cloudfoundry.org/cli/cf/requirements"
    12  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    13  	"code.cloudfoundry.org/cli/cf/trace/tracefakes"
    14  	"code.cloudfoundry.org/cli/plugin/models"
    15  	testcmd "code.cloudfoundry.org/cli/util/testhelpers/commands"
    16  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    17  	testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  
    21  	"code.cloudfoundry.org/cli/cf/commands/organization"
    22  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    23  )
    24  
    25  var _ = Describe("orgs command", func() {
    26  	var (
    27  		ui                  *testterm.FakeUI
    28  		orgRepo             *organizationsfakes.FakeOrganizationRepository
    29  		configRepo          coreconfig.Repository
    30  		requirementsFactory *requirementsfakes.FakeFactory
    31  		deps                commandregistry.Dependency
    32  	)
    33  
    34  	updateCommandDependency := func(pluginCall bool) {
    35  		deps.UI = ui
    36  		deps.Config = configRepo
    37  		deps.RepoLocator = deps.RepoLocator.SetOrganizationRepository(orgRepo)
    38  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("orgs").SetDependency(deps, pluginCall))
    39  	}
    40  
    41  	runCommand := func(args ...string) bool {
    42  		return testcmd.RunCLICommand("orgs", args, requirementsFactory, updateCommandDependency, false, ui)
    43  	}
    44  
    45  	BeforeEach(func() {
    46  		ui = &testterm.FakeUI{}
    47  		configRepo = testconfig.NewRepositoryWithDefaults()
    48  		orgRepo = new(organizationsfakes.FakeOrganizationRepository)
    49  		requirementsFactory = new(requirementsfakes.FakeFactory)
    50  		requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    51  
    52  		deps = commandregistry.NewDependency(os.Stdout, new(tracefakes.FakePrinter), "")
    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  		Context("when arguments are provided", func() {
    63  			var cmd commandregistry.Command
    64  			var flagContext flags.FlagContext
    65  
    66  			BeforeEach(func() {
    67  				cmd = &organization.ListOrgs{}
    68  				cmd.SetDependency(deps, false)
    69  				flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
    70  			})
    71  
    72  			It("should fail with usage", func() {
    73  				flagContext.Parse("blahblah")
    74  
    75  				reqs, err := cmd.Requirements(requirementsFactory, flagContext)
    76  				Expect(err).NotTo(HaveOccurred())
    77  
    78  				err = testcmd.RunRequirements(reqs)
    79  				Expect(err).To(HaveOccurred())
    80  				Expect(err.Error()).To(ContainSubstring("Incorrect Usage"))
    81  				Expect(err.Error()).To(ContainSubstring("No argument required"))
    82  			})
    83  		})
    84  	})
    85  
    86  	Describe("when invoked by a plugin", func() {
    87  		var (
    88  			pluginOrgsModel []plugin_models.GetOrgs_Model
    89  		)
    90  
    91  		BeforeEach(func() {
    92  			org1 := models.Organization{}
    93  			org1.Name = "Organization-1"
    94  			org1.GUID = "org-1-guid"
    95  
    96  			org2 := models.Organization{}
    97  			org2.Name = "Organization-2"
    98  
    99  			org3 := models.Organization{}
   100  			org3.Name = "Organization-3"
   101  
   102  			orgRepo.ListOrgsReturns([]models.Organization{org1, org2, org3}, nil)
   103  
   104  			pluginOrgsModel = []plugin_models.GetOrgs_Model{}
   105  			deps.PluginModels.Organizations = &pluginOrgsModel
   106  		})
   107  
   108  		It("populates the plugin models upon execution", func() {
   109  			testcmd.RunCLICommand("orgs", []string{}, requirementsFactory, updateCommandDependency, true, ui)
   110  			Expect(pluginOrgsModel[0].Name).To(Equal("Organization-1"))
   111  			Expect(pluginOrgsModel[0].Guid).To(Equal("org-1-guid"))
   112  			Expect(pluginOrgsModel[1].Name).To(Equal("Organization-2"))
   113  			Expect(pluginOrgsModel[2].Name).To(Equal("Organization-3"))
   114  		})
   115  	})
   116  
   117  	Context("when there are orgs to be listed", func() {
   118  		BeforeEach(func() {
   119  			org1 := models.Organization{}
   120  			org1.Name = "Organization-1"
   121  
   122  			org2 := models.Organization{}
   123  			org2.Name = "Organization-2"
   124  
   125  			org3 := models.Organization{}
   126  			org3.Name = "Organization-3"
   127  
   128  			orgRepo.ListOrgsReturns([]models.Organization{org1, org2, org3}, nil)
   129  		})
   130  
   131  		It("tries to get the organizations", func() {
   132  			runCommand()
   133  			Expect(orgRepo.ListOrgsCallCount()).To(Equal(1))
   134  			Expect(orgRepo.ListOrgsArgsForCall(0)).To(Equal(0))
   135  		})
   136  
   137  		It("lists orgs", func() {
   138  			runCommand()
   139  
   140  			Expect(ui.Outputs()).To(ContainSubstrings(
   141  				[]string{"Getting orgs as my-user"},
   142  				[]string{"Organization-1"},
   143  				[]string{"Organization-2"},
   144  				[]string{"Organization-3"},
   145  			))
   146  		})
   147  	})
   148  
   149  	It("tells the user when no orgs were found", func() {
   150  		orgRepo.ListOrgsReturns([]models.Organization{}, nil)
   151  		runCommand()
   152  
   153  		Expect(ui.Outputs()).To(ContainSubstrings(
   154  			[]string{"Getting orgs as my-user"},
   155  			[]string{"No orgs found"},
   156  		))
   157  	})
   158  })