github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+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/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       *v6fakes.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(v6fakes.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  	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  	When("the user is logged in", func() {
    64  		BeforeEach(func() {
    65  			fakeConfig.RefreshTokenReturns("existing-refresh-token")
    66  		})
    67  
    68  		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  		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  })