github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/actor/v2action/organization_test.go (about)

     1  package v2action_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	. "code.cloudfoundry.org/cli/actor/v2action"
     7  	"code.cloudfoundry.org/cli/actor/v2action/v2actionfakes"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  )
    13  
    14  var _ = Describe("Org Actions", func() {
    15  	var (
    16  		actor                     *Actor
    17  		fakeCloudControllerClient *v2actionfakes.FakeCloudControllerClient
    18  	)
    19  
    20  	BeforeEach(func() {
    21  		fakeCloudControllerClient = new(v2actionfakes.FakeCloudControllerClient)
    22  		actor = NewActor(fakeCloudControllerClient, nil, nil)
    23  	})
    24  
    25  	Describe("GetOrganization", func() {
    26  		var (
    27  			org      Organization
    28  			warnings Warnings
    29  			err      error
    30  		)
    31  
    32  		JustBeforeEach(func() {
    33  			org, warnings, err = actor.GetOrganization("some-org-guid")
    34  		})
    35  
    36  		Context("when the org exists", func() {
    37  			BeforeEach(func() {
    38  				fakeCloudControllerClient.GetOrganizationReturns(
    39  					ccv2.Organization{
    40  						GUID:                "some-org-guid",
    41  						Name:                "some-org",
    42  						QuotaDefinitionGUID: "some-quota-definition-guid",
    43  					},
    44  					ccv2.Warnings{"warning-1", "warning-2"},
    45  					nil)
    46  			})
    47  
    48  			It("returns the org and all warnings", func() {
    49  				Expect(err).ToNot(HaveOccurred())
    50  
    51  				Expect(org.GUID).To(Equal("some-org-guid"))
    52  				Expect(org.Name).To(Equal("some-org"))
    53  				Expect(org.QuotaDefinitionGUID).To(Equal("some-quota-definition-guid"))
    54  
    55  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
    56  
    57  				Expect(fakeCloudControllerClient.GetOrganizationCallCount()).To(Equal(1))
    58  				guid := fakeCloudControllerClient.GetOrganizationArgsForCall(0)
    59  				Expect(guid).To(Equal("some-org-guid"))
    60  			})
    61  		})
    62  
    63  		Context("when the org does not exist", func() {
    64  			BeforeEach(func() {
    65  				fakeCloudControllerClient.GetOrganizationReturns(
    66  					ccv2.Organization{},
    67  					ccv2.Warnings{"warning-1", "warning-2"},
    68  					ccerror.ResourceNotFoundError{},
    69  				)
    70  			})
    71  
    72  			It("returns warnings and OrganizationNotFoundError", func() {
    73  				Expect(err).To(MatchError(OrganizationNotFoundError{GUID: "some-org-guid"}))
    74  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
    75  			})
    76  		})
    77  
    78  		Context("when client returns an error", func() {
    79  			var expectedErr error
    80  
    81  			BeforeEach(func() {
    82  				expectedErr = errors.New("some get org error")
    83  				fakeCloudControllerClient.GetOrganizationReturns(
    84  					ccv2.Organization{},
    85  					ccv2.Warnings{"warning-1", "warning-2"},
    86  					expectedErr,
    87  				)
    88  			})
    89  
    90  			It("returns warnings and the error", func() {
    91  				Expect(err).To(MatchError(expectedErr))
    92  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
    93  			})
    94  		})
    95  	})
    96  
    97  	Describe("GetOrganizationByName", func() {
    98  		var (
    99  			org      Organization
   100  			warnings Warnings
   101  			err      error
   102  		)
   103  
   104  		JustBeforeEach(func() {
   105  			org, warnings, err = actor.GetOrganizationByName("some-org")
   106  		})
   107  
   108  		Context("when the org exists", func() {
   109  			BeforeEach(func() {
   110  				fakeCloudControllerClient.GetOrganizationsReturns(
   111  					[]ccv2.Organization{
   112  						{GUID: "some-org-guid"},
   113  					},
   114  					ccv2.Warnings{"warning-1", "warning-2"},
   115  					nil)
   116  			})
   117  
   118  			It("returns the org and all warnings", func() {
   119  				Expect(org.GUID).To(Equal("some-org-guid"))
   120  				Expect(err).ToNot(HaveOccurred())
   121  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   122  
   123  				Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1))
   124  				query := fakeCloudControllerClient.GetOrganizationsArgsForCall(0)
   125  				Expect(query).To(Equal(
   126  					[]ccv2.Query{{
   127  						Filter:   ccv2.NameFilter,
   128  						Operator: ccv2.EqualOperator,
   129  						Values:   []string{"some-org"},
   130  					}}))
   131  			})
   132  		})
   133  
   134  		Context("when the org does not exist", func() {
   135  			BeforeEach(func() {
   136  				fakeCloudControllerClient.GetOrganizationsReturns(
   137  					[]ccv2.Organization{},
   138  					nil,
   139  					nil,
   140  				)
   141  			})
   142  
   143  			It("returns OrganizationNotFoundError", func() {
   144  				Expect(err).To(MatchError(OrganizationNotFoundError{Name: "some-org"}))
   145  			})
   146  		})
   147  
   148  		Context("when multiple orgs exist with the same name", func() {
   149  			BeforeEach(func() {
   150  				fakeCloudControllerClient.GetOrganizationsReturns(
   151  					[]ccv2.Organization{
   152  						{GUID: "org-1-guid"},
   153  						{GUID: "org-2-guid"},
   154  					},
   155  					nil,
   156  					nil,
   157  				)
   158  			})
   159  
   160  			It("returns MultipleOrganizationsFoundError", func() {
   161  				Expect(err).To(MatchError("Organization name 'some-org' matches multiple GUIDs: org-1-guid, org-2-guid"))
   162  			})
   163  		})
   164  
   165  		Context("when an error is encountered", func() {
   166  			var returnedErr error
   167  
   168  			BeforeEach(func() {
   169  				returnedErr = errors.New("get-orgs-error")
   170  				fakeCloudControllerClient.GetOrganizationsReturns(
   171  					[]ccv2.Organization{},
   172  					ccv2.Warnings{
   173  						"warning-1",
   174  						"warning-2",
   175  					},
   176  					returnedErr,
   177  				)
   178  			})
   179  
   180  			It("returns the error and all warnings", func() {
   181  				Expect(err).To(MatchError(returnedErr))
   182  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   183  			})
   184  		})
   185  	})
   186  
   187  	Describe("DeleteOrganization", func() {
   188  		var (
   189  			warnings     Warnings
   190  			deleteOrgErr error
   191  			job          ccv2.Job
   192  		)
   193  
   194  		JustBeforeEach(func() {
   195  			warnings, deleteOrgErr = actor.DeleteOrganization("some-org")
   196  		})
   197  
   198  		Context("the organization is deleted successfully", func() {
   199  			BeforeEach(func() {
   200  				fakeCloudControllerClient.GetOrganizationsReturns([]ccv2.Organization{
   201  					{GUID: "some-org-guid"},
   202  				}, ccv2.Warnings{"get-org-warning"}, nil)
   203  
   204  				job = ccv2.Job{
   205  					GUID:   "some-job-guid",
   206  					Status: ccv2.JobStatusFinished,
   207  				}
   208  
   209  				fakeCloudControllerClient.DeleteOrganizationReturns(
   210  					job, ccv2.Warnings{"delete-org-warning"}, nil)
   211  
   212  				fakeCloudControllerClient.PollJobReturns(ccv2.Warnings{"polling-warnings"}, nil)
   213  			})
   214  
   215  			It("returns warnings and deletes the org", func() {
   216  				Expect(warnings).To(ConsistOf("get-org-warning", "delete-org-warning", "polling-warnings"))
   217  				Expect(deleteOrgErr).ToNot(HaveOccurred())
   218  
   219  				Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1))
   220  				query := fakeCloudControllerClient.GetOrganizationsArgsForCall(0)
   221  				Expect(query).To(Equal(
   222  					[]ccv2.Query{{
   223  						Filter:   ccv2.NameFilter,
   224  						Operator: ccv2.EqualOperator,
   225  						Values:   []string{"some-org"},
   226  					}}))
   227  
   228  				Expect(fakeCloudControllerClient.DeleteOrganizationCallCount()).To(Equal(1))
   229  				orgGuid := fakeCloudControllerClient.DeleteOrganizationArgsForCall(0)
   230  				Expect(orgGuid).To(Equal("some-org-guid"))
   231  
   232  				Expect(fakeCloudControllerClient.PollJobCallCount()).To(Equal(1))
   233  				job := fakeCloudControllerClient.PollJobArgsForCall(0)
   234  				Expect(job.GUID).To(Equal("some-job-guid"))
   235  			})
   236  		})
   237  
   238  		Context("when getting the org returns an error", func() {
   239  			BeforeEach(func() {
   240  				fakeCloudControllerClient.GetOrganizationsReturns(
   241  					[]ccv2.Organization{},
   242  					ccv2.Warnings{
   243  						"get-org-warning",
   244  					},
   245  					nil,
   246  				)
   247  			})
   248  
   249  			It("returns an error and all warnings", func() {
   250  				Expect(warnings).To(ConsistOf("get-org-warning"))
   251  				Expect(deleteOrgErr).To(MatchError(OrganizationNotFoundError{
   252  					Name: "some-org",
   253  				}))
   254  			})
   255  		})
   256  
   257  		Context("when the delete returns an error", func() {
   258  			var returnedErr error
   259  
   260  			BeforeEach(func() {
   261  				returnedErr = errors.New("delete-org-error")
   262  
   263  				fakeCloudControllerClient.GetOrganizationsReturns(
   264  					[]ccv2.Organization{{GUID: "org-1-guid"}},
   265  					ccv2.Warnings{
   266  						"get-org-warning",
   267  					},
   268  					nil,
   269  				)
   270  
   271  				fakeCloudControllerClient.DeleteOrganizationReturns(
   272  					ccv2.Job{},
   273  					ccv2.Warnings{"delete-org-warning"},
   274  					returnedErr)
   275  			})
   276  
   277  			It("returns the error and all warnings", func() {
   278  				Expect(deleteOrgErr).To(MatchError(returnedErr))
   279  				Expect(warnings).To(ConsistOf("get-org-warning", "delete-org-warning"))
   280  			})
   281  		})
   282  
   283  		Context("when the job polling has an error", func() {
   284  			var expectedErr error
   285  			BeforeEach(func() {
   286  				fakeCloudControllerClient.GetOrganizationsReturns([]ccv2.Organization{
   287  					{GUID: "some-org-guid"},
   288  				}, ccv2.Warnings{"get-org-warning"}, nil)
   289  
   290  				fakeCloudControllerClient.DeleteOrganizationReturns(
   291  					ccv2.Job{}, ccv2.Warnings{"delete-org-warning"}, nil)
   292  
   293  				expectedErr = errors.New("Never expected, by anyone")
   294  				fakeCloudControllerClient.PollJobReturns(ccv2.Warnings{"polling-warnings"}, expectedErr)
   295  			})
   296  
   297  			It("returns the error from job polling", func() {
   298  				Expect(warnings).To(ConsistOf("get-org-warning", "delete-org-warning", "polling-warnings"))
   299  				Expect(deleteOrgErr).To(MatchError(expectedErr))
   300  			})
   301  		})
   302  	})
   303  
   304  	Describe("GetOrganizations", func() {
   305  		var (
   306  			orgs     []Organization
   307  			warnings Warnings
   308  			err      error
   309  		)
   310  
   311  		JustBeforeEach(func() {
   312  			orgs, warnings, err = actor.GetOrganizations()
   313  		})
   314  
   315  		Context("when there are multiple organizations", func() {
   316  			BeforeEach(func() {
   317  				fakeCloudControllerClient.GetOrganizationsReturns(
   318  					[]ccv2.Organization{
   319  						{
   320  							Name: "some-org-1",
   321  						},
   322  						{
   323  							Name: "some-org-2",
   324  						},
   325  					},
   326  					ccv2.Warnings{"warning-1", "warning-2"},
   327  					nil)
   328  			})
   329  
   330  			It("returns the org and all warnings", func() {
   331  				Expect(err).ToNot(HaveOccurred())
   332  
   333  				Expect(orgs).To(HaveLen(2))
   334  				Expect(orgs[0].Name).To(Equal("some-org-1"))
   335  				Expect(orgs[1].Name).To(Equal("some-org-2"))
   336  
   337  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   338  
   339  				Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1))
   340  				queriesArg := fakeCloudControllerClient.GetOrganizationsArgsForCall(0)
   341  				Expect(queriesArg).To(BeNil())
   342  			})
   343  		})
   344  
   345  		Context("when there are no orgs", func() {
   346  			BeforeEach(func() {
   347  				fakeCloudControllerClient.GetOrganizationsReturns(
   348  					[]ccv2.Organization{},
   349  					ccv2.Warnings{"warning-1", "warning-2"},
   350  					nil,
   351  				)
   352  			})
   353  
   354  			It("returns warnings and an empty list of orgs", func() {
   355  				Expect(err).ToNot(HaveOccurred())
   356  
   357  				Expect(orgs).To(HaveLen(0))
   358  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   359  			})
   360  		})
   361  
   362  		Context("when client returns an error", func() {
   363  			var expectedErr error
   364  
   365  			BeforeEach(func() {
   366  				expectedErr = errors.New("some get org error")
   367  				fakeCloudControllerClient.GetOrganizationsReturns(
   368  					[]ccv2.Organization{},
   369  					ccv2.Warnings{"warning-1", "warning-2"},
   370  					expectedErr,
   371  				)
   372  			})
   373  
   374  			It("returns warnings and the error", func() {
   375  				Expect(err).To(MatchError(expectedErr))
   376  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   377  			})
   378  		})
   379  	})
   380  })