github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+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/util/testhelpers/configuration"
    17  	testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal"
    18  
    19  	. "code.cloudfoundry.org/cli/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  			Context("when the config version is >=2.37.0", func() {
   114  				BeforeEach(func() {
   115  					configRepo.SetAPIVersion("2.37.0")
   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("unset_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 the config version is <2.37.0", func() {
   177  				BeforeEach(func() {
   178  					configRepo.SetAPIVersion("2.36.0")
   179  				})
   180  
   181  				It("returns a UserRequirement", func() {
   182  					actualRequirements, err := cmd.Requirements(factory, flagContext)
   183  					Expect(err).NotTo(HaveOccurred())
   184  					Expect(factory.NewUserRequirementCallCount()).To(Equal(1))
   185  					actualUsername, actualWantGUID := factory.NewUserRequirementArgsForCall(0)
   186  					Expect(actualUsername).To(Equal("the-user-name"))
   187  					Expect(actualWantGUID).To(BeTrue())
   188  
   189  					Expect(actualRequirements).To(ContainElement(userRequirement))
   190  				})
   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 the UserRequirement returns a user with a GUID", func() {
   213  			BeforeEach(func() {
   214  				userFields := models.UserFields{GUID: "the-user-guid", Username: "the-user-name"}
   215  				userRequirement.GetUserReturns(userFields)
   216  			})
   217  
   218  			It("tells the user it is removing the role", func() {
   219  				Expect(err).NotTo(HaveOccurred())
   220  				Expect(ui.Outputs()).To(ContainSubstrings(
   221  					[]string{"Removing role", "OrgManager", "the-user-name", "the-org", "the-user-name"},
   222  					[]string{"OK"},
   223  				))
   224  			})
   225  
   226  			It("removes the role using the GUID", func() {
   227  				Expect(err).NotTo(HaveOccurred())
   228  				Expect(userRepo.UnsetOrgRoleByGUIDCallCount()).To(Equal(1))
   229  				actualUserGUID, actualOrgGUID, actualRole := userRepo.UnsetOrgRoleByGUIDArgsForCall(0)
   230  				Expect(actualUserGUID).To(Equal("the-user-guid"))
   231  				Expect(actualOrgGUID).To(Equal("the-org-guid"))
   232  				Expect(actualRole).To(Equal(models.RoleOrgManager))
   233  			})
   234  
   235  			Context("when the call to CC fails", func() {
   236  				BeforeEach(func() {
   237  					userRepo.UnsetOrgRoleByGUIDReturns(errors.New("user-repo-error"))
   238  				})
   239  
   240  				It("returns an error", func() {
   241  					Expect(err).To(HaveOccurred())
   242  					Expect(err.Error()).To(Equal("user-repo-error"))
   243  				})
   244  			})
   245  		})
   246  
   247  		Context("when the UserRequirement returns a user without a GUID", func() {
   248  			BeforeEach(func() {
   249  				userRequirement.GetUserReturns(models.UserFields{Username: "the-user-name"})
   250  			})
   251  
   252  			It("removes the role using the given username", func() {
   253  				Expect(err).NotTo(HaveOccurred())
   254  				Expect(userRepo.UnsetOrgRoleByUsernameCallCount()).To(Equal(1))
   255  				username, orgGUID, role := userRepo.UnsetOrgRoleByUsernameArgsForCall(0)
   256  				Expect(username).To(Equal("the-user-name"))
   257  				Expect(orgGUID).To(Equal("the-org-guid"))
   258  				Expect(role).To(Equal(models.RoleOrgManager))
   259  			})
   260  
   261  			It("tells the user it is removing the role", func() {
   262  				Expect(err).NotTo(HaveOccurred())
   263  				Expect(ui.Outputs()).To(ContainSubstrings(
   264  					[]string{"Removing role", "OrgManager", "the-user-name", "the-org", "the-user-name"},
   265  					[]string{"OK"},
   266  				))
   267  			})
   268  
   269  			Context("when the call to CC fails", func() {
   270  				BeforeEach(func() {
   271  					userRepo.UnsetOrgRoleByUsernameReturns(errors.New("user-repo-error"))
   272  				})
   273  
   274  				It("returns an error", func() {
   275  					Expect(err).To(HaveOccurred())
   276  					Expect(err.Error()).To(Equal("user-repo-error"))
   277  				})
   278  			})
   279  		})
   280  	})
   281  })