github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/space/create_space_test.go (about)

     1  package space_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	testapi "github.com/cloudfoundry/cli/cf/api/fakes"
     7  	fake_org "github.com/cloudfoundry/cli/cf/api/organizations/fakes"
     8  	"github.com/cloudfoundry/cli/cf/api/space_quotas/fakes"
     9  	"github.com/cloudfoundry/cli/cf/command_registry"
    10  	"github.com/cloudfoundry/cli/cf/commands/user"
    11  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
    12  	"github.com/cloudfoundry/cli/cf/models"
    13  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
    14  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    15  	"github.com/cloudfoundry/cli/testhelpers/maker"
    16  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    17  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    18  
    19  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    20  	. "github.com/onsi/ginkgo"
    21  	. "github.com/onsi/gomega"
    22  )
    23  
    24  var _ = Describe("create-space command", func() {
    25  	var (
    26  		ui                  *testterm.FakeUI
    27  		requirementsFactory *testreq.FakeReqFactory
    28  		configSpace         models.SpaceFields
    29  		configOrg           models.OrganizationFields
    30  		configRepo          core_config.Repository
    31  		spaceRepo           *testapi.FakeSpaceRepository
    32  		orgRepo             *fake_org.FakeOrganizationRepository
    33  		userRepo            *testapi.FakeUserRepository
    34  		spaceRoleSetter     user.SpaceRoleSetter
    35  		spaceQuotaRepo      *fakes.FakeSpaceQuotaRepository
    36  		OriginalCommand     command_registry.Command
    37  		deps                command_registry.Dependency
    38  	)
    39  
    40  	updateCommandDependency := func(pluginCall bool) {
    41  		deps.Ui = ui
    42  		deps.RepoLocator = deps.RepoLocator.SetSpaceRepository(spaceRepo)
    43  		deps.RepoLocator = deps.RepoLocator.SetSpaceQuotaRepository(spaceQuotaRepo)
    44  		deps.RepoLocator = deps.RepoLocator.SetOrganizationRepository(orgRepo)
    45  		deps.RepoLocator = deps.RepoLocator.SetUserRepository(userRepo)
    46  		deps.Config = configRepo
    47  
    48  		//inject fake 'command dependency' into registry
    49  		command_registry.Register(spaceRoleSetter)
    50  
    51  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("create-space").SetDependency(deps, pluginCall))
    52  	}
    53  
    54  	runCommand := func(args ...string) bool {
    55  		return testcmd.RunCliCommand("create-space", args, requirementsFactory, updateCommandDependency, false)
    56  	}
    57  
    58  	BeforeEach(func() {
    59  		ui = &testterm.FakeUI{}
    60  		configRepo = testconfig.NewRepositoryWithDefaults()
    61  
    62  		orgRepo = &fake_org.FakeOrganizationRepository{}
    63  		userRepo = &testapi.FakeUserRepository{}
    64  		spaceRoleSetter = command_registry.Commands.FindCommand("set-space-role").(user.SpaceRoleSetter)
    65  		spaceQuotaRepo = &fakes.FakeSpaceQuotaRepository{}
    66  
    67  		requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true, TargetedOrgSuccess: true}
    68  		configOrg = models.OrganizationFields{
    69  			Name: "my-org",
    70  			Guid: "my-org-guid",
    71  		}
    72  
    73  		configSpace = models.SpaceFields{
    74  			Name: "config-space",
    75  			Guid: "config-space-guid",
    76  		}
    77  
    78  		//save original command and restore later
    79  		OriginalCommand = command_registry.Commands.FindCommand("set-space-role")
    80  
    81  		spaceRepo = &testapi.FakeSpaceRepository{
    82  			CreateSpaceSpace: maker.NewSpace(maker.Overrides{"name": "my-space", "guid": "my-space-guid", "organization": configOrg}),
    83  		}
    84  		Expect(spaceRepo.CreateSpaceSpace.Name).To(Equal("my-space"))
    85  	})
    86  
    87  	AfterEach(func() {
    88  		command_registry.Register(OriginalCommand)
    89  	})
    90  
    91  	Describe("Requirements", func() {
    92  		It("fails with usage when not provided exactly one argument", func() {
    93  			runCommand()
    94  			Expect(ui.Outputs).To(ContainSubstrings(
    95  				[]string{"Incorrect Usage", "Requires", "argument"},
    96  			))
    97  		})
    98  
    99  		Context("when not logged in", func() {
   100  			BeforeEach(func() {
   101  				requirementsFactory.LoginSuccess = false
   102  			})
   103  
   104  			It("fails requirements", func() {
   105  				Expect(runCommand("some-space")).To(BeFalse())
   106  			})
   107  		})
   108  
   109  		Context("when a org is not targeted", func() {
   110  			BeforeEach(func() {
   111  				requirementsFactory.TargetedOrgSuccess = false
   112  			})
   113  
   114  			It("fails requirements", func() {
   115  				Expect(runCommand("what-is-space?")).To(BeFalse())
   116  			})
   117  		})
   118  	})
   119  
   120  	It("creates a space", func() {
   121  		runCommand("my-space")
   122  		Expect(ui.Outputs).To(ContainSubstrings(
   123  			[]string{"Creating space", "my-space", "my-org", "my-user"},
   124  			[]string{"OK"},
   125  			[]string{"Assigning", models.SpaceRoleToUserInput[models.SPACE_MANAGER], "my-user", "my-space"},
   126  			[]string{"Assigning", models.SpaceRoleToUserInput[models.SPACE_DEVELOPER], "my-user", "my-space"},
   127  			[]string{"TIP"},
   128  		))
   129  
   130  		Expect(spaceRepo.CreateSpaceName).To(Equal("my-space"))
   131  		Expect(spaceRepo.CreateSpaceOrgGuid).To(Equal("my-org-guid"))
   132  		Expect(userRepo.SetSpaceRoleUserGuid).To(Equal("my-user-guid"))
   133  		Expect(userRepo.SetSpaceRoleSpaceGuid).To(Equal("my-space-guid"))
   134  		Expect(userRepo.SetSpaceRoleRole).To(Equal(models.SPACE_DEVELOPER))
   135  	})
   136  
   137  	It("warns the user when a space with that name already exists", func() {
   138  		spaceRepo.CreateSpaceExists = true
   139  		runCommand("my-space")
   140  
   141  		Expect(ui.Outputs).To(ContainSubstrings(
   142  			[]string{"Creating space", "my-space"},
   143  			[]string{"OK"},
   144  		))
   145  		Expect(ui.WarnOutputs).To(ContainSubstrings([]string{"my-space", "already exists"}))
   146  		Expect(ui.Outputs).ToNot(ContainSubstrings(
   147  			[]string{"Assigning", "my-user", "my-space", models.SpaceRoleToUserInput[models.SPACE_MANAGER]},
   148  		))
   149  
   150  		Expect(spaceRepo.CreateSpaceName).To(Equal(""))
   151  		Expect(spaceRepo.CreateSpaceOrgGuid).To(Equal(""))
   152  		Expect(userRepo.SetSpaceRoleUserGuid).To(Equal(""))
   153  		Expect(userRepo.SetSpaceRoleSpaceGuid).To(Equal(""))
   154  	})
   155  
   156  	Context("when the -o flag is provided", func() {
   157  		It("creates a space within that org", func() {
   158  			org := models.Organization{
   159  				OrganizationFields: models.OrganizationFields{
   160  					Name: "other-org",
   161  					Guid: "org-guid-1",
   162  				}}
   163  			orgRepo.FindByNameReturns(org, nil)
   164  
   165  			runCommand("-o", "other-org", "my-space")
   166  
   167  			Expect(ui.Outputs).To(ContainSubstrings(
   168  				[]string{"Creating space", "my-space", "other-org", "my-user"},
   169  				[]string{"OK"},
   170  				[]string{"Assigning", "my-user", "my-space", models.SpaceRoleToUserInput[models.SPACE_MANAGER]},
   171  				[]string{"Assigning", "my-user", "my-space", models.SpaceRoleToUserInput[models.SPACE_DEVELOPER]},
   172  				[]string{"TIP"},
   173  			))
   174  
   175  			Expect(spaceRepo.CreateSpaceName).To(Equal("my-space"))
   176  			Expect(spaceRepo.CreateSpaceOrgGuid).To(Equal(org.Guid))
   177  			Expect(userRepo.SetSpaceRoleUserGuid).To(Equal("my-user-guid"))
   178  			Expect(userRepo.SetSpaceRoleSpaceGuid).To(Equal("my-space-guid"))
   179  			Expect(userRepo.SetSpaceRoleRole).To(Equal(models.SPACE_DEVELOPER))
   180  		})
   181  
   182  		It("fails when the org provided does not exist", func() {
   183  			orgRepo.FindByNameReturns(models.Organization{}, errors.New("cool-organization does not exist"))
   184  			runCommand("-o", "cool-organization", "my-space")
   185  
   186  			Expect(ui.Outputs).To(ContainSubstrings(
   187  				[]string{"FAILED"},
   188  				[]string{"cool-organization", "does not exist"},
   189  			))
   190  
   191  			Expect(spaceRepo.CreateSpaceName).To(Equal(""))
   192  		})
   193  
   194  		It("fails when finding the org returns an error", func() {
   195  			orgRepo.FindByNameReturns(models.Organization{}, errors.New("cool-organization does not exist"))
   196  			runCommand("-o", "cool-organization", "my-space")
   197  
   198  			Expect(ui.Outputs).To(ContainSubstrings(
   199  				[]string{"FAILED"},
   200  				[]string{"Error"},
   201  			))
   202  
   203  			Expect(spaceRepo.CreateSpaceName).To(Equal(""))
   204  		})
   205  	})
   206  
   207  	Context("when the -q flag is provided", func() {
   208  		It("assigns the space-quota specified to the space", func() {
   209  			spaceQuota := models.SpaceQuota{
   210  				Name: "my-space-quota",
   211  				Guid: "my-space-quota-guid",
   212  			}
   213  			spaceQuotaRepo.FindByNameReturns(spaceQuota, nil)
   214  			runCommand("-q", "my-space-quota", "my-space")
   215  
   216  			Expect(spaceQuotaRepo.FindByNameArgsForCall(0)).To(Equal(spaceQuota.Name))
   217  			Expect(spaceRepo.CreateSpaceSpaceQuotaGuid).To(Equal(spaceQuota.Guid))
   218  
   219  		})
   220  
   221  		Context("when the space-quota provided does not exist", func() {
   222  			It("fails", func() {
   223  				spaceQuotaRepo.FindByNameReturns(models.SpaceQuota{}, errors.New("Error"))
   224  				runCommand("-q", "my-space-quota", "my-space")
   225  
   226  				Expect(ui.Outputs).To(ContainSubstrings(
   227  					[]string{"FAILED"},
   228  					[]string{"Error"},
   229  				))
   230  			})
   231  		})
   232  	})
   233  })