github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/api/cloudcontroller/ccv2/user_test.go (about)

     1  package ccv2_test
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     7  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
     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 client *Client
    15  
    16  	BeforeEach(func() {
    17  		client = NewTestClient()
    18  	})
    19  
    20  	Describe("CreateUser", func() {
    21  		Context("when an error does not occur", func() {
    22  			BeforeEach(func() {
    23  				response := `{
    24  					 "metadata": {
    25  							"guid": "some-guid",
    26  							"url": "/v2/users/some-guid",
    27  							"created_at": "2016-12-07T18:18:30Z",
    28  							"updated_at": null
    29  					 },
    30  					 "entity": {
    31  							"admin": false,
    32  							"active": false,
    33  							"default_space_guid": null,
    34  							"spaces_url": "/v2/users/some-guid/spaces",
    35  							"organizations_url": "/v2/users/some-guid/organizations",
    36  							"managed_organizations_url": "/v2/users/some-guid/managed_organizations",
    37  							"billing_managed_organizations_url": "/v2/users/some-guid/billing_managed_organizations",
    38  							"audited_organizations_url": "/v2/users/some-guid/audited_organizations",
    39  							"managed_spaces_url": "/v2/users/some-guid/managed_spaces",
    40  							"audited_spaces_url": "/v2/users/some-guid/audited_spaces"
    41  					 }
    42  				}`
    43  				server.AppendHandlers(
    44  					CombineHandlers(
    45  						VerifyRequest(http.MethodPost, "/v2/users"),
    46  						VerifyJSON(`{"guid":"some-uaa-guid"}`),
    47  						RespondWith(http.StatusCreated, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}),
    48  					),
    49  				)
    50  			})
    51  
    52  			It("creates and returns the user and all warnings", func() {
    53  				user, warnings, err := client.CreateUser("some-uaa-guid")
    54  				Expect(err).ToNot(HaveOccurred())
    55  
    56  				Expect(user).To(Equal(User{GUID: "some-guid"}))
    57  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
    58  			})
    59  		})
    60  
    61  		Context("when cloud controller returns an error and warnings", func() {
    62  			BeforeEach(func() {
    63  				response := `{
    64  					"code": 10008,
    65  					"description": "The request is semantically invalid: command presence",
    66  					"error_code": "CF-UnprocessableEntity"
    67  				}`
    68  				server.AppendHandlers(
    69  					CombineHandlers(
    70  						VerifyRequest(http.MethodPost, "/v2/users"),
    71  						VerifyJSON(`{"guid":"some-uaa-guid"}`),
    72  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}),
    73  					))
    74  			})
    75  
    76  			It("returns the errors and all warnings", func() {
    77  				_, warnings, err := client.CreateUser("some-uaa-guid")
    78  				Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{
    79  					ResponseCode: 418,
    80  					V2ErrorResponse: ccerror.V2ErrorResponse{
    81  						Code:        10008,
    82  						Description: "The request is semantically invalid: command presence",
    83  						ErrorCode:   "CF-UnprocessableEntity",
    84  					},
    85  				}))
    86  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
    87  			})
    88  		})
    89  	})
    90  })