github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+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 (
    14  		client *Client
    15  	)
    16  
    17  	BeforeEach(func() {
    18  		client = NewTestUAAClientAndStore()
    19  	})
    20  
    21  	Describe("CreateUser", func() {
    22  		Context("when no errors occur", func() {
    23  			Context("when creating user with origin", func() {
    24  				BeforeEach(func() {
    25  					response := `{
    26  					"ID": "new-user-id"
    27  				}`
    28  					server.AppendHandlers(
    29  						CombineHandlers(
    30  							VerifyRequest(http.MethodPost, "/Users"),
    31  							VerifyHeaderKV("Content-Type", "application/json"),
    32  							VerifyBody([]byte(`{"userName":"new-user","password":"","origin":"some-origin","name":{"familyName":"new-user","givenName":"new-user"},"emails":[{"value":"new-user","primary":true}]}`)),
    33  							RespondWith(http.StatusOK, response),
    34  						))
    35  				})
    36  
    37  				It("creates a new user", func() {
    38  					user, err := client.CreateUser("new-user", "", "some-origin")
    39  					Expect(err).NotTo(HaveOccurred())
    40  
    41  					Expect(user).To(Equal(User{
    42  						ID: "new-user-id",
    43  					}))
    44  				})
    45  			})
    46  			Context("when creating user in UAA", func() {
    47  				BeforeEach(func() {
    48  					response := `{
    49  					"ID": "new-user-id"
    50  				}`
    51  					server.AppendHandlers(
    52  						CombineHandlers(
    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  				server.AppendHandlers(
    80  					CombineHandlers(
    81  						VerifyRequest(http.MethodPost, "/Users"),
    82  						RespondWith(http.StatusTeapot, response),
    83  					))
    84  			})
    85  
    86  			It("returns the error", func() {
    87  				_, err := client.CreateUser("new-user", "new-password", "")
    88  				Expect(err).To(MatchError(RawHTTPStatusError{
    89  					StatusCode:  http.StatusTeapot,
    90  					RawResponse: []byte(response),
    91  				}))
    92  			})
    93  		})
    94  	})
    95  })