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