github.com/cloudfoundry/cli@v7.1.0+incompatible/actor/v3action/organization_test.go.txt (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  		When("the orgs exists", func() {
   100  			var (
   101  				executeErr error
   102  				warnings   Warnings
   103  				orgs       []Organization
   104  			)
   105  			BeforeEach(func() {
   106  				fakeCloudControllerClient.GetOrganizationsReturns(
   107  					[]ccv3.Organization{
   108  						{
   109  							Name: "some-org-name",
   110  							GUID: "some-org-guid",
   111  						},
   112  						{
   113  							Name: "another-org-name",
   114  							GUID: "another-org-guid",
   115  						},
   116  					},
   117  					ccv3.Warnings{"some-warning"},
   118  					nil,
   119  				)
   120  				fakeCloudControllerClient.CloudControllerAPIVersionReturns("3.65.0")
   121  			})
   122  
   123  			JustBeforeEach(func() {
   124  				orgs, warnings, executeErr = actor.GetOrganizationsByGUIDs("some-org-guid", "another-org-guid")
   125  			})
   126  
   127  			It("returns the organizations and warnings", func() {
   128  				Expect(executeErr).ToNot(HaveOccurred())
   129  				Expect(orgs).To(ConsistOf(
   130  					Organization{
   131  						Name: "some-org-name",
   132  						GUID: "some-org-guid",
   133  					},
   134  					Organization{
   135  						Name: "another-org-name",
   136  						GUID: "another-org-guid",
   137  					},
   138  				))
   139  				Expect(warnings).To(ConsistOf("some-warning"))
   140  
   141  				Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1))
   142  				Expect(fakeCloudControllerClient.GetOrganizationsArgsForCall(0)).To(ConsistOf(
   143  					ccv3.Query{Key: ccv3.GUIDFilter, Values: []string{"some-org-guid", "another-org-guid"}},
   144  				))
   145  			})
   146  
   147  			When("the cloud controller client returns an error", func() {
   148  				var expectedError error
   149  
   150  				BeforeEach(func() {
   151  					expectedError = errors.New("I am a CloudControllerClient Error")
   152  					fakeCloudControllerClient.GetOrganizationsReturns(
   153  						[]ccv3.Organization{},
   154  						ccv3.Warnings{"some-warning"},
   155  						expectedError)
   156  				})
   157  
   158  				It("returns the warnings and the error", func() {
   159  					Expect(warnings).To(ConsistOf("some-warning"))
   160  					Expect(executeErr).To(MatchError(expectedError))
   161  				})
   162  			})
   163  		})
   164  	})
   165  
   166  	Describe("GetOrganizations", func() {
   167  		var (
   168  			executeErr error
   169  			warnings   Warnings
   170  			orgs       []Organization
   171  		)
   172  
   173  		JustBeforeEach(func() {
   174  			orgs, warnings, executeErr = actor.GetOrganizations()
   175  		})
   176  
   177  		It("fetches all the organizations", func() {
   178  			Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1))
   179  		})
   180  
   181  		When("no error occurs", func() {
   182  			BeforeEach(func() {
   183  				fakeCloudControllerClient.GetOrganizationsReturns(
   184  					[]ccv3.Organization{
   185  						ccv3.Organization{
   186  							Name: "some-org-1",
   187  							GUID: "some-org-guid-1",
   188  						},
   189  						ccv3.Organization{
   190  							Name: "some-org-2",
   191  							GUID: "some-org-guid-2",
   192  						},
   193  					},
   194  					ccv3.Warnings{
   195  						"some-warning-1",
   196  						"some-warning-2",
   197  					},
   198  					nil,
   199  				)
   200  			})
   201  
   202  			It("returns the organizations", func() {
   203  				Expect(orgs).To(Equal(
   204  					[]Organization{
   205  						Organization{
   206  							Name: "some-org-1",
   207  							GUID: "some-org-guid-1",
   208  						},
   209  						Organization{
   210  							Name: "some-org-2",
   211  							GUID: "some-org-guid-2",
   212  						},
   213  					},
   214  				))
   215  			})
   216  
   217  			It("returns all the warnings", func() {
   218  				Expect(warnings).To(Equal(Warnings{"some-warning-1", "some-warning-2"}))
   219  			})
   220  		})
   221  
   222  		When("an error occurs", func() {
   223  			BeforeEach(func() {
   224  				fakeCloudControllerClient.GetOrganizationsReturns(
   225  					[]ccv3.Organization{},
   226  					ccv3.Warnings{
   227  						"some-warning-1",
   228  						"some-warning-2",
   229  					},
   230  					errors.New("get-organization-error"),
   231  				)
   232  			})
   233  
   234  			It("returns the error", func() {
   235  				Expect(executeErr).To(MatchError("get-organization-error"))
   236  			})
   237  
   238  			It("returns the warnings", func() {
   239  				Expect(warnings).To(Equal(Warnings{"some-warning-1", "some-warning-2"}))
   240  			})
   241  		})
   242  	})
   243  
   244  })