github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+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/ccv2"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  )
    12  
    13  var _ = Describe("Org Actions", func() {
    14  	var (
    15  		actor                     Actor
    16  		fakeCloudControllerClient *v2actionfakes.FakeCloudControllerClient
    17  	)
    18  
    19  	BeforeEach(func() {
    20  		fakeCloudControllerClient = new(v2actionfakes.FakeCloudControllerClient)
    21  		actor = NewActor(fakeCloudControllerClient, nil)
    22  	})
    23  
    24  	Describe("GetOrganizationByName", func() {
    25  		var (
    26  			org      Organization
    27  			warnings Warnings
    28  			err      error
    29  		)
    30  
    31  		JustBeforeEach(func() {
    32  			org, warnings, err = actor.GetOrganizationByName("some-org")
    33  		})
    34  
    35  		Context("when the org exists", func() {
    36  			BeforeEach(func() {
    37  				fakeCloudControllerClient.GetOrganizationsReturns(
    38  					[]ccv2.Organization{
    39  						{GUID: "some-org-guid"},
    40  					},
    41  					ccv2.Warnings{"warning-1", "warning-2"},
    42  					nil)
    43  			})
    44  
    45  			It("returns the org and all warnings", func() {
    46  				Expect(org.GUID).To(Equal("some-org-guid"))
    47  				Expect(err).ToNot(HaveOccurred())
    48  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
    49  
    50  				Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1))
    51  				query := fakeCloudControllerClient.GetOrganizationsArgsForCall(0)
    52  				Expect(query).To(Equal(
    53  					[]ccv2.Query{{
    54  						Filter:   ccv2.NameFilter,
    55  						Operator: ccv2.EqualOperator,
    56  						Value:    "some-org",
    57  					}}))
    58  			})
    59  		})
    60  
    61  		Context("when the org does not exist", func() {
    62  			BeforeEach(func() {
    63  				fakeCloudControllerClient.GetOrganizationsReturns(
    64  					[]ccv2.Organization{},
    65  					nil,
    66  					nil,
    67  				)
    68  			})
    69  
    70  			It("returns OrganizationNotFoundError", func() {
    71  				Expect(err).To(MatchError(OrganizationNotFoundError{Name: "some-org"}))
    72  			})
    73  		})
    74  
    75  		Context("when multiple orgs exist with the same name", func() {
    76  			BeforeEach(func() {
    77  				fakeCloudControllerClient.GetOrganizationsReturns(
    78  					[]ccv2.Organization{
    79  						{GUID: "org-1-guid"},
    80  						{GUID: "org-2-guid"},
    81  					},
    82  					nil,
    83  					nil,
    84  				)
    85  			})
    86  
    87  			It("returns MultipleOrganizationsFoundError", func() {
    88  				Expect(err).To(MatchError("Organization name 'some-org' matches multiple GUIDs: org-1-guid, org-2-guid"))
    89  			})
    90  		})
    91  
    92  		Context("when an error is encountered", func() {
    93  			var returnedErr error
    94  
    95  			BeforeEach(func() {
    96  				returnedErr = errors.New("get-orgs-error")
    97  				fakeCloudControllerClient.GetOrganizationsReturns(
    98  					[]ccv2.Organization{},
    99  					ccv2.Warnings{
   100  						"warning-1",
   101  						"warning-2",
   102  					},
   103  					returnedErr,
   104  				)
   105  			})
   106  
   107  			It("returns the error and all warnings", func() {
   108  				Expect(err).To(MatchError(returnedErr))
   109  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   110  			})
   111  		})
   112  	})
   113  
   114  	Describe("DeleteOrganization", func() {
   115  		var (
   116  			warnings     Warnings
   117  			deleteOrgErr error
   118  			job          ccv2.Job
   119  		)
   120  
   121  		JustBeforeEach(func() {
   122  			warnings, deleteOrgErr = actor.DeleteOrganization("some-org")
   123  		})
   124  
   125  		Context("the organization is deleted successfully", func() {
   126  			BeforeEach(func() {
   127  				fakeCloudControllerClient.GetOrganizationsReturns([]ccv2.Organization{
   128  					{GUID: "some-org-guid"},
   129  				}, ccv2.Warnings{"get-org-warning"}, nil)
   130  
   131  				job = ccv2.Job{
   132  					GUID:   "some-job-guid",
   133  					Status: ccv2.JobStatusFinished,
   134  				}
   135  
   136  				fakeCloudControllerClient.DeleteOrganizationReturns(
   137  					job, ccv2.Warnings{"delete-org-warning"}, nil)
   138  
   139  				fakeCloudControllerClient.PollJobReturns(ccv2.Warnings{"polling-warnings"}, nil)
   140  			})
   141  
   142  			It("returns warnings and deletes the org", func() {
   143  				Expect(warnings).To(ConsistOf("get-org-warning", "delete-org-warning", "polling-warnings"))
   144  				Expect(deleteOrgErr).ToNot(HaveOccurred())
   145  
   146  				Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1))
   147  				query := fakeCloudControllerClient.GetOrganizationsArgsForCall(0)
   148  				Expect(query).To(Equal(
   149  					[]ccv2.Query{{
   150  						Filter:   ccv2.NameFilter,
   151  						Operator: ccv2.EqualOperator,
   152  						Value:    "some-org",
   153  					}}))
   154  
   155  				Expect(fakeCloudControllerClient.DeleteOrganizationCallCount()).To(Equal(1))
   156  				orgGuid := fakeCloudControllerClient.DeleteOrganizationArgsForCall(0)
   157  				Expect(orgGuid).To(Equal("some-org-guid"))
   158  
   159  				Expect(fakeCloudControllerClient.PollJobCallCount()).To(Equal(1))
   160  				job := fakeCloudControllerClient.PollJobArgsForCall(0)
   161  				Expect(job.GUID).To(Equal("some-job-guid"))
   162  			})
   163  		})
   164  
   165  		Context("when getting the org returns an error", func() {
   166  			BeforeEach(func() {
   167  				fakeCloudControllerClient.GetOrganizationsReturns(
   168  					[]ccv2.Organization{},
   169  					ccv2.Warnings{
   170  						"get-org-warning",
   171  					},
   172  					nil,
   173  				)
   174  			})
   175  
   176  			It("returns an error and all warnings", func() {
   177  				Expect(warnings).To(ConsistOf("get-org-warning"))
   178  				Expect(deleteOrgErr).To(MatchError(OrganizationNotFoundError{
   179  					Name: "some-org",
   180  				}))
   181  			})
   182  		})
   183  
   184  		Context("when the delete returns an error", func() {
   185  			var returnedErr error
   186  
   187  			BeforeEach(func() {
   188  				returnedErr = errors.New("delete-org-error")
   189  
   190  				fakeCloudControllerClient.GetOrganizationsReturns(
   191  					[]ccv2.Organization{{GUID: "org-1-guid"}},
   192  					ccv2.Warnings{
   193  						"get-org-warning",
   194  					},
   195  					nil,
   196  				)
   197  
   198  				fakeCloudControllerClient.DeleteOrganizationReturns(
   199  					ccv2.Job{},
   200  					ccv2.Warnings{"delete-org-warning"},
   201  					returnedErr)
   202  			})
   203  
   204  			It("returns the error and all warnings", func() {
   205  				Expect(deleteOrgErr).To(MatchError(returnedErr))
   206  				Expect(warnings).To(ConsistOf("get-org-warning", "delete-org-warning"))
   207  			})
   208  		})
   209  
   210  		Context("when the job polling has an error", func() {
   211  			var expectedErr error
   212  			BeforeEach(func() {
   213  				fakeCloudControllerClient.GetOrganizationsReturns([]ccv2.Organization{
   214  					{GUID: "some-org-guid"},
   215  				}, ccv2.Warnings{"get-org-warning"}, nil)
   216  
   217  				fakeCloudControllerClient.DeleteOrganizationReturns(
   218  					ccv2.Job{}, ccv2.Warnings{"delete-org-warning"}, nil)
   219  
   220  				expectedErr = errors.New("Never expected, by anyone")
   221  				fakeCloudControllerClient.PollJobReturns(ccv2.Warnings{"polling-warnings"}, expectedErr)
   222  			})
   223  
   224  			It("returns the error from job polling", func() {
   225  				Expect(warnings).To(ConsistOf("get-org-warning", "delete-org-warning", "polling-warnings"))
   226  				Expect(deleteOrgErr).To(MatchError(expectedErr))
   227  			})
   228  		})
   229  	})
   230  })