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