github.com/cloudfoundry/cli@v7.1.0+incompatible/cf/commands/user/unset_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  	testapi "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("UnsetOrgRole", func() {
    25  	var (
    26  		ui         *testterm.FakeUI
    27  		configRepo coreconfig.Repository
    28  		userRepo   *testapi.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 = &testapi.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.UnsetOrgRole{}
    56  		cmd.SetDependency(deps, false)
    57  
    58  		flagContext = flags.NewFlagContext(map[string]flags.FlagSet{})
    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  
    69  		organizationRequirement = new(requirementsfakes.FakeOrganizationRequirement)
    70  		organizationRequirement.ExecuteReturns(nil)
    71  		factory.NewOrganizationRequirementReturns(organizationRequirement)
    72  	})
    73  
    74  	Describe("Requirements", func() {
    75  		Context("when not provided exactly three args", func() {
    76  			BeforeEach(func() {
    77  				flagContext.Parse("the-user-name", "the-org-name")
    78  			})
    79  
    80  			It("fails with usage", func() {
    81  				_, err := cmd.Requirements(factory, flagContext)
    82  				Expect(err).To(HaveOccurred())
    83  				Expect(ui.Outputs()).To(ContainSubstrings(
    84  					[]string{"Incorrect Usage. Requires USERNAME, ORG, ROLE as arguments"},
    85  					[]string{"NAME"},
    86  					[]string{"USAGE"},
    87  				))
    88  			})
    89  		})
    90  
    91  		Context("when provided three args", func() {
    92  			BeforeEach(func() {
    93  				flagContext.Parse("the-user-name", "the-org-name", "OrgManager")
    94  			})
    95  
    96  			It("returns a LoginRequirement", func() {
    97  				actualRequirements, err := cmd.Requirements(factory, flagContext)
    98  				Expect(err).NotTo(HaveOccurred())
    99  				Expect(factory.NewLoginRequirementCallCount()).To(Equal(1))
   100  
   101  				Expect(actualRequirements).To(ContainElement(loginRequirement))
   102  			})
   103  
   104  			It("returns an OrgRequirement", func() {
   105  				actualRequirements, err := cmd.Requirements(factory, flagContext)
   106  				Expect(err).NotTo(HaveOccurred())
   107  				Expect(factory.NewOrganizationRequirementCallCount()).To(Equal(1))
   108  				Expect(factory.NewOrganizationRequirementArgsForCall(0)).To(Equal("the-org-name"))
   109  
   110  				Expect(actualRequirements).To(ContainElement(organizationRequirement))
   111  			})
   112  
   113  			It("requests the set_roles_by_username flag", func() {
   114  				cmd.Requirements(factory, flagContext)
   115  				Expect(flagRepo.FindByNameCallCount()).To(Equal(1))
   116  				Expect(flagRepo.FindByNameArgsForCall(0)).To(Equal("unset_roles_by_username"))
   117  			})
   118  
   119  			Context("when the set_roles_by_username flag exists and is enabled", func() {
   120  				BeforeEach(func() {
   121  					flagRepo.FindByNameReturns(models.FeatureFlag{Enabled: true}, nil)
   122  				})
   123  
   124  				It("returns a UserRequirement", func() {
   125  					actualRequirements, err := cmd.Requirements(factory, flagContext)
   126  					Expect(err).NotTo(HaveOccurred())
   127  					Expect(factory.NewUserRequirementCallCount()).To(Equal(1))
   128  					actualUsername, actualWantGUID := factory.NewUserRequirementArgsForCall(0)
   129  					Expect(actualUsername).To(Equal("the-user-name"))
   130  					Expect(actualWantGUID).To(BeFalse())
   131  
   132  					Expect(actualRequirements).To(ContainElement(userRequirement))
   133  				})
   134  			})
   135  
   136  			Context("when the set_roles_by_username flag exists and is disabled", func() {
   137  				BeforeEach(func() {
   138  					flagRepo.FindByNameReturns(models.FeatureFlag{Enabled: false}, nil)
   139  				})
   140  
   141  				It("returns a UserRequirement", func() {
   142  					actualRequirements, err := cmd.Requirements(factory, flagContext)
   143  					Expect(err).NotTo(HaveOccurred())
   144  					Expect(factory.NewUserRequirementCallCount()).To(Equal(1))
   145  					actualUsername, actualWantGUID := factory.NewUserRequirementArgsForCall(0)
   146  					Expect(actualUsername).To(Equal("the-user-name"))
   147  					Expect(actualWantGUID).To(BeTrue())
   148  
   149  					Expect(actualRequirements).To(ContainElement(userRequirement))
   150  				})
   151  			})
   152  
   153  			Context("when the set_roles_by_username flag cannot be retrieved", func() {
   154  				BeforeEach(func() {
   155  					flagRepo.FindByNameReturns(models.FeatureFlag{}, errors.New("some error"))
   156  				})
   157  
   158  				It("returns a UserRequirement", func() {
   159  					actualRequirements, err := cmd.Requirements(factory, flagContext)
   160  					Expect(err).NotTo(HaveOccurred())
   161  					Expect(factory.NewUserRequirementCallCount()).To(Equal(1))
   162  					actualUsername, actualWantGUID := factory.NewUserRequirementArgsForCall(0)
   163  					Expect(actualUsername).To(Equal("the-user-name"))
   164  					Expect(actualWantGUID).To(BeTrue())
   165  
   166  					Expect(actualRequirements).To(ContainElement(userRequirement))
   167  				})
   168  			})
   169  		})
   170  	})
   171  
   172  	Describe("Execute", func() {
   173  		var err error
   174  
   175  		BeforeEach(func() {
   176  			flagContext.Parse("the-user-name", "the-org-name", "OrgManager")
   177  			cmd.Requirements(factory, flagContext)
   178  
   179  			org := models.Organization{}
   180  			org.GUID = "the-org-guid"
   181  			org.Name = "the-org-name"
   182  			organizationRequirement.GetOrganizationReturns(org)
   183  		})
   184  
   185  		JustBeforeEach(func() {
   186  			err = cmd.Execute(flagContext)
   187  		})
   188  
   189  		Context("when the UserRequirement returns a user with a GUID", func() {
   190  			BeforeEach(func() {
   191  				userFields := models.UserFields{GUID: "the-user-guid", Username: "the-user-name"}
   192  				userRequirement.GetUserReturns(userFields)
   193  			})
   194  
   195  			It("tells the user it is removing the role", func() {
   196  				Expect(err).NotTo(HaveOccurred())
   197  				Expect(ui.Outputs()).To(ContainSubstrings(
   198  					[]string{"Removing role", "OrgManager", "the-user-name", "the-org", "the-user-name"},
   199  					[]string{"OK"},
   200  				))
   201  			})
   202  
   203  			It("removes the role using the GUID", func() {
   204  				Expect(err).NotTo(HaveOccurred())
   205  				Expect(userRepo.UnsetOrgRoleByGUIDCallCount()).To(Equal(1))
   206  				actualUserGUID, actualOrgGUID, actualRole := userRepo.UnsetOrgRoleByGUIDArgsForCall(0)
   207  				Expect(actualUserGUID).To(Equal("the-user-guid"))
   208  				Expect(actualOrgGUID).To(Equal("the-org-guid"))
   209  				Expect(actualRole).To(Equal(models.RoleOrgManager))
   210  			})
   211  
   212  			Context("when the call to CC fails", func() {
   213  				BeforeEach(func() {
   214  					userRepo.UnsetOrgRoleByGUIDReturns(errors.New("user-repo-error"))
   215  				})
   216  
   217  				It("returns an error", func() {
   218  					Expect(err).To(HaveOccurred())
   219  					Expect(err.Error()).To(Equal("user-repo-error"))
   220  				})
   221  			})
   222  		})
   223  
   224  		Context("when the UserRequirement returns a user without a GUID", func() {
   225  			BeforeEach(func() {
   226  				userRequirement.GetUserReturns(models.UserFields{Username: "the-user-name"})
   227  			})
   228  
   229  			It("removes the role using the given username", func() {
   230  				Expect(err).NotTo(HaveOccurred())
   231  				Expect(userRepo.UnsetOrgRoleByUsernameCallCount()).To(Equal(1))
   232  				username, orgGUID, role := userRepo.UnsetOrgRoleByUsernameArgsForCall(0)
   233  				Expect(username).To(Equal("the-user-name"))
   234  				Expect(orgGUID).To(Equal("the-org-guid"))
   235  				Expect(role).To(Equal(models.RoleOrgManager))
   236  			})
   237  
   238  			It("tells the user it is removing the role", func() {
   239  				Expect(err).NotTo(HaveOccurred())
   240  				Expect(ui.Outputs()).To(ContainSubstrings(
   241  					[]string{"Removing role", "OrgManager", "the-user-name", "the-org", "the-user-name"},
   242  					[]string{"OK"},
   243  				))
   244  			})
   245  
   246  			Context("when the call to CC fails", func() {
   247  				BeforeEach(func() {
   248  					userRepo.UnsetOrgRoleByUsernameReturns(errors.New("user-repo-error"))
   249  				})
   250  
   251  				It("returns an error", func() {
   252  					Expect(err).To(HaveOccurred())
   253  					Expect(err.Error()).To(Equal("user-repo-error"))
   254  				})
   255  			})
   256  		})
   257  	})
   258  })