github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/command/v2/oauth_token_command_test.go (about)

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