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

     1  package user_test
     2  
     3  import (
     4  	testapi "github.com/cloudfoundry/cli/cf/api/fakes"
     5  	"github.com/cloudfoundry/cli/cf/command_registry"
     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  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  
    15  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    16  )
    17  
    18  var _ = Describe("set-space-role command", func() {
    19  	var (
    20  		ui                  *testterm.FakeUI
    21  		requirementsFactory *testreq.FakeReqFactory
    22  		spaceRepo           *testapi.FakeSpaceRepository
    23  		userRepo            *testapi.FakeUserRepository
    24  		configRepo          core_config.Repository
    25  		deps                command_registry.Dependency
    26  	)
    27  
    28  	updateCommandDependency := func(pluginCall bool) {
    29  		deps.Ui = ui
    30  		deps.RepoLocator = deps.RepoLocator.SetSpaceRepository(spaceRepo)
    31  		deps.RepoLocator = deps.RepoLocator.SetUserRepository(userRepo)
    32  		deps.Config = configRepo
    33  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("set-space-role").SetDependency(deps, pluginCall))
    34  	}
    35  
    36  	BeforeEach(func() {
    37  		configRepo = testconfig.NewRepositoryWithDefaults()
    38  		accessToken, err := testconfig.EncodeAccessToken(core_config.TokenInfo{Username: "current-user"})
    39  		Expect(err).NotTo(HaveOccurred())
    40  		configRepo.SetAccessToken(accessToken)
    41  
    42  		ui = &testterm.FakeUI{}
    43  		requirementsFactory = &testreq.FakeReqFactory{}
    44  		spaceRepo = &testapi.FakeSpaceRepository{}
    45  		userRepo = &testapi.FakeUserRepository{}
    46  	})
    47  
    48  	runCommand := func(args ...string) bool {
    49  		return testcmd.RunCliCommand("set-space-role", args, requirementsFactory, updateCommandDependency, false)
    50  	}
    51  
    52  	It("fails with usage when not provided exactly four args", func() {
    53  		runCommand("foo", "bar", "baz")
    54  		Expect(ui.Outputs).To(ContainSubstrings(
    55  			[]string{"Incorrect Usage", "Requires", "arguments"},
    56  		))
    57  	})
    58  
    59  	It("does not fail with usage when provided four args", func() {
    60  		runCommand("whatever", "these", "are", "args")
    61  		Expect(ui.Outputs).ToNot(ContainSubstrings(
    62  			[]string{"Incorrect Usage", "Requires", "arguments"},
    63  		))
    64  	})
    65  
    66  	Describe("requirements", func() {
    67  		It("fails when not logged in", func() {
    68  			Expect(runCommand("username", "org", "space", "role")).To(BeFalse())
    69  		})
    70  
    71  		It("succeeds when logged in", func() {
    72  			requirementsFactory.LoginSuccess = true
    73  			passed := runCommand("username", "org", "space", "role")
    74  
    75  			Expect(passed).To(BeTrue())
    76  			Expect(requirementsFactory.UserUsername).To(Equal("username"))
    77  			Expect(requirementsFactory.OrganizationName).To(Equal("org"))
    78  		})
    79  	})
    80  
    81  	Context("when logged in", func() {
    82  		BeforeEach(func() {
    83  			requirementsFactory.LoginSuccess = true
    84  
    85  			org := models.Organization{}
    86  			org.Guid = "my-org-guid"
    87  			org.Name = "my-org"
    88  
    89  			requirementsFactory.UserFields = models.UserFields{Guid: "my-user-guid", Username: "my-user"}
    90  			requirementsFactory.Organization = org
    91  
    92  			spaceRepo.FindByNameInOrgSpace = models.Space{}
    93  			spaceRepo.FindByNameInOrgSpace.Guid = "my-space-guid"
    94  			spaceRepo.FindByNameInOrgSpace.Name = "my-space"
    95  			spaceRepo.FindByNameInOrgSpace.Organization = org.OrganizationFields
    96  		})
    97  
    98  		It("sets the given space role on the given user", func() {
    99  			runCommand("some-user", "some-org", "some-space", "SpaceManager")
   100  
   101  			Expect(ui.Outputs).To(ContainSubstrings(
   102  				[]string{"Assigning role ", "SpaceManager", "my-user", "my-org", "my-space", "current-user"},
   103  				[]string{"OK"},
   104  			))
   105  
   106  			Expect(spaceRepo.FindByNameInOrgName).To(Equal("some-space"))
   107  			Expect(spaceRepo.FindByNameInOrgOrgGuid).To(Equal("my-org-guid"))
   108  
   109  			Expect(userRepo.SetSpaceRoleUserGuid).To(Equal("my-user-guid"))
   110  			Expect(userRepo.SetSpaceRoleSpaceGuid).To(Equal("my-space-guid"))
   111  			Expect(userRepo.SetSpaceRoleRole).To(Equal(models.SPACE_MANAGER))
   112  		})
   113  	})
   114  })