github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/api/uaa/noaabridge/token_refresher_test.go (about) 1 package noaabridge_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/api/uaa" 7 . "code.cloudfoundry.org/cli/api/uaa/noaabridge" 8 "code.cloudfoundry.org/cli/api/uaa/noaabridge/noaabridgefakes" 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 ) 12 13 var _ = Describe("TokenRefresher", func() { 14 Describe("RefreshAuthToken", func() { 15 var ( 16 fakeUAAClient *noaabridgefakes.FakeUAAClient 17 fakeTokenCache *noaabridgefakes.FakeTokenCache 18 tokenRefresher *TokenRefresher 19 ) 20 21 BeforeEach(func() { 22 fakeUAAClient = new(noaabridgefakes.FakeUAAClient) 23 fakeTokenCache = new(noaabridgefakes.FakeTokenCache) 24 tokenRefresher = NewTokenRefresher(fakeUAAClient, fakeTokenCache) 25 }) 26 27 When("UAA communication is successful", func() { 28 BeforeEach(func() { 29 fakeTokenCache.RefreshTokenReturns("old-refresh-token") 30 31 refreshToken := uaa.RefreshedTokens{ 32 AccessToken: "some-access-token", 33 RefreshToken: "some-refresh-token", 34 Type: "bearer", 35 } 36 fakeUAAClient.RefreshAccessTokenReturns(refreshToken, nil) 37 }) 38 39 It("refreshes the token", func() { 40 token, err := tokenRefresher.RefreshAuthToken() 41 Expect(err).ToNot(HaveOccurred()) 42 Expect(token).To(Equal("bearer some-access-token")) 43 44 Expect(fakeUAAClient.RefreshAccessTokenCallCount()).To(Equal(1)) 45 Expect(fakeUAAClient.RefreshAccessTokenArgsForCall(0)).To(Equal("old-refresh-token")) 46 }) 47 48 It("stores the new access and refresh tokens", func() { 49 _, err := tokenRefresher.RefreshAuthToken() 50 Expect(err).ToNot(HaveOccurred()) 51 52 Expect(fakeTokenCache.SetAccessTokenCallCount()).To(Equal(1)) 53 Expect(fakeTokenCache.SetAccessTokenArgsForCall(0)).To(Equal("bearer some-access-token")) 54 Expect(fakeTokenCache.SetRefreshTokenCallCount()).To(Equal(1)) 55 Expect(fakeTokenCache.SetRefreshTokenArgsForCall(0)).To(Equal("some-refresh-token")) 56 }) 57 }) 58 59 When("UAA communication returns an error", func() { 60 var expectedErr error 61 62 BeforeEach(func() { 63 expectedErr = errors.New("it's not working!!!!") 64 fakeUAAClient.RefreshAccessTokenReturns(uaa.RefreshedTokens{}, expectedErr) 65 }) 66 67 It("returns the error", func() { 68 _, err := tokenRefresher.RefreshAuthToken() 69 Expect(err).To(MatchError(expectedErr)) 70 }) 71 }) 72 }) 73 })