github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/actors/services_test.go (about)

     1  package actors_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/actors"
     5  	"code.cloudfoundry.org/cli/cf/actors/brokerbuilder/brokerbuilderfakes"
     6  	"code.cloudfoundry.org/cli/cf/actors/servicebuilder/servicebuilderfakes"
     7  	"code.cloudfoundry.org/cli/cf/api/organizations/organizationsfakes"
     8  	"code.cloudfoundry.org/cli/cf/errors"
     9  	"code.cloudfoundry.org/cli/cf/models"
    10  
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("Services", func() {
    16  	var (
    17  		actor          actors.ServiceActor
    18  		brokerBuilder  *brokerbuilderfakes.FakeBrokerBuilder
    19  		serviceBuilder *servicebuilderfakes.FakeServiceBuilder
    20  		orgRepo        *organizationsfakes.FakeOrganizationRepository
    21  		serviceBroker1 models.ServiceBroker
    22  		service1       models.ServiceOffering
    23  	)
    24  
    25  	BeforeEach(func() {
    26  		orgRepo = new(organizationsfakes.FakeOrganizationRepository)
    27  		brokerBuilder = new(brokerbuilderfakes.FakeBrokerBuilder)
    28  		serviceBuilder = new(servicebuilderfakes.FakeServiceBuilder)
    29  
    30  		actor = actors.NewServiceHandler(orgRepo, brokerBuilder, serviceBuilder)
    31  
    32  		serviceBroker1 = models.ServiceBroker{GUID: "my-service-broker-guid1", Name: "my-service-broker1"}
    33  
    34  		service1 = models.ServiceOffering{ServiceOfferingFields: models.ServiceOfferingFields{
    35  			Label:      "my-service1",
    36  			GUID:       "service-guid1",
    37  			BrokerGUID: "my-service-broker-guid1"},
    38  		}
    39  
    40  		org1 := models.Organization{}
    41  		org1.Name = "org1"
    42  		org1.GUID = "org-guid"
    43  
    44  		org2 := models.Organization{}
    45  		org2.Name = "org2"
    46  		org2.GUID = "org2-guid"
    47  	})
    48  
    49  	Describe("FilterBrokers", func() {
    50  		Context("when no flags are passed", func() {
    51  			It("returns all brokers", func() {
    52  				returnedBrokers := []models.ServiceBroker{serviceBroker1}
    53  				brokerBuilder.GetAllServiceBrokersReturns(returnedBrokers, nil)
    54  
    55  				brokers, err := actor.FilterBrokers("", "", "")
    56  				Expect(err).NotTo(HaveOccurred())
    57  
    58  				Expect(len(brokers)).To(Equal(1))
    59  			})
    60  		})
    61  
    62  		Context("when the -b flag is passed", func() {
    63  			It("returns a single broker contained in a slice with all dependencies populated", func() {
    64  				returnedBroker := serviceBroker1
    65  				brokerBuilder.GetBrokerWithAllServicesReturns(returnedBroker, nil)
    66  
    67  				brokers, err := actor.FilterBrokers("my-service-broker1", "", "")
    68  				Expect(err).NotTo(HaveOccurred())
    69  
    70  				Expect(len(brokers)).To(Equal(1))
    71  			})
    72  		})
    73  
    74  		Context("when the -e flag is passed", func() {
    75  			It("returns a single broker containing a single service", func() {
    76  				serviceBroker1.Services = []models.ServiceOffering{service1}
    77  				returnedBroker := serviceBroker1
    78  				brokerBuilder.GetBrokerWithSpecifiedServiceReturns(returnedBroker, nil)
    79  
    80  				brokers, err := actor.FilterBrokers("", "my-service1", "")
    81  				Expect(err).NotTo(HaveOccurred())
    82  
    83  				Expect(len(brokers)).To(Equal(1))
    84  				Expect(len(brokers[0].Services)).To(Equal(1))
    85  
    86  				Expect(brokers[0].Services[0].GUID).To(Equal("service-guid1"))
    87  			})
    88  		})
    89  
    90  		Context("when the -o flag is passed", func() {
    91  			It("returns an error if the org does not actually exist", func() {
    92  				orgRepo.FindByNameReturns(models.Organization{}, errors.NewModelNotFoundError("organization", "org-that-shall-not-be-found"))
    93  				_, err := actor.FilterBrokers("", "", "org-that-shall-not-be-found")
    94  
    95  				Expect(err).To(HaveOccurred())
    96  			})
    97  
    98  			It("returns a slice of brokers containing Services/Service Plans visible to the org", func() {
    99  				serviceBroker1.Services = []models.ServiceOffering{service1}
   100  				returnedBroker := []models.ServiceBroker{serviceBroker1}
   101  
   102  				serviceBuilder.GetServicesVisibleToOrgReturns([]models.ServiceOffering{service1}, nil)
   103  				brokerBuilder.AttachBrokersToServicesReturns(returnedBroker, nil)
   104  
   105  				brokers, err := actor.FilterBrokers("", "", "org1")
   106  				Expect(err).NotTo(HaveOccurred())
   107  
   108  				orgName := serviceBuilder.GetServicesVisibleToOrgArgsForCall(0)
   109  				Expect(orgName).To(Equal("org1"))
   110  
   111  				Expect(len(brokers)).To(Equal(1))
   112  				Expect(len(brokers[0].Services)).To(Equal(1))
   113  				Expect(brokers[0].Services[0].GUID).To(Equal("service-guid1"))
   114  			})
   115  		})
   116  
   117  		Context("when the -b AND the -e flags are passed", func() {
   118  			It("returns the intersection set", func() {
   119  				serviceBroker1.Services = []models.ServiceOffering{service1}
   120  				returnedBroker := serviceBroker1
   121  				brokerBuilder.GetBrokerWithSpecifiedServiceReturns(returnedBroker, nil)
   122  
   123  				brokers, err := actor.FilterBrokers("my-service-broker1", "my-service1", "")
   124  				Expect(err).NotTo(HaveOccurred())
   125  
   126  				Expect(len(brokers)).To(Equal(1))
   127  				Expect(len(brokers[0].Services)).To(Equal(1))
   128  
   129  				Expect(brokers[0].Services[0].Label).To(Equal("my-service1"))
   130  				Expect(brokers[0].Services[0].GUID).To(Equal("service-guid1"))
   131  			})
   132  
   133  			Context("when the -b AND -e intersection is the empty set", func() {
   134  				It("returns an empty set", func() {
   135  					brokerBuilder.GetBrokerWithSpecifiedServiceReturns(models.ServiceBroker{}, nil)
   136  					brokers, err := actor.FilterBrokers("my-service-broker", "my-service2", "")
   137  
   138  					Expect(len(brokers)).To(Equal(0))
   139  					Expect(err).To(BeNil())
   140  				})
   141  			})
   142  		})
   143  
   144  		Context("when the -b AND the -o flags are passed", func() {
   145  			It("returns the intersection set", func() {
   146  				serviceBroker1.Services = []models.ServiceOffering{service1}
   147  				returnedBroker := serviceBroker1
   148  
   149  				serviceBuilder.GetServiceVisibleToOrgReturns(service1, nil)
   150  				brokerBuilder.AttachSpecificBrokerToServicesReturns(returnedBroker, nil)
   151  
   152  				brokers, err := actor.FilterBrokers("my-service-broker", "", "org1")
   153  				Expect(err).NotTo(HaveOccurred())
   154  
   155  				Expect(len(brokers)).To(Equal(1))
   156  				Expect(len(brokers[0].Services)).To(Equal(1))
   157  
   158  				Expect(brokers[0].Services[0].Label).To(Equal("my-service1"))
   159  				Expect(brokers[0].Services[0].GUID).To(Equal("service-guid1"))
   160  			})
   161  		})
   162  
   163  		Context("when the -e AND the -o flags are passed", func() {
   164  			It("returns the intersection set", func() {
   165  				serviceBroker1.Services = []models.ServiceOffering{service1}
   166  				returnedBrokers := []models.ServiceBroker{serviceBroker1}
   167  
   168  				serviceBuilder.GetServicesVisibleToOrgReturns([]models.ServiceOffering{service1}, nil)
   169  				brokerBuilder.AttachBrokersToServicesReturns(returnedBrokers, nil)
   170  
   171  				brokers, err := actor.FilterBrokers("", "my-service1", "org1")
   172  				Expect(err).NotTo(HaveOccurred())
   173  
   174  				Expect(len(brokers)).To(Equal(1))
   175  				Expect(len(brokers[0].Services)).To(Equal(1))
   176  
   177  				Expect(brokers[0].Services[0].Label).To(Equal("my-service1"))
   178  				Expect(brokers[0].Services[0].GUID).To(Equal("service-guid1"))
   179  			})
   180  		})
   181  
   182  		Context("when the -b AND -e AND the -o flags are passed", func() {
   183  			It("returns the intersection set", func() {
   184  				serviceBroker1.Services = []models.ServiceOffering{service1}
   185  				returnedBroker := serviceBroker1
   186  
   187  				serviceBuilder.GetServicesVisibleToOrgReturns([]models.ServiceOffering{service1}, nil)
   188  				brokerBuilder.AttachSpecificBrokerToServicesReturns(returnedBroker, nil)
   189  
   190  				brokers, err := actor.FilterBrokers("my-service-broker1", "my-service1", "org1")
   191  				Expect(err).NotTo(HaveOccurred())
   192  
   193  				Expect(len(brokers)).To(Equal(1))
   194  				Expect(len(brokers[0].Services)).To(Equal(1))
   195  
   196  				Expect(brokers[0].Services[0].Label).To(Equal("my-service1"))
   197  				Expect(brokers[0].Services[0].GUID).To(Equal("service-guid1"))
   198  			})
   199  		})
   200  	})
   201  })