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