github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/cf/commands/routergroups/router_groups_test.go (about)

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