github.com/cloudfoundry/cli@v7.1.0+incompatible/cf/commands/user/set_space_role_test.go (about)

     1  package user_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/cf/commandregistry"
     7  	"code.cloudfoundry.org/cli/cf/commands/user"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/flags"
    10  	"code.cloudfoundry.org/cli/cf/models"
    11  	"code.cloudfoundry.org/cli/cf/requirements"
    12  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    13  
    14  	"code.cloudfoundry.org/cli/cf/api/apifakes"
    15  	"code.cloudfoundry.org/cli/cf/api/featureflags/featureflagsfakes"
    16  	"code.cloudfoundry.org/cli/cf/api/spaces/spacesfakes"
    17  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    18  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    19  
    20  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    21  	. "github.com/onsi/ginkgo"
    22  	. "github.com/onsi/gomega"
    23  )
    24  
    25  var _ = Describe("SetSpaceRole", func() {
    26  	var (
    27  		ui         *testterm.FakeUI
    28  		configRepo coreconfig.Repository
    29  		userRepo   *apifakes.FakeUserRepository
    30  		spaceRepo  *spacesfakes.FakeSpaceRepository
    31  		flagRepo   *featureflagsfakes.FakeFeatureFlagRepository
    32  
    33  		cmd         commandregistry.Command
    34  		deps        commandregistry.Dependency
    35  		factory     *requirementsfakes.FakeFactory
    36  		flagContext flags.FlagContext
    37  
    38  		loginRequirement        requirements.Requirement
    39  		userRequirement         *requirementsfakes.FakeUserRequirement
    40  		organizationRequirement *requirementsfakes.FakeOrganizationRequirement
    41  	)
    42  
    43  	BeforeEach(func() {
    44  		ui = &testterm.FakeUI{}
    45  		configRepo = testconfig.NewRepositoryWithDefaults()
    46  		userRepo = new(apifakes.FakeUserRepository)
    47  		repoLocator := deps.RepoLocator.SetUserRepository(userRepo)
    48  		spaceRepo = new(spacesfakes.FakeSpaceRepository)
    49  		repoLocator = repoLocator.SetSpaceRepository(spaceRepo)
    50  		flagRepo = new(featureflagsfakes.FakeFeatureFlagRepository)
    51  		repoLocator = repoLocator.SetFeatureFlagRepository(flagRepo)
    52  
    53  		deps = commandregistry.Dependency{
    54  			UI:          ui,
    55  			Config:      configRepo,
    56  			RepoLocator: repoLocator,
    57  		}
    58  
    59  		cmd = &user.SetSpaceRole{}
    60  		cmd.SetDependency(deps, false)
    61  
    62  		flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
    63  
    64  		factory = new(requirementsfakes.FakeFactory)
    65  
    66  		loginRequirement = &passingRequirement{}
    67  		factory.NewLoginRequirementReturns(loginRequirement)
    68  
    69  		userRequirement = new(requirementsfakes.FakeUserRequirement)
    70  		userRequirement.ExecuteReturns(nil)
    71  		factory.NewUserRequirementReturns(userRequirement)
    72  		factory.NewClientRequirementReturns(userRequirement)
    73  
    74  		organizationRequirement = new(requirementsfakes.FakeOrganizationRequirement)
    75  		organizationRequirement.ExecuteReturns(nil)
    76  		factory.NewOrganizationRequirementReturns(organizationRequirement)
    77  	})
    78  
    79  	Describe("Requirements", func() {
    80  		Context("when not provided exactly four args", func() {
    81  			BeforeEach(func() {
    82  				flagContext.Parse("the-user-name", "the-org-name", "the-space-name")
    83  			})
    84  
    85  			It("fails with usage", func() {
    86  				_, err := cmd.Requirements(factory, flagContext)
    87  				Expect(err).To(HaveOccurred())
    88  				Expect(ui.Outputs()).To(ContainSubstrings(
    89  					[]string{"Incorrect Usage. Requires USERNAME, ORG, SPACE, ROLE as arguments"},
    90  					[]string{"NAME"},
    91  					[]string{"USAGE"},
    92  				))
    93  			})
    94  		})
    95  
    96  		Context("when provided four args", func() {
    97  			BeforeEach(func() {
    98  				flagContext.Parse("the-user-name", "the-org-name", "the-space-name", "SpaceManager")
    99  			})
   100  
   101  			It("returns a LoginRequirement", func() {
   102  				actualRequirements, err := cmd.Requirements(factory, flagContext)
   103  				Expect(err).NotTo(HaveOccurred())
   104  				Expect(factory.NewLoginRequirementCallCount()).To(Equal(1))
   105  
   106  				Expect(actualRequirements).To(ContainElement(loginRequirement))
   107  			})
   108  
   109  			It("returns an OrgRequirement", func() {
   110  				actualRequirements, err := cmd.Requirements(factory, flagContext)
   111  				Expect(err).NotTo(HaveOccurred())
   112  				Expect(factory.NewOrganizationRequirementCallCount()).To(Equal(1))
   113  				Expect(factory.NewOrganizationRequirementArgsForCall(0)).To(Equal("the-org-name"))
   114  
   115  				Expect(actualRequirements).To(ContainElement(organizationRequirement))
   116  			})
   117  
   118  			It("requests the set_roles_by_username flag", func() {
   119  				cmd.Requirements(factory, flagContext)
   120  				Expect(flagRepo.FindByNameCallCount()).To(Equal(1))
   121  				Expect(flagRepo.FindByNameArgsForCall(0)).To(Equal("set_roles_by_username"))
   122  			})
   123  
   124  			Context("when the set_roles_by_username flag exists and is enabled", func() {
   125  				BeforeEach(func() {
   126  					flagRepo.FindByNameReturns(models.FeatureFlag{Enabled: true}, nil)
   127  				})
   128  
   129  				It("returns a UserRequirement", func() {
   130  					actualRequirements, err := cmd.Requirements(factory, flagContext)
   131  					Expect(err).NotTo(HaveOccurred())
   132  					Expect(factory.NewUserRequirementCallCount()).To(Equal(1))
   133  					actualUsername, actualWantGUID := factory.NewUserRequirementArgsForCall(0)
   134  					Expect(actualUsername).To(Equal("the-user-name"))
   135  					Expect(actualWantGUID).To(BeFalse())
   136  
   137  					Expect(actualRequirements).To(ContainElement(userRequirement))
   138  				})
   139  			})
   140  
   141  			Context("when the set_roles_by_username flag exists and is disabled", func() {
   142  				BeforeEach(func() {
   143  					flagRepo.FindByNameReturns(models.FeatureFlag{Enabled: false}, nil)
   144  				})
   145  
   146  				It("returns a UserRequirement", func() {
   147  					actualRequirements, err := cmd.Requirements(factory, flagContext)
   148  					Expect(err).NotTo(HaveOccurred())
   149  					Expect(factory.NewUserRequirementCallCount()).To(Equal(1))
   150  					actualUsername, actualWantGUID := factory.NewUserRequirementArgsForCall(0)
   151  					Expect(actualUsername).To(Equal("the-user-name"))
   152  					Expect(actualWantGUID).To(BeTrue())
   153  
   154  					Expect(actualRequirements).To(ContainElement(userRequirement))
   155  				})
   156  			})
   157  
   158  			Context("when the set_roles_by_username flag cannot be retrieved", func() {
   159  				BeforeEach(func() {
   160  					flagRepo.FindByNameReturns(models.FeatureFlag{}, errors.New("some error"))
   161  				})
   162  
   163  				It("returns a UserRequirement", func() {
   164  					actualRequirements, err := cmd.Requirements(factory, flagContext)
   165  					Expect(err).NotTo(HaveOccurred())
   166  					Expect(factory.NewUserRequirementCallCount()).To(Equal(1))
   167  					actualUsername, actualWantGUID := factory.NewUserRequirementArgsForCall(0)
   168  					Expect(actualUsername).To(Equal("the-user-name"))
   169  					Expect(actualWantGUID).To(BeTrue())
   170  
   171  					Expect(actualRequirements).To(ContainElement(userRequirement))
   172  				})
   173  			})
   174  		})
   175  
   176  		Context("when given the --client flag", func() {
   177  			BeforeEach(func() {
   178  				flagContext.Parse("the-client-id", "the-org-name", "the-space-name", "OrgManager", "--client")
   179  			})
   180  
   181  			It("returns a User Requirement with USERNAME field as it's GUID", func() {
   182  				actualRequirements, err := cmd.Requirements(factory, flagContext)
   183  				Expect(err).NotTo(HaveOccurred())
   184  				Expect(factory.NewClientRequirementCallCount()).To(Equal(1))
   185  				actualUsername := factory.NewClientRequirementArgsForCall(0)
   186  				Expect(actualUsername).To(Equal("the-client-id"))
   187  
   188  				Expect(actualRequirements).To(ContainElement(userRequirement))
   189  			})
   190  
   191  			It("ignores the set_roles_by_username feature flag", func() {
   192  				cmd.Requirements(factory, flagContext)
   193  				Expect(flagRepo.FindByNameCallCount()).To(BeZero())
   194  			})
   195  		})
   196  	})
   197  
   198  	Describe("Execute", func() {
   199  		var (
   200  			org models.Organization
   201  			err error
   202  		)
   203  
   204  		BeforeEach(func() {
   205  			flagContext.Parse("the-user-name", "the-org-name", "the-space-name", "SpaceManager")
   206  			cmd.Requirements(factory, flagContext)
   207  
   208  			org = models.Organization{}
   209  			org.GUID = "the-org-guid"
   210  			org.Name = "the-org-name"
   211  			organizationRequirement.GetOrganizationReturns(org)
   212  		})
   213  
   214  		JustBeforeEach(func() {
   215  			err = cmd.Execute(flagContext)
   216  		})
   217  
   218  		Context("when the space is not found", func() {
   219  			BeforeEach(func() {
   220  				spaceRepo.FindByNameInOrgReturns(models.Space{}, errors.New("space-repo-error"))
   221  			})
   222  
   223  			It("doesn't call CC", func() {
   224  				Expect(userRepo.SetSpaceRoleByGUIDCallCount()).To(BeZero())
   225  				Expect(userRepo.SetSpaceRoleByUsernameCallCount()).To(BeZero())
   226  			})
   227  
   228  			It("returns an error", func() {
   229  				Expect(err).To(HaveOccurred())
   230  				Expect(err.Error()).To(Equal("space-repo-error"))
   231  			})
   232  		})
   233  
   234  		Context("when provided a differently cased role", func() {
   235  			BeforeEach(func() {
   236  				flagContext.Parse("the-user-name", "the-org-name", "the-space-name", "spacemanager")
   237  				userRequirement.GetUserReturns(models.UserFields{Username: "the-user-name"})
   238  			})
   239  
   240  			It("tells the user it is assigning the role", func() {
   241  				Expect(err).NotTo(HaveOccurred())
   242  				Expect(ui.Outputs()).To(ContainSubstrings(
   243  					[]string{"Assigning role", "SpaceManager", "the-user-name", "the-org", "the-user-name"},
   244  					[]string{"OK"},
   245  				))
   246  			})
   247  		})
   248  
   249  		Context("when the space is found", func() {
   250  			BeforeEach(func() {
   251  				space := models.Space{}
   252  				space.GUID = "the-space-guid"
   253  				space.Name = "the-space-name"
   254  				spaceRepo.FindByNameInOrgReturns(space, nil)
   255  			})
   256  
   257  			Context("when the UserRequirement returns a user with a GUID", func() {
   258  				BeforeEach(func() {
   259  					userFields := models.UserFields{GUID: "the-user-guid", Username: "the-user-name"}
   260  					userRequirement.GetUserReturns(userFields)
   261  				})
   262  
   263  				It("tells the user it is assigning the role", func() {
   264  					Expect(err).NotTo(HaveOccurred())
   265  					Expect(ui.Outputs()).To(ContainSubstrings(
   266  						[]string{"Assigning role", "SpaceManager", "the-user-name", "the-org", "the-user-name"},
   267  						[]string{"OK"},
   268  					))
   269  				})
   270  
   271  				It("sets the role using the GUID", func() {
   272  					Expect(err).NotTo(HaveOccurred())
   273  					Expect(userRepo.SetSpaceRoleByGUIDCallCount()).To(Equal(1))
   274  					actualUserGUID, actualSpaceGUID, actualOrgGUID, actualRole := userRepo.SetSpaceRoleByGUIDArgsForCall(0)
   275  					Expect(actualUserGUID).To(Equal("the-user-guid"))
   276  					Expect(actualSpaceGUID).To(Equal("the-space-guid"))
   277  					Expect(actualOrgGUID).To(Equal("the-org-guid"))
   278  					Expect(actualRole).To(Equal(models.RoleSpaceManager))
   279  				})
   280  
   281  				Context("when the call to CC fails", func() {
   282  					BeforeEach(func() {
   283  						userRepo.SetSpaceRoleByGUIDReturns(errors.New("user-repo-error"))
   284  					})
   285  
   286  					It("returns an error", func() {
   287  						Expect(err).To(HaveOccurred())
   288  						Expect(err.Error()).To(Equal("user-repo-error"))
   289  					})
   290  				})
   291  			})
   292  
   293  			Context("when the UserRequirement returns a user without a GUID", func() {
   294  				BeforeEach(func() {
   295  					userRequirement.GetUserReturns(models.UserFields{Username: "the-user-name"})
   296  				})
   297  
   298  				It("sets the role using the given username", func() {
   299  					Expect(err).NotTo(HaveOccurred())
   300  					username, spaceGUID, orgGUID, role := userRepo.SetSpaceRoleByUsernameArgsForCall(0)
   301  					Expect(username).To(Equal("the-user-name"))
   302  					Expect(spaceGUID).To(Equal("the-space-guid"))
   303  					Expect(orgGUID).To(Equal("the-org-guid"))
   304  					Expect(role).To(Equal(models.RoleSpaceManager))
   305  				})
   306  
   307  				It("tells the user it assigned the role", func() {
   308  					Expect(err).NotTo(HaveOccurred())
   309  					Expect(ui.Outputs()).To(ContainSubstrings(
   310  						[]string{"Assigning role", "SpaceManager", "the-user-name", "the-org", "the-user-name"},
   311  						[]string{"OK"},
   312  					))
   313  				})
   314  
   315  				Context("when the call to CC fails", func() {
   316  					BeforeEach(func() {
   317  						userRepo.SetSpaceRoleByUsernameReturns(errors.New("user-repo-error"))
   318  					})
   319  
   320  					It("returns an error", func() {
   321  						Expect(err).To(HaveOccurred())
   322  						Expect(err.Error()).To(Equal("user-repo-error"))
   323  					})
   324  				})
   325  			})
   326  		})
   327  	})
   328  })