github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/commands/domain/create_domain_test.go (about)

     1  package domain_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/api/apifakes"
     5  	"code.cloudfoundry.org/cli/cf/commandregistry"
     6  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     7  	"code.cloudfoundry.org/cli/cf/models"
     8  	"code.cloudfoundry.org/cli/cf/requirements"
     9  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    10  	testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands"
    11  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    12  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    13  
    14  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  )
    18  
    19  var _ = Describe("create domain command", func() {
    20  
    21  	var (
    22  		requirementsFactory *requirementsfakes.FakeFactory
    23  		ui                  *testterm.FakeUI
    24  		domainRepo          *apifakes.FakeDomainRepository
    25  		configRepo          coreconfig.Repository
    26  		deps                commandregistry.Dependency
    27  	)
    28  
    29  	updateCommandDependency := func(pluginCall bool) {
    30  		deps.UI = ui
    31  		deps.RepoLocator = deps.RepoLocator.SetDomainRepository(domainRepo)
    32  		deps.Config = configRepo
    33  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("create-domain").SetDependency(deps, pluginCall))
    34  	}
    35  
    36  	BeforeEach(func() {
    37  		requirementsFactory = new(requirementsfakes.FakeFactory)
    38  		requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    39  		domainRepo = new(apifakes.FakeDomainRepository)
    40  		configRepo = testconfig.NewRepositoryWithAccessToken(coreconfig.TokenInfo{Username: "my-user"})
    41  	})
    42  
    43  	runCommand := func(args ...string) bool {
    44  		ui = new(testterm.FakeUI)
    45  		return testcmd.RunCLICommand("create-domain", args, requirementsFactory, updateCommandDependency, false, ui)
    46  	}
    47  
    48  	It("fails with usage", func() {
    49  		runCommand("")
    50  		Expect(ui.Outputs()).To(ContainSubstrings(
    51  			[]string{"Incorrect Usage", "Requires", "arguments"},
    52  		))
    53  
    54  		runCommand("org1")
    55  		Expect(ui.Outputs()).To(ContainSubstrings(
    56  			[]string{"Incorrect Usage", "Requires", "arguments"},
    57  		))
    58  	})
    59  
    60  	Context("checks login", func() {
    61  		It("passes when logged in", func() {
    62  			fakeOrgRequirement := new(requirementsfakes.FakeOrganizationRequirement)
    63  			fakeOrgRequirement.GetOrganizationReturns(models.Organization{
    64  				OrganizationFields: models.OrganizationFields{
    65  					Name: "my-org",
    66  				},
    67  			})
    68  			requirementsFactory.NewOrganizationRequirementReturns(fakeOrgRequirement)
    69  			Expect(runCommand("my-org", "example.com")).To(BeTrue())
    70  			Expect(ui.Outputs()).To(ContainSubstrings([]string{"my-org"}))
    71  		})
    72  
    73  		It("fails when not logged in", func() {
    74  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    75  
    76  			Expect(runCommand("my-org", "example.com")).To(BeFalse())
    77  		})
    78  	})
    79  
    80  	It("creates a domain", func() {
    81  		org := models.Organization{}
    82  		org.Name = "myOrg"
    83  		org.GUID = "myOrg-guid"
    84  		fakeOrgRequirement := new(requirementsfakes.FakeOrganizationRequirement)
    85  		fakeOrgRequirement.GetOrganizationReturns(org)
    86  		requirementsFactory.NewOrganizationRequirementReturns(fakeOrgRequirement)
    87  		requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    88  		runCommand("myOrg", "example.com")
    89  
    90  		domainName, domainOwningOrgGUID := domainRepo.CreateArgsForCall(0)
    91  		Expect(domainName).To(Equal("example.com"))
    92  		Expect(domainOwningOrgGUID).To(Equal("myOrg-guid"))
    93  		Expect(ui.Outputs()).To(ContainSubstrings(
    94  			[]string{"Creating domain", "example.com", "myOrg", "my-user"},
    95  			[]string{"OK"},
    96  		))
    97  	})
    98  })