github.com/lukasheimann/cloudfoundrycli@v7.1.0+incompatible/actor/v7action/organization_test.go (about)

     1  package v7action_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	. "code.cloudfoundry.org/cli/actor/v7action"
     8  	"code.cloudfoundry.org/cli/actor/v7action/v7actionfakes"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    10  	"code.cloudfoundry.org/cli/resources"
    11  	. "code.cloudfoundry.org/cli/resources"
    12  
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  )
    16  
    17  var _ = Describe("Organization Actions", func() {
    18  	var (
    19  		actor                     *Actor
    20  		fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient
    21  	)
    22  
    23  	BeforeEach(func() {
    24  		fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient)
    25  		actor = NewActor(fakeCloudControllerClient, nil, nil, nil, nil, nil)
    26  	})
    27  
    28  	Describe("GetOrganizations", func() {
    29  		var (
    30  			returnOrganizations []Organization
    31  			organizations       []Organization
    32  
    33  			organization1Name string
    34  			organization1GUID string
    35  
    36  			organization2Name string
    37  			organization2GUID string
    38  
    39  			organization3Name string
    40  			organization3GUID string
    41  
    42  			warnings   Warnings
    43  			executeErr error
    44  		)
    45  
    46  		BeforeEach(func() {
    47  			returnOrganizations = []Organization{
    48  				{Name: organization1Name, GUID: organization1GUID},
    49  				{Name: organization2Name, GUID: organization2GUID},
    50  				{Name: organization3Name, GUID: organization3GUID},
    51  			}
    52  		})
    53  
    54  		When("the API layer call is successful", func() {
    55  
    56  			BeforeEach(func() {
    57  				fakeCloudControllerClient.GetOrganizationsReturns(
    58  					returnOrganizations,
    59  					ccv3.Warnings{"some-organizations-warning"},
    60  					nil,
    61  				)
    62  			})
    63  
    64  			JustBeforeEach(func() {
    65  				organizations, warnings, executeErr = actor.GetOrganizations("")
    66  			})
    67  
    68  			It("does not error", func() {
    69  				Expect(executeErr).ToNot(HaveOccurred())
    70  			})
    71  
    72  			It("does not pass through the label selector", func() {
    73  				Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1))
    74  				expectedQuery := []ccv3.Query{
    75  					{Key: ccv3.OrderBy, Values: []string{ccv3.NameOrder}},
    76  				}
    77  				actualQuery := fakeCloudControllerClient.GetOrganizationsArgsForCall(0)
    78  				Expect(actualQuery).To(Equal(expectedQuery))
    79  			})
    80  
    81  			It("returns back the organizations and warnings", func() {
    82  				Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1))
    83  
    84  				Expect(organizations).To(ConsistOf(
    85  					Organization{Name: organization1Name, GUID: organization1GUID},
    86  					Organization{Name: organization2Name, GUID: organization2GUID},
    87  					Organization{Name: organization3Name, GUID: organization3GUID},
    88  				))
    89  				Expect(warnings).To(ConsistOf("some-organizations-warning"))
    90  			})
    91  		})
    92  
    93  		When("a label selector is provided", func() {
    94  			JustBeforeEach(func() {
    95  				organizations, warnings, executeErr = actor.GetOrganizations("some-label-selector")
    96  			})
    97  
    98  			It("passes the label selector through", func() {
    99  				Expect(executeErr).ToNot(HaveOccurred())
   100  
   101  				Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1))
   102  
   103  				expectedQuery := []ccv3.Query{
   104  					{Key: ccv3.OrderBy, Values: []string{ccv3.NameOrder}},
   105  					{Key: ccv3.LabelSelectorFilter, Values: []string{"some-label-selector"}},
   106  				}
   107  				actualQuery := fakeCloudControllerClient.GetOrganizationsArgsForCall(0)
   108  				Expect(actualQuery).To(Equal(expectedQuery))
   109  			})
   110  
   111  		})
   112  
   113  		When("when the API layer call returns an error", func() {
   114  			BeforeEach(func() {
   115  				fakeCloudControllerClient.GetOrganizationsReturns(
   116  					[]Organization{},
   117  					ccv3.Warnings{"some-organizations-warning"},
   118  					errors.New("some-organizations-error"),
   119  				)
   120  			})
   121  
   122  			JustBeforeEach(func() {
   123  				organizations, warnings, executeErr = actor.GetOrganizations("")
   124  			})
   125  
   126  			It("returns the error and prints warnings", func() {
   127  				Expect(executeErr).To(MatchError("some-organizations-error"))
   128  				Expect(warnings).To(ConsistOf("some-organizations-warning"))
   129  				Expect(organizations).To(ConsistOf([]Organization{}))
   130  
   131  				Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1))
   132  			})
   133  
   134  		})
   135  	})
   136  
   137  	Describe("GetOrganizationByGUID", func() {
   138  		When("the org exists", func() {
   139  			BeforeEach(func() {
   140  				fakeCloudControllerClient.GetOrganizationReturns(
   141  					Organization{
   142  						Name: "some-org-name",
   143  						GUID: "some-org-guid",
   144  					},
   145  					ccv3.Warnings{"some-warning"},
   146  					nil,
   147  				)
   148  			})
   149  
   150  			It("returns the organization and warnings", func() {
   151  				org, warnings, err := actor.GetOrganizationByGUID("some-org-guid")
   152  				Expect(err).ToNot(HaveOccurred())
   153  				Expect(org).To(Equal(Organization{
   154  					Name: "some-org-name",
   155  					GUID: "some-org-guid",
   156  				}))
   157  				Expect(warnings).To(ConsistOf("some-warning"))
   158  
   159  				Expect(fakeCloudControllerClient.GetOrganizationCallCount()).To(Equal(1))
   160  				Expect(fakeCloudControllerClient.GetOrganizationArgsForCall(0)).To(Equal(
   161  					"some-org-guid",
   162  				))
   163  			})
   164  		})
   165  
   166  		When("the cloud controller client returns an error", func() {
   167  			var expectedError error
   168  
   169  			BeforeEach(func() {
   170  				expectedError = errors.New("I am a CloudControllerClient Error")
   171  				fakeCloudControllerClient.GetOrganizationReturns(
   172  					Organization{},
   173  					ccv3.Warnings{"some-warning"},
   174  					expectedError)
   175  			})
   176  
   177  			It("returns the warnings and the error", func() {
   178  				_, warnings, err := actor.GetOrganizationByGUID("some-org-guid")
   179  				Expect(warnings).To(ConsistOf("some-warning"))
   180  				Expect(err).To(MatchError(expectedError))
   181  			})
   182  		})
   183  	})
   184  
   185  	Describe("GetOrganizationByName", func() {
   186  		When("the org exists", func() {
   187  			BeforeEach(func() {
   188  				fakeCloudControllerClient.GetOrganizationsReturns(
   189  					[]Organization{
   190  						{
   191  							Name: "some-org-name",
   192  							GUID: "some-org-guid",
   193  						},
   194  					},
   195  					ccv3.Warnings{"some-warning"},
   196  					nil,
   197  				)
   198  			})
   199  
   200  			It("returns the organization and warnings", func() {
   201  				org, warnings, err := actor.GetOrganizationByName("some-org-name")
   202  				Expect(err).ToNot(HaveOccurred())
   203  				Expect(org).To(Equal(Organization{
   204  					Name: "some-org-name",
   205  					GUID: "some-org-guid",
   206  				}))
   207  				Expect(warnings).To(ConsistOf("some-warning"))
   208  
   209  				Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1))
   210  				Expect(fakeCloudControllerClient.GetOrganizationsArgsForCall(0)).To(ConsistOf(
   211  					ccv3.Query{Key: ccv3.NameFilter, Values: []string{"some-org-name"}},
   212  				))
   213  			})
   214  		})
   215  
   216  		When("the cloud controller client returns an error", func() {
   217  			var expectedError error
   218  
   219  			BeforeEach(func() {
   220  				expectedError = errors.New("I am a CloudControllerClient Error")
   221  				fakeCloudControllerClient.GetOrganizationsReturns(
   222  					[]Organization{},
   223  					ccv3.Warnings{"some-warning"},
   224  					expectedError)
   225  			})
   226  
   227  			It("returns the warnings and the error", func() {
   228  				_, warnings, err := actor.GetOrganizationByName("some-org-name")
   229  				Expect(warnings).To(ConsistOf("some-warning"))
   230  				Expect(err).To(MatchError(expectedError))
   231  			})
   232  		})
   233  	})
   234  
   235  	Describe("GetDefaultDomain", func() {
   236  		var (
   237  			domain     resources.Domain
   238  			warnings   Warnings
   239  			executeErr error
   240  
   241  			orgGUID = "org-guid"
   242  		)
   243  
   244  		JustBeforeEach(func() {
   245  			domain, warnings, executeErr = actor.GetDefaultDomain(orgGUID)
   246  		})
   247  
   248  		When("the api call is successful", func() {
   249  			BeforeEach(func() {
   250  				fakeCloudControllerClient.GetDefaultDomainReturns(
   251  					resources.Domain{
   252  						Name: "some-domain-name",
   253  						GUID: "some-domain-guid",
   254  					},
   255  					ccv3.Warnings{"some-warning"},
   256  					nil,
   257  				)
   258  			})
   259  
   260  			It("returns the domain and warnings", func() {
   261  				Expect(executeErr).ToNot(HaveOccurred())
   262  				Expect(domain).To(Equal(resources.Domain{
   263  					Name: "some-domain-name",
   264  					GUID: "some-domain-guid",
   265  				}))
   266  				Expect(warnings).To(ConsistOf("some-warning"))
   267  
   268  				Expect(fakeCloudControllerClient.GetDefaultDomainCallCount()).To(Equal(1))
   269  				Expect(fakeCloudControllerClient.GetDefaultDomainArgsForCall(0)).To(Equal(orgGUID))
   270  			})
   271  		})
   272  
   273  		When("the cloud controller client returns an error", func() {
   274  			var expectedError error
   275  
   276  			BeforeEach(func() {
   277  				expectedError = errors.New("I am a CloudControllerClient Error")
   278  				fakeCloudControllerClient.GetDefaultDomainReturns(
   279  					resources.Domain{},
   280  					ccv3.Warnings{"some-warning"},
   281  					expectedError)
   282  			})
   283  
   284  			It("returns the warnings and the error", func() {
   285  				Expect(warnings).To(ConsistOf("some-warning"))
   286  				Expect(executeErr).To(MatchError(expectedError))
   287  			})
   288  		})
   289  	})
   290  
   291  	When("the org does not exist", func() {
   292  		BeforeEach(func() {
   293  			fakeCloudControllerClient.GetOrganizationsReturns(
   294  				[]Organization{},
   295  				ccv3.Warnings{"some-warning"},
   296  				nil,
   297  			)
   298  		})
   299  
   300  		It("returns an OrganizationNotFoundError and the warnings", func() {
   301  			_, warnings, err := actor.GetOrganizationByName("some-org-name")
   302  			Expect(warnings).To(ConsistOf("some-warning"))
   303  			Expect(err).To(MatchError(actionerror.OrganizationNotFoundError{Name: "some-org-name"}))
   304  		})
   305  	})
   306  
   307  	Describe("CreateOrganization", func() {
   308  		var (
   309  			org      Organization
   310  			warnings Warnings
   311  			err      error
   312  		)
   313  
   314  		JustBeforeEach(func() {
   315  			org, warnings, err = actor.CreateOrganization("some-org-name")
   316  		})
   317  
   318  		When("the org is created successfully", func() {
   319  			BeforeEach(func() {
   320  				fakeCloudControllerClient.CreateOrganizationReturns(
   321  					Organization{Name: "some-org-name", GUID: "some-org-guid"},
   322  					ccv3.Warnings{"warning-1", "warning-2"},
   323  					nil,
   324  				)
   325  			})
   326  
   327  			It("returns the org resource", func() {
   328  				Expect(err).NotTo(HaveOccurred())
   329  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   330  				Expect(org).To(Equal(Organization{Name: "some-org-name", GUID: "some-org-guid"}))
   331  			})
   332  		})
   333  
   334  		When("the request fails", func() {
   335  			BeforeEach(func() {
   336  				fakeCloudControllerClient.CreateOrganizationReturns(
   337  					Organization{},
   338  					ccv3.Warnings{"warning-1", "warning-2"},
   339  					errors.New("create-org-failed"),
   340  				)
   341  			})
   342  
   343  			It("returns warnings and the client error", func() {
   344  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   345  				Expect(err).To(MatchError("create-org-failed"))
   346  			})
   347  		})
   348  	})
   349  
   350  	Describe("DeleteOrganization", func() {
   351  		var (
   352  			warnings Warnings
   353  			err      error
   354  		)
   355  
   356  		JustBeforeEach(func() {
   357  			warnings, err = actor.DeleteOrganization("some-org")
   358  		})
   359  
   360  		When("the org is not found", func() {
   361  			BeforeEach(func() {
   362  				fakeCloudControllerClient.GetOrganizationsReturns(
   363  					[]Organization{},
   364  					ccv3.Warnings{
   365  						"warning-1",
   366  						"warning-2",
   367  					},
   368  					nil,
   369  				)
   370  			})
   371  
   372  			It("returns an OrganizationNotFoundError", func() {
   373  				Expect(err).To(MatchError(actionerror.OrganizationNotFoundError{Name: "some-org"}))
   374  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   375  			})
   376  		})
   377  
   378  		When("the org is found", func() {
   379  			BeforeEach(func() {
   380  				fakeCloudControllerClient.GetOrganizationsReturns(
   381  					[]Organization{{Name: "some-org", GUID: "some-org-guid"}},
   382  					ccv3.Warnings{"warning-1", "warning-2"},
   383  					nil,
   384  				)
   385  			})
   386  
   387  			When("the delete returns an error", func() {
   388  				var expectedErr error
   389  
   390  				BeforeEach(func() {
   391  					expectedErr = errors.New("some delete space error")
   392  					fakeCloudControllerClient.DeleteOrganizationReturns(
   393  						ccv3.JobURL(""),
   394  						ccv3.Warnings{"warning-5", "warning-6"},
   395  						expectedErr,
   396  					)
   397  				})
   398  
   399  				It("returns the error", func() {
   400  					Expect(err).To(Equal(expectedErr))
   401  					Expect(warnings).To(ConsistOf("warning-1", "warning-2", "warning-5", "warning-6"))
   402  				})
   403  			})
   404  
   405  			When("the delete returns a job", func() {
   406  				BeforeEach(func() {
   407  					fakeCloudControllerClient.DeleteOrganizationReturns(
   408  						ccv3.JobURL("some-url"),
   409  						ccv3.Warnings{"warning-5", "warning-6"},
   410  						nil,
   411  					)
   412  				})
   413  
   414  				When("polling errors", func() {
   415  					var expectedErr error
   416  
   417  					BeforeEach(func() {
   418  						expectedErr = errors.New("Never expected, by anyone")
   419  						fakeCloudControllerClient.PollJobReturns(ccv3.Warnings{"warning-7", "warning-8"}, expectedErr)
   420  					})
   421  
   422  					It("returns the error", func() {
   423  						Expect(err).To(Equal(expectedErr))
   424  						Expect(warnings).To(ConsistOf("warning-1", "warning-2", "warning-5", "warning-6", "warning-7", "warning-8"))
   425  					})
   426  				})
   427  
   428  				When("the job is successful", func() {
   429  					BeforeEach(func() {
   430  						fakeCloudControllerClient.PollJobReturns(ccv3.Warnings{"warning-7", "warning-8"}, nil)
   431  					})
   432  
   433  					It("returns warnings and no error", func() {
   434  						Expect(err).ToNot(HaveOccurred())
   435  						Expect(warnings).To(ConsistOf("warning-1", "warning-2", "warning-5", "warning-6", "warning-7", "warning-8"))
   436  
   437  						Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1))
   438  						Expect(fakeCloudControllerClient.GetOrganizationsArgsForCall(0)).To(Equal([]ccv3.Query{{
   439  							Key:    ccv3.NameFilter,
   440  							Values: []string{"some-org"},
   441  						}}))
   442  
   443  						Expect(fakeCloudControllerClient.DeleteOrganizationCallCount()).To(Equal(1))
   444  						Expect(fakeCloudControllerClient.DeleteOrganizationArgsForCall(0)).To(Equal("some-org-guid"))
   445  
   446  						Expect(fakeCloudControllerClient.PollJobCallCount()).To(Equal(1))
   447  						Expect(fakeCloudControllerClient.PollJobArgsForCall(0)).To(Equal(ccv3.JobURL("some-url")))
   448  					})
   449  				})
   450  			})
   451  		})
   452  	})
   453  
   454  	Describe("RenameOrganization", func() {
   455  		var (
   456  			oldOrgName = "old-and-stale-org-name"
   457  			newOrgName = "fresh-and-new-org-name"
   458  
   459  			org        Organization
   460  			warnings   Warnings
   461  			executeErr error
   462  		)
   463  
   464  		JustBeforeEach(func() {
   465  			org, warnings, executeErr = actor.RenameOrganization(
   466  				oldOrgName,
   467  				newOrgName,
   468  			)
   469  		})
   470  
   471  		It("delegate to the actor to get the org", func() {
   472  			// assert on the underlying client call because we dont have a fake actor
   473  			Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1))
   474  		})
   475  
   476  		When("getting the org fails", func() {
   477  			BeforeEach(func() {
   478  				fakeCloudControllerClient.GetOrganizationsReturns(
   479  					nil,
   480  					ccv3.Warnings{"get-org-warning"},
   481  					errors.New("get-org-error"),
   482  				)
   483  			})
   484  
   485  			It("returns the error and warnings", func() {
   486  				Expect(executeErr).To(MatchError("get-org-error"))
   487  				Expect(warnings).To(ConsistOf("get-org-warning"))
   488  			})
   489  		})
   490  
   491  		When("getting the org succeeds", func() {
   492  			BeforeEach(func() {
   493  				fakeCloudControllerClient.GetOrganizationsReturns(
   494  					[]Organization{{Name: oldOrgName, GUID: "org-guid"}},
   495  					ccv3.Warnings{"get-org-warning"},
   496  					nil,
   497  				)
   498  			})
   499  
   500  			It("delegates to the client to update the org", func() {
   501  				Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1))
   502  				Expect(fakeCloudControllerClient.UpdateOrganizationCallCount()).To(Equal(1))
   503  				Expect(fakeCloudControllerClient.UpdateOrganizationArgsForCall(0)).To(Equal(Organization{
   504  					GUID: "org-guid",
   505  					Name: newOrgName,
   506  				}))
   507  			})
   508  
   509  			When("updating the org fails", func() {
   510  				BeforeEach(func() {
   511  					fakeCloudControllerClient.UpdateOrganizationReturns(
   512  						Organization{},
   513  						ccv3.Warnings{"update-org-warning"},
   514  						errors.New("update-org-error"),
   515  					)
   516  				})
   517  
   518  				It("returns an error and all warnings", func() {
   519  					Expect(executeErr).To(MatchError("update-org-error"))
   520  					Expect(warnings).To(ConsistOf("get-org-warning", "update-org-warning"))
   521  				})
   522  
   523  			})
   524  
   525  			When("updating the org succeeds", func() {
   526  				BeforeEach(func() {
   527  					fakeCloudControllerClient.UpdateOrganizationReturns(
   528  						Organization{Name: newOrgName, GUID: "org-guid"},
   529  						ccv3.Warnings{"update-org-warning"},
   530  						nil,
   531  					)
   532  				})
   533  
   534  				It("returns warnings and no error", func() {
   535  					Expect(executeErr).ToNot(HaveOccurred())
   536  					Expect(warnings).To(ConsistOf("get-org-warning", "update-org-warning"))
   537  					Expect(org).To(Equal(Organization{Name: newOrgName, GUID: "org-guid"}))
   538  				})
   539  			})
   540  		})
   541  	})
   542  })