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

     1  package route_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  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
     9  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    10  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    11  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  
    15  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    16  )
    17  
    18  var _ = Describe("routes command", func() {
    19  	var (
    20  		ui                  *testterm.FakeUI
    21  		routeRepo           *testapi.FakeRouteRepository
    22  		configRepo          core_config.Repository
    23  		requirementsFactory *testreq.FakeReqFactory
    24  		deps                command_registry.Dependency
    25  	)
    26  
    27  	updateCommandDependency := func(pluginCall bool) {
    28  		deps.Ui = ui
    29  		deps.RepoLocator = deps.RepoLocator.SetRouteRepository(routeRepo)
    30  		deps.Config = configRepo
    31  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("routes").SetDependency(deps, pluginCall))
    32  	}
    33  
    34  	BeforeEach(func() {
    35  		ui = &testterm.FakeUI{}
    36  		configRepo = testconfig.NewRepositoryWithDefaults()
    37  		requirementsFactory = &testreq.FakeReqFactory{
    38  			LoginSuccess:         true,
    39  			TargetedSpaceSuccess: true,
    40  		}
    41  		routeRepo = &testapi.FakeRouteRepository{}
    42  	})
    43  
    44  	runCommand := func(args ...string) bool {
    45  		return testcmd.RunCliCommand("routes", args, requirementsFactory, updateCommandDependency, false)
    46  	}
    47  
    48  	Describe("login requirements", func() {
    49  		It("fails if the user is not logged in", func() {
    50  			requirementsFactory.LoginSuccess = false
    51  			Expect(runCommand()).To(BeFalse())
    52  		})
    53  
    54  		It("fails when an org and space is not targeted", func() {
    55  			requirementsFactory.TargetedSpaceSuccess = false
    56  
    57  			Expect(runCommand()).To(BeFalse())
    58  		})
    59  		It("should fail with usage when provided any arguments", func() {
    60  			requirementsFactory.LoginSuccess = true
    61  			requirementsFactory.TargetedSpaceSuccess = true
    62  			Expect(runCommand("blahblah")).To(BeFalse())
    63  			Expect(ui.Outputs).To(ContainSubstrings(
    64  				[]string{"Incorrect Usage", "No argument required"},
    65  			))
    66  		})
    67  	})
    68  
    69  	Context("when there are routes", func() {
    70  		BeforeEach(func() {
    71  			domain := models.DomainFields{Name: "example.com"}
    72  			domain2 := models.DomainFields{Name: "cookieclicker.co"}
    73  
    74  			app1 := models.ApplicationFields{Name: "dora"}
    75  			app2 := models.ApplicationFields{Name: "bora"}
    76  
    77  			route := models.Route{}
    78  			route.Host = "hostname-1"
    79  			route.Domain = domain
    80  			route.Apps = []models.ApplicationFields{app1}
    81  
    82  			route2 := models.Route{}
    83  			route2.Host = "hostname-2"
    84  			route2.Domain = domain2
    85  			route2.Apps = []models.ApplicationFields{app1, app2}
    86  			routeRepo.Routes = []models.Route{route, route2}
    87  		})
    88  
    89  		It("lists routes", func() {
    90  			runCommand()
    91  
    92  			Expect(ui.Outputs).To(ContainSubstrings(
    93  				[]string{"Getting routes", "my-user"},
    94  				[]string{"host", "domain", "apps"},
    95  				[]string{"hostname-1", "example.com", "dora"},
    96  				[]string{"hostname-2", "cookieclicker.co", "dora", "bora"},
    97  			))
    98  		})
    99  	})
   100  
   101  	Context("when there are routes in different spaces", func() {
   102  		BeforeEach(func() {
   103  			space1 := models.SpaceFields{Name: "space-1"}
   104  			space2 := models.SpaceFields{Name: "space-2"}
   105  
   106  			domain := models.DomainFields{Name: "example.com"}
   107  			domain2 := models.DomainFields{Name: "cookieclicker.co"}
   108  
   109  			app1 := models.ApplicationFields{Name: "dora"}
   110  			app2 := models.ApplicationFields{Name: "bora"}
   111  
   112  			route := models.Route{}
   113  			route.Host = "hostname-1"
   114  			route.Domain = domain
   115  			route.Apps = []models.ApplicationFields{app1}
   116  			route.Space = space1
   117  
   118  			route2 := models.Route{}
   119  			route2.Host = "hostname-2"
   120  			route2.Domain = domain2
   121  			route2.Apps = []models.ApplicationFields{app1, app2}
   122  			route2.Space = space2
   123  			routeRepo.Routes = []models.Route{route, route2}
   124  		})
   125  
   126  		It("lists routes at orglevel", func() {
   127  			runCommand("--orglevel")
   128  
   129  			Expect(ui.Outputs).To(ContainSubstrings(
   130  				[]string{"Getting routes", "my-user"},
   131  				[]string{"space", "host", "domain", "apps"},
   132  				[]string{"space-1", "hostname-1", "example.com", "dora"},
   133  				[]string{"space-2", "hostname-2", "cookieclicker.co", "dora", "bora"},
   134  			))
   135  		})
   136  
   137  	})
   138  	Context("when there are not routes", func() {
   139  		It("tells the user when no routes were found", func() {
   140  			runCommand()
   141  
   142  			Expect(ui.Outputs).To(ContainSubstrings(
   143  				[]string{"Getting routes"},
   144  				[]string{"No routes found"},
   145  			))
   146  		})
   147  	})
   148  
   149  	Context("when there is an error listing routes", func() {
   150  		BeforeEach(func() {
   151  			routeRepo.ListErr = true
   152  		})
   153  
   154  		It("returns an error to the user", func() {
   155  			runCommand()
   156  
   157  			Expect(ui.Outputs).To(ContainSubstrings(
   158  				[]string{"Getting routes"},
   159  				[]string{"FAILED"},
   160  			))
   161  		})
   162  	})
   163  })