github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/commands/domain/create_domain_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/commands/domain"
     6  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     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/testhelpers/matchers"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  )
    17  
    18  var _ = Describe("create domain command", func() {
    19  
    20  	var (
    21  		requirementsFactory *testreq.FakeReqFactory
    22  		ui                  *testterm.FakeUI
    23  		domainRepo          *testapi.FakeDomainRepository
    24  		configRepo          core_config.ReadWriter
    25  	)
    26  
    27  	BeforeEach(func() {
    28  		requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true}
    29  		domainRepo = &testapi.FakeDomainRepository{}
    30  		configRepo = testconfig.NewRepositoryWithAccessToken(core_config.TokenInfo{Username: "my-user"})
    31  	})
    32  
    33  	runCommand := func(args ...string) bool {
    34  		ui = new(testterm.FakeUI)
    35  		cmd := domain.NewCreateDomain(ui, configRepo, domainRepo)
    36  		return testcmd.RunCommand(cmd, args, requirementsFactory)
    37  	}
    38  
    39  	It("fails with usage", func() {
    40  		runCommand("")
    41  		Expect(ui.FailedWithUsage).To(BeTrue())
    42  
    43  		runCommand("org1")
    44  		Expect(ui.FailedWithUsage).To(BeTrue())
    45  
    46  		runCommand("org1", "example.com")
    47  		Expect(ui.FailedWithUsage).To(BeFalse())
    48  	})
    49  
    50  	Context("checks login", func() {
    51  		It("passes when logged in", func() {
    52  			Expect(runCommand("my-org", "example.com")).To(BeTrue())
    53  			Expect(requirementsFactory.OrganizationName).To(Equal("my-org"))
    54  		})
    55  
    56  		It("fails when not logged in", func() {
    57  			requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: false}
    58  
    59  			Expect(runCommand("my-org", "example.com")).To(BeFalse())
    60  		})
    61  	})
    62  
    63  	It("creates a domain", func() {
    64  		org := models.Organization{}
    65  		org.Name = "myOrg"
    66  		org.Guid = "myOrg-guid"
    67  		requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true, Organization: org}
    68  		runCommand("myOrg", "example.com")
    69  
    70  		Expect(domainRepo.CreateDomainName).To(Equal("example.com"))
    71  		Expect(domainRepo.CreateDomainOwningOrgGuid).To(Equal("myOrg-guid"))
    72  		Expect(ui.Outputs).To(ContainSubstrings(
    73  			[]string{"Creating domain", "example.com", "myOrg", "my-user"},
    74  			[]string{"OK"},
    75  		))
    76  	})
    77  })