github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/route/create_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/configuration/core_config"
     6  	"github.com/cloudfoundry/cli/cf/models"
     7  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
     8  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
     9  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    10  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    11  
    12  	"github.com/cloudfoundry/cli/cf/command_registry"
    13  	. "github.com/cloudfoundry/cli/cf/commands/route"
    14  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  )
    18  
    19  var _ = Describe("create-route command", func() {
    20  	var (
    21  		ui                  *testterm.FakeUI
    22  		routeRepo           *testapi.FakeRouteRepository
    23  		requirementsFactory *testreq.FakeReqFactory
    24  		config              core_config.Repository
    25  		deps                command_registry.Dependency
    26  	)
    27  
    28  	updateCommandDependency := func(pluginCall bool) {
    29  		deps.Ui = ui
    30  		deps.RepoLocator = deps.RepoLocator.SetRouteRepository(routeRepo)
    31  		deps.Config = config
    32  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("create-route").SetDependency(deps, pluginCall))
    33  	}
    34  
    35  	BeforeEach(func() {
    36  		ui = &testterm.FakeUI{}
    37  		routeRepo = &testapi.FakeRouteRepository{}
    38  		requirementsFactory = &testreq.FakeReqFactory{}
    39  		config = testconfig.NewRepositoryWithDefaults()
    40  	})
    41  
    42  	runCommand := func(args ...string) bool {
    43  		return testcmd.RunCliCommand("create-route", args, requirementsFactory, updateCommandDependency, false)
    44  	}
    45  
    46  	Describe("requirements", func() {
    47  		It("fails when not logged in", func() {
    48  			requirementsFactory.TargetedOrgSuccess = true
    49  
    50  			Expect(runCommand("my-space", "example.com", "-n", "foo")).To(BeFalse())
    51  		})
    52  
    53  		It("fails when an org is not targeted", func() {
    54  			requirementsFactory.LoginSuccess = true
    55  
    56  			Expect(runCommand("my-space", "example.com", "-n", "foo")).To(BeFalse())
    57  		})
    58  
    59  		It("fails with usage when not provided two args", func() {
    60  			requirementsFactory.LoginSuccess = true
    61  			requirementsFactory.TargetedOrgSuccess = true
    62  
    63  			runCommand("my-space")
    64  			Expect(ui.Outputs).To(ContainSubstrings(
    65  				[]string{"Incorrect Usage", "Requires", "arguments"},
    66  			))
    67  		})
    68  	})
    69  
    70  	Context("when logged in, targeted a space and given a domain that exists", func() {
    71  		BeforeEach(func() {
    72  			requirementsFactory.LoginSuccess = true
    73  			requirementsFactory.TargetedOrgSuccess = true
    74  			requirementsFactory.Domain = models.DomainFields{
    75  				Guid: "domain-guid",
    76  				Name: "example.com",
    77  			}
    78  			requirementsFactory.Space = models.Space{SpaceFields: models.SpaceFields{
    79  				Guid: "my-space-guid",
    80  				Name: "my-space",
    81  			}}
    82  		})
    83  
    84  		It("creates routes, obviously", func() {
    85  			runCommand("-n", "host", "my-space", "example.com")
    86  
    87  			Expect(ui.Outputs).To(ContainSubstrings(
    88  				[]string{"Creating route", "host.example.com", "my-org", "my-space", "my-user"},
    89  				[]string{"OK"},
    90  			))
    91  
    92  			Expect(routeRepo.CreateInSpaceHost).To(Equal("host"))
    93  			Expect(routeRepo.CreateInSpaceDomainGuid).To(Equal("domain-guid"))
    94  			Expect(routeRepo.CreateInSpaceSpaceGuid).To(Equal("my-space-guid"))
    95  		})
    96  
    97  		It("is idempotent", func() {
    98  			routeRepo.CreateInSpaceErr = true
    99  			routeRepo.FindByHostAndDomainReturns.Route = models.Route{
   100  				Space:  requirementsFactory.Space.SpaceFields,
   101  				Guid:   "my-route-guid",
   102  				Host:   "host",
   103  				Domain: requirementsFactory.Domain,
   104  			}
   105  
   106  			runCommand("-n", "host", "my-space", "example.com")
   107  
   108  			Expect(ui.Outputs).To(ContainSubstrings(
   109  				[]string{"Creating route"},
   110  				[]string{"OK"},
   111  				[]string{"host.example.com", "already exists"},
   112  			))
   113  
   114  			Expect(routeRepo.CreateInSpaceHost).To(Equal("host"))
   115  			Expect(routeRepo.CreateInSpaceDomainGuid).To(Equal("domain-guid"))
   116  			Expect(routeRepo.CreateInSpaceSpaceGuid).To(Equal("my-space-guid"))
   117  		})
   118  
   119  		Describe("RouteCreator interface", func() {
   120  			It("creates a route, given a domain and space", func() {
   121  				createdRoute := models.Route{}
   122  				createdRoute.Host = "my-host"
   123  				createdRoute.Guid = "my-route-guid"
   124  				routeRepo = &testapi.FakeRouteRepository{
   125  					CreateInSpaceCreatedRoute: createdRoute,
   126  				}
   127  
   128  				updateCommandDependency(false)
   129  				c := command_registry.Commands.FindCommand("create-route")
   130  				cmd := c.(RouteCreator)
   131  				route, apiErr := cmd.CreateRoute("my-host", requirementsFactory.Domain, requirementsFactory.Space.SpaceFields)
   132  
   133  				Expect(apiErr).NotTo(HaveOccurred())
   134  				Expect(route.Guid).To(Equal(createdRoute.Guid))
   135  				Expect(ui.Outputs).To(ContainSubstrings(
   136  					[]string{"Creating route", "my-host.example.com", "my-org", "my-space", "my-user"},
   137  					[]string{"OK"},
   138  				))
   139  
   140  				Expect(routeRepo.CreateInSpaceHost).To(Equal("my-host"))
   141  				Expect(routeRepo.CreateInSpaceDomainGuid).To(Equal("domain-guid"))
   142  				Expect(routeRepo.CreateInSpaceSpaceGuid).To(Equal("my-space-guid"))
   143  			})
   144  		})
   145  	})
   146  })