github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+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/util/testhelpers/configuration"
    18  	testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal"
    19  
    20  	. "code.cloudfoundry.org/cli/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(map[string]flags.FlagSet{})
    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  
    73  		organizationRequirement = new(requirementsfakes.FakeOrganizationRequirement)
    74  		organizationRequirement.ExecuteReturns(nil)
    75  		factory.NewOrganizationRequirementReturns(organizationRequirement)
    76  	})
    77  
    78  	Describe("Requirements", func() {
    79  		Context("when not provided exactly four args", func() {
    80  			BeforeEach(func() {
    81  				flagContext.Parse("the-user-name", "the-org-name", "the-space-name")
    82  			})
    83  
    84  			It("fails with usage", func() {
    85  				_, err := cmd.Requirements(factory, flagContext)
    86  				Expect(err).To(HaveOccurred())
    87  				Expect(ui.Outputs()).To(ContainSubstrings(
    88  					[]string{"Incorrect Usage. Requires USERNAME, ORG, SPACE, ROLE as arguments"},
    89  					[]string{"NAME"},
    90  					[]string{"USAGE"},
    91  				))
    92  			})
    93  		})
    94  
    95  		Context("when provided four args", func() {
    96  			BeforeEach(func() {
    97  				flagContext.Parse("the-user-name", "the-org-name", "the-space-name", "SpaceManager")
    98  			})
    99  
   100  			It("returns a LoginRequirement", func() {
   101  				actualRequirements, err := cmd.Requirements(factory, flagContext)
   102  				Expect(err).NotTo(HaveOccurred())
   103  				Expect(factory.NewLoginRequirementCallCount()).To(Equal(1))
   104  
   105  				Expect(actualRequirements).To(ContainElement(loginRequirement))
   106  			})
   107  
   108  			It("returns an OrgRequirement", func() {
   109  				actualRequirements, err := cmd.Requirements(factory, flagContext)
   110  				Expect(err).NotTo(HaveOccurred())
   111  				Expect(factory.NewOrganizationRequirementCallCount()).To(Equal(1))
   112  				Expect(factory.NewOrganizationRequirementArgsForCall(0)).To(Equal("the-org-name"))
   113  
   114  				Expect(actualRequirements).To(ContainElement(organizationRequirement))
   115  			})
   116  
   117  			Context("when the config version is >=2.37.0", func() {
   118  				BeforeEach(func() {
   119  					configRepo.SetAPIVersion("2.37.0")
   120  				})
   121  
   122  				It("requests the set_roles_by_username flag", func() {
   123  					cmd.Requirements(factory, flagContext)
   124  					Expect(flagRepo.FindByNameCallCount()).To(Equal(1))
   125  					Expect(flagRepo.FindByNameArgsForCall(0)).To(Equal("set_roles_by_username"))
   126  				})
   127  
   128  				Context("when the set_roles_by_username flag exists and is enabled", func() {
   129  					BeforeEach(func() {
   130  						flagRepo.FindByNameReturns(models.FeatureFlag{Enabled: true}, nil)
   131  					})
   132  
   133  					It("returns a UserRequirement", func() {
   134  						actualRequirements, err := cmd.Requirements(factory, flagContext)
   135  						Expect(err).NotTo(HaveOccurred())
   136  						Expect(factory.NewUserRequirementCallCount()).To(Equal(1))
   137  						actualUsername, actualWantGUID := factory.NewUserRequirementArgsForCall(0)
   138  						Expect(actualUsername).To(Equal("the-user-name"))
   139  						Expect(actualWantGUID).To(BeFalse())
   140  
   141  						Expect(actualRequirements).To(ContainElement(userRequirement))
   142  					})
   143  				})
   144  
   145  				Context("when the set_roles_by_username flag exists and is disabled", func() {
   146  					BeforeEach(func() {
   147  						flagRepo.FindByNameReturns(models.FeatureFlag{Enabled: false}, nil)
   148  					})
   149  
   150  					It("returns a UserRequirement", func() {
   151  						actualRequirements, err := cmd.Requirements(factory, flagContext)
   152  						Expect(err).NotTo(HaveOccurred())
   153  						Expect(factory.NewUserRequirementCallCount()).To(Equal(1))
   154  						actualUsername, actualWantGUID := factory.NewUserRequirementArgsForCall(0)
   155  						Expect(actualUsername).To(Equal("the-user-name"))
   156  						Expect(actualWantGUID).To(BeTrue())
   157  
   158  						Expect(actualRequirements).To(ContainElement(userRequirement))
   159  					})
   160  				})
   161  
   162  				Context("when the set_roles_by_username flag cannot be retrieved", func() {
   163  					BeforeEach(func() {
   164  						flagRepo.FindByNameReturns(models.FeatureFlag{}, errors.New("some error"))
   165  					})
   166  
   167  					It("returns a UserRequirement", func() {
   168  						actualRequirements, err := cmd.Requirements(factory, flagContext)
   169  						Expect(err).NotTo(HaveOccurred())
   170  						Expect(factory.NewUserRequirementCallCount()).To(Equal(1))
   171  						actualUsername, actualWantGUID := factory.NewUserRequirementArgsForCall(0)
   172  						Expect(actualUsername).To(Equal("the-user-name"))
   173  						Expect(actualWantGUID).To(BeTrue())
   174  
   175  						Expect(actualRequirements).To(ContainElement(userRequirement))
   176  					})
   177  				})
   178  			})
   179  
   180  			Context("when the config version is <2.37.0", func() {
   181  				BeforeEach(func() {
   182  					configRepo.SetAPIVersion("2.36.0")
   183  				})
   184  
   185  				It("returns a UserRequirement", func() {
   186  					actualRequirements, err := cmd.Requirements(factory, flagContext)
   187  					Expect(err).NotTo(HaveOccurred())
   188  					Expect(factory.NewUserRequirementCallCount()).To(Equal(1))
   189  					actualUsername, actualWantGUID := factory.NewUserRequirementArgsForCall(0)
   190  					Expect(actualUsername).To(Equal("the-user-name"))
   191  					Expect(actualWantGUID).To(BeTrue())
   192  
   193  					Expect(actualRequirements).To(ContainElement(userRequirement))
   194  				})
   195  			})
   196  		})
   197  	})
   198  
   199  	Describe("Execute", func() {
   200  		var (
   201  			org models.Organization
   202  			err error
   203  		)
   204  
   205  		BeforeEach(func() {
   206  			flagContext.Parse("the-user-name", "the-org-name", "the-space-name", "SpaceManager")
   207  			cmd.Requirements(factory, flagContext)
   208  
   209  			org = models.Organization{}
   210  			org.GUID = "the-org-guid"
   211  			org.Name = "the-org-name"
   212  			organizationRequirement.GetOrganizationReturns(org)
   213  		})
   214  
   215  		JustBeforeEach(func() {
   216  			err = cmd.Execute(flagContext)
   217  		})
   218  
   219  		Context("when the space is not found", func() {
   220  			BeforeEach(func() {
   221  				spaceRepo.FindByNameInOrgReturns(models.Space{}, errors.New("space-repo-error"))
   222  			})
   223  
   224  			It("doesn't call CC", func() {
   225  				Expect(userRepo.SetSpaceRoleByGUIDCallCount()).To(BeZero())
   226  				Expect(userRepo.SetSpaceRoleByUsernameCallCount()).To(BeZero())
   227  			})
   228  
   229  			It("returns an error", func() {
   230  				Expect(err).To(HaveOccurred())
   231  				Expect(err.Error()).To(Equal("space-repo-error"))
   232  			})
   233  		})
   234  
   235  		Context("when the space is found", func() {
   236  			BeforeEach(func() {
   237  				space := models.Space{}
   238  				space.GUID = "the-space-guid"
   239  				space.Name = "the-space-name"
   240  				spaceRepo.FindByNameInOrgReturns(space, nil)
   241  			})
   242  
   243  			Context("when the UserRequirement returns a user with a GUID", func() {
   244  				BeforeEach(func() {
   245  					userFields := models.UserFields{GUID: "the-user-guid", Username: "the-user-name"}
   246  					userRequirement.GetUserReturns(userFields)
   247  				})
   248  
   249  				It("tells the user it is assigning the role", func() {
   250  					Expect(err).NotTo(HaveOccurred())
   251  					Expect(ui.Outputs()).To(ContainSubstrings(
   252  						[]string{"Assigning role", "SpaceManager", "the-user-name", "the-org", "the-user-name"},
   253  						[]string{"OK"},
   254  					))
   255  				})
   256  
   257  				It("sets the role using the GUID", func() {
   258  					Expect(err).NotTo(HaveOccurred())
   259  					Expect(userRepo.SetSpaceRoleByGUIDCallCount()).To(Equal(1))
   260  					actualUserGUID, actualSpaceGUID, actualOrgGUID, actualRole := userRepo.SetSpaceRoleByGUIDArgsForCall(0)
   261  					Expect(actualUserGUID).To(Equal("the-user-guid"))
   262  					Expect(actualSpaceGUID).To(Equal("the-space-guid"))
   263  					Expect(actualOrgGUID).To(Equal("the-org-guid"))
   264  					Expect(actualRole).To(Equal(models.RoleSpaceManager))
   265  				})
   266  
   267  				Context("when the call to CC fails", func() {
   268  					BeforeEach(func() {
   269  						userRepo.SetSpaceRoleByGUIDReturns(errors.New("user-repo-error"))
   270  					})
   271  
   272  					It("returns an error", func() {
   273  						Expect(err).To(HaveOccurred())
   274  						Expect(err.Error()).To(Equal("user-repo-error"))
   275  					})
   276  				})
   277  			})
   278  
   279  			Context("when the UserRequirement returns a user without a GUID", func() {
   280  				BeforeEach(func() {
   281  					userRequirement.GetUserReturns(models.UserFields{Username: "the-user-name"})
   282  				})
   283  
   284  				It("sets the role using the given username", func() {
   285  					Expect(err).NotTo(HaveOccurred())
   286  					username, spaceGUID, orgGUID, role := userRepo.SetSpaceRoleByUsernameArgsForCall(0)
   287  					Expect(username).To(Equal("the-user-name"))
   288  					Expect(spaceGUID).To(Equal("the-space-guid"))
   289  					Expect(orgGUID).To(Equal("the-org-guid"))
   290  					Expect(role).To(Equal(models.RoleSpaceManager))
   291  				})
   292  
   293  				It("tells the user it assigned the role", func() {
   294  					Expect(err).NotTo(HaveOccurred())
   295  					Expect(ui.Outputs()).To(ContainSubstrings(
   296  						[]string{"Assigning role", "SpaceManager", "the-user-name", "the-org", "the-user-name"},
   297  						[]string{"OK"},
   298  					))
   299  				})
   300  
   301  				Context("when the call to CC fails", func() {
   302  					BeforeEach(func() {
   303  						userRepo.SetSpaceRoleByUsernameReturns(errors.New("user-repo-error"))
   304  					})
   305  
   306  					It("returns an error", func() {
   307  						Expect(err).To(HaveOccurred())
   308  						Expect(err.Error()).To(Equal("user-repo-error"))
   309  					})
   310  				})
   311  			})
   312  		})
   313  	})
   314  })