github.com/Thanhphan1147/cloudfoundry-cli@v7.1.0+incompatible/actor/v2action/service_access_test.go (about)

     1  package v2action_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	. "github.com/onsi/ginkgo"
     7  	. "github.com/onsi/gomega"
     8  
     9  	"code.cloudfoundry.org/cli/actor/actionerror"
    10  	. "code.cloudfoundry.org/cli/actor/v2action"
    11  	"code.cloudfoundry.org/cli/actor/v2action/v2actionfakes"
    12  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
    13  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
    14  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
    15  )
    16  
    17  var _ = Describe("Service Access", func() {
    18  	var (
    19  		actor                     *Actor
    20  		fakeCloudControllerClient *v2actionfakes.FakeCloudControllerClient
    21  	)
    22  
    23  	BeforeEach(func() {
    24  		fakeCloudControllerClient = new(v2actionfakes.FakeCloudControllerClient)
    25  		actor = NewActor(fakeCloudControllerClient, nil, nil)
    26  
    27  		fakeCloudControllerClient.GetServiceBrokersReturns([]ccv2.ServiceBroker{{GUID: "broker-guid"}}, nil, nil)
    28  	})
    29  
    30  	Describe("EnablePlanForAllOrgs", func() {
    31  		var (
    32  			enablePlanErr      error
    33  			enablePlanWarnings Warnings
    34  		)
    35  
    36  		BeforeEach(func() {
    37  			fakeCloudControllerClient.GetServicesReturns(
    38  				[]ccv2.Service{
    39  					{Label: "service-1", GUID: "service-guid-1"},
    40  				},
    41  				nil, nil)
    42  
    43  			fakeCloudControllerClient.GetServicePlansReturns(
    44  				[]ccv2.ServicePlan{
    45  					{Name: "plan-1", GUID: "service-plan-guid-1"},
    46  					{Name: "plan-2", GUID: "service-plan-guid-2"},
    47  				},
    48  				nil, nil)
    49  		})
    50  
    51  		JustBeforeEach(func() {
    52  			enablePlanWarnings, enablePlanErr = actor.EnablePlanForAllOrgs("service-1", "plan-2", "some-broker")
    53  		})
    54  
    55  		It("updates the service plan visibility", func() {
    56  			Expect(enablePlanErr).NotTo(HaveOccurred())
    57  			Expect(fakeCloudControllerClient.GetServicesCallCount()).To(Equal(1))
    58  			Expect(fakeCloudControllerClient.GetServicePlansCallCount()).To(Equal(1))
    59  			Expect(fakeCloudControllerClient.UpdateServicePlanCallCount()).To(Equal(1))
    60  
    61  			planGuid, visible := fakeCloudControllerClient.UpdateServicePlanArgsForCall(0)
    62  			Expect(planGuid).To(Equal("service-plan-guid-2"))
    63  			Expect(visible).To(BeTrue())
    64  		})
    65  
    66  		When("the plan is already visible in some orgs", func() {
    67  			BeforeEach(func() {
    68  				fakeCloudControllerClient.GetServicePlanVisibilitiesReturns(
    69  					[]ccv2.ServicePlanVisibility{
    70  						{GUID: "service-visibility-guid-1"},
    71  						{GUID: "service-visibility-guid-2"},
    72  					},
    73  					nil, nil)
    74  			})
    75  
    76  			It("disables the plan in both orgs", func() {
    77  				Expect(fakeCloudControllerClient.GetServicePlanVisibilitiesCallCount()).To(Equal(1))
    78  
    79  				filters := fakeCloudControllerClient.GetServicePlanVisibilitiesArgsForCall(0)
    80  				Expect(filters[0].Type).To(Equal(constant.ServicePlanGUIDFilter))
    81  				Expect(filters[0].Operator).To(Equal(constant.EqualOperator))
    82  				Expect(filters[0].Values).To(Equal([]string{"service-plan-guid-2"}))
    83  
    84  				Expect(fakeCloudControllerClient.DeleteServicePlanVisibilityCallCount()).To(Equal(2))
    85  			})
    86  
    87  			It("and updates the service plan visibility for all orgs", func() {
    88  				Expect(enablePlanErr).NotTo(HaveOccurred())
    89  				Expect(fakeCloudControllerClient.GetServicesCallCount()).To(Equal(1))
    90  				Expect(fakeCloudControllerClient.GetServicePlansCallCount()).To(Equal(1))
    91  				Expect(fakeCloudControllerClient.UpdateServicePlanCallCount()).To(Equal(1))
    92  
    93  				planGuid, visible := fakeCloudControllerClient.UpdateServicePlanArgsForCall(0)
    94  				Expect(planGuid).To(Equal("service-plan-guid-2"))
    95  				Expect(visible).To(BeTrue())
    96  			})
    97  
    98  			When("deleting service plan visibilities fails", func() {
    99  				BeforeEach(func() {
   100  					fakeCloudControllerClient.DeleteServicePlanVisibilityReturns(
   101  						[]string{"visibility-warning"}, errors.New("delete-visibility-error"))
   102  				})
   103  
   104  				It("propagates the error", func() {
   105  					Expect(enablePlanErr).To(MatchError(errors.New("delete-visibility-error")))
   106  				})
   107  
   108  				It("returns the warnings", func() {
   109  					Expect(enablePlanWarnings).To(Equal(Warnings{"visibility-warning"}))
   110  				})
   111  			})
   112  		})
   113  
   114  		When("getting service plan visibilities fails", func() {
   115  			BeforeEach(func() {
   116  				fakeCloudControllerClient.GetServicePlanVisibilitiesReturns(
   117  					[]ccv2.ServicePlanVisibility{},
   118  					[]string{"a-warning", "another-warning"},
   119  					errors.New("oh no"))
   120  			})
   121  
   122  			It("returns the error", func() {
   123  				Expect(enablePlanErr).To(MatchError(errors.New("oh no")))
   124  			})
   125  
   126  			It("returns the warnings", func() {
   127  				Expect(enablePlanWarnings).To(Equal(Warnings{"a-warning", "another-warning"}))
   128  			})
   129  		})
   130  
   131  		When("getting service plans fails", func() {
   132  			BeforeEach(func() {
   133  				fakeCloudControllerClient.GetServicePlansReturns(nil,
   134  					[]string{"a-warning", "another-warning"},
   135  					errors.New("it didn't work!"))
   136  			})
   137  
   138  			It("returns the error", func() {
   139  				Expect(enablePlanErr).To(MatchError(errors.New("it didn't work!")))
   140  			})
   141  
   142  			It("returns the warnings", func() {
   143  				Expect(enablePlanWarnings).To(Equal(Warnings{"a-warning", "another-warning"}))
   144  			})
   145  		})
   146  
   147  		When("getting services fails", func() {
   148  			BeforeEach(func() {
   149  				fakeCloudControllerClient.GetServicesReturns(nil,
   150  					[]string{"a-warning", "another-warning"},
   151  					errors.New("it didn't work!"))
   152  			})
   153  
   154  			It("returns the error", func() {
   155  				Expect(enablePlanErr).To(MatchError(errors.New("it didn't work!")))
   156  			})
   157  
   158  			It("returns the warnings", func() {
   159  				Expect(enablePlanWarnings).To(Equal(Warnings{"a-warning", "another-warning"}))
   160  			})
   161  		})
   162  
   163  		When("there are no matching services", func() {
   164  			BeforeEach(func() {
   165  				fakeCloudControllerClient.GetServicesReturns([]ccv2.Service{}, []string{"warning-1", "warning-2"}, nil)
   166  			})
   167  
   168  			It("returns not found error", func() {
   169  				Expect(enablePlanErr).To(MatchError(actionerror.ServiceNotFoundError{Name: "service-1"}))
   170  			})
   171  
   172  			It("returns all warnings", func() {
   173  				Expect(enablePlanWarnings).To(ConsistOf([]string{"warning-1", "warning-2"}))
   174  			})
   175  		})
   176  
   177  		When("there are no matching plans", func() {
   178  			BeforeEach(func() {
   179  				fakeCloudControllerClient.GetServicePlansReturns([]ccv2.ServicePlan{}, []string{"warning-1", "warning-2"}, nil)
   180  			})
   181  
   182  			It("returns not found error", func() {
   183  				Expect(enablePlanErr).To(MatchError(actionerror.ServicePlanNotFoundError{PlanName: "plan-2", OfferingName: "service-1"}))
   184  			})
   185  
   186  			It("returns all warnings", func() {
   187  				Expect(enablePlanWarnings).To(ConsistOf([]string{"warning-1", "warning-2"}))
   188  			})
   189  		})
   190  
   191  		When("updating service plan fails", func() {
   192  			BeforeEach(func() {
   193  				fakeCloudControllerClient.UpdateServicePlanReturns(
   194  					[]string{"a-warning", "another-warning"},
   195  					errors.New("it didn't work!"))
   196  			})
   197  
   198  			It("returns the error", func() {
   199  				Expect(enablePlanErr).To(MatchError(errors.New("it didn't work!")))
   200  			})
   201  
   202  			It("returns the warnings", func() {
   203  				Expect(enablePlanWarnings).To(Equal(Warnings{"a-warning", "another-warning"}))
   204  			})
   205  		})
   206  
   207  		When("there are warnings", func() {
   208  			BeforeEach(func() {
   209  				fakeCloudControllerClient.GetServicesReturns(
   210  					[]ccv2.Service{
   211  						{Label: "service-1", GUID: "service-guid-1"},
   212  					},
   213  					[]string{"foo"},
   214  					nil)
   215  
   216  				fakeCloudControllerClient.GetServicePlansReturns(
   217  					[]ccv2.ServicePlan{
   218  						{Name: "plan-1", GUID: "service-plan-guid-1"},
   219  						{Name: "plan-2", GUID: "service-plan-guid-2"},
   220  					},
   221  					[]string{"bar"},
   222  					nil)
   223  
   224  				fakeCloudControllerClient.GetServicePlanVisibilitiesReturns(
   225  					[]ccv2.ServicePlanVisibility{
   226  						{GUID: "service-visibility-guid-1"},
   227  						{GUID: "service-visibility-guid-2"},
   228  					},
   229  					[]string{"baz"},
   230  					nil)
   231  
   232  				fakeCloudControllerClient.UpdateServicePlanReturns(
   233  					[]string{"qux"},
   234  					nil)
   235  			})
   236  
   237  			It("returns the warnings", func() {
   238  				Expect(enablePlanWarnings).To(ConsistOf([]string{"foo", "bar", "baz", "qux"}))
   239  			})
   240  		})
   241  	})
   242  
   243  	Describe("EnablePlanForOrg", func() {
   244  		var (
   245  			enablePlanWarnings Warnings
   246  			enablePlanErr      error
   247  		)
   248  
   249  		BeforeEach(func() {
   250  			fakeCloudControllerClient.GetServicesReturns(
   251  				[]ccv2.Service{
   252  					{Label: "service-1", GUID: "service-guid-1"},
   253  				},
   254  				nil, nil)
   255  
   256  			fakeCloudControllerClient.GetServicePlansReturns(
   257  				[]ccv2.ServicePlan{
   258  					{Name: "plan-1", GUID: "service-plan-guid-1"},
   259  					{Name: "plan-2", GUID: "service-plan-guid-2"},
   260  				},
   261  				nil, nil)
   262  		})
   263  
   264  		JustBeforeEach(func() {
   265  			enablePlanWarnings, enablePlanErr = actor.EnablePlanForOrg("service-1", "plan-2", "my-org", "broker")
   266  		})
   267  
   268  		When("the specified service does not exist", func() {
   269  			BeforeEach(func() {
   270  				fakeCloudControllerClient.GetServicesReturns(
   271  					[]ccv2.Service{},
   272  					nil, nil)
   273  			})
   274  
   275  			It("returns service not found error", func() {
   276  				Expect(fakeCloudControllerClient.GetServicesCallCount()).To(Equal(1))
   277  				Expect(enablePlanErr).To(MatchError(actionerror.ServiceNotFoundError{Name: "service-1"}))
   278  				filters := fakeCloudControllerClient.GetServicesArgsForCall(0)
   279  				Expect(len(filters)).To(Equal(2))
   280  				Expect(filters).To(ConsistOf(
   281  					ccv2.Filter{
   282  						Type:     constant.LabelFilter,
   283  						Operator: constant.EqualOperator,
   284  						Values:   []string{"service-1"},
   285  					},
   286  					ccv2.Filter{
   287  						Type:     constant.ServiceBrokerGUIDFilter,
   288  						Operator: constant.EqualOperator,
   289  						Values:   []string{"broker-guid"},
   290  					},
   291  				))
   292  			})
   293  		})
   294  
   295  		When("the specified org exists", func() {
   296  			BeforeEach(func() {
   297  				fakeCloudControllerClient.GetOrganizationsReturns(
   298  					[]ccv2.Organization{
   299  						{Name: "my-org", GUID: "org-guid-1"},
   300  					}, nil, nil)
   301  			})
   302  
   303  			It("enables the plan", func() {
   304  				Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1))
   305  
   306  				Expect(fakeCloudControllerClient.GetServicePlansCallCount()).To(Equal(1))
   307  				serviceFilter := fakeCloudControllerClient.GetServicePlansArgsForCall(0)
   308  				Expect(serviceFilter[0].Values).To(ContainElement("service-guid-1"))
   309  
   310  				Expect(fakeCloudControllerClient.CreateServicePlanVisibilityCallCount()).To(Equal(1))
   311  				planGUID, orgGUID := fakeCloudControllerClient.CreateServicePlanVisibilityArgsForCall(0)
   312  				Expect(planGUID).To(Equal("service-plan-guid-2"))
   313  				Expect(orgGUID).To(Equal("org-guid-1"))
   314  
   315  				Expect(enablePlanErr).NotTo(HaveOccurred())
   316  			})
   317  
   318  			When("the plan is already globally enabled", func() {
   319  				BeforeEach(func() {
   320  					fakeCloudControllerClient.GetServicePlansReturns(
   321  						[]ccv2.ServicePlan{
   322  							{Name: "plan-2", GUID: "service-plan-guid-2", Public: true},
   323  						},
   324  						[]string{"foo"},
   325  						nil)
   326  				})
   327  
   328  				It("should not create org visibility", func() {
   329  					Expect(enablePlanErr).ToNot(HaveOccurred())
   330  					Expect(enablePlanWarnings).To(ConsistOf([]string{"foo"}))
   331  
   332  					Expect(fakeCloudControllerClient.GetServicesCallCount()).To(Equal(1))
   333  					Expect(fakeCloudControllerClient.GetServicePlansCallCount()).To(Equal(1))
   334  
   335  					Expect(fakeCloudControllerClient.CreateServicePlanVisibilityCallCount()).To(Equal(0))
   336  				})
   337  			})
   338  
   339  			When("warnings are raised", func() {
   340  				BeforeEach(func() {
   341  					fakeCloudControllerClient.CreateServicePlanVisibilityReturns(ccv2.ServicePlanVisibility{}, []string{"foo", "bar"}, nil)
   342  				})
   343  
   344  				It("returns all warnings", func() {
   345  					Expect(enablePlanWarnings).To(ConsistOf([]string{"foo", "bar"}))
   346  				})
   347  			})
   348  
   349  			When("the specified plan does exist", func() {
   350  				BeforeEach(func() {
   351  					fakeCloudControllerClient.GetServicePlansReturns(
   352  						[]ccv2.ServicePlan{
   353  							{Name: "plan-4", GUID: "service-plan-guid-4"},
   354  							{Name: "plan-5", GUID: "service-plan-guid-5"},
   355  						},
   356  						nil, nil)
   357  				})
   358  
   359  				It("returns an error", func() {
   360  					Expect(enablePlanErr.Error()).To(Equal("Service plan 'plan-2' not found"))
   361  				})
   362  			})
   363  
   364  			When("getting services fails", func() {
   365  				BeforeEach(func() {
   366  					fakeCloudControllerClient.GetServicesReturns(nil, []string{"foo", "bar"}, errors.New("it broke"))
   367  				})
   368  
   369  				It("returns the error", func() {
   370  					Expect(enablePlanErr).To(MatchError(errors.New("it broke")))
   371  				})
   372  
   373  				It("returns all warnings", func() {
   374  					Expect(enablePlanWarnings).To(ConsistOf([]string{"foo", "bar"}))
   375  				})
   376  			})
   377  
   378  			When("getting service plans fails", func() {
   379  				BeforeEach(func() {
   380  					fakeCloudControllerClient.GetServicesReturns(
   381  						[]ccv2.Service{
   382  							{Label: "service-1", GUID: "service-guid-1"},
   383  						},
   384  						[]string{"foo"},
   385  						nil)
   386  
   387  					fakeCloudControllerClient.GetServicePlansReturns(nil, []string{"bar"}, errors.New("it broke"))
   388  				})
   389  
   390  				It("returns the error", func() {
   391  					Expect(enablePlanErr).To(MatchError(errors.New("it broke")))
   392  				})
   393  
   394  				It("returns all warnings", func() {
   395  					Expect(enablePlanWarnings).To(ConsistOf([]string{"foo", "bar"}))
   396  				})
   397  			})
   398  
   399  			When("create service plan visibility fails", func() {
   400  				BeforeEach(func() {
   401  					fakeCloudControllerClient.GetServicesReturns(
   402  						[]ccv2.Service{
   403  							{Label: "service-1", GUID: "service-guid-1"},
   404  						},
   405  						[]string{"foo"},
   406  						nil)
   407  
   408  					fakeCloudControllerClient.GetServicePlansReturns(
   409  						[]ccv2.ServicePlan{
   410  							{Name: "plan-1", GUID: "service-plan-guid-1"},
   411  							{Name: "plan-2", GUID: "service-plan-guid-2"},
   412  						},
   413  						[]string{"bar"},
   414  						nil)
   415  
   416  					fakeCloudControllerClient.CreateServicePlanVisibilityReturns(ccv2.ServicePlanVisibility{}, []string{"baz"}, errors.New("some error"))
   417  				})
   418  
   419  				It("returns all warnings", func() {
   420  					Expect(enablePlanWarnings).To(ConsistOf([]string{"foo", "bar", "baz"}))
   421  				})
   422  
   423  				It("returns the error", func() {
   424  					Expect(enablePlanErr).To(MatchError(errors.New("some error")))
   425  				})
   426  
   427  				Context("because the service plan visibility already exists", func() {
   428  					BeforeEach(func() {
   429  						fakeCloudControllerClient.CreateServicePlanVisibilityReturns(
   430  							ccv2.ServicePlanVisibility{},
   431  							ccv2.Warnings{"qux"},
   432  							ccerror.ServicePlanVisibilityExistsError{Message: "sorry"})
   433  					})
   434  
   435  					It("does not return the error", func() {
   436  						Expect(enablePlanErr).NotTo(HaveOccurred())
   437  					})
   438  
   439  					It("returns all warnings", func() {
   440  						Expect(enablePlanWarnings).To(ConsistOf("foo", "bar", "qux"))
   441  					})
   442  				})
   443  			})
   444  		})
   445  
   446  		When("the specified org does not exist", func() {
   447  			BeforeEach(func() {
   448  				fakeCloudControllerClient.GetOrganizationsReturns([]ccv2.Organization{}, nil, nil)
   449  			})
   450  
   451  			It("returns an organization not found error", func() {
   452  				Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1))
   453  				Expect(enablePlanErr).To(MatchError(actionerror.OrganizationNotFoundError{Name: "my-org"}))
   454  				filters := fakeCloudControllerClient.GetOrganizationsArgsForCall(0)
   455  				Expect(len(filters)).To(Equal(1))
   456  				Expect(filters[0]).To(Equal(ccv2.Filter{
   457  					Type:     constant.NameFilter,
   458  					Operator: constant.EqualOperator,
   459  					Values:   []string{"my-org"},
   460  				}))
   461  			})
   462  		})
   463  	})
   464  
   465  	Describe("EnableServiceForOrg", func() {
   466  		var enableServiceForOrgErr error
   467  		var enableServiceForOrgWarnings Warnings
   468  
   469  		JustBeforeEach(func() {
   470  			enableServiceForOrgWarnings, enableServiceForOrgErr = actor.EnableServiceForOrg("service-1", "my-org", "some-broker")
   471  		})
   472  
   473  		When("the service does not exist", func() {
   474  			BeforeEach(func() {
   475  				fakeCloudControllerClient.GetServicesReturns(
   476  					[]ccv2.Service{},
   477  					nil, nil)
   478  			})
   479  
   480  			It("returns service not found error", func() {
   481  				Expect(fakeCloudControllerClient.GetServicesCallCount()).To(Equal(1))
   482  				Expect(enableServiceForOrgErr).To(MatchError(actionerror.ServiceNotFoundError{Name: "service-1"}))
   483  
   484  				filters := fakeCloudControllerClient.GetServicesArgsForCall(0)
   485  				Expect(len(filters)).To(Equal(2))
   486  				Expect(filters).To(ConsistOf(
   487  					ccv2.Filter{
   488  						Type:     constant.LabelFilter,
   489  						Operator: constant.EqualOperator,
   490  						Values:   []string{"service-1"},
   491  					},
   492  					ccv2.Filter{
   493  						Type:     constant.ServiceBrokerGUIDFilter,
   494  						Operator: constant.EqualOperator,
   495  						Values:   []string{"broker-guid"},
   496  					},
   497  				))
   498  			})
   499  		})
   500  
   501  		When("the specified org does not exist", func() {
   502  			BeforeEach(func() {
   503  				fakeCloudControllerClient.GetServicesReturns(
   504  					[]ccv2.Service{{Label: "service-1", GUID: "service-guid-1"}},
   505  					nil, nil)
   506  				fakeCloudControllerClient.GetOrganizationsReturns([]ccv2.Organization{}, nil, nil)
   507  			})
   508  
   509  			It("returns an organization not found error", func() {
   510  				Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1))
   511  				Expect(enableServiceForOrgErr).To(MatchError(actionerror.OrganizationNotFoundError{Name: "my-org"}))
   512  				filters := fakeCloudControllerClient.GetOrganizationsArgsForCall(0)
   513  				Expect(len(filters)).To(Equal(1))
   514  				Expect(filters[0]).To(Equal(ccv2.Filter{
   515  					Type:     constant.NameFilter,
   516  					Operator: constant.EqualOperator,
   517  					Values:   []string{"my-org"},
   518  				}))
   519  			})
   520  		})
   521  
   522  		When("the service and org exist", func() {
   523  			BeforeEach(func() {
   524  				fakeCloudControllerClient.GetServicesReturns(
   525  					[]ccv2.Service{{Label: "service-1", GUID: "service-guid-1"}},
   526  					nil, nil)
   527  				fakeCloudControllerClient.GetOrganizationsReturns(
   528  					[]ccv2.Organization{{Name: "my-org", GUID: "org-guid-1"}},
   529  					nil, nil)
   530  				fakeCloudControllerClient.GetServicePlansReturns(
   531  					[]ccv2.ServicePlan{
   532  						{Name: "plan-1", GUID: "service-plan-guid-1"},
   533  						{Name: "plan-2", GUID: "service-plan-guid-2"},
   534  					},
   535  					nil,
   536  					nil)
   537  			})
   538  
   539  			It("enables all the plans for that org", func() {
   540  				Expect(enableServiceForOrgErr).NotTo(HaveOccurred())
   541  				Expect(fakeCloudControllerClient.GetServicesCallCount()).To(Equal(1))
   542  				Expect(fakeCloudControllerClient.GetServicePlansCallCount()).To(Equal(1))
   543  
   544  				Expect(fakeCloudControllerClient.CreateServicePlanVisibilityCallCount()).To(Equal(2))
   545  				planGUID1, orgGUID1 := fakeCloudControllerClient.CreateServicePlanVisibilityArgsForCall(0)
   546  				planGUID2, orgGUID2 := fakeCloudControllerClient.CreateServicePlanVisibilityArgsForCall(1)
   547  
   548  				Expect(orgGUID1).To(Equal("org-guid-1"))
   549  				Expect(orgGUID2).To(Equal("org-guid-1"))
   550  				Expect([]string{planGUID1, planGUID2}).To(ConsistOf([]string{"service-plan-guid-1", "service-plan-guid-2"}))
   551  			})
   552  
   553  			When("one of the service plans is public", func() {
   554  				BeforeEach(func() {
   555  					fakeCloudControllerClient.GetServicePlansReturns(
   556  						[]ccv2.ServicePlan{
   557  							{Name: "plan-1", GUID: "service-plan-guid-1", Public: true},
   558  							{Name: "plan-2", GUID: "service-plan-guid-2", Public: false},
   559  						},
   560  						ccv2.Warnings{"foo", "bar"},
   561  						nil)
   562  				})
   563  
   564  				It("creates a service plan visibility only for the non-public plan", func() {
   565  					Expect(enableServiceForOrgErr).NotTo(HaveOccurred())
   566  					Expect(enableServiceForOrgWarnings).To(ConsistOf("foo", "bar"))
   567  
   568  					Expect(fakeCloudControllerClient.GetServicesCallCount()).To(Equal(1))
   569  					Expect(fakeCloudControllerClient.GetServicePlansCallCount()).To(Equal(1))
   570  
   571  					Expect(fakeCloudControllerClient.CreateServicePlanVisibilityCallCount()).To(Equal(1))
   572  					planGUID1, orgGUID1 := fakeCloudControllerClient.CreateServicePlanVisibilityArgsForCall(0)
   573  					Expect(orgGUID1).To(Equal("org-guid-1"))
   574  					Expect(planGUID1).To(Equal("service-plan-guid-2"))
   575  				})
   576  			})
   577  		})
   578  
   579  		When("getting services fails", func() {
   580  			BeforeEach(func() {
   581  				fakeCloudControllerClient.GetServicesReturns(
   582  					[]ccv2.Service{},
   583  					ccv2.Warnings{"foo", "bar"},
   584  					errors.New("this is very bad"))
   585  			})
   586  
   587  			It("returns the error", func() {
   588  				Expect(enableServiceForOrgErr).To(MatchError(errors.New("this is very bad")))
   589  			})
   590  
   591  			It("returns all warnings", func() {
   592  				Expect(enableServiceForOrgWarnings).To(ConsistOf("foo", "bar"))
   593  			})
   594  		})
   595  
   596  		When("getting organizations fails", func() {
   597  			BeforeEach(func() {
   598  				fakeCloudControllerClient.GetServicesReturns(
   599  					[]ccv2.Service{{Label: "service-1", GUID: "service-guid-1"}},
   600  					ccv2.Warnings{"foo"},
   601  					nil)
   602  				fakeCloudControllerClient.GetOrganizationsReturns(
   603  					[]ccv2.Organization{{Name: "my-org", GUID: "org-guid-1"}},
   604  					ccv2.Warnings{"bar"},
   605  					errors.New("this is very bad"))
   606  			})
   607  
   608  			It("returns the error", func() {
   609  				Expect(enableServiceForOrgErr).To(MatchError(errors.New("this is very bad")))
   610  			})
   611  
   612  			It("returns all warnings", func() {
   613  				Expect(enableServiceForOrgWarnings).To(ConsistOf("foo", "bar"))
   614  			})
   615  		})
   616  
   617  		When("getting service plans fails", func() {
   618  			BeforeEach(func() {
   619  				fakeCloudControllerClient.GetServicesReturns(
   620  					[]ccv2.Service{{Label: "service-1", GUID: "service-guid-1"}},
   621  					ccv2.Warnings{"foo"},
   622  					nil)
   623  				fakeCloudControllerClient.GetServicePlansReturns(
   624  					[]ccv2.ServicePlan{},
   625  					ccv2.Warnings{"bar"},
   626  					errors.New("this is very bad"))
   627  			})
   628  
   629  			It("returns the error", func() {
   630  				Expect(enableServiceForOrgErr).To(MatchError(errors.New("this is very bad")))
   631  			})
   632  
   633  			It("returns all warnings", func() {
   634  				Expect(enableServiceForOrgWarnings).To(ConsistOf("foo", "bar"))
   635  			})
   636  		})
   637  
   638  		When("creating service plan visibility fails", func() {
   639  			BeforeEach(func() {
   640  				fakeCloudControllerClient.GetServicesReturns(
   641  					[]ccv2.Service{{Label: "service-1", GUID: "service-guid-1"}},
   642  					ccv2.Warnings{"foo"},
   643  					nil)
   644  				fakeCloudControllerClient.GetOrganizationsReturns(
   645  					[]ccv2.Organization{{Name: "my-org", GUID: "org-guid-1"}},
   646  					ccv2.Warnings{"bar"},
   647  					nil)
   648  				fakeCloudControllerClient.GetServicePlansReturns(
   649  					[]ccv2.ServicePlan{
   650  						{Name: "service-plan-1", GUID: "service-plan-guid-1"}},
   651  					ccv2.Warnings{"baz"},
   652  					nil)
   653  				fakeCloudControllerClient.CreateServicePlanVisibilityReturns(
   654  					ccv2.ServicePlanVisibility{},
   655  					ccv2.Warnings{"qux"},
   656  					errors.New("this is very bad"))
   657  			})
   658  
   659  			It("returns the error", func() {
   660  				Expect(enableServiceForOrgErr).To(MatchError(errors.New("this is very bad")))
   661  			})
   662  
   663  			It("returns all warnings", func() {
   664  				Expect(enableServiceForOrgWarnings).To(ConsistOf("foo", "bar", "baz", "qux"))
   665  			})
   666  
   667  			Context("because the service plan visibility already exists", func() {
   668  				BeforeEach(func() {
   669  					fakeCloudControllerClient.CreateServicePlanVisibilityReturns(
   670  						ccv2.ServicePlanVisibility{},
   671  						ccv2.Warnings{"qux"},
   672  						ccerror.ServicePlanVisibilityExistsError{Message: "sorry"})
   673  				})
   674  
   675  				It("does not return the error", func() {
   676  					Expect(enableServiceForOrgErr).NotTo(HaveOccurred())
   677  				})
   678  
   679  				It("returns all warnings", func() {
   680  					Expect(enableServiceForOrgWarnings).To(ConsistOf("foo", "bar", "baz", "qux"))
   681  				})
   682  			})
   683  		})
   684  	})
   685  
   686  	Describe("EnableServiceForAllOrgs", func() {
   687  		var (
   688  			enableServiceErr      error
   689  			enableServiceWarnings Warnings
   690  		)
   691  
   692  		BeforeEach(func() {
   693  			fakeCloudControllerClient.GetServicesReturns(
   694  				[]ccv2.Service{
   695  					{Label: "service-1", GUID: "service-guid-1"},
   696  				},
   697  				[]string{"foo"}, nil)
   698  
   699  			fakeCloudControllerClient.GetServicePlansReturns(
   700  				[]ccv2.ServicePlan{
   701  					{Name: "plan-1", GUID: "service-plan-guid-1"},
   702  					{Name: "plan-2", GUID: "service-plan-guid-2"},
   703  				},
   704  				[]string{"bar"}, nil)
   705  		})
   706  
   707  		JustBeforeEach(func() {
   708  			enableServiceWarnings, enableServiceErr = actor.EnableServiceForAllOrgs("service-1", "some-broker")
   709  		})
   710  
   711  		It("should update all plans to public", func() {
   712  			Expect(enableServiceErr).NotTo(HaveOccurred())
   713  			Expect(enableServiceWarnings).To(ConsistOf("foo", "bar"))
   714  
   715  			Expect(fakeCloudControllerClient.GetServicesCallCount()).To(Equal(1))
   716  			Expect(fakeCloudControllerClient.GetServicePlansCallCount()).To(Equal(1))
   717  			Expect(fakeCloudControllerClient.UpdateServicePlanCallCount()).To(Equal(2))
   718  
   719  			planGuid, public := fakeCloudControllerClient.UpdateServicePlanArgsForCall(0)
   720  			Expect(planGuid).To(Equal("service-plan-guid-1"))
   721  			Expect(public).To(BeTrue())
   722  
   723  			planGuid, public = fakeCloudControllerClient.UpdateServicePlanArgsForCall(1)
   724  			Expect(planGuid).To(Equal("service-plan-guid-2"))
   725  			Expect(public).To(BeTrue())
   726  		})
   727  
   728  		When("service plan visibilities for the orgs exists", func() {
   729  			BeforeEach(func() {
   730  				fakeCloudControllerClient.GetServicePlanVisibilitiesReturns(
   731  					[]ccv2.ServicePlanVisibility{
   732  						{GUID: "service-visibility-guid-1"},
   733  					},
   734  					nil, nil)
   735  			})
   736  
   737  			It("deletes the service plan visibilities", func() {
   738  				Expect(fakeCloudControllerClient.GetServicePlanVisibilitiesCallCount()).To(Equal(2))
   739  
   740  				filters := fakeCloudControllerClient.GetServicePlanVisibilitiesArgsForCall(0)
   741  				Expect(filters[0].Type).To(Equal(constant.ServicePlanGUIDFilter))
   742  				Expect(filters[0].Operator).To(Equal(constant.EqualOperator))
   743  				Expect(filters[0].Values).To(Equal([]string{"service-plan-guid-1"}))
   744  
   745  				Expect(fakeCloudControllerClient.DeleteServicePlanVisibilityCallCount()).To(Equal(2))
   746  			})
   747  
   748  			When("deleting service plan visibilities fails", func() {
   749  				BeforeEach(func() {
   750  					fakeCloudControllerClient.DeleteServicePlanVisibilityReturns(
   751  						[]string{"visibility-warning"}, errors.New("delete-visibility-error"))
   752  				})
   753  
   754  				It("propagates the error", func() {
   755  					Expect(enableServiceErr).To(MatchError(errors.New("delete-visibility-error")))
   756  				})
   757  
   758  				It("returns the warnings", func() {
   759  					Expect(enableServiceWarnings).To(Equal(Warnings{"foo", "bar", "visibility-warning"}))
   760  				})
   761  			})
   762  		})
   763  
   764  		When("the service does not exist", func() {
   765  			BeforeEach(func() {
   766  				fakeCloudControllerClient.GetServicesReturns(
   767  					[]ccv2.Service{},
   768  					nil, nil)
   769  			})
   770  
   771  			It("returns service not found error", func() {
   772  				Expect(fakeCloudControllerClient.GetServicesCallCount()).To(Equal(1))
   773  				Expect(enableServiceErr).To(MatchError(actionerror.ServiceNotFoundError{Name: "service-1"}))
   774  
   775  				filters := fakeCloudControllerClient.GetServicesArgsForCall(0)
   776  				Expect(len(filters)).To(Equal(2))
   777  				Expect(filters).To(ConsistOf(
   778  					ccv2.Filter{
   779  						Type:     constant.LabelFilter,
   780  						Operator: constant.EqualOperator,
   781  						Values:   []string{"service-1"},
   782  					},
   783  					ccv2.Filter{
   784  						Type:     constant.ServiceBrokerGUIDFilter,
   785  						Operator: constant.EqualOperator,
   786  						Values:   []string{"broker-guid"},
   787  					},
   788  				))
   789  			})
   790  		})
   791  
   792  		When("getting services fails", func() {
   793  			BeforeEach(func() {
   794  				fakeCloudControllerClient.GetServicesReturns(nil, []string{"foo", "bar"}, errors.New("it broke"))
   795  			})
   796  
   797  			It("returns the error", func() {
   798  				Expect(enableServiceErr).To(MatchError(errors.New("it broke")))
   799  			})
   800  
   801  			It("returns all warnings", func() {
   802  				Expect(enableServiceWarnings).To(ConsistOf([]string{"foo", "bar"}))
   803  			})
   804  		})
   805  
   806  		When("getting service plans fails", func() {
   807  			BeforeEach(func() {
   808  				fakeCloudControllerClient.GetServicesReturns(
   809  					[]ccv2.Service{
   810  						{Label: "service-1", GUID: "service-guid-1"},
   811  					},
   812  					[]string{"foo", "bar"},
   813  					nil)
   814  
   815  				fakeCloudControllerClient.GetServicePlansReturns(nil, []string{"baz"}, errors.New("it broke"))
   816  			})
   817  
   818  			It("returns all warnings", func() {
   819  				Expect(enableServiceWarnings).To(ConsistOf([]string{"foo", "bar", "baz"}))
   820  			})
   821  
   822  			It("returns the error", func() {
   823  				Expect(enableServiceErr).To(MatchError(errors.New("it broke")))
   824  			})
   825  		})
   826  
   827  		When("update service plan fails", func() {
   828  			BeforeEach(func() {
   829  				fakeCloudControllerClient.GetServicesReturns(
   830  					[]ccv2.Service{
   831  						{Label: "service-1", GUID: "service-guid-1"},
   832  					},
   833  					[]string{"foo"},
   834  					nil)
   835  
   836  				fakeCloudControllerClient.GetServicePlansReturns(
   837  					[]ccv2.ServicePlan{
   838  						{Name: "plan-1", GUID: "service-plan-guid-1"},
   839  						{Name: "plan-2", GUID: "service-plan-guid-2"},
   840  					},
   841  					[]string{"bar"},
   842  					nil)
   843  
   844  				fakeCloudControllerClient.UpdateServicePlanReturns([]string{"baz", "quux"}, errors.New("some error"))
   845  			})
   846  
   847  			It("returns all warnings", func() {
   848  				Expect(enableServiceWarnings).To(ConsistOf([]string{"foo", "bar", "baz", "quux"}))
   849  			})
   850  
   851  			It("returns the error", func() {
   852  				Expect(enableServiceErr).To(MatchError(errors.New("some error")))
   853  			})
   854  		})
   855  	})
   856  
   857  	Describe("DisableServiceForAllOrgs", func() {
   858  		var (
   859  			disableServiceErr      error
   860  			disableServiceWarnings Warnings
   861  		)
   862  
   863  		BeforeEach(func() {
   864  			fakeCloudControllerClient.GetServicesReturns(
   865  				[]ccv2.Service{
   866  					{Label: "service-1", GUID: "service-guid-1"},
   867  				},
   868  				nil, nil)
   869  
   870  			fakeCloudControllerClient.GetServicePlansReturns(
   871  				[]ccv2.ServicePlan{
   872  					{Name: "plan-1", GUID: "service-plan-guid-1", Public: true},
   873  					{Name: "plan-2", GUID: "service-plan-guid-2", Public: true},
   874  					{Name: "plan-3", GUID: "service-plan-guid-3", Public: false},
   875  				},
   876  				nil, nil)
   877  		})
   878  
   879  		JustBeforeEach(func() {
   880  			disableServiceWarnings, disableServiceErr = actor.DisableServiceForAllOrgs("service-1", "broker-1")
   881  		})
   882  
   883  		It("should call GetServices with proper filters", func() {
   884  			filters := fakeCloudControllerClient.GetServicesArgsForCall(0)
   885  			Expect(len(filters)).To(Equal(2))
   886  			Expect(filters).To(ConsistOf(
   887  				ccv2.Filter{
   888  					Type:     constant.LabelFilter,
   889  					Operator: constant.EqualOperator,
   890  					Values:   []string{"service-1"},
   891  				},
   892  				ccv2.Filter{
   893  					Type:     constant.ServiceBrokerGUIDFilter,
   894  					Operator: constant.EqualOperator,
   895  					Values:   []string{"broker-guid"},
   896  				},
   897  			))
   898  		})
   899  
   900  		It("should update all public plans to non-public", func() {
   901  			Expect(disableServiceErr).NotTo(HaveOccurred())
   902  			Expect(fakeCloudControllerClient.GetServicesCallCount()).To(Equal(1))
   903  			Expect(fakeCloudControllerClient.GetServicePlansCallCount()).To(Equal(1))
   904  			Expect(fakeCloudControllerClient.UpdateServicePlanCallCount()).To(Equal(2))
   905  			planGuid, public := fakeCloudControllerClient.UpdateServicePlanArgsForCall(0)
   906  			Expect(planGuid).To(Equal("service-plan-guid-1"))
   907  			Expect(public).To(BeFalse())
   908  			planGuid, public = fakeCloudControllerClient.UpdateServicePlanArgsForCall(1)
   909  			Expect(planGuid).To(Equal("service-plan-guid-2"))
   910  			Expect(public).To(BeFalse())
   911  		})
   912  
   913  		When("the plan is enabled for an org", func() {
   914  			BeforeEach(func() {
   915  				fakeCloudControllerClient.GetServicePlanVisibilitiesReturnsOnCall(2,
   916  					[]ccv2.ServicePlanVisibility{{GUID: "service-plan-visibility-guid-1"}},
   917  					nil, nil)
   918  			})
   919  
   920  			It("should delete the service plan visibility for the plan", func() {
   921  				Expect(fakeCloudControllerClient.DeleteServicePlanVisibilityCallCount()).To(Equal(1))
   922  				Expect(fakeCloudControllerClient.DeleteServicePlanVisibilityArgsForCall(0)).To(Equal("service-plan-visibility-guid-1"))
   923  			})
   924  		})
   925  
   926  		When("the service does not exist", func() {
   927  			BeforeEach(func() {
   928  				fakeCloudControllerClient.GetServicesReturns(
   929  					[]ccv2.Service{},
   930  					nil, nil)
   931  			})
   932  
   933  			It("returns service not found error", func() {
   934  				Expect(fakeCloudControllerClient.GetServicesCallCount()).To(Equal(1))
   935  				Expect(disableServiceErr).To(MatchError(actionerror.ServiceNotFoundError{Name: "service-1"}))
   936  			})
   937  		})
   938  
   939  		When("getting services fails", func() {
   940  			BeforeEach(func() {
   941  				fakeCloudControllerClient.GetServicesReturns(nil, []string{"foo", "bar"}, errors.New("it broke"))
   942  			})
   943  
   944  			It("returns the error", func() {
   945  				Expect(disableServiceErr).To(MatchError(errors.New("it broke")))
   946  			})
   947  
   948  			It("returns all warnings", func() {
   949  				Expect(disableServiceWarnings).To(ConsistOf([]string{"foo", "bar"}))
   950  			})
   951  		})
   952  
   953  		When("getting service plans fails", func() {
   954  			BeforeEach(func() {
   955  				fakeCloudControllerClient.GetServicesReturns(
   956  					[]ccv2.Service{
   957  						{Label: "service-1", GUID: "service-guid-1"},
   958  					},
   959  					[]string{"foo", "bar"},
   960  					nil)
   961  
   962  				fakeCloudControllerClient.GetServicePlansReturns(nil, []string{"baz"}, errors.New("it broke"))
   963  			})
   964  
   965  			It("returns all warnings", func() {
   966  				Expect(disableServiceWarnings).To(ConsistOf([]string{"foo", "bar", "baz"}))
   967  			})
   968  
   969  			It("returns the error", func() {
   970  				Expect(disableServiceErr).To(MatchError(errors.New("it broke")))
   971  			})
   972  		})
   973  
   974  		When("update service plan fails", func() {
   975  			BeforeEach(func() {
   976  				fakeCloudControllerClient.GetServicesReturns(
   977  					[]ccv2.Service{
   978  						{Label: "service-1", GUID: "service-guid-1"},
   979  					},
   980  					[]string{"foo"},
   981  					nil)
   982  
   983  				fakeCloudControllerClient.GetServicePlansReturns(
   984  					[]ccv2.ServicePlan{
   985  						{Name: "plan-1", GUID: "service-plan-guid-1", Public: true},
   986  						{Name: "plan-2", GUID: "service-plan-guid-2", Public: true},
   987  					},
   988  					[]string{"bar"},
   989  					nil)
   990  
   991  				fakeCloudControllerClient.UpdateServicePlanReturns([]string{"baz", "quux"}, errors.New("some error"))
   992  			})
   993  
   994  			It("returns all warnings", func() {
   995  				Expect(disableServiceWarnings).To(ConsistOf([]string{"foo", "bar", "baz", "quux"}))
   996  			})
   997  
   998  			It("returns the error", func() {
   999  				Expect(disableServiceErr).To(MatchError(errors.New("some error")))
  1000  			})
  1001  		})
  1002  
  1003  		When("there are warnings", func() {
  1004  			BeforeEach(func() {
  1005  				fakeCloudControllerClient.GetServicesReturns(
  1006  					[]ccv2.Service{
  1007  						{Label: "service-1", GUID: "service-guid-1"},
  1008  					},
  1009  					[]string{"foo"},
  1010  					nil)
  1011  
  1012  				fakeCloudControllerClient.GetServicePlansReturns(
  1013  					[]ccv2.ServicePlan{
  1014  						{Name: "plan-1", GUID: "service-plan-guid-1", Public: true},
  1015  						{Name: "plan-2", GUID: "service-plan-guid-2", Public: true},
  1016  					},
  1017  					[]string{"bar"},
  1018  					nil)
  1019  
  1020  				fakeCloudControllerClient.GetServicePlanVisibilitiesReturns(
  1021  					[]ccv2.ServicePlanVisibility{
  1022  						{GUID: "service-visibility-guid-1"},
  1023  						{GUID: "service-visibility-guid-2"},
  1024  					},
  1025  					[]string{"baz"},
  1026  					nil)
  1027  
  1028  				fakeCloudControllerClient.UpdateServicePlanReturns(
  1029  					[]string{"qux"},
  1030  					nil)
  1031  			})
  1032  
  1033  			It("returns the warnings", func() {
  1034  				Expect(disableServiceWarnings).To(ConsistOf([]string{"foo", "bar", "baz", "qux", "baz", "qux"}))
  1035  			})
  1036  		})
  1037  	})
  1038  
  1039  	Describe("DisablePlanForAllOrgs", func() {
  1040  		var (
  1041  			disablePlanErr      error
  1042  			disablePlanWarnings Warnings
  1043  		)
  1044  
  1045  		BeforeEach(func() {
  1046  			fakeCloudControllerClient.GetServicesReturns(
  1047  				[]ccv2.Service{
  1048  					{Label: "service-1", GUID: "service-guid-1"},
  1049  				},
  1050  				nil, nil)
  1051  		})
  1052  
  1053  		JustBeforeEach(func() {
  1054  			disablePlanWarnings, disablePlanErr = actor.DisablePlanForAllOrgs("service-1", "plan-2", "broker-1")
  1055  		})
  1056  
  1057  		It("should call GetServices with proper filters", func() {
  1058  			filters := fakeCloudControllerClient.GetServicesArgsForCall(0)
  1059  			Expect(len(filters)).To(Equal(2))
  1060  			Expect(filters).To(ConsistOf(
  1061  				ccv2.Filter{
  1062  					Type:     constant.LabelFilter,
  1063  					Operator: constant.EqualOperator,
  1064  					Values:   []string{"service-1"},
  1065  				},
  1066  				ccv2.Filter{
  1067  					Type:     constant.ServiceBrokerGUIDFilter,
  1068  					Operator: constant.EqualOperator,
  1069  					Values:   []string{"broker-guid"},
  1070  				},
  1071  			))
  1072  		})
  1073  
  1074  		When("the plan is public", func() {
  1075  			BeforeEach(func() {
  1076  				fakeCloudControllerClient.GetServicePlansReturns(
  1077  					[]ccv2.ServicePlan{
  1078  						{Name: "plan-1", GUID: "service-plan-guid-1", Public: true},
  1079  						{Name: "plan-2", GUID: "service-plan-guid-2", Public: true},
  1080  					},
  1081  					nil, nil)
  1082  			})
  1083  
  1084  			It("updates the service plan to be not public", func() {
  1085  				Expect(disablePlanErr).NotTo(HaveOccurred())
  1086  				Expect(fakeCloudControllerClient.GetServicesCallCount()).To(Equal(1))
  1087  				Expect(fakeCloudControllerClient.GetServicePlansCallCount()).To(Equal(1))
  1088  				Expect(fakeCloudControllerClient.UpdateServicePlanCallCount()).To(Equal(1))
  1089  
  1090  				planGuid, visible := fakeCloudControllerClient.UpdateServicePlanArgsForCall(0)
  1091  				Expect(planGuid).To(Equal("service-plan-guid-2"))
  1092  				Expect(visible).To(BeFalse())
  1093  			})
  1094  
  1095  			When("the plan is visible in some orgs", func() {
  1096  				BeforeEach(func() {
  1097  					fakeCloudControllerClient.GetServicePlanVisibilitiesReturns(
  1098  						[]ccv2.ServicePlanVisibility{
  1099  							{GUID: "service-visibility-guid-1"},
  1100  							{GUID: "service-visibility-guid-2"},
  1101  						},
  1102  						nil, nil)
  1103  				})
  1104  
  1105  				It("disables the plan in both orgs", func() {
  1106  					Expect(fakeCloudControllerClient.GetServicePlanVisibilitiesCallCount()).To(Equal(1))
  1107  
  1108  					filters := fakeCloudControllerClient.GetServicePlanVisibilitiesArgsForCall(0)
  1109  					Expect(filters[0].Type).To(Equal(constant.ServicePlanGUIDFilter))
  1110  					Expect(filters[0].Operator).To(Equal(constant.EqualOperator))
  1111  					Expect(filters[0].Values).To(Equal([]string{"service-plan-guid-2"}))
  1112  
  1113  					Expect(fakeCloudControllerClient.DeleteServicePlanVisibilityCallCount()).To(Equal(2))
  1114  				})
  1115  
  1116  				It("and updates the service plan visibility for all orgs", func() {
  1117  					Expect(disablePlanErr).NotTo(HaveOccurred())
  1118  					Expect(fakeCloudControllerClient.GetServicesCallCount()).To(Equal(1))
  1119  					Expect(fakeCloudControllerClient.GetServicePlansCallCount()).To(Equal(1))
  1120  					Expect(fakeCloudControllerClient.UpdateServicePlanCallCount()).To(Equal(1))
  1121  
  1122  					planGuid, visible := fakeCloudControllerClient.UpdateServicePlanArgsForCall(0)
  1123  					Expect(planGuid).To(Equal("service-plan-guid-2"))
  1124  					Expect(visible).To(BeFalse())
  1125  				})
  1126  
  1127  				When("deleting service plan visibilities fails", func() {
  1128  					BeforeEach(func() {
  1129  						fakeCloudControllerClient.DeleteServicePlanVisibilityReturns(
  1130  							[]string{"visibility-warning"}, errors.New("delete-visibility-error"))
  1131  					})
  1132  
  1133  					It("propagates the error", func() {
  1134  						Expect(disablePlanErr).To(MatchError(errors.New("delete-visibility-error")))
  1135  					})
  1136  
  1137  					It("returns the warnings", func() {
  1138  						Expect(disablePlanWarnings).To(Equal(Warnings{"visibility-warning"}))
  1139  					})
  1140  				})
  1141  			})
  1142  		})
  1143  
  1144  		When("the plan is not public", func() {
  1145  			BeforeEach(func() {
  1146  				fakeCloudControllerClient.GetServicePlansReturns(
  1147  					[]ccv2.ServicePlan{
  1148  						{Name: "plan-1", GUID: "service-plan-guid-1", Public: true},
  1149  						{Name: "plan-2", GUID: "service-plan-guid-2", Public: false},
  1150  					},
  1151  					nil, nil)
  1152  			})
  1153  
  1154  			It("does not update the plan to be not public", func() {
  1155  				Expect(disablePlanErr).NotTo(HaveOccurred())
  1156  				Expect(fakeCloudControllerClient.GetServicesCallCount()).To(Equal(1))
  1157  				Expect(fakeCloudControllerClient.GetServicePlansCallCount()).To(Equal(1))
  1158  				Expect(fakeCloudControllerClient.UpdateServicePlanCallCount()).To(Equal(0))
  1159  			})
  1160  		})
  1161  
  1162  		When("getting service plan visibilities fails", func() {
  1163  			BeforeEach(func() {
  1164  				fakeCloudControllerClient.GetServicePlansReturns(
  1165  					[]ccv2.ServicePlan{
  1166  						{Name: "plan-1", GUID: "service-plan-guid-1", Public: true},
  1167  						{Name: "plan-2", GUID: "service-plan-guid-2", Public: true},
  1168  					},
  1169  					nil, nil)
  1170  				fakeCloudControllerClient.GetServicePlanVisibilitiesReturns(
  1171  					[]ccv2.ServicePlanVisibility{},
  1172  					[]string{"a-warning", "another-warning"},
  1173  					errors.New("oh no"))
  1174  			})
  1175  
  1176  			It("returns the error", func() {
  1177  				Expect(disablePlanErr).To(MatchError(errors.New("oh no")))
  1178  			})
  1179  
  1180  			It("returns the warnings", func() {
  1181  				Expect(disablePlanWarnings).To(Equal(Warnings{"a-warning", "another-warning"}))
  1182  			})
  1183  		})
  1184  
  1185  		When("getting service plans fails", func() {
  1186  			BeforeEach(func() {
  1187  				fakeCloudControllerClient.GetServicePlansReturns(nil,
  1188  					[]string{"a-warning", "another-warning"},
  1189  					errors.New("it didn't work!"))
  1190  			})
  1191  
  1192  			It("returns the error", func() {
  1193  				Expect(disablePlanErr).To(MatchError(errors.New("it didn't work!")))
  1194  			})
  1195  
  1196  			It("returns the warnings", func() {
  1197  				Expect(disablePlanWarnings).To(Equal(Warnings{"a-warning", "another-warning"}))
  1198  			})
  1199  		})
  1200  
  1201  		When("getting services fails", func() {
  1202  			BeforeEach(func() {
  1203  				fakeCloudControllerClient.GetServicesReturns(nil,
  1204  					[]string{"a-warning", "another-warning"},
  1205  					errors.New("it didn't work!"))
  1206  			})
  1207  
  1208  			It("returns the error", func() {
  1209  				Expect(disablePlanErr).To(MatchError(errors.New("it didn't work!")))
  1210  			})
  1211  
  1212  			It("returns the warnings", func() {
  1213  				Expect(disablePlanWarnings).To(Equal(Warnings{"a-warning", "another-warning"}))
  1214  			})
  1215  		})
  1216  
  1217  		When("there are no matching services", func() {
  1218  			BeforeEach(func() {
  1219  				fakeCloudControllerClient.GetServicesReturns([]ccv2.Service{}, []string{"warning-1", "warning-2"}, nil)
  1220  			})
  1221  
  1222  			It("returns not found error", func() {
  1223  				Expect(disablePlanErr).To(MatchError(actionerror.ServiceNotFoundError{Name: "service-1"}))
  1224  			})
  1225  
  1226  			It("returns all warnings", func() {
  1227  				Expect(disablePlanWarnings).To(ConsistOf([]string{"warning-1", "warning-2"}))
  1228  			})
  1229  		})
  1230  
  1231  		When("there are no matching plans", func() {
  1232  			BeforeEach(func() {
  1233  				fakeCloudControllerClient.GetServicePlansReturns([]ccv2.ServicePlan{}, []string{"warning-1", "warning-2"}, nil)
  1234  			})
  1235  
  1236  			It("returns not found error", func() {
  1237  				Expect(disablePlanErr).To(MatchError(actionerror.ServicePlanNotFoundError{PlanName: "plan-2", OfferingName: "service-1"}))
  1238  			})
  1239  
  1240  			It("returns all warnings", func() {
  1241  				Expect(disablePlanWarnings).To(ConsistOf([]string{"warning-1", "warning-2"}))
  1242  			})
  1243  		})
  1244  
  1245  		When("updating service plan fails", func() {
  1246  			BeforeEach(func() {
  1247  				fakeCloudControllerClient.GetServicePlansReturns(
  1248  					[]ccv2.ServicePlan{
  1249  						{Name: "plan-1", GUID: "service-plan-guid-1", Public: true},
  1250  						{Name: "plan-2", GUID: "service-plan-guid-2", Public: true},
  1251  					},
  1252  					nil, nil)
  1253  				fakeCloudControllerClient.UpdateServicePlanReturns(
  1254  					[]string{"a-warning", "another-warning"},
  1255  					errors.New("it didn't work!"))
  1256  			})
  1257  
  1258  			It("returns the error", func() {
  1259  				Expect(disablePlanErr).To(MatchError(errors.New("it didn't work!")))
  1260  			})
  1261  
  1262  			It("returns the warnings", func() {
  1263  				Expect(disablePlanWarnings).To(Equal(Warnings{"a-warning", "another-warning"}))
  1264  			})
  1265  		})
  1266  
  1267  		When("there are warnings", func() {
  1268  			BeforeEach(func() {
  1269  				fakeCloudControllerClient.GetServicesReturns(
  1270  					[]ccv2.Service{
  1271  						{Label: "service-1", GUID: "service-guid-1"},
  1272  					},
  1273  					[]string{"foo"},
  1274  					nil)
  1275  
  1276  				fakeCloudControllerClient.GetServicePlansReturns(
  1277  					[]ccv2.ServicePlan{
  1278  						{Name: "plan-1", GUID: "service-plan-guid-1", Public: true},
  1279  						{Name: "plan-2", GUID: "service-plan-guid-2", Public: true},
  1280  					},
  1281  					[]string{"bar"},
  1282  					nil)
  1283  
  1284  				fakeCloudControllerClient.GetServicePlanVisibilitiesReturns(
  1285  					[]ccv2.ServicePlanVisibility{
  1286  						{GUID: "service-visibility-guid-1"},
  1287  						{GUID: "service-visibility-guid-2"},
  1288  					},
  1289  					[]string{"baz"},
  1290  					nil)
  1291  
  1292  				fakeCloudControllerClient.UpdateServicePlanReturns(
  1293  					[]string{"qux"},
  1294  					nil)
  1295  			})
  1296  
  1297  			It("returns the warnings", func() {
  1298  				Expect(disablePlanWarnings).To(ConsistOf([]string{"foo", "bar", "baz", "qux"}))
  1299  			})
  1300  		})
  1301  	})
  1302  
  1303  	Describe("DisableServiceForOrg", func() {
  1304  		var disableServiceForOrgErr error
  1305  		var disableServiceForOrgWarnings Warnings
  1306  
  1307  		JustBeforeEach(func() {
  1308  			disableServiceForOrgWarnings, disableServiceForOrgErr = actor.DisableServiceForOrg("service-1", "my-org", "broker-1")
  1309  		})
  1310  
  1311  		When("the service and org exist", func() {
  1312  			BeforeEach(func() {
  1313  				fakeCloudControllerClient.GetServicesReturns(
  1314  					[]ccv2.Service{{Label: "service-1", GUID: "service-guid-1"}},
  1315  					ccv2.Warnings{"foo"},
  1316  					nil)
  1317  				fakeCloudControllerClient.GetOrganizationsReturns(
  1318  					[]ccv2.Organization{{Name: "my-org", GUID: "org-guid-1"}},
  1319  					ccv2.Warnings{"bar"},
  1320  					nil)
  1321  				fakeCloudControllerClient.GetServicePlansReturns(
  1322  					[]ccv2.ServicePlan{
  1323  						{Name: "service-plan-1", GUID: "service-plan-guid-1"},
  1324  						{Name: "service-plan-2", GUID: "service-plan-guid-2"},
  1325  					},
  1326  					ccv2.Warnings{"baz"},
  1327  					nil)
  1328  				fakeCloudControllerClient.GetServicePlanVisibilitiesReturnsOnCall(0,
  1329  					[]ccv2.ServicePlanVisibility{
  1330  						{GUID: "service-visibility-guid-1"},
  1331  					},
  1332  					ccv2.Warnings{"qux-1"}, nil)
  1333  				fakeCloudControllerClient.GetServicePlanVisibilitiesReturnsOnCall(1,
  1334  					[]ccv2.ServicePlanVisibility{
  1335  						{GUID: "service-visibility-guid-2"},
  1336  					},
  1337  					ccv2.Warnings{"qux-2"}, nil)
  1338  			})
  1339  
  1340  			It("deletes the service plan visibility for all plans for org", func() {
  1341  				Expect(disableServiceForOrgErr).NotTo(HaveOccurred())
  1342  				Expect(fakeCloudControllerClient.GetServiceBrokersCallCount()).To(Equal(1))
  1343  				brokerFilters := fakeCloudControllerClient.GetServiceBrokersArgsForCall(0)
  1344  				Expect(len(brokerFilters)).To(Equal(1))
  1345  				Expect(brokerFilters[0]).To(Equal(ccv2.Filter{
  1346  					Type:     constant.NameFilter,
  1347  					Operator: constant.EqualOperator,
  1348  					Values:   []string{"broker-1"},
  1349  				}))
  1350  				Expect(fakeCloudControllerClient.GetServicesCallCount()).To(Equal(1))
  1351  				Expect(fakeCloudControllerClient.GetServicePlansCallCount()).To(Equal(1))
  1352  				Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1))
  1353  				orgFilters := fakeCloudControllerClient.GetOrganizationsArgsForCall(0)
  1354  				Expect(len(orgFilters)).To(Equal(1))
  1355  				Expect(orgFilters[0]).To(Equal(ccv2.Filter{
  1356  					Type:     constant.NameFilter,
  1357  					Operator: constant.EqualOperator,
  1358  					Values:   []string{"my-org"},
  1359  				}))
  1360  				Expect(fakeCloudControllerClient.GetServicePlanVisibilitiesCallCount()).To(Equal(2))
  1361  				filters := fakeCloudControllerClient.GetServicePlanVisibilitiesArgsForCall(0)
  1362  
  1363  				Expect(filters).To(ConsistOf(
  1364  					ccv2.Filter{Type: constant.ServicePlanGUIDFilter, Operator: constant.EqualOperator, Values: []string{"service-plan-guid-1"}},
  1365  					ccv2.Filter{Type: constant.OrganizationGUIDFilter, Operator: constant.EqualOperator, Values: []string{"org-guid-1"}},
  1366  				))
  1367  				Expect(fakeCloudControllerClient.DeleteServicePlanVisibilityCallCount()).To(Equal(2))
  1368  				Expect(fakeCloudControllerClient.DeleteServicePlanVisibilityArgsForCall(0)).To(Equal("service-visibility-guid-1"))
  1369  				Expect(fakeCloudControllerClient.DeleteServicePlanVisibilityArgsForCall(1)).To(Equal("service-visibility-guid-2"))
  1370  			})
  1371  
  1372  			When("there are warnings", func() {
  1373  				BeforeEach(func() {
  1374  					fakeCloudControllerClient.DeleteServicePlanVisibilityReturns(
  1375  						[]string{"quux"},
  1376  						nil)
  1377  				})
  1378  
  1379  				It("returns the warnings", func() {
  1380  					Expect(disableServiceForOrgWarnings).To(ConsistOf([]string{"foo", "bar", "baz", "qux-1", "quux", "qux-2", "quux"}))
  1381  				})
  1382  			})
  1383  
  1384  			When("deleting service plan visibility fails", func() {
  1385  				BeforeEach(func() {
  1386  					fakeCloudControllerClient.DeleteServicePlanVisibilityReturns(
  1387  						ccv2.Warnings{"quux"},
  1388  						errors.New("this is very bad"))
  1389  				})
  1390  
  1391  				It("returns the error", func() {
  1392  					Expect(disableServiceForOrgErr).To(MatchError(errors.New("this is very bad")))
  1393  				})
  1394  
  1395  				It("returns all warnings", func() {
  1396  					Expect(disableServiceForOrgWarnings).To(ConsistOf("foo", "bar", "baz", "qux-1", "quux"))
  1397  				})
  1398  			})
  1399  		})
  1400  
  1401  		When("the service does not exist", func() {
  1402  			BeforeEach(func() {
  1403  				fakeCloudControllerClient.GetServicesReturns(
  1404  					[]ccv2.Service{},
  1405  					nil, nil)
  1406  			})
  1407  
  1408  			It("returns service not found error", func() {
  1409  				Expect(fakeCloudControllerClient.GetServicesCallCount()).To(Equal(1))
  1410  				Expect(disableServiceForOrgErr).To(MatchError(actionerror.ServiceNotFoundError{Name: "service-1"}))
  1411  
  1412  				filters := fakeCloudControllerClient.GetServicesArgsForCall(0)
  1413  				Expect(len(filters)).To(Equal(2))
  1414  				Expect(filters).To(ConsistOf(
  1415  					ccv2.Filter{
  1416  						Type:     constant.LabelFilter,
  1417  						Operator: constant.EqualOperator,
  1418  						Values:   []string{"service-1"},
  1419  					},
  1420  					ccv2.Filter{
  1421  						Type:     constant.ServiceBrokerGUIDFilter,
  1422  						Operator: constant.EqualOperator,
  1423  						Values:   []string{"broker-guid"},
  1424  					},
  1425  				))
  1426  			})
  1427  		})
  1428  
  1429  		When("the specified org does not exist", func() {
  1430  			BeforeEach(func() {
  1431  				fakeCloudControllerClient.GetServicesReturns(
  1432  					[]ccv2.Service{{Label: "service-1", GUID: "service-guid-1"}},
  1433  					nil, nil)
  1434  				fakeCloudControllerClient.GetOrganizationsReturns([]ccv2.Organization{}, nil, nil)
  1435  			})
  1436  
  1437  			It("returns an organization not found error", func() {
  1438  				Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1))
  1439  				Expect(disableServiceForOrgErr).To(MatchError(actionerror.OrganizationNotFoundError{Name: "my-org"}))
  1440  				filters := fakeCloudControllerClient.GetOrganizationsArgsForCall(0)
  1441  				Expect(len(filters)).To(Equal(1))
  1442  				Expect(filters[0]).To(Equal(ccv2.Filter{
  1443  					Type:     constant.NameFilter,
  1444  					Operator: constant.EqualOperator,
  1445  					Values:   []string{"my-org"},
  1446  				}))
  1447  			})
  1448  		})
  1449  
  1450  		When("getting services fails", func() {
  1451  			BeforeEach(func() {
  1452  				fakeCloudControllerClient.GetServicesReturns(
  1453  					[]ccv2.Service{},
  1454  					ccv2.Warnings{"foo", "bar"},
  1455  					errors.New("this is very bad"))
  1456  			})
  1457  
  1458  			It("returns the error", func() {
  1459  				Expect(disableServiceForOrgErr).To(MatchError(errors.New("this is very bad")))
  1460  			})
  1461  
  1462  			It("returns all warnings", func() {
  1463  				Expect(disableServiceForOrgWarnings).To(ConsistOf("foo", "bar"))
  1464  			})
  1465  		})
  1466  
  1467  		When("getting organizations fails", func() {
  1468  			BeforeEach(func() {
  1469  				fakeCloudControllerClient.GetServicesReturns(
  1470  					[]ccv2.Service{{Label: "service-1", GUID: "service-guid-1"}},
  1471  					ccv2.Warnings{"foo"},
  1472  					nil)
  1473  				fakeCloudControllerClient.GetOrganizationsReturns(
  1474  					[]ccv2.Organization{{Name: "my-org", GUID: "org-guid-1"}},
  1475  					ccv2.Warnings{"bar"},
  1476  					errors.New("this is very bad"))
  1477  			})
  1478  
  1479  			It("returns the error", func() {
  1480  				Expect(disableServiceForOrgErr).To(MatchError(errors.New("this is very bad")))
  1481  			})
  1482  
  1483  			It("returns all warnings", func() {
  1484  				Expect(disableServiceForOrgWarnings).To(ConsistOf("foo", "bar"))
  1485  			})
  1486  		})
  1487  
  1488  		When("getting service plans fails", func() {
  1489  			BeforeEach(func() {
  1490  				fakeCloudControllerClient.GetServicesReturns(
  1491  					[]ccv2.Service{{Label: "service-1", GUID: "service-guid-1"}},
  1492  					ccv2.Warnings{"foo"},
  1493  					nil)
  1494  				fakeCloudControllerClient.GetServicePlansReturns(
  1495  					[]ccv2.ServicePlan{},
  1496  					ccv2.Warnings{"bar"},
  1497  					errors.New("this is very bad"))
  1498  			})
  1499  
  1500  			It("returns the error", func() {
  1501  				Expect(disableServiceForOrgErr).To(MatchError(errors.New("this is very bad")))
  1502  			})
  1503  
  1504  			It("returns all warnings", func() {
  1505  				Expect(disableServiceForOrgWarnings).To(ConsistOf("foo", "bar"))
  1506  			})
  1507  		})
  1508  	})
  1509  
  1510  	Describe("DisablePlanForOrg", func() {
  1511  		var disablePlanForOrgErr error
  1512  		var disablePlanForOrgWarnings Warnings
  1513  
  1514  		JustBeforeEach(func() {
  1515  			disablePlanForOrgWarnings, disablePlanForOrgErr = actor.DisablePlanForOrg("service-1", "service-plan-1", "my-org", "broker-1")
  1516  		})
  1517  
  1518  		When("the service and plan and org exist", func() {
  1519  			BeforeEach(func() {
  1520  				fakeCloudControllerClient.GetServicesReturns(
  1521  					[]ccv2.Service{{Label: "service-1", GUID: "service-guid-1"}},
  1522  					ccv2.Warnings{"foo"},
  1523  					nil)
  1524  				fakeCloudControllerClient.GetOrganizationsReturns(
  1525  					[]ccv2.Organization{{Name: "my-org", GUID: "org-guid-1"}},
  1526  					ccv2.Warnings{"bar"},
  1527  					nil)
  1528  				fakeCloudControllerClient.GetServicePlansReturns(
  1529  					[]ccv2.ServicePlan{
  1530  						{Name: "service-plan-1", GUID: "service-plan-guid-1"},
  1531  						{Name: "service-plan-2", GUID: "service-plan-guid-2"},
  1532  					},
  1533  					ccv2.Warnings{"baz"},
  1534  					nil)
  1535  				fakeCloudControllerClient.GetServicePlanVisibilitiesReturnsOnCall(0,
  1536  					[]ccv2.ServicePlanVisibility{
  1537  						{GUID: "service-visibility-guid-1"},
  1538  					},
  1539  					nil, nil)
  1540  			})
  1541  
  1542  			It("deletes the service plan visibility for all plans for org", func() {
  1543  				Expect(disablePlanForOrgErr).NotTo(HaveOccurred())
  1544  				Expect(fakeCloudControllerClient.GetServiceBrokersCallCount()).To(Equal(1))
  1545  				brokerFilters := fakeCloudControllerClient.GetServiceBrokersArgsForCall(0)
  1546  				Expect(len(brokerFilters)).To(Equal(1))
  1547  				Expect(brokerFilters[0]).To(Equal(ccv2.Filter{
  1548  					Type:     constant.NameFilter,
  1549  					Operator: constant.EqualOperator,
  1550  					Values:   []string{"broker-1"},
  1551  				}))
  1552  				Expect(fakeCloudControllerClient.GetServicesCallCount()).To(Equal(1))
  1553  				Expect(fakeCloudControllerClient.GetServicePlansCallCount()).To(Equal(1))
  1554  				Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1))
  1555  				orgFilters := fakeCloudControllerClient.GetOrganizationsArgsForCall(0)
  1556  				Expect(len(orgFilters)).To(Equal(1))
  1557  				Expect(orgFilters[0]).To(Equal(ccv2.Filter{
  1558  					Type:     constant.NameFilter,
  1559  					Operator: constant.EqualOperator,
  1560  					Values:   []string{"my-org"},
  1561  				}))
  1562  				Expect(fakeCloudControllerClient.GetServicePlanVisibilitiesCallCount()).To(Equal(1))
  1563  				filters := fakeCloudControllerClient.GetServicePlanVisibilitiesArgsForCall(0)
  1564  
  1565  				Expect(filters).To(ConsistOf(
  1566  					ccv2.Filter{Type: constant.ServicePlanGUIDFilter, Operator: constant.EqualOperator, Values: []string{"service-plan-guid-1"}},
  1567  					ccv2.Filter{Type: constant.OrganizationGUIDFilter, Operator: constant.EqualOperator, Values: []string{"org-guid-1"}},
  1568  				))
  1569  				Expect(fakeCloudControllerClient.DeleteServicePlanVisibilityCallCount()).To(Equal(1))
  1570  				Expect(fakeCloudControllerClient.DeleteServicePlanVisibilityArgsForCall(0)).To(Equal("service-visibility-guid-1"))
  1571  			})
  1572  
  1573  			When("deleting service plan visibility fails", func() {
  1574  				BeforeEach(func() {
  1575  					fakeCloudControllerClient.DeleteServicePlanVisibilityReturns(
  1576  						ccv2.Warnings{"qux"},
  1577  						errors.New("this is very bad"))
  1578  				})
  1579  
  1580  				It("returns the error", func() {
  1581  					Expect(disablePlanForOrgErr).To(MatchError(errors.New("this is very bad")))
  1582  				})
  1583  
  1584  				It("returns all warnings", func() {
  1585  					Expect(disablePlanForOrgWarnings).To(ConsistOf("foo", "bar", "baz", "qux"))
  1586  				})
  1587  			})
  1588  		})
  1589  
  1590  		When("the service does not exist", func() {
  1591  			BeforeEach(func() {
  1592  				fakeCloudControllerClient.GetServicesReturns(
  1593  					[]ccv2.Service{},
  1594  					nil, nil)
  1595  			})
  1596  
  1597  			It("returns service not found error", func() {
  1598  				Expect(fakeCloudControllerClient.GetServicesCallCount()).To(Equal(1))
  1599  				Expect(disablePlanForOrgErr).To(MatchError(actionerror.ServiceNotFoundError{Name: "service-1"}))
  1600  
  1601  				filters := fakeCloudControllerClient.GetServicesArgsForCall(0)
  1602  				Expect(len(filters)).To(Equal(2))
  1603  				Expect(filters).To(ConsistOf(
  1604  					ccv2.Filter{
  1605  						Type:     constant.LabelFilter,
  1606  						Operator: constant.EqualOperator,
  1607  						Values:   []string{"service-1"},
  1608  					},
  1609  					ccv2.Filter{
  1610  						Type:     constant.ServiceBrokerGUIDFilter,
  1611  						Operator: constant.EqualOperator,
  1612  						Values:   []string{"broker-guid"},
  1613  					}))
  1614  			})
  1615  		})
  1616  
  1617  		When("the service plan does not exist", func() {
  1618  			BeforeEach(func() {
  1619  				fakeCloudControllerClient.GetServicesReturns(
  1620  					[]ccv2.Service{{Label: "service-1", GUID: "service-guid-1"}},
  1621  					ccv2.Warnings{"foo"},
  1622  					nil)
  1623  				fakeCloudControllerClient.GetServicePlansReturns([]ccv2.ServicePlan{}, []string{"warning-1", "warning-2"}, nil)
  1624  				fakeCloudControllerClient.GetOrganizationsReturns(
  1625  					[]ccv2.Organization{{Name: "my-org", GUID: "org-guid-1"}},
  1626  					ccv2.Warnings{"bar"},
  1627  					nil)
  1628  			})
  1629  
  1630  			It("returns not found error", func() {
  1631  				Expect(disablePlanForOrgErr).To(MatchError(actionerror.ServicePlanNotFoundError{PlanName: "service-plan-1", OfferingName: "service-1"}))
  1632  			})
  1633  
  1634  			It("returns all warnings", func() {
  1635  				Expect(disablePlanForOrgWarnings).To(ConsistOf([]string{"foo", "warning-1", "warning-2", "bar"}))
  1636  			})
  1637  		})
  1638  
  1639  		When("the specified org does not exist", func() {
  1640  			BeforeEach(func() {
  1641  				fakeCloudControllerClient.GetServicesReturns(
  1642  					[]ccv2.Service{{Label: "service-1", GUID: "service-guid-1"}},
  1643  					nil, nil)
  1644  				fakeCloudControllerClient.GetOrganizationsReturns([]ccv2.Organization{}, nil, nil)
  1645  			})
  1646  
  1647  			It("returns an organization not found error", func() {
  1648  				Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1))
  1649  				Expect(disablePlanForOrgErr).To(MatchError(actionerror.OrganizationNotFoundError{Name: "my-org"}))
  1650  				filters := fakeCloudControllerClient.GetOrganizationsArgsForCall(0)
  1651  				Expect(len(filters)).To(Equal(1))
  1652  				Expect(filters[0]).To(Equal(ccv2.Filter{
  1653  					Type:     constant.NameFilter,
  1654  					Operator: constant.EqualOperator,
  1655  					Values:   []string{"my-org"},
  1656  				}))
  1657  			})
  1658  		})
  1659  
  1660  		When("getting services fails", func() {
  1661  			BeforeEach(func() {
  1662  				fakeCloudControllerClient.GetServicesReturns(
  1663  					[]ccv2.Service{},
  1664  					ccv2.Warnings{"foo", "bar"},
  1665  					errors.New("this is very bad"))
  1666  			})
  1667  
  1668  			It("returns the error", func() {
  1669  				Expect(disablePlanForOrgErr).To(MatchError(errors.New("this is very bad")))
  1670  			})
  1671  
  1672  			It("returns all warnings", func() {
  1673  				Expect(disablePlanForOrgWarnings).To(ConsistOf("foo", "bar"))
  1674  			})
  1675  		})
  1676  
  1677  		When("getting organizations fails", func() {
  1678  			BeforeEach(func() {
  1679  				fakeCloudControllerClient.GetServicesReturns(
  1680  					[]ccv2.Service{{Label: "service-1", GUID: "service-guid-1"}},
  1681  					ccv2.Warnings{"foo"},
  1682  					nil)
  1683  				fakeCloudControllerClient.GetOrganizationsReturns(
  1684  					[]ccv2.Organization{{Name: "my-org", GUID: "org-guid-1"}},
  1685  					ccv2.Warnings{"bar"},
  1686  					errors.New("this is very bad"))
  1687  			})
  1688  
  1689  			It("returns the error", func() {
  1690  				Expect(disablePlanForOrgErr).To(MatchError(errors.New("this is very bad")))
  1691  			})
  1692  
  1693  			It("returns all warnings", func() {
  1694  				Expect(disablePlanForOrgWarnings).To(ConsistOf("foo", "bar"))
  1695  			})
  1696  		})
  1697  
  1698  		When("getting service plans fails", func() {
  1699  			BeforeEach(func() {
  1700  				fakeCloudControllerClient.GetServicesReturns(
  1701  					[]ccv2.Service{{Label: "service-1", GUID: "service-guid-1"}},
  1702  					ccv2.Warnings{"foo"},
  1703  					nil)
  1704  				fakeCloudControllerClient.GetServicePlansReturns(
  1705  					[]ccv2.ServicePlan{},
  1706  					ccv2.Warnings{"bar"},
  1707  					errors.New("this is very bad"))
  1708  			})
  1709  
  1710  			It("returns the error", func() {
  1711  				Expect(disablePlanForOrgErr).To(MatchError(errors.New("this is very bad")))
  1712  			})
  1713  
  1714  			It("returns all warnings", func() {
  1715  				Expect(disablePlanForOrgWarnings).To(ConsistOf("foo", "bar"))
  1716  			})
  1717  		})
  1718  	})
  1719  })