github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/commands/domain/domains_test.go (about) 1 package domain_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/errors" 7 "github.com/cloudfoundry/cli/cf/models" 8 testcmd "github.com/cloudfoundry/cli/testhelpers/commands" 9 testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" 10 testreq "github.com/cloudfoundry/cli/testhelpers/requirements" 11 testterm "github.com/cloudfoundry/cli/testhelpers/terminal" 12 13 . "github.com/cloudfoundry/cli/cf/commands/domain" 14 . "github.com/cloudfoundry/cli/testhelpers/matchers" 15 . "github.com/onsi/ginkgo" 16 . "github.com/onsi/gomega" 17 ) 18 19 var _ = Describe("domains command", func() { 20 var ( 21 ui *testterm.FakeUI 22 configRepo core_config.ReadWriter 23 domainRepo *testapi.FakeDomainRepository 24 requirementsFactory *testreq.FakeReqFactory 25 ) 26 27 BeforeEach(func() { 28 ui = &testterm.FakeUI{} 29 configRepo = testconfig.NewRepositoryWithDefaults() 30 domainRepo = &testapi.FakeDomainRepository{} 31 requirementsFactory = &testreq.FakeReqFactory{} 32 }) 33 34 runCommand := func(args ...string) bool { 35 return testcmd.RunCommand(NewListDomains(ui, configRepo, domainRepo), args, requirementsFactory) 36 } 37 38 Describe("requirements", func() { 39 It("fails when an org is not targeted", func() { 40 requirementsFactory.LoginSuccess = true 41 42 Expect(runCommand()).To(BeFalse()) 43 }) 44 45 It("fails when not logged in", func() { 46 requirementsFactory.TargetedOrgSuccess = true 47 48 Expect(runCommand()).To(BeFalse()) 49 }) 50 51 It("fails with usage when invoked with any args what so ever ", func() { 52 requirementsFactory.LoginSuccess = true 53 requirementsFactory.TargetedOrgSuccess = true 54 55 runCommand("whoops") 56 Expect(ui.FailedWithUsage).To(BeTrue()) 57 }) 58 }) 59 60 Context("when logged in and an org is targeted", func() { 61 BeforeEach(func() { 62 requirementsFactory.LoginSuccess = true 63 requirementsFactory.TargetedOrgSuccess = true 64 requirementsFactory.OrganizationFields = models.OrganizationFields{ 65 Name: "my-org", 66 Guid: "my-org-guid", 67 } 68 }) 69 70 Context("when there is at least one domain", func() { 71 BeforeEach(func() { 72 domainRepo.ListDomainsForOrgDomains = []models.DomainFields{ 73 models.DomainFields{ 74 Shared: false, 75 Name: "Private-domain1", 76 }, 77 models.DomainFields{ 78 Shared: true, 79 Name: "The-shared-domain", 80 }, 81 models.DomainFields{ 82 Shared: false, 83 Name: "Private-domain2", 84 }, 85 } 86 }) 87 88 It("lists domains", func() { 89 runCommand() 90 91 Expect(domainRepo.ListDomainsForOrgGuid).To(Equal("my-org-guid")) 92 Expect(ui.Outputs).To(ContainSubstrings( 93 []string{"Getting domains in org", "my-org", "my-user"}, 94 []string{"name", "status"}, 95 []string{"The-shared-domain", "shared"}, 96 []string{"Private-domain1", "owned"}, 97 []string{"Private-domain2", "owned"}, 98 )) 99 }) 100 }) 101 102 It("displays a message when no domains are found", func() { 103 runCommand() 104 Expect(ui.Outputs).To(ContainSubstrings( 105 []string{"Getting domains in org", "my-org", "my-user"}, 106 []string{"No domains found"}, 107 )) 108 }) 109 110 It("fails when the domains API returns an error", func() { 111 domainRepo.ListDomainsForOrgApiResponse = errors.New("borked!") 112 runCommand() 113 114 Expect(ui.Outputs).To(ContainSubstrings( 115 []string{"Getting domains in org", "my-org", "my-user"}, 116 []string{"FAILED"}, 117 []string{"Failed fetching domains"}, 118 []string{"borked!"}, 119 )) 120 }) 121 }) 122 })