github.com/sleungcy-sap/cli@v7.1.0+incompatible/command/v7/set_space_role_command_test.go (about) 1 package v7_test 2 3 import ( 4 "code.cloudfoundry.org/cli/actor/v7action" 5 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 6 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant" 7 "code.cloudfoundry.org/cli/cf/errors" 8 "code.cloudfoundry.org/cli/command/flag" 9 "code.cloudfoundry.org/cli/command/translatableerror" 10 "code.cloudfoundry.org/cli/command/v7/v7fakes" 11 "code.cloudfoundry.org/cli/resources" 12 "code.cloudfoundry.org/cli/util/configv3" 13 14 "code.cloudfoundry.org/cli/command/commandfakes" 15 . "code.cloudfoundry.org/cli/command/v7" 16 "code.cloudfoundry.org/cli/util/ui" 17 . "github.com/onsi/ginkgo" 18 . "github.com/onsi/gomega" 19 . "github.com/onsi/gomega/gbytes" 20 ) 21 22 var _ = Describe("set-space-role Command", func() { 23 var ( 24 cmd SetSpaceRoleCommand 25 testUI *ui.UI 26 fakeConfig *commandfakes.FakeConfig 27 fakeSharedActor *commandfakes.FakeSharedActor 28 fakeActor *v7fakes.FakeActor 29 binaryName string 30 executeErr error 31 input *Buffer 32 ) 33 34 BeforeEach(func() { 35 input = NewBuffer() 36 testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer()) 37 fakeConfig = new(commandfakes.FakeConfig) 38 fakeSharedActor = new(commandfakes.FakeSharedActor) 39 fakeActor = new(v7fakes.FakeActor) 40 41 cmd = SetSpaceRoleCommand{ 42 BaseCommand: BaseCommand{ 43 UI: testUI, 44 Config: fakeConfig, 45 SharedActor: fakeSharedActor, 46 Actor: fakeActor, 47 }, 48 } 49 50 binaryName = "faceman" 51 fakeConfig.BinaryNameReturns(binaryName) 52 }) 53 54 JustBeforeEach(func() { 55 executeErr = cmd.Execute(nil) 56 }) 57 58 BeforeEach(func() { 59 fakeConfig.CurrentUserReturns(configv3.User{Name: "current-user"}, nil) 60 61 fakeActor.GetOrganizationByNameReturns( 62 resources.Organization{ 63 GUID: "some-org-guid", 64 Name: "some-org-name", 65 Metadata: nil, 66 }, 67 v7action.Warnings{"get-org-warning"}, 68 nil, 69 ) 70 71 fakeActor.GetSpaceByNameAndOrganizationReturns( 72 resources.Space{GUID: "some-space-guid", Name: "some-space-name"}, 73 v7action.Warnings{"get-space-warning"}, 74 nil, 75 ) 76 }) 77 78 When("neither origin nor client flag is provided", func() { 79 BeforeEach(func() { 80 cmd.Args.Organization = "some-org-name" 81 cmd.Args.Space = "some-space-name" 82 cmd.Args.Role = flag.SpaceRole{Role: "SpaceDeveloper"} 83 cmd.Args.Username = "target-user-name" 84 }) 85 86 It("creates the space role", func() { 87 Expect(fakeActor.CreateSpaceRoleCallCount()).To(Equal(1)) 88 givenRoleType, givenOrgGUID, givenSpaceGUID, givenUserName, givenOrigin, givenIsClient := fakeActor.CreateSpaceRoleArgsForCall(0) 89 Expect(givenRoleType).To(Equal(constant.SpaceDeveloperRole)) 90 Expect(givenOrgGUID).To(Equal("some-org-guid")) 91 Expect(givenSpaceGUID).To(Equal("some-space-guid")) 92 Expect(givenUserName).To(Equal("target-user-name")) 93 Expect(givenOrigin).To(Equal("")) 94 Expect(givenIsClient).To(BeFalse()) 95 }) 96 97 It("displays flavor text and returns without error", func() { 98 Expect(testUI.Out).To(Say("Assigning role SpaceDeveloper to user target-user-name in org some-org-name / space some-space-name as current-user...")) 99 Expect(testUI.Out).To(Say("OK")) 100 Expect(executeErr).NotTo(HaveOccurred()) 101 }) 102 }) 103 104 When("origin flag is provided", func() { 105 BeforeEach(func() { 106 cmd.Args.Organization = "some-org-name" 107 cmd.Args.Space = "some-space-name" 108 cmd.Args.Role = flag.SpaceRole{Role: "SpaceAuditor"} 109 cmd.Args.Username = "target-user-name" 110 cmd.Origin = "ldap" 111 }) 112 113 It("creates the space role", func() { 114 Expect(fakeActor.CreateSpaceRoleCallCount()).To(Equal(1)) 115 givenRoleType, givenOrgGUID, givenSpaceGUID, givenUserName, givenOrigin, givenIsClient := fakeActor.CreateSpaceRoleArgsForCall(0) 116 Expect(givenRoleType).To(Equal(constant.SpaceAuditorRole)) 117 Expect(givenOrgGUID).To(Equal("some-org-guid")) 118 Expect(givenSpaceGUID).To(Equal("some-space-guid")) 119 Expect(givenUserName).To(Equal("target-user-name")) 120 Expect(givenOrigin).To(Equal("ldap")) 121 Expect(givenIsClient).To(BeFalse()) 122 }) 123 124 It("displays flavor text and returns without error", func() { 125 Expect(testUI.Out).To(Say("Assigning role SpaceAuditor to user target-user-name in org some-org-name / space some-space-name as current-user...")) 126 Expect(testUI.Out).To(Say("OK")) 127 Expect(executeErr).NotTo(HaveOccurred()) 128 }) 129 }) 130 131 When("client flag is provided", func() { 132 BeforeEach(func() { 133 cmd.Args.Organization = "some-org-name" 134 cmd.Args.Space = "some-space-name" 135 cmd.Args.Role = flag.SpaceRole{Role: "SpaceAuditor"} 136 cmd.Args.Username = "target-user-name" 137 cmd.IsClient = true 138 }) 139 140 It("does not try to get the user", func() { 141 Expect(fakeActor.GetUserCallCount()).To(Equal(0)) 142 }) 143 144 It("creates the space role correctly", func() { 145 givenRoleType, givenOrgGUID, givenSpaceGUID, givenUserName, givenOrigin, givenIsClient := fakeActor.CreateSpaceRoleArgsForCall(0) 146 Expect(givenRoleType).To(Equal(constant.SpaceAuditorRole)) 147 Expect(givenOrgGUID).To(Equal("some-org-guid")) 148 Expect(givenSpaceGUID).To(Equal("some-space-guid")) 149 Expect(givenUserName).To(Equal("target-user-name")) 150 Expect(givenOrigin).To(Equal("")) 151 Expect(givenIsClient).To(BeTrue()) 152 }) 153 154 It("displays flavor text and returns without error", func() { 155 Expect(testUI.Out).To(Say("Assigning role SpaceAuditor to user target-user-name in org some-org-name / space some-space-name as current-user...")) 156 Expect(testUI.Out).To(Say("OK")) 157 Expect(executeErr).NotTo(HaveOccurred()) 158 }) 159 }) 160 161 When("both client and origin flags are provided", func() { 162 BeforeEach(func() { 163 cmd.Args.Organization = "some-org-name" 164 cmd.Args.Space = "some-space-name" 165 cmd.Args.Role = flag.SpaceRole{Role: "SpaceAuditor"} 166 cmd.Args.Username = "target-user-name" 167 cmd.Origin = "ldap" 168 cmd.IsClient = true 169 }) 170 171 It("returns an error", func() { 172 Expect(executeErr).To(MatchError(translatableerror.ArgumentCombinationError{ 173 Args: []string{"--client", "--origin"}, 174 })) 175 }) 176 }) 177 178 When("the role already exists", func() { 179 BeforeEach(func() { 180 cmd.Args.Organization = "some-org-name" 181 cmd.Args.Space = "some-space-name" 182 cmd.Args.Role = flag.SpaceRole{Role: "SpaceAuditor"} 183 cmd.Args.Username = "target-user-name" 184 185 fakeActor.CreateSpaceRoleReturns( 186 v7action.Warnings{"create-role-warning"}, 187 ccerror.RoleAlreadyExistsError{}, 188 ) 189 }) 190 191 It("displays warnings and returns without error", func() { 192 Expect(testUI.Err).To(Say("create-role-warning")) 193 Expect(testUI.Err).To(Say("User 'target-user-name' already has role 'SpaceAuditor' in org 'some-org-name' / space 'some-space-name'.")) 194 Expect(testUI.Out).To(Say("OK")) 195 Expect(executeErr).NotTo(HaveOccurred()) 196 }) 197 }) 198 199 When("invalid role arg is given", func() { 200 BeforeEach(func() { 201 cmd.Args.Role = flag.SpaceRole{Role: "Astronaut"} 202 }) 203 204 It("returns an error", func() { 205 Expect(executeErr).To(MatchError("Invalid role type.")) 206 }) 207 }) 208 209 When("getting the org fails", func() { 210 BeforeEach(func() { 211 cmd.Args.Role = flag.SpaceRole{Role: "SpaceAuditor"} 212 213 fakeActor.GetOrganizationByNameReturns( 214 resources.Organization{}, 215 v7action.Warnings{"get-user-warning"}, 216 errors.New("get-org-error"), 217 ) 218 }) 219 220 It("displays warnings and returns an error", func() { 221 Expect(testUI.Err).To(Say("get-user-warning")) 222 Expect(executeErr).To(MatchError("get-org-error")) 223 }) 224 }) 225 226 When("getting the space fails", func() { 227 BeforeEach(func() { 228 cmd.Args.Role = flag.SpaceRole{Role: "SpaceAuditor"} 229 230 fakeActor.GetSpaceByNameAndOrganizationReturns( 231 resources.Space{}, 232 v7action.Warnings{"get-space-warning"}, 233 errors.New("get-space-error"), 234 ) 235 }) 236 237 It("displays warnings and returns an error", func() { 238 Expect(testUI.Err).To(Say("get-space-warning")) 239 Expect(executeErr).To(MatchError("get-space-error")) 240 }) 241 }) 242 243 When("creating the space role fails", func() { 244 BeforeEach(func() { 245 cmd.Args.Organization = "some-org-name" 246 cmd.Args.Space = "some-space-name" 247 cmd.Args.Role = flag.SpaceRole{Role: "SpaceAuditor"} 248 cmd.Args.Username = "target-user-name" 249 250 fakeActor.CreateSpaceRoleReturns( 251 v7action.Warnings{"create-role-warning"}, 252 errors.New("create-role-error"), 253 ) 254 }) 255 256 It("displays warnings and returns without error", func() { 257 Expect(testUI.Err).To(Say("create-role-warning")) 258 Expect(executeErr).To(MatchError("create-role-error")) 259 }) 260 }) 261 })