github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/api/uaa/resources_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("SetupResources", func() {
    14  	var (
    15  		client            *Client
    16  		setupResourcesErr error
    17  		fakeStore         *uaafakes.FakeUAAEndpointStore
    18  	)
    19  
    20  	JustBeforeEach(func() {
    21  		fakeStore = new(uaafakes.FakeUAAEndpointStore)
    22  		setupResourcesErr = client.SetupResources(fakeStore, server.URL())
    23  	})
    24  
    25  	BeforeEach(func() {
    26  		client = NewClient(Config{
    27  			AppName:           "CF CLI UAA API Test",
    28  			AppVersion:        "Unknown",
    29  			ClientID:          "client-id",
    30  			ClientSecret:      "client-secret",
    31  			SkipSSLValidation: true,
    32  		})
    33  	})
    34  
    35  	Context("when the authentication server returns an error", func() {
    36  		BeforeEach(func() {
    37  			server.AppendHandlers(
    38  				CombineHandlers(
    39  					VerifyRequest(http.MethodGet, "/login"),
    40  					RespondWith(http.StatusNotFound, `{"errors": [{}]}`, nil),
    41  				),
    42  			)
    43  		})
    44  
    45  		It("returns the error", func() {
    46  			Expect(setupResourcesErr).To(HaveOccurred())
    47  			Expect(fakeStore.SetUAAEndpointCallCount()).To(Equal(0))
    48  		})
    49  	})
    50  
    51  	Context("when the request succeeds", func() {
    52  		Context("and the UAA field is populated", func() {
    53  			BeforeEach(func() {
    54  				response := `{
    55  				"links": {
    56  					"uaa": "https://uaa.bosh-lite.com"
    57  				}
    58  			}`
    59  
    60  				server.AppendHandlers(
    61  					CombineHandlers(
    62  						VerifyRequest(http.MethodGet, "/login"),
    63  						RespondWith(http.StatusOK, response, nil),
    64  					),
    65  				)
    66  			})
    67  
    68  			It("sets the UAA endpoint to the UAA link and does not return an error", func() {
    69  				Expect(setupResourcesErr).ToNot(HaveOccurred())
    70  				Expect(fakeStore.SetUAAEndpointCallCount()).To(Equal(1))
    71  				Expect(fakeStore.SetUAAEndpointArgsForCall(0)).To(Equal("https://uaa.bosh-lite.com"))
    72  			})
    73  		})
    74  
    75  		Context("when the UAA field is not populated", func() {
    76  			BeforeEach(func() {
    77  				response := `{
    78  				"links": {}
    79  			}`
    80  
    81  				server.AppendHandlers(
    82  					CombineHandlers(
    83  						VerifyRequest(http.MethodGet, "/login"),
    84  						RespondWith(http.StatusOK, response, nil),
    85  					),
    86  				)
    87  			})
    88  
    89  			It("sets the UAA endpoint to the bootstrap endpoint and does not return an error", func() {
    90  				Expect(setupResourcesErr).ToNot(HaveOccurred())
    91  				Expect(fakeStore.SetUAAEndpointCallCount()).To(Equal(1))
    92  				Expect(fakeStore.SetUAAEndpointArgsForCall(0)).To(Equal(server.URL()))
    93  			})
    94  		})
    95  	})
    96  })