github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/command/v2/oauth_token_command_test.go (about) 1 package v2_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/actor/actionerror" 7 "code.cloudfoundry.org/cli/command/commandfakes" 8 . "code.cloudfoundry.org/cli/command/v2" 9 "code.cloudfoundry.org/cli/command/v2/v2fakes" 10 "code.cloudfoundry.org/cli/util/ui" 11 . "github.com/onsi/ginkgo" 12 . "github.com/onsi/gomega" 13 . "github.com/onsi/gomega/gbytes" 14 ) 15 16 var _ = Describe("oauth-token command", func() { 17 var ( 18 cmd OauthTokenCommand 19 testUI *ui.UI 20 fakeConfig *commandfakes.FakeConfig 21 fakeSharedActor *commandfakes.FakeSharedActor 22 fakeActor *v2fakes.FakeOauthTokenActor 23 binaryName string 24 executeErr error 25 ) 26 27 BeforeEach(func() { 28 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 29 fakeConfig = new(commandfakes.FakeConfig) 30 fakeSharedActor = new(commandfakes.FakeSharedActor) 31 fakeActor = new(v2fakes.FakeOauthTokenActor) 32 33 cmd = OauthTokenCommand{ 34 UI: testUI, 35 Config: fakeConfig, 36 SharedActor: fakeSharedActor, 37 Actor: fakeActor, 38 } 39 40 binaryName = "faceman" 41 fakeConfig.BinaryNameReturns(binaryName) 42 }) 43 44 JustBeforeEach(func() { 45 executeErr = cmd.Execute(nil) 46 }) 47 48 Context("when checking the target fails", func() { 49 BeforeEach(func() { 50 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 51 }) 52 53 It("returns a wrapped error", func() { 54 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 55 56 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 57 checkTargettedOrgArg, checkTargettedSpaceArg := fakeSharedActor.CheckTargetArgsForCall(0) 58 Expect(checkTargettedOrgArg).To(BeFalse()) 59 Expect(checkTargettedSpaceArg).To(BeFalse()) 60 }) 61 }) 62 63 Context("when the user is logged in", func() { 64 BeforeEach(func() { 65 fakeConfig.RefreshTokenReturns("existing-refresh-token") 66 }) 67 68 Context("when an error is encountered refreshing the access token", func() { 69 var expectedErr error 70 71 BeforeEach(func() { 72 expectedErr = errors.New("refresh access token error") 73 fakeActor.RefreshAccessTokenReturns("", expectedErr) 74 }) 75 76 It("returns the error", func() { 77 Expect(executeErr).To(MatchError(expectedErr)) 78 79 Expect(testUI.Out).ToNot(Say("new-access-token")) 80 81 Expect(fakeActor.RefreshAccessTokenCallCount()).To(Equal(1)) 82 Expect(fakeActor.RefreshAccessTokenArgsForCall(0)).To(Equal("existing-refresh-token")) 83 }) 84 }) 85 86 Context("when no errors are encountered refreshing the access token", func() { 87 BeforeEach(func() { 88 fakeActor.RefreshAccessTokenReturns("new-access-token", nil) 89 }) 90 91 It("refreshes the access and refresh tokens and displays the access token", func() { 92 Expect(executeErr).ToNot(HaveOccurred()) 93 94 Expect(testUI.Out).To(Say("new-access-token")) 95 96 Expect(fakeActor.RefreshAccessTokenCallCount()).To(Equal(1)) 97 Expect(fakeActor.RefreshAccessTokenArgsForCall(0)).To(Equal("existing-refresh-token")) 98 }) 99 }) 100 }) 101 })