github.com/loafoe/cli@v7.1.0+incompatible/command/v7/oauth_token_command_test.go (about) 1 package v7_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/v7" 9 "code.cloudfoundry.org/cli/command/v7/v7fakes" 10 "code.cloudfoundry.org/cli/util/ui" 11 "github.com/SermoDigital/jose/crypto" 12 "github.com/SermoDigital/jose/jws" 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 . "github.com/onsi/gomega/gbytes" 16 ) 17 18 var _ = Describe("oauth-token command", func() { 19 var ( 20 cmd OauthTokenCommand 21 testUI *ui.UI 22 fakeConfig *commandfakes.FakeConfig 23 fakeSharedActor *commandfakes.FakeSharedActor 24 fakeActor *v7fakes.FakeActor 25 binaryName string 26 executeErr error 27 ) 28 29 BeforeEach(func() { 30 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 31 fakeConfig = new(commandfakes.FakeConfig) 32 fakeSharedActor = new(commandfakes.FakeSharedActor) 33 fakeActor = new(v7fakes.FakeActor) 34 35 cmd = OauthTokenCommand{ 36 BaseCommand: BaseCommand{ 37 UI: testUI, 38 Config: fakeConfig, 39 SharedActor: fakeSharedActor, 40 Actor: fakeActor, 41 }, 42 } 43 44 binaryName = "faceman" 45 fakeConfig.BinaryNameReturns(binaryName) 46 }) 47 48 JustBeforeEach(func() { 49 executeErr = cmd.Execute(nil) 50 }) 51 52 When("checking the target fails", func() { 53 BeforeEach(func() { 54 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 55 }) 56 57 It("returns a wrapped error", func() { 58 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 59 60 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 61 checkTargettedOrgArg, checkTargettedSpaceArg := fakeSharedActor.CheckTargetArgsForCall(0) 62 Expect(checkTargettedOrgArg).To(BeFalse()) 63 Expect(checkTargettedSpaceArg).To(BeFalse()) 64 }) 65 }) 66 67 When("logged in as a client", func() { 68 BeforeEach(func() { 69 fakeConfig.UAAGrantTypeReturns("client_credentials") 70 }) 71 72 When("the existing access token is invalid", func() { 73 BeforeEach(func() { 74 token := jws.NewJWT(jws.Claims{}, crypto.SigningMethodHS256) 75 fakeConfig.AccessTokenReturns("invalid-existing-access-token") 76 fakeActor.ParseAccessTokenReturns(token, errors.New("Access token is invalid")) 77 }) 78 79 It("errors", func() { 80 Expect(executeErr).To(MatchError(errors.New("Access token is invalid."))) 81 82 Expect(testUI.Out).ToNot(Say("new-access-token")) 83 84 Expect(fakeActor.RefreshAccessTokenCallCount()).To(Equal(0)) 85 Expect(fakeActor.ParseAccessTokenCallCount()).To(Equal(1)) 86 Expect(fakeActor.ParseAccessTokenArgsForCall(0)).To(Equal("invalid-existing-access-token")) 87 }) 88 }) 89 90 When("the existing access token does not have an expiry time", func() { 91 BeforeEach(func() { 92 token := jws.NewJWT(jws.Claims{}, crypto.SigningMethodHS256) 93 fakeConfig.AccessTokenReturns("existing-access-token") 94 fakeActor.ParseAccessTokenReturns(token, nil) 95 }) 96 97 It("errors", func() { 98 Expect(executeErr).To(MatchError(errors.New("Access token is missing expiration claim."))) 99 100 Expect(testUI.Out).ToNot(Say("new-access-token")) 101 102 Expect(fakeActor.RefreshAccessTokenCallCount()).To(Equal(0)) 103 Expect(fakeActor.ParseAccessTokenCallCount()).To(Equal(1)) 104 Expect(fakeActor.ParseAccessTokenArgsForCall(0)).To(Equal("existing-access-token")) 105 }) 106 }) 107 108 }) 109 110 When("logged in as a user", func() { 111 BeforeEach(func() { 112 fakeConfig.RefreshTokenReturns("existing-refresh-token") 113 }) 114 115 When("an error is encountered refreshing the access token", func() { 116 var expectedErr error 117 118 BeforeEach(func() { 119 expectedErr = errors.New("refresh access token error") 120 fakeActor.RefreshAccessTokenReturns("", expectedErr) 121 }) 122 123 It("returns the error", func() { 124 Expect(executeErr).To(MatchError(expectedErr)) 125 126 Expect(testUI.Out).ToNot(Say("new-access-token")) 127 128 Expect(fakeActor.RefreshAccessTokenCallCount()).To(Equal(1)) 129 }) 130 }) 131 132 When("no errors are encountered refreshing the access token", func() { 133 BeforeEach(func() { 134 fakeActor.RefreshAccessTokenReturns("new-access-token", nil) 135 }) 136 137 It("refreshes the access and refresh tokens and displays the access token", func() { 138 Expect(executeErr).ToNot(HaveOccurred()) 139 140 Expect(testUI.Out).To(Say("new-access-token")) 141 142 Expect(fakeActor.RefreshAccessTokenCallCount()).To(Equal(1)) 143 }) 144 }) 145 }) 146 })