github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/actor/v2action/token_test.go (about)

     1  package v2action_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	. "code.cloudfoundry.org/cli/actor/v2action"
     7  	"code.cloudfoundry.org/cli/actor/v2action/v2actionfakes"
     8  	"code.cloudfoundry.org/cli/api/uaa"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  )
    12  
    13  var _ = Describe("Token Actions", func() {
    14  	var (
    15  		actor         *Actor
    16  		fakeConfig    *v2actionfakes.FakeConfig
    17  		fakeUAAClient *v2actionfakes.FakeUAAClient
    18  	)
    19  
    20  	BeforeEach(func() {
    21  		fakeConfig = new(v2actionfakes.FakeConfig)
    22  		fakeUAAClient = new(v2actionfakes.FakeUAAClient)
    23  		actor = NewActor(nil, fakeUAAClient, fakeConfig)
    24  	})
    25  
    26  	Describe("RefreshAccessToken", func() {
    27  		When("an error is encountered refreshing the access token", func() {
    28  			var expectedErr error
    29  
    30  			BeforeEach(func() {
    31  				expectedErr = errors.New("refresh tokens error")
    32  				fakeUAAClient.RefreshAccessTokenReturns(uaa.RefreshedTokens{}, expectedErr)
    33  			})
    34  
    35  			It("does not save any tokens to config and returns the error", func() {
    36  				_, err := actor.RefreshAccessToken("existing-refresh-token")
    37  				Expect(err).To(MatchError(expectedErr))
    38  
    39  				Expect(fakeUAAClient.RefreshAccessTokenCallCount()).To(Equal(1))
    40  				Expect(fakeUAAClient.RefreshAccessTokenArgsForCall(0)).To(Equal("existing-refresh-token"))
    41  
    42  				Expect(fakeConfig.SetRefreshTokenCallCount()).To(Equal(0))
    43  			})
    44  		})
    45  
    46  		When("no errors are encountered refreshing the access token", func() {
    47  			BeforeEach(func() {
    48  				fakeUAAClient.RefreshAccessTokenReturns(
    49  					uaa.RefreshedTokens{
    50  						AccessToken:  "new-access-token",
    51  						RefreshToken: "new-refresh-token",
    52  						Type:         "bob",
    53  					},
    54  					nil)
    55  			})
    56  
    57  			It("saves the new access and refresh tokens in the config and returns the access token", func() {
    58  				accessToken, err := actor.RefreshAccessToken("existing-refresh-token")
    59  				Expect(err).ToNot(HaveOccurred())
    60  				Expect(accessToken).To(Equal("bob new-access-token"))
    61  
    62  				Expect(fakeUAAClient.RefreshAccessTokenCallCount()).To(Equal(1))
    63  				Expect(fakeUAAClient.RefreshAccessTokenArgsForCall(0)).To(Equal("existing-refresh-token"))
    64  
    65  				Expect(fakeConfig.SetAccessTokenCallCount()).To(Equal(1))
    66  				Expect(fakeConfig.SetAccessTokenArgsForCall(0)).To(Equal("bob new-access-token"))
    67  
    68  				Expect(fakeConfig.SetRefreshTokenCallCount()).To(Equal(1))
    69  				Expect(fakeConfig.SetRefreshTokenArgsForCall(0)).To(Equal("new-refresh-token"))
    70  			})
    71  		})
    72  	})
    73  })