github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/route/map_route_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/models"
     6  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
     7  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
     8  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
     9  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    10  
    11  	"github.com/cloudfoundry/cli/cf/command_registry"
    12  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
    13  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  )
    17  
    18  var _ = Describe("map-route command", func() {
    19  	var (
    20  		ui                  *testterm.FakeUI
    21  		configRepo          core_config.Repository
    22  		routeRepo           *testapi.FakeRouteRepository
    23  		requirementsFactory *testreq.FakeReqFactory
    24  		routeCreator        *testcmd.FakeRouteCreator
    25  		OriginalCreateRoute command_registry.Command
    26  		deps                command_registry.Dependency
    27  	)
    28  
    29  	updateCommandDependency := func(pluginCall bool) {
    30  		deps.Ui = ui
    31  		deps.RepoLocator = deps.RepoLocator.SetRouteRepository(routeRepo)
    32  		deps.Config = configRepo
    33  
    34  		//save original create-route and restore later
    35  		OriginalCreateRoute = command_registry.Commands.FindCommand("create-route")
    36  		//inject fake 'CreateRoute' into registry
    37  		command_registry.Register(routeCreator)
    38  
    39  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("map-route").SetDependency(deps, pluginCall))
    40  	}
    41  
    42  	BeforeEach(func() {
    43  		ui = new(testterm.FakeUI)
    44  		configRepo = testconfig.NewRepositoryWithDefaults()
    45  		routeRepo = new(testapi.FakeRouteRepository)
    46  		routeCreator = &testcmd.FakeRouteCreator{}
    47  		requirementsFactory = new(testreq.FakeReqFactory)
    48  	})
    49  
    50  	AfterEach(func() {
    51  		command_registry.Register(OriginalCreateRoute)
    52  	})
    53  
    54  	runCommand := func(args ...string) bool {
    55  		return testcmd.RunCliCommand("map-route", args, requirementsFactory, updateCommandDependency, false)
    56  	}
    57  
    58  	Describe("requirements", func() {
    59  		It("fails when not invoked with exactly two args", func() {
    60  			runCommand("whoops-all-crunchberries")
    61  			Expect(ui.Outputs).To(ContainSubstrings(
    62  				[]string{"Incorrect Usage", "Requires", "arguments"},
    63  			))
    64  		})
    65  
    66  		It("fails when not logged in", func() {
    67  			Expect(runCommand("whatever", "shuttup")).To(BeFalse())
    68  		})
    69  	})
    70  
    71  	Context("when the user is logged in", func() {
    72  		BeforeEach(func() {
    73  			domain := models.DomainFields{Guid: "my-domain-guid", Name: "example.com"}
    74  			route := models.Route{Guid: "my-route-guid", Host: "foo", Domain: domain}
    75  
    76  			app := models.Application{}
    77  			app.Guid = "my-app-guid"
    78  			app.Name = "my-app"
    79  
    80  			requirementsFactory.LoginSuccess = true
    81  			requirementsFactory.Application = app
    82  			requirementsFactory.Domain = domain
    83  			routeCreator.ReservedRoute = route
    84  		})
    85  
    86  		It("maps a route, obviously", func() {
    87  			passed := runCommand("-n", "my-host", "my-app", "my-domain.com")
    88  
    89  			Expect(ui.Outputs).To(ContainSubstrings(
    90  				[]string{"Adding route", "foo.example.com", "my-app", "my-org", "my-space", "my-user"},
    91  				[]string{"OK"},
    92  			))
    93  
    94  			Expect(routeRepo.BoundRouteGuid).To(Equal("my-route-guid"))
    95  			Expect(routeRepo.BoundAppGuid).To(Equal("my-app-guid"))
    96  			Expect(passed).To(BeTrue())
    97  			Expect(requirementsFactory.ApplicationName).To(Equal("my-app"))
    98  			Expect(requirementsFactory.DomainName).To(Equal("my-domain.com"))
    99  		})
   100  	})
   101  })