github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/user/unset_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("UnsetSpaceRole", 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.UnsetSpaceRole{}
    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 unset_roles_by_username flag", func() {
   123  					_, err := cmd.Requirements(factory, flagContext)
   124  					Expect(err).NotTo(HaveOccurred())
   125  					Expect(flagRepo.FindByNameCallCount()).To(Equal(1))
   126  					Expect(flagRepo.FindByNameArgsForCall(0)).To(Equal("unset_roles_by_username"))
   127  				})
   128  
   129  				Context("when the unset_roles_by_username flag exists and is enabled", func() {
   130  					BeforeEach(func() {
   131  						flagRepo.FindByNameReturns(models.FeatureFlag{Enabled: true}, nil)
   132  					})
   133  
   134  					It("returns a UserRequirement", func() {
   135  						actualRequirements, err := cmd.Requirements(factory, flagContext)
   136  						Expect(err).NotTo(HaveOccurred())
   137  						Expect(factory.NewUserRequirementCallCount()).To(Equal(1))
   138  						actualUsername, actualWantGUID := factory.NewUserRequirementArgsForCall(0)
   139  						Expect(actualUsername).To(Equal("the-user-name"))
   140  						Expect(actualWantGUID).To(BeFalse())
   141  
   142  						Expect(actualRequirements).To(ContainElement(userRequirement))
   143  					})
   144  				})
   145  
   146  				Context("when the unset_roles_by_username flag exists and is disabled", func() {
   147  					BeforeEach(func() {
   148  						flagRepo.FindByNameReturns(models.FeatureFlag{Enabled: false}, nil)
   149  					})
   150  
   151  					It("returns a UserRequirement", func() {
   152  						actualRequirements, err := cmd.Requirements(factory, flagContext)
   153  						Expect(err).NotTo(HaveOccurred())
   154  						Expect(factory.NewUserRequirementCallCount()).To(Equal(1))
   155  						actualUsername, actualWantGUID := factory.NewUserRequirementArgsForCall(0)
   156  						Expect(actualUsername).To(Equal("the-user-name"))
   157  						Expect(actualWantGUID).To(BeTrue())
   158  
   159  						Expect(actualRequirements).To(ContainElement(userRequirement))
   160  					})
   161  				})
   162  
   163  				Context("when the unset_roles_by_username flag cannot be retrieved", func() {
   164  					BeforeEach(func() {
   165  						flagRepo.FindByNameReturns(models.FeatureFlag{}, errors.New("some error"))
   166  					})
   167  
   168  					It("returns a UserRequirement", func() {
   169  						actualRequirements, err := cmd.Requirements(factory, flagContext)
   170  						Expect(err).NotTo(HaveOccurred())
   171  						Expect(factory.NewUserRequirementCallCount()).To(Equal(1))
   172  						actualUsername, actualWantGUID := factory.NewUserRequirementArgsForCall(0)
   173  						Expect(actualUsername).To(Equal("the-user-name"))
   174  						Expect(actualWantGUID).To(BeTrue())
   175  
   176  						Expect(actualRequirements).To(ContainElement(userRequirement))
   177  					})
   178  				})
   179  			})
   180  
   181  			Context("when the config version is <2.37.0", func() {
   182  				BeforeEach(func() {
   183  					configRepo.SetAPIVersion("2.36.0")
   184  				})
   185  
   186  				It("returns a UserRequirement", func() {
   187  					actualRequirements, err := cmd.Requirements(factory, flagContext)
   188  					Expect(err).NotTo(HaveOccurred())
   189  					Expect(factory.NewUserRequirementCallCount()).To(Equal(1))
   190  					actualUsername, actualWantGUID := factory.NewUserRequirementArgsForCall(0)
   191  					Expect(actualUsername).To(Equal("the-user-name"))
   192  					Expect(actualWantGUID).To(BeTrue())
   193  
   194  					Expect(actualRequirements).To(ContainElement(userRequirement))
   195  				})
   196  			})
   197  		})
   198  	})
   199  
   200  	Describe("Execute", func() {
   201  		var (
   202  			org models.Organization
   203  			err error
   204  		)
   205  
   206  		BeforeEach(func() {
   207  			flagContext.Parse("the-user-name", "the-org-name", "the-space-name", "SpaceManager")
   208  			cmd.Requirements(factory, flagContext)
   209  
   210  			org = models.Organization{}
   211  			org.GUID = "the-org-guid"
   212  			org.Name = "the-org-name"
   213  			organizationRequirement.GetOrganizationReturns(org)
   214  		})
   215  
   216  		JustBeforeEach(func() {
   217  			err = cmd.Execute(flagContext)
   218  		})
   219  
   220  		Context("when the space is not found", func() {
   221  			BeforeEach(func() {
   222  				spaceRepo.FindByNameInOrgReturns(models.Space{}, errors.New("space-repo-error"))
   223  			})
   224  
   225  			It("doesn't call CC", func() {
   226  				Expect(userRepo.UnsetSpaceRoleByGUIDCallCount()).To(BeZero())
   227  				Expect(userRepo.UnsetSpaceRoleByUsernameCallCount()).To(BeZero())
   228  			})
   229  
   230  			It("return an error", func() {
   231  				Expect(err).To(HaveOccurred())
   232  				Expect(err.Error()).To(Equal("space-repo-error"))
   233  			})
   234  		})
   235  
   236  		Context("when the space is found", func() {
   237  			BeforeEach(func() {
   238  				space := models.Space{}
   239  				space.GUID = "the-space-guid"
   240  				space.Name = "the-space-name"
   241  				space.Organization = org.OrganizationFields
   242  				spaceRepo.FindByNameInOrgReturns(space, nil)
   243  			})
   244  
   245  			Context("when the UserRequirement returns a user with a GUID", func() {
   246  				BeforeEach(func() {
   247  					userFields := models.UserFields{GUID: "the-user-guid", Username: "the-user-name"}
   248  					userRequirement.GetUserReturns(userFields)
   249  				})
   250  
   251  				It("tells the user it is removing the role", func() {
   252  					Expect(err).NotTo(HaveOccurred())
   253  					Expect(ui.Outputs()).To(ContainSubstrings(
   254  						[]string{"Removing role", "SpaceManager", "the-user-name", "the-org", "the-user-name"},
   255  						[]string{"OK"},
   256  					))
   257  				})
   258  
   259  				It("removes the role using the GUID", func() {
   260  					Expect(err).NotTo(HaveOccurred())
   261  					Expect(userRepo.UnsetSpaceRoleByGUIDCallCount()).To(Equal(1))
   262  					actualUserGUID, actualSpaceGUID, actualRole := userRepo.UnsetSpaceRoleByGUIDArgsForCall(0)
   263  					Expect(actualUserGUID).To(Equal("the-user-guid"))
   264  					Expect(actualSpaceGUID).To(Equal("the-space-guid"))
   265  					Expect(actualRole).To(Equal(models.RoleSpaceManager))
   266  				})
   267  
   268  				Context("when the call to CC fails", func() {
   269  					BeforeEach(func() {
   270  						userRepo.UnsetSpaceRoleByGUIDReturns(errors.New("user-repo-error"))
   271  					})
   272  
   273  					It("return an error", func() {
   274  						Expect(err).To(HaveOccurred())
   275  						Expect(err.Error()).To(Equal("user-repo-error"))
   276  					})
   277  				})
   278  			})
   279  
   280  			Context("when the UserRequirement returns a user without a GUID", func() {
   281  				BeforeEach(func() {
   282  					userRequirement.GetUserReturns(models.UserFields{Username: "the-user-name"})
   283  				})
   284  
   285  				It("removes the role using the given username", func() {
   286  					Expect(err).NotTo(HaveOccurred())
   287  					Expect(userRepo.UnsetSpaceRoleByUsernameCallCount()).To(Equal(1))
   288  					actualUsername, actualSpaceGUID, actualRole := userRepo.UnsetSpaceRoleByUsernameArgsForCall(0)
   289  					Expect(actualUsername).To(Equal("the-user-name"))
   290  					Expect(actualSpaceGUID).To(Equal("the-space-guid"))
   291  					Expect(actualRole).To(Equal(models.RoleSpaceManager))
   292  				})
   293  
   294  				It("tells the user it is removing the role", func() {
   295  					Expect(err).NotTo(HaveOccurred())
   296  					Expect(ui.Outputs()).To(ContainSubstrings(
   297  						[]string{"Removing role", "SpaceManager", "the-user-name", "the-org", "the-user-name"},
   298  						[]string{"OK"},
   299  					))
   300  				})
   301  
   302  				Context("when the call to CC fails", func() {
   303  					BeforeEach(func() {
   304  						userRepo.UnsetSpaceRoleByUsernameReturns(errors.New("user-repo-error"))
   305  					})
   306  
   307  					It("return an error", func() {
   308  						Expect(err).To(HaveOccurred())
   309  						Expect(err.Error()).To(Equal("user-repo-error"))
   310  					})
   311  				})
   312  			})
   313  		})
   314  	})
   315  })