github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/api/cloudcontroller/ccv3/user_test.go (about) 1 package ccv3_test 2 3 import ( 4 "net/http" 5 6 "fmt" 7 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 9 . "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 10 "code.cloudfoundry.org/cli/resources" 11 . "github.com/onsi/ginkgo" 12 . "github.com/onsi/gomega" 13 . "github.com/onsi/gomega/ghttp" 14 ) 15 16 var _ = Describe("User", func() { 17 var client *Client 18 19 BeforeEach(func() { 20 client, _ = NewTestClient() 21 }) 22 23 Describe("CreateUser", func() { 24 var ( 25 warnings Warnings 26 executeErr error 27 user resources.User 28 ) 29 30 JustBeforeEach(func() { 31 user, warnings, executeErr = client.CreateUser("some-uaa-guid") 32 }) 33 34 When("no error occurs", func() { 35 BeforeEach(func() { 36 response := `{ 37 "guid": "some-uaa-guid", 38 "username": "some-user-name", 39 "presentation_name": "some-user-name", 40 "origin": "ldap", 41 "created_at": "2016-12-07T18:18:30Z", 42 "updated_at": null 43 44 }` 45 expectedBody := `{"guid":"some-uaa-guid"}` 46 server.AppendHandlers( 47 CombineHandlers( 48 VerifyRequest(http.MethodPost, "/v3/users"), 49 VerifyJSON(expectedBody), 50 RespondWith(http.StatusCreated, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}), 51 ), 52 ) 53 }) 54 55 It("creates and returns the user and all warnings", func() { 56 Expect(executeErr).ToNot(HaveOccurred()) 57 58 Expect(user).To(Equal(resources.User{GUID: "some-uaa-guid", Username: "some-user-name", PresentationName: "some-user-name", Origin: "ldap"})) 59 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 60 }) 61 }) 62 63 When("the cloud controller returns errors and warnings", func() { 64 BeforeEach(func() { 65 response := `{ 66 "errors": [ 67 { 68 "code": 10008, 69 "detail": "The request is semantically invalid: command presence", 70 "title": "CF-UnprocessableEntity" 71 }, 72 { 73 "code": 10010, 74 "detail": "Isolation segment not found", 75 "title": "CF-ResourceNotFound" 76 } 77 ] 78 }` 79 server.AppendHandlers( 80 CombineHandlers( 81 VerifyRequest(http.MethodPost, "/v3/users"), 82 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 83 ), 84 ) 85 }) 86 87 It("returns the error and all warnings", func() { 88 Expect(executeErr).To(MatchError(ccerror.MultiError{ 89 ResponseCode: http.StatusTeapot, 90 Errors: []ccerror.V3Error{ 91 { 92 Code: 10008, 93 Detail: "The request is semantically invalid: command presence", 94 Title: "CF-UnprocessableEntity", 95 }, 96 { 97 Code: 10010, 98 Detail: "Isolation segment not found", 99 Title: "CF-ResourceNotFound", 100 }, 101 }, 102 })) 103 Expect(warnings).To(ConsistOf("this is a warning")) 104 }) 105 }) 106 }) 107 108 Describe("DeleteUser", func() { 109 var ( 110 jobUrl JobURL 111 warnings Warnings 112 executeErr error 113 ) 114 115 JustBeforeEach(func() { 116 jobUrl, warnings, executeErr = client.DeleteUser("some-uaa-guid") 117 }) 118 119 When("no error occurs", func() { 120 BeforeEach(func() { 121 response := `{}` 122 server.AppendHandlers( 123 CombineHandlers( 124 VerifyRequest(http.MethodDelete, "/v3/users/some-uaa-guid"), 125 RespondWith(http.StatusCreated, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}, "Location": []string{"job-url"}}), 126 ), 127 ) 128 }) 129 130 It("deletes and returns all warnings", func() { 131 Expect(executeErr).ToNot(HaveOccurred()) 132 133 Expect(jobUrl).To(Equal(JobURL("job-url"))) 134 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 135 }) 136 }) 137 138 When("the cloud controller returns errors and warnings", func() { 139 When("the error should be raise", func() { 140 141 BeforeEach(func() { 142 response := `{ 143 "errors": [ 144 { 145 "code": 10008, 146 "detail": "The request is semantically invalid: command presence", 147 "title": "CF-UnprocessableEntity" 148 }, 149 { 150 "code": 10010, 151 "detail": "Isolation segment not found", 152 "title": "CF-ResourceNotFound" 153 } 154 ] 155 }` 156 server.AppendHandlers( 157 CombineHandlers( 158 VerifyRequest(http.MethodDelete, "/v3/users/some-uaa-guid"), 159 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a delete warning"}}), 160 ), 161 ) 162 }) 163 164 It("returns the error and all warnings", func() { 165 Expect(executeErr).To(MatchError(ccerror.MultiError{ 166 ResponseCode: http.StatusTeapot, 167 Errors: []ccerror.V3Error{ 168 { 169 Code: 10008, 170 Detail: "The request is semantically invalid: command presence", 171 Title: "CF-UnprocessableEntity", 172 }, 173 { 174 Code: 10010, 175 Detail: "Isolation segment not found", 176 Title: "CF-ResourceNotFound", 177 }, 178 }, 179 })) 180 Expect(warnings).To(ConsistOf("this is a delete warning")) 181 }) 182 }) 183 }) 184 }) 185 186 Describe("GetUsers", func() { 187 var ( 188 users []resources.User 189 warnings Warnings 190 executeErr error 191 query []Query 192 ) 193 194 BeforeEach(func() { 195 query = []Query{ 196 { 197 Key: UsernamesFilter, 198 Values: []string{"some-user-name"}, 199 }, 200 { 201 Key: OriginsFilter, 202 Values: []string{"uaa"}, 203 }, 204 } 205 }) 206 JustBeforeEach(func() { 207 users, warnings, executeErr = client.GetUsers(query...) 208 }) 209 210 Describe("listing users", func() { 211 When("the request succeeds", func() { 212 BeforeEach(func() { 213 response1 := fmt.Sprintf(`{ 214 "pagination": { 215 "next": { 216 "href": null 217 } 218 }, 219 "resources": [ 220 { 221 "guid": "user-guid-1", 222 "username": "some-user-name", 223 "origin": "uaa" 224 } 225 ] 226 }`) 227 228 server.AppendHandlers( 229 CombineHandlers( 230 VerifyRequest(http.MethodGet, "/v3/users", "usernames=some-user-name&origins=uaa"), 231 RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"warning-1"}}), 232 ), 233 ) 234 }) 235 236 It("returns the given user and all warnings", func() { 237 Expect(executeErr).ToNot(HaveOccurred()) 238 Expect(warnings).To(ConsistOf("warning-1")) 239 240 Expect(users).To(Equal([]resources.User{{ 241 GUID: "user-guid-1", 242 Username: "some-user-name", 243 Origin: "uaa", 244 }, 245 })) 246 }) 247 }) 248 249 When("the cloud controller returns errors and warnings", func() { 250 BeforeEach(func() { 251 response := `{ 252 "errors": [ 253 { 254 "code": 10008, 255 "detail": "The request is semantically invalid: command presence", 256 "title": "CF-UnprocessableEntity" 257 }, 258 { 259 "code": 10010, 260 "detail": "Org not found", 261 "title": "CF-ResourceNotFound" 262 } 263 ] 264 }` 265 server.AppendHandlers( 266 CombineHandlers( 267 VerifyRequest(http.MethodGet, "/v3/users"), 268 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 269 ), 270 ) 271 }) 272 273 It("returns the error and all warnings", func() { 274 Expect(executeErr).To(MatchError(ccerror.MultiError{ 275 ResponseCode: http.StatusTeapot, 276 Errors: []ccerror.V3Error{ 277 { 278 Code: 10008, 279 Detail: "The request is semantically invalid: command presence", 280 Title: "CF-UnprocessableEntity", 281 }, 282 { 283 Code: 10010, 284 Detail: "Org not found", 285 Title: "CF-ResourceNotFound", 286 }, 287 }, 288 })) 289 Expect(warnings).To(ConsistOf("this is a warning")) 290 }) 291 }) 292 }) 293 }) 294 295 Describe("GetUser", func() { 296 var ( 297 user resources.User 298 warnings Warnings 299 executeErr error 300 ) 301 302 JustBeforeEach(func() { 303 user, warnings, executeErr = client.GetUser("some-guid") 304 }) 305 306 When("the request succeeds", func() { 307 BeforeEach(func() { 308 response := `{ 309 "guid": "some-guid", 310 "username": "some-user-name", 311 "origin": "some-origin" 312 }` 313 server.AppendHandlers( 314 CombineHandlers( 315 VerifyRequest(http.MethodGet, "/v3/users/some-guid"), 316 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"warning-1"}}), 317 ), 318 ) 319 }) 320 321 It("returns the given user and all warnings", func() { 322 Expect(executeErr).ToNot(HaveOccurred()) 323 324 Expect(user).To(Equal(resources.User{ 325 GUID: "some-guid", 326 Username: "some-user-name", 327 Origin: "some-origin", 328 })) 329 Expect(warnings).To(ConsistOf("warning-1")) 330 }) 331 }) 332 333 When("cloud controller returns an error", func() { 334 BeforeEach(func() { 335 response := `{ 336 "errors": [ 337 { 338 "code": 10010, 339 "detail": "User not found", 340 "title": "CF-ResourceNotFound" 341 } 342 ] 343 }` 344 server.AppendHandlers( 345 CombineHandlers( 346 VerifyRequest(http.MethodGet, "/v3/users/some-guid"), 347 RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"warning-1"}}), 348 ), 349 ) 350 }) 351 352 It("returns the error", func() { 353 Expect(executeErr).To(MatchError(ccerror.UserNotFoundError{})) 354 Expect(warnings).To(ConsistOf("warning-1")) 355 }) 356 }) 357 }) 358 })