github.com/arunkumar7540/cli@v6.45.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  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("Organization Actions", func() {
    16  	var (
    17  		actor                     *Actor
    18  		fakeCloudControllerClient *v3actionfakes.FakeCloudControllerClient
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		fakeCloudControllerClient = new(v3actionfakes.FakeCloudControllerClient)
    23  		actor = NewActor(fakeCloudControllerClient, nil, nil, nil)
    24  	})
    25  
    26  	Describe("GetOrganizationByName", func() {
    27  		var (
    28  			executeErr error
    29  			warnings   Warnings
    30  			org        Organization
    31  		)
    32  		JustBeforeEach(func() {
    33  			org, warnings, executeErr = actor.GetOrganizationByName("some-org-name")
    34  		})
    35  
    36  		When("the org exists", func() {
    37  			BeforeEach(func() {
    38  				fakeCloudControllerClient.GetOrganizationsReturns(
    39  					[]ccv3.Organization{
    40  						{
    41  							Name: "some-org-name",
    42  							GUID: "some-org-guid",
    43  						},
    44  					},
    45  					ccv3.Warnings{"some-warning"},
    46  					nil,
    47  				)
    48  			})
    49  
    50  			It("returns the organization and warnings", func() {
    51  				Expect(executeErr).ToNot(HaveOccurred())
    52  				Expect(org).To(Equal(Organization{
    53  					Name: "some-org-name",
    54  					GUID: "some-org-guid",
    55  				}))
    56  				Expect(warnings).To(ConsistOf("some-warning"))
    57  
    58  				Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1))
    59  				Expect(fakeCloudControllerClient.GetOrganizationsArgsForCall(0)).To(ConsistOf(
    60  					ccv3.Query{Key: ccv3.NameFilter, Values: []string{"some-org-name"}},
    61  				))
    62  			})
    63  		})
    64  
    65  		When("the cloud controller client returns an error", func() {
    66  			var expectedError error
    67  
    68  			BeforeEach(func() {
    69  				expectedError = errors.New("I am a CloudControllerClient Error")
    70  				fakeCloudControllerClient.GetOrganizationsReturns(
    71  					[]ccv3.Organization{},
    72  					ccv3.Warnings{"some-warning"},
    73  					expectedError)
    74  			})
    75  
    76  			It("returns the warnings and the error", func() {
    77  				Expect(warnings).To(ConsistOf("some-warning"))
    78  				Expect(executeErr).To(MatchError(expectedError))
    79  			})
    80  		})
    81  
    82  		When("the org does not exist", func() {
    83  			BeforeEach(func() {
    84  				fakeCloudControllerClient.GetOrganizationsReturns(
    85  					[]ccv3.Organization{},
    86  					ccv3.Warnings{"some-warning"},
    87  					nil,
    88  				)
    89  			})
    90  
    91  			It("returns an OrganizationNotFoundError and the warnings", func() {
    92  				Expect(warnings).To(ConsistOf("some-warning"))
    93  				Expect(executeErr).To(MatchError(actionerror.OrganizationNotFoundError{Name: "some-org-name"}))
    94  			})
    95  		})
    96  	})
    97  
    98  	Describe("GetOrganizationsByGUIDs", func() {
    99  		Context("when organizations endpoint supports the 'guids' param", 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  						[]ccv3.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  							[]ccv3.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  		Context("when organizations endpoint does not support the 'guids' param", func() {
   168  			var (
   169  				executeErr error
   170  				warnings   Warnings
   171  				orgs       []Organization
   172  			)
   173  			BeforeEach(func() {
   174  				fakeCloudControllerClient.GetOrganizationsReturns(
   175  					[]ccv3.Organization{
   176  						{
   177  							Name: "some-org-name",
   178  							GUID: "some-org-guid",
   179  						},
   180  						{
   181  							Name: "another-org-name",
   182  							GUID: "another-org-guid",
   183  						},
   184  						{
   185  							Name: "yetanother-org-name",
   186  							GUID: "yetanother-org-guid",
   187  						},
   188  						{
   189  							Name: "fourth-org-name",
   190  							GUID: "fourth-org-guid",
   191  						},
   192  					},
   193  					ccv3.Warnings{"some-warning"},
   194  					nil,
   195  				)
   196  				fakeCloudControllerClient.CloudControllerAPIVersionReturns("3.45.0")
   197  			})
   198  
   199  			JustBeforeEach(func() {
   200  				orgs, warnings, executeErr = actor.GetOrganizationsByGUIDs("some-org-guid", "another-org-guid")
   201  			})
   202  
   203  			It("returns the organizations and warnings", func() {
   204  				Expect(executeErr).ToNot(HaveOccurred())
   205  				Expect(orgs).To(ConsistOf(
   206  					Organization{
   207  						Name: "some-org-name",
   208  						GUID: "some-org-guid",
   209  					},
   210  					Organization{
   211  						Name: "another-org-name",
   212  						GUID: "another-org-guid",
   213  					},
   214  				))
   215  				Expect(warnings).To(ConsistOf("some-warning"))
   216  
   217  				Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1))
   218  				Expect(fakeCloudControllerClient.GetOrganizationsArgsForCall(0)).To(BeEmpty())
   219  			})
   220  		})
   221  	})
   222  
   223  	Describe("GetOrganizations", func() {
   224  		var (
   225  			executeErr error
   226  			warnings   Warnings
   227  			orgs       []Organization
   228  		)
   229  
   230  		JustBeforeEach(func() {
   231  			orgs, warnings, executeErr = actor.GetOrganizations()
   232  		})
   233  
   234  		It("fetches all the organizations", func() {
   235  			Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1))
   236  		})
   237  
   238  		When("no error occurs", func() {
   239  			BeforeEach(func() {
   240  				fakeCloudControllerClient.GetOrganizationsReturns(
   241  					[]ccv3.Organization{
   242  						ccv3.Organization{
   243  							Name: "some-org-1",
   244  							GUID: "some-org-guid-1",
   245  						},
   246  						ccv3.Organization{
   247  							Name: "some-org-2",
   248  							GUID: "some-org-guid-2",
   249  						},
   250  					},
   251  					ccv3.Warnings{
   252  						"some-warning-1",
   253  						"some-warning-2",
   254  					},
   255  					nil,
   256  				)
   257  			})
   258  
   259  			It("returns the organizations", func() {
   260  				Expect(orgs).To(Equal(
   261  					[]Organization{
   262  						Organization{
   263  							Name: "some-org-1",
   264  							GUID: "some-org-guid-1",
   265  						},
   266  						Organization{
   267  							Name: "some-org-2",
   268  							GUID: "some-org-guid-2",
   269  						},
   270  					},
   271  				))
   272  			})
   273  
   274  			It("returns all the warnings", func() {
   275  				Expect(warnings).To(Equal(Warnings{"some-warning-1", "some-warning-2"}))
   276  			})
   277  		})
   278  
   279  		When("an error occurs", func() {
   280  			BeforeEach(func() {
   281  				fakeCloudControllerClient.GetOrganizationsReturns(
   282  					[]ccv3.Organization{},
   283  					ccv3.Warnings{
   284  						"some-warning-1",
   285  						"some-warning-2",
   286  					},
   287  					errors.New("get-organization-error"),
   288  				)
   289  			})
   290  
   291  			It("returns the error", func() {
   292  				Expect(executeErr).To(MatchError("get-organization-error"))
   293  			})
   294  
   295  			It("returns the warnings", func() {
   296  				Expect(warnings).To(Equal(Warnings{"some-warning-1", "some-warning-2"}))
   297  			})
   298  		})
   299  	})
   300  
   301  })