github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/actor/v7action/token_test.go (about)

     1  package v7action_test
     2  
     3  import (
     4  	"errors"
     5  	"time"
     6  
     7  	. "code.cloudfoundry.org/cli/actor/v7action"
     8  	"code.cloudfoundry.org/cli/actor/v7action/v7actionfakes"
     9  	"code.cloudfoundry.org/cli/api/uaa"
    10  	"github.com/SermoDigital/jose/crypto"
    11  	"github.com/SermoDigital/jose/jws"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  )
    15  
    16  var _ = Describe("Token Actions", func() {
    17  	var (
    18  		actor         *Actor
    19  		fakeUAAClient *v7actionfakes.FakeUAAClient
    20  		fakeConfig    *v7actionfakes.FakeConfig
    21  	)
    22  
    23  	BeforeEach(func() {
    24  		fakeUAAClient = new(v7actionfakes.FakeUAAClient)
    25  		fakeConfig = new(v7actionfakes.FakeConfig)
    26  		actor = NewActor(nil, fakeConfig, nil, fakeUAAClient, nil)
    27  	})
    28  
    29  	Describe("RefreshAccessToken", func() {
    30  		var (
    31  			accessToken string
    32  			err         error
    33  		)
    34  		JustBeforeEach(func() {
    35  			accessToken, err = actor.RefreshAccessToken()
    36  		})
    37  
    38  		When("the token is invalid", func() {
    39  			BeforeEach(func() {
    40  				fakeUAAClient.RefreshAccessTokenReturns(uaa.RefreshedTokens{
    41  					AccessToken:  "some-token",
    42  					Type:         "bearer",
    43  					RefreshToken: "new-refresh-token",
    44  				}, nil)
    45  				fakeConfig.RefreshTokenReturns("some-refresh-token")
    46  				fakeConfig.AccessTokenReturns("im a bad token :(")
    47  			})
    48  
    49  			It("returns the new access token from the uaa client", func() {
    50  				Expect(err).ToNot(HaveOccurred())
    51  				Expect(fakeUAAClient.RefreshAccessTokenCallCount()).To(Equal(1))
    52  				Expect(fakeUAAClient.RefreshAccessTokenArgsForCall(0)).To(Equal("some-refresh-token"))
    53  				Expect(accessToken).To(Equal("bearer some-token"))
    54  			})
    55  
    56  			It("updates the config with the refreshed token", func() {
    57  				Expect(fakeConfig.SetAccessTokenCallCount()).To(Equal(1))
    58  				Expect(fakeConfig.SetRefreshTokenCallCount()).To(Equal(1))
    59  				Expect(fakeConfig.SetAccessTokenArgsForCall(0)).To(Equal("bearer some-token"))
    60  				Expect(fakeConfig.SetRefreshTokenArgsForCall(0)).To(Equal("new-refresh-token"))
    61  			})
    62  		})
    63  
    64  		When("the token is not about to expire", func() {
    65  			var notExpiringAccessToken string
    66  			BeforeEach(func() {
    67  				fakeUAAClient.RefreshAccessTokenReturns(uaa.RefreshedTokens{
    68  					AccessToken: "some-token",
    69  					Type:        "bearer",
    70  				}, nil)
    71  				fakeConfig.RefreshTokenReturns("some-refresh-token")
    72  				notExpiringAccessToken = buildTokenString(time.Now().AddDate(5, 0, 0))
    73  				fakeConfig.AccessTokenReturns(notExpiringAccessToken)
    74  			})
    75  
    76  			It("returns the current token without refreshing it", func() {
    77  				Expect(fakeUAAClient.RefreshAccessTokenCallCount()).To(Equal(0))
    78  				Expect(err).ToNot(HaveOccurred())
    79  				Expect(accessToken).To(Equal(notExpiringAccessToken))
    80  			})
    81  		})
    82  
    83  		When("the token is about to expire", func() {
    84  			BeforeEach(func() {
    85  				fakeUAAClient.RefreshAccessTokenReturns(uaa.RefreshedTokens{
    86  					AccessToken:  "some-token",
    87  					RefreshToken: "new-refresh-token",
    88  					Type:         "bearer",
    89  				}, nil)
    90  				fakeConfig.RefreshTokenReturns("some-refresh-token")
    91  				expiringAccessToken := buildTokenString(time.Now().Add(5))
    92  				fakeConfig.AccessTokenReturns(expiringAccessToken)
    93  			})
    94  
    95  			It("returns the new access token from the uaa client", func() {
    96  				Expect(err).ToNot(HaveOccurred())
    97  				Expect(fakeUAAClient.RefreshAccessTokenCallCount()).To(Equal(1))
    98  				Expect(fakeUAAClient.RefreshAccessTokenArgsForCall(0)).To(Equal("some-refresh-token"))
    99  				Expect(accessToken).To(Equal("bearer some-token"))
   100  			})
   101  
   102  			It("updates the config with the refreshed token", func() {
   103  				Expect(fakeConfig.SetAccessTokenCallCount()).To(Equal(1))
   104  				Expect(fakeConfig.SetRefreshTokenCallCount()).To(Equal(1))
   105  				Expect(fakeConfig.SetAccessTokenArgsForCall(0)).To(Equal("bearer some-token"))
   106  				Expect(fakeConfig.SetRefreshTokenArgsForCall(0)).To(Equal("new-refresh-token"))
   107  			})
   108  
   109  			When("refreshing the access token fails", func() {
   110  				BeforeEach(func() {
   111  					fakeUAAClient.RefreshAccessTokenReturns(
   112  						uaa.RefreshedTokens{},
   113  						errors.New("I'm still an error!"),
   114  					)
   115  				})
   116  
   117  				It("returns that error", func() {
   118  
   119  					Expect(accessToken).To(Equal(""))
   120  					Expect(err).To(MatchError("I'm still an error!"))
   121  				})
   122  			})
   123  		})
   124  	})
   125  })
   126  
   127  func buildTokenString(expiration time.Time) string {
   128  	c := jws.Claims{}
   129  	c.SetExpiration(expiration)
   130  	token := jws.NewJWT(c, crypto.Unsecured)
   131  	tokenBytes, _ := token.Serialize(nil) //nolint: errcheck
   132  	return string(tokenBytes)
   133  }