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