github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/routergroups/router_groups_test.go (about)

     1  package routergroups_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/cf/api"
     7  	"code.cloudfoundry.org/cli/cf/api/apifakes"
     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/requirementsfakes"
    13  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    14  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    15  
    16  	"code.cloudfoundry.org/cli/cf/commands/routergroups"
    17  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  )
    21  
    22  var _ = Describe("RouterGroups", func() {
    23  
    24  	var (
    25  		ui             *testterm.FakeUI
    26  		routingAPIRepo *apifakes.FakeRoutingAPIRepository
    27  		deps           commandregistry.Dependency
    28  		cmd            *routergroups.RouterGroups
    29  		flagContext    flags.FlagContext
    30  		repoLocator    api.RepositoryLocator
    31  		config         coreconfig.Repository
    32  
    33  		requirementsFactory      *requirementsfakes.FakeFactory
    34  		minAPIVersionRequirement *requirementsfakes.FakeRequirement
    35  		loginRequirement         *requirementsfakes.FakeRequirement
    36  	)
    37  
    38  	BeforeEach(func() {
    39  		ui = new(testterm.FakeUI)
    40  		routingAPIRepo = new(apifakes.FakeRoutingAPIRepository)
    41  		config = testconfig.NewRepositoryWithDefaults()
    42  		repoLocator = api.RepositoryLocator{}.SetRoutingAPIRepository(routingAPIRepo)
    43  		deps = commandregistry.Dependency{
    44  			UI:          ui,
    45  			Config:      config,
    46  			RepoLocator: repoLocator,
    47  		}
    48  
    49  		minAPIVersionRequirement = new(requirementsfakes.FakeRequirement)
    50  		loginRequirement = new(requirementsfakes.FakeRequirement)
    51  
    52  		requirementsFactory = new(requirementsfakes.FakeFactory)
    53  		requirementsFactory.NewMinAPIVersionRequirementReturns(minAPIVersionRequirement)
    54  		requirementsFactory.NewLoginRequirementReturns(loginRequirement)
    55  
    56  		cmd = new(routergroups.RouterGroups)
    57  		cmd = cmd.SetDependency(deps, false).(*routergroups.RouterGroups)
    58  		flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
    59  	})
    60  
    61  	Describe("Requirements", func() {
    62  		It("fails if the user is not logged in", func() {
    63  			cmd.Requirements(requirementsFactory, flagContext)
    64  
    65  			Expect(requirementsFactory.NewLoginRequirementCallCount()).To(Equal(1))
    66  		})
    67  
    68  		It("should fail with usage", func() {
    69  			flagContext.Parse("blahblah")
    70  			cmd.Requirements(requirementsFactory, flagContext)
    71  
    72  			Expect(requirementsFactory.NewUsageRequirementCallCount()).To(Equal(1))
    73  		})
    74  	})
    75  
    76  	Describe("Execute", func() {
    77  		var err error
    78  
    79  		BeforeEach(func() {
    80  			err := flagContext.Parse()
    81  			Expect(err).NotTo(HaveOccurred())
    82  		})
    83  
    84  		JustBeforeEach(func() {
    85  			err = cmd.Execute(flagContext)
    86  		})
    87  
    88  		Context("when there are router groups", func() {
    89  			BeforeEach(func() {
    90  				routerGroups := models.RouterGroups{
    91  					models.RouterGroup{
    92  						GUID: "guid-0001",
    93  						Name: "default-router-group",
    94  						Type: "tcp",
    95  					},
    96  				}
    97  				routingAPIRepo.ListRouterGroupsStub = func(cb func(models.RouterGroup) bool) (apiErr error) {
    98  					for _, r := range routerGroups {
    99  						if !cb(r) {
   100  							break
   101  						}
   102  					}
   103  					return nil
   104  				}
   105  			})
   106  
   107  			It("lists router groups", func() {
   108  				Expect(err).NotTo(HaveOccurred())
   109  
   110  				Expect(ui.Outputs()).To(ContainSubstrings(
   111  					[]string{"Getting router groups", "my-user"},
   112  					[]string{"name", "type"},
   113  					[]string{"default-router-group", "tcp"},
   114  				))
   115  			})
   116  		})
   117  
   118  		Context("when there are no router groups", func() {
   119  			It("tells the user when no router groups were found", func() {
   120  				Expect(err).NotTo(HaveOccurred())
   121  
   122  				Expect(ui.Outputs()).To(ContainSubstrings(
   123  					[]string{"Getting router groups"},
   124  					[]string{"No router groups found"},
   125  				))
   126  			})
   127  		})
   128  
   129  		Context("when there is an error listing router groups", func() {
   130  			BeforeEach(func() {
   131  				routingAPIRepo.ListRouterGroupsReturns(errors.New("BOOM"))
   132  			})
   133  
   134  			It("returns an error to the user", func() {
   135  				Expect(ui.Outputs()).To(ContainSubstrings(
   136  					[]string{"Getting router groups"},
   137  				))
   138  
   139  				Expect(err).To(HaveOccurred())
   140  				errStr := err.Error()
   141  				Expect(errStr).To(ContainSubstring("BOOM"))
   142  				Expect(errStr).To(ContainSubstring("Failed fetching router groups"))
   143  			})
   144  		})
   145  	})
   146  })