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

     1  package organization_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/api/organizations/organizationsfakes"
     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  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  
    16  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    17  )
    18  
    19  var _ = Describe("rename-org command", func() {
    20  	var (
    21  		requirementsFactory *requirementsfakes.FakeFactory
    22  		orgRepo             *organizationsfakes.FakeOrganizationRepository
    23  		ui                  *testterm.FakeUI
    24  		configRepo          coreconfig.Repository
    25  		deps                commandregistry.Dependency
    26  	)
    27  
    28  	updateCommandDependency := func(pluginCall bool) {
    29  		deps.UI = ui
    30  		deps.RepoLocator = deps.RepoLocator.SetOrganizationRepository(orgRepo)
    31  		deps.Config = configRepo
    32  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("rename-org").SetDependency(deps, pluginCall))
    33  	}
    34  
    35  	BeforeEach(func() {
    36  		requirementsFactory = new(requirementsfakes.FakeFactory)
    37  		orgRepo = new(organizationsfakes.FakeOrganizationRepository)
    38  		ui = new(testterm.FakeUI)
    39  		configRepo = testconfig.NewRepositoryWithDefaults()
    40  	})
    41  
    42  	var callRenameOrg = func(args []string) bool {
    43  		return testcmd.RunCLICommand("rename-org", args, requirementsFactory, updateCommandDependency, false, ui)
    44  	}
    45  
    46  	It("fails with usage when given less than two args", func() {
    47  		callRenameOrg([]string{})
    48  		Expect(ui.Outputs()).To(ContainSubstrings(
    49  			[]string{"Incorrect Usage", "Requires", "arguments"},
    50  		))
    51  
    52  		callRenameOrg([]string{"foo"})
    53  		Expect(ui.Outputs()).To(ContainSubstrings(
    54  			[]string{"Incorrect Usage", "Requires", "arguments"},
    55  		))
    56  	})
    57  
    58  	It("fails requirements when not logged in", func() {
    59  		requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    60  		Expect(callRenameOrg([]string{"my-org", "my-new-org"})).To(BeFalse())
    61  	})
    62  
    63  	Context("when logged in and given an org to rename", func() {
    64  		BeforeEach(func() {
    65  			org := models.Organization{}
    66  			org.Name = "the-old-org-name"
    67  			org.GUID = "the-old-org-guid"
    68  			orgReq := new(requirementsfakes.FakeOrganizationRequirement)
    69  			orgReq.GetOrganizationReturns(org)
    70  			requirementsFactory.NewOrganizationRequirementReturns(orgReq)
    71  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    72  		})
    73  
    74  		It("passes requirements", func() {
    75  			Expect(callRenameOrg([]string{"the-old-org-name", "the-new-org-name"})).To(BeTrue())
    76  		})
    77  
    78  		It("renames an organization", func() {
    79  			targetedOrgName := configRepo.OrganizationFields().Name
    80  			callRenameOrg([]string{"the-old-org-name", "the-new-org-name"})
    81  			Expect(ui.Outputs()).To(ContainSubstrings(
    82  				[]string{"Renaming org", "the-old-org-name", "the-new-org-name", "my-user"},
    83  				[]string{"OK"},
    84  			))
    85  
    86  			guid, name := orgRepo.RenameArgsForCall(0)
    87  
    88  			Expect(guid).To(Equal("the-old-org-guid"))
    89  			Expect(name).To(Equal("the-new-org-name"))
    90  			Expect(configRepo.OrganizationFields().Name).To(Equal(targetedOrgName))
    91  		})
    92  
    93  		Describe("when the organization is currently targeted", func() {
    94  			It("updates the name of the org in the config", func() {
    95  				configRepo.SetOrganizationFields(models.OrganizationFields{
    96  					GUID: "the-old-org-guid",
    97  					Name: "the-old-org-name",
    98  				})
    99  				callRenameOrg([]string{"the-old-org-name", "the-new-org-name"})
   100  				Expect(configRepo.OrganizationFields().Name).To(Equal("the-new-org-name"))
   101  			})
   102  		})
   103  	})
   104  })