github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/api/uaa/user_test.go (about) 1 package uaa_test 2 3 import ( 4 "net/http" 5 6 . "code.cloudfoundry.org/cli/api/uaa" 7 . "github.com/onsi/ginkgo" 8 . "github.com/onsi/gomega" 9 . "github.com/onsi/gomega/ghttp" 10 ) 11 12 var _ = Describe("User", func() { 13 var client *Client 14 15 BeforeEach(func() { 16 client = NewTestUAAClientAndStore() 17 }) 18 19 Describe("CreateUser", func() { 20 Context("when no errors occur", func() { 21 Context("when creating user with origin", func() { 22 BeforeEach(func() { 23 response := `{ 24 "ID": "new-user-id" 25 }` 26 uaaServer.AppendHandlers( 27 CombineHandlers( 28 verifyRequestHost(TestUAAResource), 29 VerifyRequest(http.MethodPost, "/Users"), 30 VerifyHeaderKV("Content-Type", "application/json"), 31 VerifyBody([]byte(`{"userName":"new-user","password":"","origin":"some-origin","name":{"familyName":"new-user","givenName":"new-user"},"emails":[{"value":"new-user","primary":true}]}`)), 32 RespondWith(http.StatusOK, response), 33 )) 34 }) 35 36 It("creates a new user", func() { 37 user, err := client.CreateUser("new-user", "", "some-origin") 38 Expect(err).NotTo(HaveOccurred()) 39 40 Expect(user).To(Equal(User{ 41 ID: "new-user-id", 42 })) 43 }) 44 }) 45 Context("when creating user in UAA", func() { 46 BeforeEach(func() { 47 response := `{ 48 "ID": "new-user-id" 49 }` 50 uaaServer.AppendHandlers( 51 CombineHandlers( 52 verifyRequestHost(TestUAAResource), 53 VerifyRequest(http.MethodPost, "/Users"), 54 VerifyHeaderKV("Content-Type", "application/json"), 55 VerifyBody([]byte(`{"userName":"new-user","password":"new-password","origin":"","name":{"familyName":"new-user","givenName":"new-user"},"emails":[{"value":"new-user","primary":true}]}`)), 56 RespondWith(http.StatusOK, response), 57 )) 58 }) 59 60 It("creates a new user", func() { 61 user, err := client.CreateUser("new-user", "new-password", "") 62 Expect(err).NotTo(HaveOccurred()) 63 64 Expect(user).To(Equal(User{ 65 ID: "new-user-id", 66 })) 67 }) 68 }) 69 }) 70 71 Context("when an error occurs", func() { 72 var response string 73 74 BeforeEach(func() { 75 response = `{ 76 "error": "some-error", 77 "error_description": "some-description" 78 }` 79 uaaServer.AppendHandlers( 80 CombineHandlers( 81 verifyRequestHost(TestUAAResource), 82 VerifyRequest(http.MethodPost, "/Users"), 83 RespondWith(http.StatusTeapot, response), 84 )) 85 }) 86 87 It("returns the error", func() { 88 _, err := client.CreateUser("new-user", "new-password", "") 89 Expect(err).To(MatchError(RawHTTPStatusError{ 90 StatusCode: http.StatusTeapot, 91 RawResponse: []byte(response), 92 })) 93 }) 94 }) 95 }) 96 })