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