github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/domain/domains_test.go (about)

     1  package domain_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/cf/commandregistry"
     7  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     8  	"code.cloudfoundry.org/cli/cf/flags"
     9  	"code.cloudfoundry.org/cli/cf/models"
    10  	"code.cloudfoundry.org/cli/cf/requirements"
    11  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    12  
    13  	"code.cloudfoundry.org/cli/cf/api/apifakes"
    14  	testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands"
    15  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    16  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    17  
    18  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    19  
    20  	"code.cloudfoundry.org/cli/cf/commands/domain"
    21  	. "github.com/onsi/ginkgo"
    22  	. "github.com/onsi/gomega"
    23  )
    24  
    25  var _ = Describe("ListDomains", func() {
    26  	var (
    27  		ui             *testterm.FakeUI
    28  		routingAPIRepo *apifakes.FakeRoutingAPIRepository
    29  		domainRepo     *apifakes.FakeDomainRepository
    30  		configRepo     coreconfig.Repository
    31  
    32  		cmd         domain.ListDomains
    33  		deps        commandregistry.Dependency
    34  		factory     *requirementsfakes.FakeFactory
    35  		flagContext flags.FlagContext
    36  
    37  		loginRequirement       requirements.Requirement
    38  		targetedOrgRequirement *requirementsfakes.FakeTargetedOrgRequirement
    39  
    40  		domainFields []models.DomainFields
    41  		routerGroups models.RouterGroups
    42  	)
    43  
    44  	BeforeEach(func() {
    45  		ui = &testterm.FakeUI{}
    46  		configRepo = testconfig.NewRepositoryWithDefaults()
    47  		routingAPIRepo = new(apifakes.FakeRoutingAPIRepository)
    48  		repoLocator := deps.RepoLocator.SetRoutingAPIRepository(routingAPIRepo)
    49  
    50  		domainRepo = new(apifakes.FakeDomainRepository)
    51  		repoLocator = repoLocator.SetDomainRepository(domainRepo)
    52  
    53  		deps = commandregistry.Dependency{
    54  			UI:          ui,
    55  			Config:      configRepo,
    56  			RepoLocator: repoLocator,
    57  		}
    58  
    59  		cmd = domain.ListDomains{}
    60  		cmd.SetDependency(deps, false)
    61  
    62  		flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
    63  
    64  		factory = new(requirementsfakes.FakeFactory)
    65  		loginRequirement = &passingRequirement{Name: "LoginRequirement"}
    66  		factory.NewLoginRequirementReturns(loginRequirement)
    67  
    68  		targetedOrgRequirement = new(requirementsfakes.FakeTargetedOrgRequirement)
    69  		factory.NewTargetedOrgRequirementReturns(targetedOrgRequirement)
    70  
    71  		domainRepo.ListDomainsForOrgStub = func(orgGUID string, cb func(models.DomainFields) bool) error {
    72  			for _, field := range domainFields {
    73  				if !cb(field) {
    74  					break
    75  				}
    76  			}
    77  			return nil
    78  		}
    79  
    80  		routerGroups = models.RouterGroups{
    81  			models.RouterGroup{
    82  				GUID: "router-group-guid",
    83  				Name: "my-router-name1",
    84  				Type: "tcp",
    85  			},
    86  		}
    87  		routingAPIRepo.ListRouterGroupsStub = func(cb func(models.RouterGroup) bool) error {
    88  			for _, routerGroup := range routerGroups {
    89  				if !cb(routerGroup) {
    90  					break
    91  				}
    92  			}
    93  			return nil
    94  		}
    95  	})
    96  
    97  	Describe("Requirements", func() {
    98  		Context("when arguments are provided", func() {
    99  			var cmd commandregistry.Command
   100  			var flagContext flags.FlagContext
   101  
   102  			BeforeEach(func() {
   103  				cmd = &domain.ListDomains{}
   104  				cmd.SetDependency(deps, false)
   105  				flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
   106  			})
   107  
   108  			It("should fail with usage", func() {
   109  				flagContext.Parse("blahblah")
   110  
   111  				reqs, err := cmd.Requirements(factory, flagContext)
   112  				Expect(err).NotTo(HaveOccurred())
   113  
   114  				err = testcmd.RunRequirements(reqs)
   115  				Expect(err).To(HaveOccurred())
   116  				Expect(err.Error()).To(ContainSubstring("Incorrect Usage"))
   117  				Expect(err.Error()).To(ContainSubstring("No argument required"))
   118  			})
   119  		})
   120  
   121  		Context("when provided no arguments", func() {
   122  			BeforeEach(func() {
   123  				flagContext.Parse()
   124  			})
   125  
   126  			It("does not fail with usage", func() {
   127  				_, err := cmd.Requirements(factory, flagContext)
   128  				Expect(err).NotTo(HaveOccurred())
   129  				Expect(ui.Outputs()).NotTo(ContainSubstrings(
   130  					[]string{"Incorrect Usage. No argument required"},
   131  					[]string{"NAME"},
   132  					[]string{"USAGE"},
   133  				))
   134  			})
   135  
   136  			It("returns a LoginRequirement", func() {
   137  				actualRequirements, err := cmd.Requirements(factory, flagContext)
   138  				Expect(err).NotTo(HaveOccurred())
   139  				Expect(factory.NewLoginRequirementCallCount()).To(Equal(1))
   140  				Expect(actualRequirements).To(ContainElement(loginRequirement))
   141  			})
   142  
   143  			It("returns a TargetedOrgRequirement", func() {
   144  				actualRequirements, err := cmd.Requirements(factory, flagContext)
   145  				Expect(err).NotTo(HaveOccurred())
   146  				Expect(factory.NewTargetedOrgRequirementCallCount()).To(Equal(1))
   147  				Expect(actualRequirements).To(ContainElement(targetedOrgRequirement))
   148  			})
   149  		})
   150  	})
   151  
   152  	Describe("Execute", func() {
   153  		var err error
   154  
   155  		JustBeforeEach(func() {
   156  			err = cmd.Execute(flagContext)
   157  		})
   158  
   159  		It("prints getting domains message", func() {
   160  			Expect(err).NotTo(HaveOccurred())
   161  			Expect(ui.Outputs()).To(ContainSubstrings(
   162  				[]string{"Getting domains in org my-org"},
   163  			))
   164  		})
   165  
   166  		It("tries to get the list of domains for org", func() {
   167  			Expect(err).NotTo(HaveOccurred())
   168  			Expect(domainRepo.ListDomainsForOrgCallCount()).To(Equal(1))
   169  			orgGUID, _ := domainRepo.ListDomainsForOrgArgsForCall(0)
   170  			Expect(orgGUID).To(Equal("my-org-guid"))
   171  		})
   172  
   173  		It("prints no domains found message", func() {
   174  			Expect(err).NotTo(HaveOccurred())
   175  			Expect(ui.Outputs()).To(BeInDisplayOrder(
   176  				[]string{"name", "status"},
   177  				[]string{"No domains found"},
   178  			))
   179  		})
   180  
   181  		Context("when list domains for org returns error", func() {
   182  			BeforeEach(func() {
   183  				domainRepo.ListDomainsForOrgReturns(errors.New("org-domain-err"))
   184  			})
   185  
   186  			It("fails with message", func() {
   187  				Expect(err).To(HaveOccurred())
   188  				Expect(err.Error()).To(ContainSubstring("Failed fetching domains."))
   189  				Expect(err.Error()).To(ContainSubstring("org-domain-err"))
   190  			})
   191  		})
   192  
   193  		Context("when domains are found", func() {
   194  			BeforeEach(func() {
   195  				domainFields = []models.DomainFields{
   196  					{Shared: false, Name: "Private-domain1"},
   197  					{Shared: false, Name: "Private-domain2", RouterGroupType: "tcp"},
   198  					{Shared: true, Name: "Shared-domain1"},
   199  					{Shared: true, Name: "Shared-domain2", RouterGroupType: "foobar"},
   200  				}
   201  			})
   202  
   203  			AfterEach(func() {
   204  				domainFields = []models.DomainFields{}
   205  			})
   206  
   207  			It("does not print no domains found message", func() {
   208  				Expect(err).NotTo(HaveOccurred())
   209  				Expect(ui.Outputs()).NotTo(ContainSubstrings(
   210  					[]string{"No domains found"},
   211  				))
   212  			})
   213  
   214  			It("prints the domain information", func() {
   215  				Expect(err).NotTo(HaveOccurred())
   216  				Expect(ui.Outputs()).To(BeInDisplayOrder(
   217  					[]string{"name", "status", "type"},
   218  					[]string{"Shared-domain1", "shared"},
   219  					[]string{"Shared-domain2", "shared", "foobar"},
   220  					[]string{"Private-domain1", "owned"},
   221  					[]string{"Private-domain2", "owned", "tcp"},
   222  				))
   223  			})
   224  		})
   225  	})
   226  })