github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/cf/api/service_plan_test.go (about)

     1  package api_test
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"time"
     7  
     8  	"code.cloudfoundry.org/cli/cf/api/apifakes"
     9  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    10  	"code.cloudfoundry.org/cli/cf/models"
    11  	"code.cloudfoundry.org/cli/cf/net"
    12  	"code.cloudfoundry.org/cli/cf/terminal/terminalfakes"
    13  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    14  	testnet "code.cloudfoundry.org/cli/util/testhelpers/net"
    15  
    16  	. "code.cloudfoundry.org/cli/cf/api"
    17  	"code.cloudfoundry.org/cli/cf/trace/tracefakes"
    18  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    19  	. "github.com/onsi/ginkgo"
    20  	. "github.com/onsi/gomega"
    21  )
    22  
    23  var _ = Describe("Service Plan Repository", func() {
    24  	var (
    25  		testServer  *httptest.Server
    26  		testHandler *testnet.TestHandler
    27  		configRepo  coreconfig.ReadWriter
    28  		repo        CloudControllerServicePlanRepository
    29  	)
    30  
    31  	BeforeEach(func() {
    32  		configRepo = testconfig.NewRepositoryWithDefaults()
    33  		gateway := net.NewCloudControllerGateway(configRepo, time.Now, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), "")
    34  		repo = NewCloudControllerServicePlanRepository(configRepo, gateway)
    35  	})
    36  
    37  	AfterEach(func() {
    38  		testServer.Close()
    39  	})
    40  
    41  	setupTestServer := func(reqs ...testnet.TestRequest) {
    42  		testServer, testHandler = testnet.NewServer(reqs)
    43  		configRepo.SetAPIEndpoint(testServer.URL)
    44  	}
    45  
    46  	Describe(".Search", func() {
    47  		Context("No query parameters", func() {
    48  			BeforeEach(func() {
    49  				setupTestServer(firstPlanRequest, secondPlanRequest)
    50  			})
    51  
    52  			It("returns service plans", func() {
    53  				servicePlansFields, err := repo.Search(map[string]string{})
    54  
    55  				Expect(err).NotTo(HaveOccurred())
    56  				Expect(testHandler).To(HaveAllRequestsCalled())
    57  				Expect(len(servicePlansFields)).To(Equal(2))
    58  				Expect(servicePlansFields[0].Name).To(Equal("The big one"))
    59  				Expect(servicePlansFields[0].GUID).To(Equal("the-big-guid"))
    60  				Expect(servicePlansFields[0].Free).To(BeTrue())
    61  				Expect(servicePlansFields[0].Public).To(BeTrue())
    62  				Expect(servicePlansFields[0].Active).To(BeTrue())
    63  				Expect(servicePlansFields[1].Name).To(Equal("The small second"))
    64  				Expect(servicePlansFields[1].GUID).To(Equal("the-small-second"))
    65  				Expect(servicePlansFields[1].Free).To(BeTrue())
    66  				Expect(servicePlansFields[1].Public).To(BeFalse())
    67  				Expect(servicePlansFields[1].Active).To(BeFalse())
    68  			})
    69  		})
    70  		Context("With query parameters", func() {
    71  			BeforeEach(func() {
    72  				setupTestServer(firstPlanRequestWithParams, secondPlanRequestWithParams)
    73  			})
    74  
    75  			It("returns service plans", func() {
    76  				servicePlansFields, err := repo.Search(map[string]string{"service_guid": "Foo"})
    77  
    78  				Expect(err).NotTo(HaveOccurred())
    79  				Expect(testHandler).To(HaveAllRequestsCalled())
    80  				Expect(len(servicePlansFields)).To(Equal(2))
    81  				Expect(servicePlansFields[0].Name).To(Equal("The big one"))
    82  				Expect(servicePlansFields[0].GUID).To(Equal("the-big-guid"))
    83  				Expect(servicePlansFields[0].Free).To(BeTrue())
    84  				Expect(servicePlansFields[0].Public).To(BeTrue())
    85  				Expect(servicePlansFields[0].Active).To(BeTrue())
    86  				Expect(servicePlansFields[1].Name).To(Equal("The small second"))
    87  				Expect(servicePlansFields[1].GUID).To(Equal("the-small-second"))
    88  				Expect(servicePlansFields[1].Free).To(BeTrue())
    89  				Expect(servicePlansFields[1].Public).To(BeFalse())
    90  				Expect(servicePlansFields[1].Active).To(BeFalse())
    91  			})
    92  		})
    93  	})
    94  
    95  	Describe(".Update", func() {
    96  		BeforeEach(func() {
    97  			setupTestServer(apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
    98  				Method:   "PUT",
    99  				Path:     "/v2/service_plans/my-service-plan-guid",
   100  				Matcher:  testnet.RequestBodyMatcher(`{"public":true}`),
   101  				Response: testnet.TestResponse{Status: http.StatusCreated},
   102  			}))
   103  		})
   104  
   105  		It("updates public on the service to whatever is passed", func() {
   106  			servicePlan := models.ServicePlanFields{
   107  				Name:        "my-service-plan",
   108  				GUID:        "my-service-plan-guid",
   109  				Description: "descriptive text",
   110  				Free:        true,
   111  				Public:      false,
   112  			}
   113  
   114  			err := repo.Update(servicePlan, "service-guid", true)
   115  			Expect(testHandler).To(HaveAllRequestsCalled())
   116  			Expect(err).NotTo(HaveOccurred())
   117  		})
   118  	})
   119  
   120  	Describe(".ListPlansFromManyServices", func() {
   121  		BeforeEach(func() {
   122  			setupTestServer(manyServiceRequest1, manyServiceRequest2)
   123  		})
   124  
   125  		It("returns all service plans for a list of service guids", func() {
   126  			serviceGUIDs := []string{"service-guid1", "service-guid2"}
   127  
   128  			servicePlansFields, err := repo.ListPlansFromManyServices(serviceGUIDs)
   129  			Expect(err).NotTo(HaveOccurred())
   130  
   131  			Expect(testHandler).To(HaveAllRequestsCalled())
   132  			Expect(len(servicePlansFields)).To(Equal(2))
   133  
   134  			Expect(servicePlansFields[0].Name).To(Equal("plan one"))
   135  			Expect(servicePlansFields[0].GUID).To(Equal("plan1"))
   136  
   137  			Expect(servicePlansFields[1].Name).To(Equal("plan two"))
   138  			Expect(servicePlansFields[1].GUID).To(Equal("plan2"))
   139  		})
   140  	})
   141  })
   142  
   143  var firstPlanRequest = apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
   144  	Method: "GET",
   145  	Path:   "/v2/service_plans",
   146  	Response: testnet.TestResponse{
   147  		Status: http.StatusOK,
   148  		Body: `{
   149    "total_results": 2,
   150    "total_pages": 2,
   151    "next_url": "/v2/service_plans?page=2",
   152    "resources": [
   153      {
   154        "metadata": {
   155          "guid": "the-big-guid"
   156        },
   157        "entity": {
   158          "name": "The big one",
   159          "free": true,
   160          "public": true,
   161          "active": true
   162        }
   163      }
   164    ]
   165  }`,
   166  	},
   167  })
   168  
   169  var secondPlanRequest = apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
   170  	Method: "GET",
   171  	Path:   "/v2/service_plans?page=2",
   172  	Response: testnet.TestResponse{
   173  		Status: http.StatusOK,
   174  		Body: `{
   175    "total_results": 2,
   176    "total_pages": 2,
   177    "resources": [
   178      {
   179        "metadata": {
   180          "guid": "the-small-second"
   181        },
   182        "entity": {
   183          "name": "The small second",
   184          "free": true,
   185          "public": false,
   186          "active": false
   187        }
   188      }
   189    ]
   190  }`,
   191  	},
   192  })
   193  
   194  var firstPlanRequestWithParams = apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
   195  	Method: "GET",
   196  	Path:   "/v2/service_plans?q=service_guid%3AFoo",
   197  	Response: testnet.TestResponse{
   198  		Status: http.StatusOK,
   199  		Body: `{
   200    "total_results": 2,
   201    "total_pages": 2,
   202    "next_url": "/v2/service_plans?q=service_guid%3AFoo&page=2",
   203    "resources": [
   204      {
   205        "metadata": {
   206          "guid": "the-big-guid"
   207        },
   208        "entity": {
   209          "name": "The big one",
   210          "free": true,
   211          "public": true,
   212          "active": true
   213        }
   214      }
   215    ]
   216  }`,
   217  	},
   218  })
   219  
   220  var secondPlanRequestWithParams = apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
   221  	Method: "GET",
   222  	Path:   "/v2/service_plans?q=service_guid%3AFoo&page=2",
   223  	Response: testnet.TestResponse{
   224  		Status: http.StatusOK,
   225  		Body: `{
   226    "total_results": 2,
   227    "total_pages": 2,
   228    "resources": [
   229      {
   230        "metadata": {
   231          "guid": "the-small-second"
   232        },
   233        "entity": {
   234          "name": "The small second",
   235          "free": true,
   236          "public": false,
   237          "active": false
   238        }
   239      }
   240    ]
   241  }`,
   242  	},
   243  })
   244  
   245  var manyServiceRequest1 = apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
   246  	Method: "GET",
   247  	Path:   "/v2/service_plans?q=service_guid+IN+service-guid1,service-guid2",
   248  	Response: testnet.TestResponse{
   249  		Status: http.StatusOK,
   250  		Body: `{
   251    "total_results": 2,
   252    "total_pages": 2,
   253    "next_url": "/v2/service_plans?q=service_guid+IN+service-guid1,service-guid2&page=2",
   254    "resources": [
   255      {
   256        "metadata": {
   257          "guid": "plan1"
   258        },
   259        "entity": {
   260          "name": "plan one",
   261          "free": true,
   262          "public": true,
   263          "active": true
   264        }
   265      }
   266    ]
   267  }`,
   268  	},
   269  })
   270  
   271  var manyServiceRequest2 = apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
   272  	Method: "GET",
   273  	Path:   "/v2/service_plans?q=service_guid+IN+service-guid1,service-guid2&page=2",
   274  	Response: testnet.TestResponse{
   275  		Status: http.StatusOK,
   276  		Body: `{
   277    "total_results": 2,
   278    "total_pages": 1,
   279    "next_url": null,
   280    "prev_url": "/v2/service_plans?q=service_guid+IN+service-guid1,service-guid2",
   281    "resources": [
   282      {
   283        "metadata": {
   284          "guid": "plan2"
   285        },
   286        "entity": {
   287          "name": "plan two",
   288          "free": true,
   289          "public": true,
   290          "active": true
   291        }
   292      }
   293    ]
   294  }`,
   295  	},
   296  })