github.com/orange-cloudfoundry/cli@v7.1.0+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  		fakeConfig *uaafakes.FakeConfig
    17  
    18  		setupResourcesErr error
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		fakeConfig = NewTestConfig()
    23  		client = NewClient(fakeConfig)
    24  	})
    25  
    26  	JustBeforeEach(func() {
    27  		setupResourcesErr = client.SetupResources(server.URL())
    28  	})
    29  
    30  	When("the authentication server returns an error", func() {
    31  		BeforeEach(func() {
    32  			server.AppendHandlers(
    33  				CombineHandlers(
    34  					VerifyRequest(http.MethodGet, "/login"),
    35  					RespondWith(http.StatusNotFound, `{"errors": [{}]}`, nil),
    36  				),
    37  			)
    38  		})
    39  
    40  		It("returns the error", func() {
    41  			Expect(setupResourcesErr).To(HaveOccurred())
    42  			Expect(fakeConfig.SetUAAEndpointCallCount()).To(Equal(0))
    43  		})
    44  	})
    45  
    46  	When("the request succeeds", func() {
    47  		Context("and the UAA field is populated", func() {
    48  			BeforeEach(func() {
    49  				response := `{
    50  					"app": {
    51  						"version": "sem.var"
    52  					},
    53  					"links": {
    54  						"uaa": "https://uaa.bosh-lite.com"
    55  					},
    56  					"prompts": {
    57  						"username": [ "text", "Email" ],
    58  						"password": [ "password", "Password" ]
    59  					}
    60  				}`
    61  
    62  				server.AppendHandlers(
    63  					CombineHandlers(
    64  						VerifyRequest(http.MethodGet, "/login"),
    65  						RespondWith(http.StatusOK, response, nil),
    66  					),
    67  				)
    68  			})
    69  
    70  			It("sets the UAA endpoint to the UAA link and does not return an error", func() {
    71  				Expect(setupResourcesErr).ToNot(HaveOccurred())
    72  				Expect(client.UAALink()).To(Equal("https://uaa.bosh-lite.com"))
    73  				Expect(client.APIVersion()).To(Equal("sem.var"))
    74  
    75  				Expect(fakeConfig.SetUAAEndpointCallCount()).To(Equal(1))
    76  				Expect(fakeConfig.SetUAAEndpointArgsForCall(0)).To(Equal("https://uaa.bosh-lite.com"))
    77  			})
    78  
    79  			It("populates the client's info", func() {
    80  				Expect(client.Info.App.Version).To(Equal("sem.var"))
    81  				Expect(client.Info.Links.UAA).To(Equal("https://uaa.bosh-lite.com"))
    82  				Expect(client.Info.Prompts).To(Equal(map[string][]string{
    83  					"username": []string{"text", "Email"},
    84  					"password": []string{"password", "Password"},
    85  				}))
    86  			})
    87  		})
    88  
    89  		When("the UAA field is not populated", func() {
    90  			BeforeEach(func() {
    91  				response := `{
    92  				"links": {}
    93  			}`
    94  
    95  				server.AppendHandlers(
    96  					CombineHandlers(
    97  						VerifyRequest(http.MethodGet, "/login"),
    98  						RespondWith(http.StatusOK, response, nil),
    99  					),
   100  				)
   101  			})
   102  
   103  			It("sets the UAA endpoint to the bootstrap endpoint and does not return an error", func() {
   104  				Expect(setupResourcesErr).ToNot(HaveOccurred())
   105  				Expect(fakeConfig.SetUAAEndpointCallCount()).To(Equal(1))
   106  				Expect(fakeConfig.SetUAAEndpointArgsForCall(0)).To(Equal(server.URL()))
   107  			})
   108  		})
   109  	})
   110  })