github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/actor/sharedaction/check_target_test.go (about) 1 package sharedaction_test 2 3 import ( 4 "code.cloudfoundry.org/cli/actor/actionerror" 5 . "code.cloudfoundry.org/cli/actor/sharedaction" 6 "code.cloudfoundry.org/cli/actor/sharedaction/sharedactionfakes" 7 . "github.com/onsi/ginkgo" 8 . "github.com/onsi/ginkgo/extensions/table" 9 . "github.com/onsi/gomega" 10 ) 11 12 var _ = Describe("CheckTarget", func() { 13 var ( 14 actor *Actor 15 binaryName string 16 fakeConfig *sharedactionfakes.FakeConfig 17 ) 18 19 BeforeEach(func() { 20 binaryName = "faceman" 21 fakeConfig = new(sharedactionfakes.FakeConfig) 22 fakeConfig.BinaryNameReturns(binaryName) 23 actor = NewActor(fakeConfig) 24 }) 25 26 Context("when the user is not logged in", func() { 27 It("returns an error", func() { 28 err := actor.CheckTarget(false, false) 29 Expect(err).To(MatchError(actionerror.NotLoggedInError{ 30 BinaryName: binaryName, 31 })) 32 }) 33 }) 34 35 Context("when the user is logged in", func() { 36 BeforeEach(func() { 37 fakeConfig.AccessTokenReturns("some-access-token") 38 fakeConfig.RefreshTokenReturns("some-refresh-token") 39 }) 40 41 DescribeTable("targeting org check", 42 func(isOrgTargeted bool, checkForTargeted bool, expectedError error) { 43 fakeConfig.HasTargetedOrganizationReturns(isOrgTargeted) 44 45 err := actor.CheckTarget(checkForTargeted, false) 46 47 if expectedError != nil { 48 Expect(err).To(MatchError(expectedError)) 49 } else { 50 Expect(err).ToNot(HaveOccurred()) 51 } 52 }, 53 54 Entry("it returns an error", false, true, actionerror.NoOrganizationTargetedError{BinaryName: "faceman"}), 55 Entry("it does not return an error", false, false, nil), 56 Entry("it does not return an error", true, false, nil), 57 Entry("it does not return an error", true, true, nil), 58 ) 59 60 Context("when the organization is targeted", func() { 61 BeforeEach(func() { 62 fakeConfig.HasTargetedOrganizationReturns(true) 63 }) 64 65 DescribeTable("targeting space check", 66 func(isSpaceTargeted bool, checkForTargeted bool, expectedError error) { 67 fakeConfig.HasTargetedSpaceReturns(isSpaceTargeted) 68 69 err := actor.CheckTarget(true, checkForTargeted) 70 71 if expectedError != nil { 72 Expect(err).To(MatchError(expectedError)) 73 } else { 74 Expect(err).ToNot(HaveOccurred()) 75 } 76 }, 77 78 Entry("it returns an error", false, true, actionerror.NoSpaceTargetedError{BinaryName: "faceman"}), 79 Entry("it does not return an error", false, false, nil), 80 Entry("it does not return an error", true, false, nil), 81 Entry("it does not return an error", true, true, nil), 82 ) 83 }) 84 }) 85 })