github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/actor/sharedaction/auth_test.go (about)

     1  package sharedaction_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	. "code.cloudfoundry.org/cli/actor/sharedaction"
     7  	"code.cloudfoundry.org/cli/actor/sharedaction/sharedactionfakes"
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  )
    11  
    12  var _ = Describe("AuthActor", func() {
    13  	var (
    14  		actor      *Actor
    15  		fakeConfig *sharedactionfakes.FakeConfig
    16  	)
    17  
    18  	BeforeEach(func() {
    19  		fakeConfig = new(sharedactionfakes.FakeConfig)
    20  	})
    21  
    22  	Context("Default CF on VMs", func() {
    23  		BeforeEach(func() {
    24  			actor = NewActor(fakeConfig)
    25  		})
    26  
    27  		When("only the access token is set", func() {
    28  			BeforeEach(func() {
    29  				fakeConfig.AccessTokenReturns("some-access-token")
    30  			})
    31  
    32  			It("returns true", func() {
    33  				Expect(actor.IsLoggedIn()).To(BeTrue())
    34  			})
    35  		})
    36  
    37  		When("only the refresh token is set", func() {
    38  			BeforeEach(func() {
    39  				fakeConfig.RefreshTokenReturns("some-refresh-token")
    40  			})
    41  
    42  			It("returns true", func() {
    43  				Expect(actor.IsLoggedIn()).To(BeTrue())
    44  			})
    45  		})
    46  
    47  		When("both access and refresh token are set", func() {
    48  			BeforeEach(func() {
    49  				fakeConfig.AccessTokenReturns("some-access-token")
    50  				fakeConfig.RefreshTokenReturns("some-refresh-token")
    51  			})
    52  
    53  			It("returns true", func() {
    54  				Expect(actor.IsLoggedIn()).To(BeTrue())
    55  			})
    56  		})
    57  
    58  		When("neither access nor refresh token are set", func() {
    59  			BeforeEach(func() {
    60  				fakeConfig.AccessTokenReturns("")
    61  				fakeConfig.RefreshTokenReturns("")
    62  			})
    63  
    64  			It("returns false", func() {
    65  				Expect(actor.IsLoggedIn()).To(BeFalse())
    66  			})
    67  		})
    68  	})
    69  
    70  	Context("CF on K8s", func() {
    71  		BeforeEach(func() {
    72  			fakeConfig.IsCFOnK8sReturns(true)
    73  			actor = NewActor(fakeConfig)
    74  		})
    75  
    76  		When("the auth info is set", func() {
    77  			BeforeEach(func() {
    78  				fakeConfig.CurrentUserNameReturns("non-empty", nil)
    79  			})
    80  
    81  			It("returns true", func() {
    82  				Expect(actor.IsLoggedIn()).To(BeTrue())
    83  			})
    84  		})
    85  
    86  		When("the auth info is not set", func() {
    87  			BeforeEach(func() {
    88  				fakeConfig.CurrentUserNameReturns("", nil)
    89  			})
    90  
    91  			It("returns false", func() {
    92  				Expect(actor.IsLoggedIn()).To(BeFalse())
    93  			})
    94  		})
    95  
    96  		When("getting the current user name fails", func() {
    97  			BeforeEach(func() {
    98  				fakeConfig.CurrentUserNameReturns("", errors.New("boom!"))
    99  			})
   100  
   101  			It("returns false", func() {
   102  				Expect(actor.IsLoggedIn()).To(BeFalse())
   103  			})
   104  		})
   105  	})
   106  })