github.com/cloudfoundry/cli@v7.1.0+incompatible/cf/commands/route/check_route_test.go (about)

     1  package route_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/cf/commandregistry"
     7  	"code.cloudfoundry.org/cli/cf/commands/route"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/flags"
    10  	"code.cloudfoundry.org/cli/cf/models"
    11  	"code.cloudfoundry.org/cli/cf/requirements"
    12  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    13  
    14  	"code.cloudfoundry.org/cli/cf/api/apifakes"
    15  
    16  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    17  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    18  
    19  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    20  	. "github.com/onsi/ginkgo"
    21  	. "github.com/onsi/gomega"
    22  )
    23  
    24  var _ = Describe("CheckRoute", func() {
    25  	var (
    26  		ui         *testterm.FakeUI
    27  		configRepo coreconfig.Repository
    28  		routeRepo  *apifakes.FakeRouteRepository
    29  		domainRepo *apifakes.FakeDomainRepository
    30  
    31  		cmd         commandregistry.Command
    32  		deps        commandregistry.Dependency
    33  		factory     *requirementsfakes.FakeFactory
    34  		flagContext flags.FlagContext
    35  
    36  		loginRequirement       requirements.Requirement
    37  		targetedOrgRequirement *requirementsfakes.FakeTargetedOrgRequirement
    38  	)
    39  
    40  	BeforeEach(func() {
    41  		ui = &testterm.FakeUI{}
    42  
    43  		configRepo = testconfig.NewRepositoryWithDefaults()
    44  		routeRepo = new(apifakes.FakeRouteRepository)
    45  		repoLocator := deps.RepoLocator.SetRouteRepository(routeRepo)
    46  
    47  		domainRepo = new(apifakes.FakeDomainRepository)
    48  		repoLocator = repoLocator.SetDomainRepository(domainRepo)
    49  
    50  		deps = commandregistry.Dependency{
    51  			UI:          ui,
    52  			Config:      configRepo,
    53  			RepoLocator: repoLocator,
    54  		}
    55  
    56  		cmd = &route.CheckRoute{}
    57  		cmd.SetDependency(deps, false)
    58  
    59  		flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
    60  
    61  		factory = new(requirementsfakes.FakeFactory)
    62  
    63  		loginRequirement = &passingRequirement{Name: "login-requirement"}
    64  		factory.NewLoginRequirementReturns(loginRequirement)
    65  
    66  		targetedOrgRequirement = new(requirementsfakes.FakeTargetedOrgRequirement)
    67  		factory.NewTargetedOrgRequirementReturns(targetedOrgRequirement)
    68  	})
    69  
    70  	Describe("Requirements", func() {
    71  		Context("when not provided exactly two args", func() {
    72  			BeforeEach(func() {
    73  				flagContext.Parse("app-name")
    74  			})
    75  
    76  			It("fails with usage", func() {
    77  				_, err := cmd.Requirements(factory, flagContext)
    78  				Expect(err).To(HaveOccurred())
    79  				Expect(ui.Outputs()).To(ContainSubstrings(
    80  					[]string{"FAILED"},
    81  					[]string{"Incorrect Usage. Requires host and domain as arguments"},
    82  				))
    83  			})
    84  		})
    85  
    86  		Context("when provided exactly two args", func() {
    87  			BeforeEach(func() {
    88  				flagContext.Parse("domain-name", "host-name")
    89  			})
    90  
    91  			It("returns a LoginRequirement", func() {
    92  				actualRequirements, err := cmd.Requirements(factory, flagContext)
    93  				Expect(err).NotTo(HaveOccurred())
    94  				Expect(factory.NewLoginRequirementCallCount()).To(Equal(1))
    95  				Expect(actualRequirements).To(ContainElement(loginRequirement))
    96  			})
    97  
    98  			It("returns a TargetedOrgRequirement", func() {
    99  				actualRequirements, err := cmd.Requirements(factory, flagContext)
   100  				Expect(err).NotTo(HaveOccurred())
   101  				Expect(factory.NewTargetedOrgRequirementCallCount()).To(Equal(1))
   102  				Expect(actualRequirements).To(ContainElement(targetedOrgRequirement))
   103  			})
   104  		})
   105  	})
   106  
   107  	Describe("Execute", func() {
   108  		var err error
   109  
   110  		BeforeEach(func() {
   111  			err := flagContext.Parse("host-name", "domain-name")
   112  			Expect(err).NotTo(HaveOccurred())
   113  			cmd.Requirements(factory, flagContext)
   114  			configRepo.SetOrganizationFields(models.OrganizationFields{
   115  				GUID: "fake-org-guid",
   116  				Name: "fake-org-name",
   117  			})
   118  		})
   119  
   120  		JustBeforeEach(func() {
   121  			err = cmd.Execute(flagContext)
   122  		})
   123  
   124  		It("tells the user that it is checking for the route", func() {
   125  			Expect(err).NotTo(HaveOccurred())
   126  			Expect(ui.Outputs()).To(ContainSubstrings(
   127  				[]string{"Checking for route"},
   128  			))
   129  		})
   130  
   131  		It("tries to find the domain", func() {
   132  			Expect(err).NotTo(HaveOccurred())
   133  			Expect(domainRepo.FindByNameInOrgCallCount()).To(Equal(1))
   134  
   135  			domainName, orgGUID := domainRepo.FindByNameInOrgArgsForCall(0)
   136  			Expect(domainName).To(Equal("domain-name"))
   137  			Expect(orgGUID).To(Equal("fake-org-guid"))
   138  		})
   139  
   140  		Context("when it finds the domain successfully", func() {
   141  			var actualDomain models.DomainFields
   142  
   143  			BeforeEach(func() {
   144  				actualDomain = models.DomainFields{
   145  					GUID: "domain-guid",
   146  					Name: "domain-name",
   147  				}
   148  				domainRepo.FindByNameInOrgReturns(actualDomain, nil)
   149  			})
   150  
   151  			It("checks if the route exists", func() {
   152  				Expect(err).NotTo(HaveOccurred())
   153  				Expect(routeRepo.CheckIfExistsCallCount()).To(Equal(1))
   154  				hostName, domain, path := routeRepo.CheckIfExistsArgsForCall(0)
   155  				Expect(hostName).To(Equal("host-name"))
   156  				Expect(actualDomain).To(Equal(domain))
   157  				Expect(path).To(Equal(""))
   158  			})
   159  
   160  			Context("when a path is passed", func() {
   161  				BeforeEach(func() {
   162  					flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
   163  					err := flagContext.Parse("hostname", "domain-name", "--path", "the-path")
   164  					Expect(err).NotTo(HaveOccurred())
   165  					cmd.Requirements(factory, flagContext)
   166  				})
   167  
   168  				It("checks if the route exists", func() {
   169  					Expect(err).NotTo(HaveOccurred())
   170  					Expect(routeRepo.CheckIfExistsCallCount()).To(Equal(1))
   171  					_, _, path := routeRepo.CheckIfExistsArgsForCall(0)
   172  					Expect(path).To(Equal("the-path"))
   173  				})
   174  
   175  				Context("when finding the route succeeds and the route exists", func() {
   176  					BeforeEach(func() {
   177  						routeRepo.CheckIfExistsReturns(true, nil)
   178  					})
   179  
   180  					It("tells the user the route exists", func() {
   181  						Expect(err).NotTo(HaveOccurred())
   182  						Expect(ui.Outputs()).To(ContainSubstrings([]string{"Route hostname.domain-name/the-path does exist"}))
   183  					})
   184  				})
   185  
   186  				Context("when finding the route succeeds and the route does not exist", func() {
   187  					BeforeEach(func() {
   188  						routeRepo.CheckIfExistsReturns(false, nil)
   189  					})
   190  
   191  					It("tells the user the route exists", func() {
   192  						Expect(err).NotTo(HaveOccurred())
   193  						Expect(ui.Outputs()).To(ContainSubstrings([]string{"Route hostname.domain-name/the-path does not exist"}))
   194  					})
   195  				})
   196  			})
   197  
   198  			Context("when finding the route succeeds and the route exists", func() {
   199  				BeforeEach(func() {
   200  					routeRepo.CheckIfExistsReturns(true, nil)
   201  				})
   202  
   203  				It("tells the user OK", func() {
   204  					Expect(err).NotTo(HaveOccurred())
   205  					Expect(ui.Outputs()).To(ContainSubstrings([]string{"OK"}))
   206  				})
   207  
   208  				It("tells the user the route exists", func() {
   209  					Expect(err).NotTo(HaveOccurred())
   210  					Expect(ui.Outputs()).To(ContainSubstrings([]string{"Route", "does exist"}))
   211  				})
   212  			})
   213  
   214  			Context("when finding the route succeeds and the route does not exist", func() {
   215  				BeforeEach(func() {
   216  					routeRepo.CheckIfExistsReturns(false, nil)
   217  				})
   218  
   219  				It("tells the user OK", func() {
   220  					Expect(err).NotTo(HaveOccurred())
   221  					Expect(ui.Outputs()).To(ContainSubstrings([]string{"OK"}))
   222  				})
   223  
   224  				It("tells the user the route does not exist", func() {
   225  					Expect(err).NotTo(HaveOccurred())
   226  					Expect(ui.Outputs()).To(ContainSubstrings([]string{"Route", "does not exist"}))
   227  				})
   228  			})
   229  
   230  			Context("when finding the route results in an error", func() {
   231  				BeforeEach(func() {
   232  					routeRepo.CheckIfExistsReturns(false, errors.New("check-if-exists-err"))
   233  				})
   234  
   235  				It("fails with error", func() {
   236  					Expect(err).To(HaveOccurred())
   237  					Expect(err.Error()).To(Equal("check-if-exists-err"))
   238  				})
   239  			})
   240  		})
   241  
   242  		Context("when finding the domain results in an error", func() {
   243  			BeforeEach(func() {
   244  				domainRepo.FindByNameInOrgReturns(models.DomainFields{}, errors.New("find-by-name-in-org-err"))
   245  			})
   246  
   247  			It("fails with error", func() {
   248  				Expect(err).To(HaveOccurred())
   249  				Expect(err.Error()).To(Equal("find-by-name-in-org-err"))
   250  			})
   251  		})
   252  	})
   253  })