github.com/lukasheimann/cloudfoundrycli@v7.1.0+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  	"code.cloudfoundry.org/cli/integration/helpers"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("Token Actions", func() {
    16  	var (
    17  		actor         *Actor
    18  		fakeUAAClient *v7actionfakes.FakeUAAClient
    19  		fakeConfig    *v7actionfakes.FakeConfig
    20  	)
    21  
    22  	BeforeEach(func() {
    23  		fakeUAAClient = new(v7actionfakes.FakeUAAClient)
    24  		fakeConfig = new(v7actionfakes.FakeConfig)
    25  		actor = NewActor(nil, fakeConfig, nil, fakeUAAClient, nil, nil)
    26  	})
    27  
    28  	Describe("RefreshAccessToken", func() {
    29  		var (
    30  			accessToken string
    31  			err         error
    32  		)
    33  		JustBeforeEach(func() {
    34  			accessToken, err = actor.RefreshAccessToken()
    35  		})
    36  
    37  		When("the token is invalid", func() {
    38  			BeforeEach(func() {
    39  				fakeUAAClient.RefreshAccessTokenReturns(uaa.RefreshedTokens{
    40  					AccessToken:  "some-token",
    41  					Type:         "bearer",
    42  					RefreshToken: "new-refresh-token",
    43  				}, nil)
    44  				fakeConfig.RefreshTokenReturns("some-refresh-token")
    45  				fakeConfig.AccessTokenReturns("im a bad token :(")
    46  			})
    47  
    48  			It("returns the new access token from the uaa client", func() {
    49  				Expect(err).ToNot(HaveOccurred())
    50  				Expect(fakeUAAClient.RefreshAccessTokenCallCount()).To(Equal(1))
    51  				Expect(fakeUAAClient.RefreshAccessTokenArgsForCall(0)).To(Equal("some-refresh-token"))
    52  				Expect(accessToken).To(Equal("bearer some-token"))
    53  			})
    54  
    55  			It("updates the config with the refreshed token", func() {
    56  				Expect(fakeConfig.SetAccessTokenCallCount()).To(Equal(1))
    57  				Expect(fakeConfig.SetRefreshTokenCallCount()).To(Equal(1))
    58  				Expect(fakeConfig.SetAccessTokenArgsForCall(0)).To(Equal("bearer some-token"))
    59  				Expect(fakeConfig.SetRefreshTokenArgsForCall(0)).To(Equal("new-refresh-token"))
    60  			})
    61  		})
    62  
    63  		When("the token is not about to expire", func() {
    64  			var notExpiringAccessToken string
    65  			BeforeEach(func() {
    66  				fakeUAAClient.RefreshAccessTokenReturns(uaa.RefreshedTokens{
    67  					AccessToken: "some-token",
    68  					Type:        "bearer",
    69  				}, nil)
    70  				fakeConfig.RefreshTokenReturns("some-refresh-token")
    71  				notExpiringAccessToken = helpers.BuildTokenString(time.Now().AddDate(5, 0, 0))
    72  				fakeConfig.AccessTokenReturns(notExpiringAccessToken)
    73  			})
    74  
    75  			It("returns the current token without refreshing it", func() {
    76  				Expect(fakeUAAClient.RefreshAccessTokenCallCount()).To(Equal(0))
    77  				Expect(err).ToNot(HaveOccurred())
    78  				Expect(accessToken).To(Equal(notExpiringAccessToken))
    79  			})
    80  		})
    81  
    82  		When("the token is about to expire", func() {
    83  			BeforeEach(func() {
    84  				fakeUAAClient.RefreshAccessTokenReturns(uaa.RefreshedTokens{
    85  					AccessToken:  "some-token",
    86  					RefreshToken: "new-refresh-token",
    87  					Type:         "bearer",
    88  				}, nil)
    89  				fakeConfig.RefreshTokenReturns("some-refresh-token")
    90  				expiringAccessToken := helpers.BuildTokenString(time.Now().Add(5))
    91  				fakeConfig.AccessTokenReturns(expiringAccessToken)
    92  			})
    93  
    94  			It("returns the new access token from the uaa client", func() {
    95  				Expect(err).ToNot(HaveOccurred())
    96  				Expect(fakeUAAClient.RefreshAccessTokenCallCount()).To(Equal(1))
    97  				Expect(fakeUAAClient.RefreshAccessTokenArgsForCall(0)).To(Equal("some-refresh-token"))
    98  				Expect(accessToken).To(Equal("bearer some-token"))
    99  			})
   100  
   101  			It("updates the config with the refreshed token", func() {
   102  				Expect(fakeConfig.SetAccessTokenCallCount()).To(Equal(1))
   103  				Expect(fakeConfig.SetRefreshTokenCallCount()).To(Equal(1))
   104  				Expect(fakeConfig.SetAccessTokenArgsForCall(0)).To(Equal("bearer some-token"))
   105  				Expect(fakeConfig.SetRefreshTokenArgsForCall(0)).To(Equal("new-refresh-token"))
   106  			})
   107  
   108  			When("refreshing the access token fails", func() {
   109  				BeforeEach(func() {
   110  					fakeUAAClient.RefreshAccessTokenReturns(
   111  						uaa.RefreshedTokens{},
   112  						errors.New("I'm still an error!"),
   113  					)
   114  				})
   115  
   116  				It("returns that error", func() {
   117  					Expect(err).To(MatchError("I'm still an error!"))
   118  					Expect(accessToken).To(Equal(""), "AccessToken was not equal to \"\"")
   119  				})
   120  			})
   121  		})
   122  	})
   123  })