github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/api/uaa/refresh_token_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  
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  	. "github.com/onsi/gomega/ghttp"
    14  )
    15  
    16  var _ = Describe("UAA Client", 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("RefreshAccessToken", func() {
    30  		var (
    31  			returnedAccessToken  string
    32  			sentRefreshToken     string
    33  			returnedRefreshToken string
    34  		)
    35  
    36  		Context("when the provided grant_type is client_credentials", func() {
    37  			BeforeEach(func() {
    38  				fakeConfig.UAAGrantTypeReturns(string(constant.GrantTypeClientCredentials))
    39  
    40  				returnedAccessToken = "I-ACCESS-TOKEN"
    41  				response := fmt.Sprintf(`{
    42  				"access_token": "%s",
    43  				"token_type": "bearer",
    44  				"expires_in": 599,
    45  				"scope": "cloud_controller.read password.write cloud_controller.write openid uaa.user",
    46  				"jti": "4150c08afa2848278e5ad57201024e32"
    47  			}`, returnedAccessToken)
    48  
    49  				server.AppendHandlers(
    50  					CombineHandlers(
    51  						verifyRequestHost(TestAuthorizationResource),
    52  						VerifyRequest(http.MethodPost, "/oauth/token"),
    53  						VerifyHeaderKV("Accept", "application/json"),
    54  						VerifyHeaderKV("Content-Type", "application/x-www-form-urlencoded"),
    55  						VerifyHeaderKV("Authorization"),
    56  						VerifyBody([]byte(fmt.Sprintf("client_id=client-id&client_secret=client-secret&grant_type=%s", constant.GrantTypeClientCredentials))),
    57  						RespondWith(http.StatusOK, response),
    58  					))
    59  			})
    60  
    61  			It("refreshes the tokens", func() {
    62  				token, err := client.RefreshAccessToken(sentRefreshToken)
    63  				Expect(err).ToNot(HaveOccurred())
    64  				Expect(token).To(Equal(RefreshedTokens{
    65  					AccessToken: returnedAccessToken,
    66  					Type:        "bearer",
    67  				}))
    68  
    69  				Expect(server.ReceivedRequests()).To(HaveLen(2))
    70  			})
    71  		})
    72  
    73  		Context("when the provided grant_type is not client_credentials", func() {
    74  			BeforeEach(func() {
    75  				returnedAccessToken = "I-ACCESS-TOKEN"
    76  				sentRefreshToken = "I-R-REFRESH-TOKEN"
    77  				returnedRefreshToken = "I-R-NEW-REFRESH-TOKEN"
    78  				response := fmt.Sprintf(`{
    79  				"access_token": "%s",
    80  				"token_type": "bearer",
    81  				"refresh_token": "%s",
    82  				"expires_in": 599,
    83  				"scope": "cloud_controller.read password.write cloud_controller.write openid uaa.user",
    84  				"jti": "4150c08afa2848278e5ad57201024e32"
    85  			}`, returnedAccessToken, returnedRefreshToken)
    86  
    87  				server.AppendHandlers(
    88  					CombineHandlers(
    89  						verifyRequestHost(TestAuthorizationResource),
    90  						VerifyRequest(http.MethodPost, "/oauth/token"),
    91  						VerifyHeaderKV("Accept", "application/json"),
    92  						VerifyHeaderKV("Content-Type", "application/x-www-form-urlencoded"),
    93  						VerifyHeaderKV("Authorization", "Basic Y2xpZW50LWlkOmNsaWVudC1zZWNyZXQ="),
    94  						VerifyBody([]byte(fmt.Sprintf("client_id=client-id&client_secret=client-secret&grant_type=%s&refresh_token=%s", constant.GrantTypeRefreshToken, sentRefreshToken))),
    95  						RespondWith(http.StatusOK, response),
    96  					))
    97  			})
    98  
    99  			It("refreshes the tokens", func() {
   100  				token, err := client.RefreshAccessToken(sentRefreshToken)
   101  				Expect(err).ToNot(HaveOccurred())
   102  				Expect(token).To(Equal(RefreshedTokens{
   103  					AccessToken:  returnedAccessToken,
   104  					RefreshToken: returnedRefreshToken,
   105  					Type:         "bearer",
   106  				}))
   107  
   108  				Expect(server.ReceivedRequests()).To(HaveLen(2))
   109  			})
   110  		})
   111  	})
   112  })