github.com/loafoe/cli@v7.1.0+incompatible/actor/v3action/organization_test.go (about) 1 package v3action_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/actor/actionerror" 7 . "code.cloudfoundry.org/cli/actor/v3action" 8 "code.cloudfoundry.org/cli/actor/v3action/v3actionfakes" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 10 11 "code.cloudfoundry.org/cli/resources" 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 ) 15 16 var _ = Describe("Organization Actions", func() { 17 var ( 18 actor *Actor 19 fakeCloudControllerClient *v3actionfakes.FakeCloudControllerClient 20 ) 21 22 BeforeEach(func() { 23 fakeCloudControllerClient = new(v3actionfakes.FakeCloudControllerClient) 24 actor = NewActor(fakeCloudControllerClient, nil, nil, nil) 25 }) 26 27 Describe("GetOrganizationByName", func() { 28 var ( 29 executeErr error 30 warnings Warnings 31 org Organization 32 ) 33 JustBeforeEach(func() { 34 org, warnings, executeErr = actor.GetOrganizationByName("some-org-name") 35 }) 36 37 When("the org exists", func() { 38 BeforeEach(func() { 39 fakeCloudControllerClient.GetOrganizationsReturns( 40 []resources.Organization{ 41 { 42 Name: "some-org-name", 43 GUID: "some-org-guid", 44 }, 45 }, 46 ccv3.Warnings{"some-warning"}, 47 nil, 48 ) 49 }) 50 51 It("returns the organization and warnings", func() { 52 Expect(executeErr).ToNot(HaveOccurred()) 53 Expect(org).To(Equal(Organization{ 54 Name: "some-org-name", 55 GUID: "some-org-guid", 56 })) 57 Expect(warnings).To(ConsistOf("some-warning")) 58 59 Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1)) 60 Expect(fakeCloudControllerClient.GetOrganizationsArgsForCall(0)).To(ConsistOf( 61 ccv3.Query{Key: ccv3.NameFilter, Values: []string{"some-org-name"}}, 62 )) 63 }) 64 }) 65 66 When("the cloud controller client returns an error", func() { 67 var expectedError error 68 69 BeforeEach(func() { 70 expectedError = errors.New("I am a CloudControllerClient Error") 71 fakeCloudControllerClient.GetOrganizationsReturns( 72 []resources.Organization{}, 73 ccv3.Warnings{"some-warning"}, 74 expectedError) 75 }) 76 77 It("returns the warnings and the error", func() { 78 Expect(warnings).To(ConsistOf("some-warning")) 79 Expect(executeErr).To(MatchError(expectedError)) 80 }) 81 }) 82 83 When("the org does not exist", func() { 84 BeforeEach(func() { 85 fakeCloudControllerClient.GetOrganizationsReturns( 86 []resources.Organization{}, 87 ccv3.Warnings{"some-warning"}, 88 nil, 89 ) 90 }) 91 92 It("returns an OrganizationNotFoundError and the warnings", func() { 93 Expect(warnings).To(ConsistOf("some-warning")) 94 Expect(executeErr).To(MatchError(actionerror.OrganizationNotFoundError{Name: "some-org-name"})) 95 }) 96 }) 97 }) 98 99 Describe("GetOrganizationsByGUIDs", func() { 100 When("the orgs exists", func() { 101 var ( 102 executeErr error 103 warnings Warnings 104 orgs []Organization 105 ) 106 BeforeEach(func() { 107 fakeCloudControllerClient.GetOrganizationsReturns( 108 []resources.Organization{ 109 { 110 Name: "some-org-name", 111 GUID: "some-org-guid", 112 }, 113 { 114 Name: "another-org-name", 115 GUID: "another-org-guid", 116 }, 117 }, 118 ccv3.Warnings{"some-warning"}, 119 nil, 120 ) 121 fakeCloudControllerClient.CloudControllerAPIVersionReturns("3.65.0") 122 }) 123 124 JustBeforeEach(func() { 125 orgs, warnings, executeErr = actor.GetOrganizationsByGUIDs("some-org-guid", "another-org-guid") 126 }) 127 128 It("returns the organizations and warnings", func() { 129 Expect(executeErr).ToNot(HaveOccurred()) 130 Expect(orgs).To(ConsistOf( 131 Organization{ 132 Name: "some-org-name", 133 GUID: "some-org-guid", 134 }, 135 Organization{ 136 Name: "another-org-name", 137 GUID: "another-org-guid", 138 }, 139 )) 140 Expect(warnings).To(ConsistOf("some-warning")) 141 142 Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1)) 143 Expect(fakeCloudControllerClient.GetOrganizationsArgsForCall(0)).To(ConsistOf( 144 ccv3.Query{Key: ccv3.GUIDFilter, Values: []string{"some-org-guid", "another-org-guid"}}, 145 )) 146 }) 147 148 When("the cloud controller client returns an error", func() { 149 var expectedError error 150 151 BeforeEach(func() { 152 expectedError = errors.New("I am a CloudControllerClient Error") 153 fakeCloudControllerClient.GetOrganizationsReturns( 154 []resources.Organization{}, 155 ccv3.Warnings{"some-warning"}, 156 expectedError) 157 }) 158 159 It("returns the warnings and the error", func() { 160 Expect(warnings).To(ConsistOf("some-warning")) 161 Expect(executeErr).To(MatchError(expectedError)) 162 }) 163 }) 164 }) 165 }) 166 167 Describe("GetOrganizations", func() { 168 var ( 169 executeErr error 170 warnings Warnings 171 orgs []Organization 172 ) 173 174 JustBeforeEach(func() { 175 orgs, warnings, executeErr = actor.GetOrganizations() 176 }) 177 178 It("fetches all the organizations", func() { 179 Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1)) 180 }) 181 182 When("no error occurs", func() { 183 BeforeEach(func() { 184 fakeCloudControllerClient.GetOrganizationsReturns( 185 []resources.Organization{ 186 resources.Organization{ 187 Name: "some-org-1", 188 GUID: "some-org-guid-1", 189 }, 190 resources.Organization{ 191 Name: "some-org-2", 192 GUID: "some-org-guid-2", 193 }, 194 }, 195 ccv3.Warnings{ 196 "some-warning-1", 197 "some-warning-2", 198 }, 199 nil, 200 ) 201 }) 202 203 It("returns the organizations", func() { 204 Expect(orgs).To(Equal( 205 []Organization{ 206 Organization{ 207 Name: "some-org-1", 208 GUID: "some-org-guid-1", 209 }, 210 Organization{ 211 Name: "some-org-2", 212 GUID: "some-org-guid-2", 213 }, 214 }, 215 )) 216 }) 217 218 It("returns all the warnings", func() { 219 Expect(warnings).To(Equal(Warnings{"some-warning-1", "some-warning-2"})) 220 }) 221 }) 222 223 When("an error occurs", func() { 224 BeforeEach(func() { 225 fakeCloudControllerClient.GetOrganizationsReturns( 226 []resources.Organization{}, 227 ccv3.Warnings{ 228 "some-warning-1", 229 "some-warning-2", 230 }, 231 errors.New("get-organization-error"), 232 ) 233 }) 234 235 It("returns the error", func() { 236 Expect(executeErr).To(MatchError("get-organization-error")) 237 }) 238 239 It("returns the warnings", func() { 240 Expect(warnings).To(Equal(Warnings{"some-warning-1", "some-warning-2"})) 241 }) 242 }) 243 }) 244 245 })