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

     1  package uaa_test
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	. "code.cloudfoundry.org/cli/api/uaa"
     8  	"code.cloudfoundry.org/cli/api/uaa/constant"
     9  	"code.cloudfoundry.org/cli/api/uaa/uaafakes"
    10  	"code.cloudfoundry.org/cli/integration/helpers"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  	. "github.com/onsi/gomega/ghttp"
    14  )
    15  
    16  var _ = Describe("Auth", func() {
    17  	var (
    18  		client *Client
    19  
    20  		fakeConfig *uaafakes.FakeConfig
    21  	)
    22  
    23  	BeforeEach(func() {
    24  		fakeConfig = NewTestConfig()
    25  
    26  		client = NewTestUAAClientAndStore(fakeConfig)
    27  	})
    28  
    29  	Describe("Authenticate", func() {
    30  		var (
    31  			identity  string
    32  			secret    string
    33  			grantType constant.GrantType
    34  
    35  			accessToken  string
    36  			refreshToken string
    37  			executeErr   error
    38  		)
    39  
    40  		JustBeforeEach(func() {
    41  			accessToken, refreshToken, executeErr = client.Authenticate(identity, secret, grantType)
    42  		})
    43  
    44  		Context("when no errors occur", func() {
    45  			Context("when the grant type is password", func() {
    46  				BeforeEach(func() {
    47  					response := `{
    48  						"access_token":"some-access-token",
    49  						"refresh_token":"some-refresh-token"
    50  					}`
    51  					identity = helpers.NewUsername()
    52  					secret = helpers.NewPassword()
    53  					grantType = constant.GrantTypePassword
    54  					server.AppendHandlers(
    55  						CombineHandlers(
    56  							verifyRequestHost(TestAuthorizationResource),
    57  							VerifyRequest(http.MethodPost, "/oauth/token"),
    58  							VerifyHeaderKV("Content-Type", "application/x-www-form-urlencoded"),
    59  							VerifyHeaderKV("Authorization", "Basic Y2xpZW50LWlkOmNsaWVudC1zZWNyZXQ="),
    60  							VerifyBody([]byte(fmt.Sprintf("grant_type=%s&password=%s&username=%s", grantType, secret, identity))),
    61  							RespondWith(http.StatusOK, response),
    62  						))
    63  				})
    64  
    65  				It("authenticates with the credentials provided", func() {
    66  					Expect(executeErr).NotTo(HaveOccurred())
    67  
    68  					Expect(accessToken).To(Equal("some-access-token"))
    69  					Expect(refreshToken).To(Equal("some-refresh-token"))
    70  				})
    71  			})
    72  
    73  			Context("when the grant type is client credentials", func() {
    74  				BeforeEach(func() {
    75  					response := `{
    76  						"access_token":"some-access-token"
    77  					}`
    78  
    79  					identity = helpers.NewUsername()
    80  					secret = helpers.NewPassword()
    81  					grantType = constant.GrantTypeClientCredentials
    82  					server.AppendHandlers(
    83  						CombineHandlers(
    84  							verifyRequestHost(TestAuthorizationResource),
    85  							VerifyRequest(http.MethodPost, "/oauth/token"),
    86  							VerifyHeaderKV("Content-Type", "application/x-www-form-urlencoded"),
    87  							VerifyHeaderKV("Authorization"),
    88  							VerifyBody([]byte(fmt.Sprintf("client_id=%s&client_secret=%s&grant_type=%s", identity, secret, grantType))),
    89  							RespondWith(http.StatusOK, response),
    90  						))
    91  				})
    92  
    93  				It("authenticates with the credentials provided", func() {
    94  					Expect(executeErr).NotTo(HaveOccurred())
    95  
    96  					Expect(accessToken).To(Equal("some-access-token"))
    97  					Expect(refreshToken).To(BeEmpty())
    98  				})
    99  			})
   100  		})
   101  
   102  		Context("when an error occurs", func() {
   103  			var response string
   104  
   105  			BeforeEach(func() {
   106  				response = `{
   107  						"error": "some-error",
   108  						"error_description": "some-description"
   109  					}`
   110  				server.AppendHandlers(
   111  					CombineHandlers(
   112  						verifyRequestHost(TestAuthorizationResource),
   113  						VerifyRequest(http.MethodPost, "/oauth/token"),
   114  						RespondWith(http.StatusTeapot, response),
   115  					))
   116  			})
   117  
   118  			It("returns the error", func() {
   119  				Expect(executeErr).To(MatchError(RawHTTPStatusError{
   120  					StatusCode:  http.StatusTeapot,
   121  					RawResponse: []byte(response),
   122  				}))
   123  			})
   124  		})
   125  	})
   126  })