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

     1  package sharedaction_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	. "code.cloudfoundry.org/cli/actor/sharedaction"
     8  	"code.cloudfoundry.org/cli/actor/sharedaction/sharedactionfakes"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/ginkgo/extensions/table"
    11  	. "github.com/onsi/gomega"
    12  )
    13  
    14  var _ = Describe("Checking target", func() {
    15  	var (
    16  		actor      *Actor
    17  		binaryName string
    18  		fakeConfig *sharedactionfakes.FakeConfig
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		binaryName = "faceman"
    23  		fakeConfig = new(sharedactionfakes.FakeConfig)
    24  		fakeConfig.BinaryNameReturns(binaryName)
    25  		actor = NewActor(fakeConfig)
    26  	})
    27  
    28  	Describe("CheckTarget", func() {
    29  		When("the user is not logged in", func() {
    30  			It("returns an error", func() {
    31  				err := actor.CheckTarget(false, false)
    32  				Expect(err).To(MatchError(actionerror.NotLoggedInError{
    33  					BinaryName: binaryName,
    34  				}))
    35  			})
    36  		})
    37  
    38  		When("the user is logged in", func() {
    39  			BeforeEach(func() {
    40  				fakeConfig.AccessTokenReturns("some-access-token")
    41  				fakeConfig.RefreshTokenReturns("some-refresh-token")
    42  			})
    43  
    44  			DescribeTable("targeting org check",
    45  				func(isOrgTargeted bool, checkForTargeted bool, expectedError error) {
    46  					fakeConfig.HasTargetedOrganizationReturns(isOrgTargeted)
    47  
    48  					err := actor.CheckTarget(checkForTargeted, false)
    49  
    50  					if expectedError != nil {
    51  						Expect(err).To(MatchError(expectedError))
    52  					} else {
    53  						Expect(err).ToNot(HaveOccurred())
    54  					}
    55  				},
    56  
    57  				Entry("it returns an error", false, true, actionerror.NoOrganizationTargetedError{BinaryName: "faceman"}),
    58  				Entry("it does not return an error", false, false, nil),
    59  				Entry("it does not return an error", true, false, nil),
    60  				Entry("it does not return an error", true, true, nil),
    61  			)
    62  
    63  			When("the organization is targeted", func() {
    64  				BeforeEach(func() {
    65  					fakeConfig.HasTargetedOrganizationReturns(true)
    66  				})
    67  
    68  				DescribeTable("targeting space check",
    69  					func(isSpaceTargeted bool, checkForTargeted bool, expectedError error) {
    70  						fakeConfig.HasTargetedSpaceReturns(isSpaceTargeted)
    71  
    72  						err := actor.CheckTarget(true, checkForTargeted)
    73  
    74  						if expectedError != nil {
    75  							Expect(err).To(MatchError(expectedError))
    76  						} else {
    77  							Expect(err).ToNot(HaveOccurred())
    78  						}
    79  					},
    80  
    81  					Entry("it returns an error", false, true, actionerror.NoSpaceTargetedError{BinaryName: "faceman"}),
    82  					Entry("it does not return an error", false, false, nil),
    83  					Entry("it does not return an error", true, false, nil),
    84  					Entry("it does not return an error", true, true, nil),
    85  				)
    86  			})
    87  		})
    88  	})
    89  
    90  	Describe("RequireCurrentUser", func() {
    91  		var (
    92  			user string
    93  			err  error
    94  		)
    95  
    96  		JustBeforeEach(func() {
    97  			user, err = actor.RequireCurrentUser()
    98  		})
    99  
   100  		When("access token and refresh token are empty", func() {
   101  			BeforeEach(func() {
   102  				fakeConfig.AccessTokenReturns("")
   103  				fakeConfig.RefreshTokenReturns("")
   104  			})
   105  
   106  			It("returns a NotLoggedInError with binary name", func() {
   107  				Expect(err).To(MatchError(actionerror.NotLoggedInError{
   108  					BinaryName: binaryName,
   109  				}))
   110  			})
   111  		})
   112  
   113  		When("access token is available", func() {
   114  			BeforeEach(func() {
   115  				fakeConfig.AccessTokenReturns("some-access-token")
   116  			})
   117  
   118  			When("getting current user returns an error", func() {
   119  				BeforeEach(func() {
   120  					fakeConfig.CurrentUserNameReturns("", errors.New("get-user-error"))
   121  				})
   122  
   123  				It("returns the error", func() {
   124  					Expect(err).To(MatchError("get-user-error"))
   125  				})
   126  			})
   127  
   128  			When("getting current user succeeds", func() {
   129  				BeforeEach(func() {
   130  					fakeConfig.CurrentUserNameReturns("some-user", nil)
   131  				})
   132  
   133  				It("returns the user", func() {
   134  					Expect(err).To(BeNil())
   135  					Expect(user).To(Equal("some-user"))
   136  				})
   137  			})
   138  		})
   139  
   140  		When("refresh token is available", func() {
   141  			BeforeEach(func() {
   142  				fakeConfig.RefreshTokenReturns("some-refresh-token")
   143  			})
   144  
   145  			When("getting current user returns an error", func() {
   146  				BeforeEach(func() {
   147  					fakeConfig.CurrentUserNameReturns("", errors.New("get-user-error"))
   148  				})
   149  
   150  				It("returns the error", func() {
   151  					Expect(err).To(MatchError("get-user-error"))
   152  				})
   153  			})
   154  
   155  			When("getting current user succeeds", func() {
   156  				BeforeEach(func() {
   157  					fakeConfig.CurrentUserNameReturns("some-user", nil)
   158  				})
   159  
   160  				It("returns the user object", func() {
   161  					Expect(err).To(BeNil())
   162  					Expect(user).To(Equal("some-user"))
   163  				})
   164  			})
   165  		})
   166  	})
   167  
   168  	Describe("RequireTargetedOrg", func() {
   169  		var (
   170  			org string
   171  			err error
   172  		)
   173  
   174  		JustBeforeEach(func() {
   175  			org, err = actor.RequireTargetedOrg()
   176  		})
   177  
   178  		When("no org is targeted", func() {
   179  			BeforeEach(func() {
   180  				fakeConfig.HasTargetedOrganizationReturns(false)
   181  			})
   182  
   183  			It("returns a NoOrganizationTargetedError with the binary name", func() {
   184  				Expect(err).To(MatchError(actionerror.NoOrganizationTargetedError{
   185  					BinaryName: binaryName,
   186  				}))
   187  				Expect(org).To(BeEmpty())
   188  			})
   189  		})
   190  
   191  		When("an org is targeted", func() {
   192  			BeforeEach(func() {
   193  				fakeConfig.HasTargetedOrganizationReturns(true)
   194  				fakeConfig.TargetedOrganizationNameReturns("some-name")
   195  			})
   196  
   197  			It("returns the org name", func() {
   198  				Expect(err).To(BeNil())
   199  				Expect(org).To(Equal("some-name"))
   200  			})
   201  		})
   202  	})
   203  })