github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+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(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 _, err := cmd.Requirements(factory, flagContext) 115 Expect(err).NotTo(HaveOccurred()) 116 Expect(flagRepo.FindByNameCallCount()).To(Equal(1)) 117 Expect(flagRepo.FindByNameArgsForCall(0)).To(Equal("set_roles_by_username")) 118 }) 119 120 Context("when the set_roles_by_username flag exists and is enabled", func() { 121 BeforeEach(func() { 122 flagRepo.FindByNameReturns(models.FeatureFlag{Enabled: true}, nil) 123 }) 124 125 It("returns a UserRequirement", func() { 126 actualRequirements, err := cmd.Requirements(factory, flagContext) 127 Expect(err).NotTo(HaveOccurred()) 128 Expect(factory.NewUserRequirementCallCount()).To(Equal(1)) 129 actualUsername, actualWantGUID := factory.NewUserRequirementArgsForCall(0) 130 Expect(actualUsername).To(Equal("the-user-name")) 131 Expect(actualWantGUID).To(BeFalse()) 132 133 Expect(actualRequirements).To(ContainElement(userRequirement)) 134 }) 135 }) 136 137 Context("when the set_roles_by_username flag exists and is disabled", func() { 138 BeforeEach(func() { 139 flagRepo.FindByNameReturns(models.FeatureFlag{Enabled: false}, nil) 140 }) 141 142 It("returns a UserRequirement", func() { 143 actualRequirements, err := cmd.Requirements(factory, flagContext) 144 Expect(err).NotTo(HaveOccurred()) 145 Expect(factory.NewUserRequirementCallCount()).To(Equal(1)) 146 actualUsername, actualWantGUID := factory.NewUserRequirementArgsForCall(0) 147 Expect(actualUsername).To(Equal("the-user-name")) 148 Expect(actualWantGUID).To(BeTrue()) 149 150 Expect(actualRequirements).To(ContainElement(userRequirement)) 151 }) 152 }) 153 154 Context("when the set_roles_by_username flag cannot be retrieved", func() { 155 BeforeEach(func() { 156 flagRepo.FindByNameReturns(models.FeatureFlag{}, errors.New("some error")) 157 }) 158 159 It("returns a UserRequirement", func() { 160 actualRequirements, err := cmd.Requirements(factory, flagContext) 161 Expect(err).NotTo(HaveOccurred()) 162 Expect(factory.NewUserRequirementCallCount()).To(Equal(1)) 163 actualUsername, actualWantGUID := factory.NewUserRequirementArgsForCall(0) 164 Expect(actualUsername).To(Equal("the-user-name")) 165 Expect(actualWantGUID).To(BeTrue()) 166 167 Expect(actualRequirements).To(ContainElement(userRequirement)) 168 }) 169 }) 170 }) 171 }) 172 173 Describe("Execute", func() { 174 var err error 175 176 BeforeEach(func() { 177 flagContext.Parse("the-user-name", "the-org-name", "OrgManager") 178 cmd.Requirements(factory, flagContext) 179 180 org := models.Organization{} 181 org.GUID = "the-org-guid" 182 org.Name = "the-org-name" 183 organizationRequirement.GetOrganizationReturns(org) 184 }) 185 186 JustBeforeEach(func() { 187 err = cmd.Execute(flagContext) 188 }) 189 190 Context("when the UserRequirement returns a user with a GUID", func() { 191 BeforeEach(func() { 192 userFields := models.UserFields{GUID: "the-user-guid", Username: "the-user-name"} 193 userRequirement.GetUserReturns(userFields) 194 }) 195 196 It("tells the user it is assigning the role", func() { 197 Expect(err).NotTo(HaveOccurred()) 198 Expect(ui.Outputs()).To(ContainSubstrings( 199 []string{"Assigning role", "OrgManager", "the-user-name", "the-org", "the-user-name"}, 200 []string{"OK"}, 201 )) 202 }) 203 204 It("sets the role using the GUID", func() { 205 Expect(err).NotTo(HaveOccurred()) 206 Expect(userRepo.SetOrgRoleByGUIDCallCount()).To(Equal(1)) 207 actualUserGUID, actualOrgGUID, actualRole := userRepo.SetOrgRoleByGUIDArgsForCall(0) 208 Expect(actualUserGUID).To(Equal("the-user-guid")) 209 Expect(actualOrgGUID).To(Equal("the-org-guid")) 210 Expect(actualRole).To(Equal(models.RoleOrgManager)) 211 }) 212 213 Context("when the call to CC fails", func() { 214 BeforeEach(func() { 215 userRepo.SetOrgRoleByGUIDReturns(errors.New("user-repo-error")) 216 }) 217 218 It("returns an error", func() { 219 Expect(err).To(HaveOccurred()) 220 Expect(err.Error()).To(Equal("user-repo-error")) 221 }) 222 }) 223 }) 224 225 Context("when the UserRequirement returns a user without a GUID", func() { 226 BeforeEach(func() { 227 userRequirement.GetUserReturns(models.UserFields{Username: "the-user-name"}) 228 }) 229 230 It("sets the role using the given username", func() { 231 Expect(err).NotTo(HaveOccurred()) 232 username, orgGUID, role := userRepo.SetOrgRoleByUsernameArgsForCall(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 assigned the role", func() { 239 Expect(err).NotTo(HaveOccurred()) 240 Expect(ui.Outputs()).To(ContainSubstrings( 241 []string{"Assigning 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.SetOrgRoleByUsernameReturns(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 })