github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/logout_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"github.com/LukasHeimann/cloudfoundrycli/v8/api/uaa"
     5  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/commandfakes"
     6  	. "github.com/LukasHeimann/cloudfoundrycli/v8/command/v7"
     7  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/v7/v7fakes"
     8  	"github.com/LukasHeimann/cloudfoundrycli/v8/util/configv3"
     9  	"github.com/LukasHeimann/cloudfoundrycli/v8/util/ui"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  	. "github.com/onsi/gomega/gbytes"
    13  )
    14  
    15  var _ = Describe("logout command", func() {
    16  	var (
    17  		cmd        LogoutCommand
    18  		testUI     *ui.UI
    19  		fakeConfig *commandfakes.FakeConfig
    20  		fakeActor  *v7fakes.FakeActor
    21  		executeErr error
    22  	)
    23  
    24  	BeforeEach(func() {
    25  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    26  		fakeConfig = new(commandfakes.FakeConfig)
    27  		fakeActor = new(v7fakes.FakeActor)
    28  		cmd = LogoutCommand{
    29  			BaseCommand: BaseCommand{
    30  				UI:     testUI,
    31  				Config: fakeConfig,
    32  				Actor:  fakeActor,
    33  			},
    34  		}
    35  
    36  		fakeActor.GetCurrentUserReturns(
    37  			configv3.User{
    38  				Name: "some-user",
    39  			},
    40  			nil)
    41  	})
    42  
    43  	JustBeforeEach(func() {
    44  		executeErr = cmd.Execute(nil)
    45  	})
    46  
    47  	It("outputs logging out display message", func() {
    48  		Expect(executeErr).ToNot(HaveOccurred())
    49  
    50  		Expect(fakeConfig.UnsetUserInformationCallCount()).To(Equal(1))
    51  		Expect(testUI.Out).To(Say("Logging out some-user..."))
    52  		Expect(testUI.Out).To(Say("OK"))
    53  	})
    54  
    55  	It("calls to revoke the auth tokens", func() {
    56  		Expect(fakeActor.RevokeAccessAndRefreshTokensCallCount()).To(Equal(1))
    57  	})
    58  
    59  	When("unable to revoke token", func() {
    60  		When("because the user is not logged in", func() {
    61  			BeforeEach(func() {
    62  				fakeActor.GetCurrentUserReturns(configv3.User{}, nil)
    63  			})
    64  
    65  			It("does not impact the logout", func() {
    66  				Expect(executeErr).ToNot(HaveOccurred())
    67  				Expect(fakeActor.GetCurrentUserCallCount()).To(Equal(1))
    68  				Expect(fakeConfig.UnsetUserInformationCallCount()).To(Equal(1))
    69  				Expect(testUI.Out).To(Say("Logging out ..."))
    70  				Expect(testUI.Out).To(Say("OK"))
    71  			})
    72  		})
    73  
    74  		When("because the attempt to revoke fails", func() {
    75  			BeforeEach(func() {
    76  				fakeActor.RevokeAccessAndRefreshTokensReturns(error(uaa.UnauthorizedError{Message: "test error"}))
    77  			})
    78  
    79  			It("does not impact the logout", func() {
    80  				Expect(executeErr).ToNot(HaveOccurred())
    81  				Expect(fakeConfig.UnsetUserInformationCallCount()).To(Equal(1))
    82  				Expect(testUI.Out).To(Say("Logging out some-user..."))
    83  				Expect(testUI.Out).To(Say("OK"))
    84  			})
    85  		})
    86  	})
    87  })