func (s *ConsumptionSuite) TestServiceInstanceStatusHandler(c *gocheck.C) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) w.Write([]byte(`Service instance "my_nosql" is up`)) })) defer ts.Close() srv := service.Service{Name: "mongodb", OwnerTeams: []string{s.team.Name}, Endpoint: map[string]string{"production": ts.URL}} err := srv.Create() c.Assert(err, gocheck.IsNil) defer srv.Delete() si := service.ServiceInstance{Name: "my_nosql", ServiceName: srv.Name, Teams: []string{s.team.Name}} err = si.Create() c.Assert(err, gocheck.IsNil) defer service.DeleteInstance(&si) recorder, request := makeRequestToStatusHandler("my_nosql", c) err = serviceInstanceStatus(recorder, request, s.token) c.Assert(err, gocheck.IsNil) b, err := ioutil.ReadAll(recorder.Body) c.Assert(string(b), gocheck.Equals, "Service instance \"my_nosql\" is up") action := testing.Action{ Action: "service-instance-status", User: s.user.Email, Extra: []interface{}{"my_nosql"}, } c.Assert(action, testing.IsRecorded) }
func (s *ConsumptionSuite) TestServicePlansHandler(c *gocheck.C) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { content := `[{"name": "ignite", "description": "some value"}, {"name": "small", "description": "not space left for you"}]` w.Write([]byte(content)) })) defer ts.Close() srvc := service.Service{Name: "mysql", Endpoint: map[string]string{"production": ts.URL}} err := srvc.Create() c.Assert(err, gocheck.IsNil) defer srvc.Delete() request, err := http.NewRequest("GET", "/services/mysql/plans?:name=mysql", nil) c.Assert(err, gocheck.IsNil) recorder := httptest.NewRecorder() err = servicePlans(recorder, request, s.token) c.Assert(err, gocheck.IsNil) body, err := ioutil.ReadAll(recorder.Body) c.Assert(err, gocheck.IsNil) var plans []service.Plan err = json.Unmarshal(body, &plans) c.Assert(err, gocheck.IsNil) expected := []service.Plan{ {Name: "ignite", Description: "some value"}, {Name: "small", Description: "not space left for you"}, } c.Assert(plans, gocheck.DeepEquals, expected) action := testing.Action{ Action: "service-plans", User: s.user.Email, Extra: []interface{}{"mysql"}, } c.Assert(action, testing.IsRecorded) }
func (s *ProvisionSuite) TestgetServiceByOwner(c *gocheck.C) { srv := service.Service{Name: "foo", OwnerTeams: []string{s.team.Name}} err := srv.Create() c.Assert(err, gocheck.IsNil) defer srv.Delete() rSrv, err := getServiceByOwner("foo", s.user) c.Assert(err, gocheck.IsNil) c.Assert(rSrv.Name, gocheck.Equals, srv.Name) }
func (s *ConsumptionSuite) TestServiceAndServiceInstancesByTeamsShouldReturnServiceInstancesByTeam(c *gocheck.C) { srv := service.Service{Name: "mongodb"} srv.Create() defer srv.Delete() si := service.ServiceInstance{Name: "my_nosql", ServiceName: srv.Name, Teams: []string{s.team.Name}} si.Create() defer service.DeleteInstance(&si) si2 := service.ServiceInstance{Name: "some_nosql", ServiceName: srv.Name} si2.Create() defer service.DeleteInstance(&si2) obtained := serviceAndServiceInstancesByTeams(s.user) expected := []service.ServiceModel{ {Service: "mongodb", Instances: []string{"my_nosql"}}, } c.Assert(obtained, gocheck.DeepEquals, expected) }
func (s *ConsumptionSuite) TestServiceInstanceStatusHandlerShouldReturnForbiddenWhenUserDontHaveAccess(c *gocheck.C) { srv := service.Service{Name: "mongodb", OwnerTeams: []string{s.team.Name}} err := srv.Create() c.Assert(err, gocheck.IsNil) defer srv.Delete() si := service.ServiceInstance{Name: "my_nosql", ServiceName: srv.Name} err = si.Create() c.Assert(err, gocheck.IsNil) defer service.DeleteInstance(&si) recorder, request := makeRequestToStatusHandler("my_nosql", c) err = serviceInstanceStatus(recorder, request, s.token) c.Assert(err, gocheck.NotNil) e, ok := err.(*errors.Http) c.Assert(ok, gocheck.Equals, true) c.Assert(e.Code, gocheck.Equals, http.StatusForbidden) }
func (s *ConsumptionSuite) TestServiceInfoHandler(c *gocheck.C) { srv := service.Service{Name: "mongodb", Teams: []string{s.team.Name}} err := srv.Create() c.Assert(err, gocheck.IsNil) defer srv.Delete() si1 := service.ServiceInstance{ Name: "my_nosql", ServiceName: srv.Name, Apps: []string{}, Teams: []string{s.team.Name}, } err = si1.Create() c.Assert(err, gocheck.IsNil) defer service.DeleteInstance(&si1) si2 := service.ServiceInstance{ Name: "your_nosql", ServiceName: srv.Name, Apps: []string{"wordpress"}, Teams: []string{s.team.Name}, } err = si2.Create() c.Assert(err, gocheck.IsNil) defer service.DeleteInstance(&si2) request, err := http.NewRequest("GET", "/services/mongodb?:name=mongodb", nil) c.Assert(err, gocheck.IsNil) recorder := httptest.NewRecorder() err = serviceInfo(recorder, request, s.token) c.Assert(err, gocheck.IsNil) body, err := ioutil.ReadAll(recorder.Body) c.Assert(err, gocheck.IsNil) var instances []service.ServiceInstance err = json.Unmarshal(body, &instances) c.Assert(err, gocheck.IsNil) expected := []service.ServiceInstance{si1, si2} c.Assert(instances, gocheck.DeepEquals, expected) action := testing.Action{ Action: "service-info", User: s.user.Email, Extra: []interface{}{"mongodb"}, } c.Assert(action, testing.IsRecorded) }
func (s *ProvisionSuite) TestServicesAndInstancesByOwnerTeams(c *gocheck.C) { srvc := service.Service{Name: "mysql", OwnerTeams: []string{s.team.Name}} err := srvc.Create() c.Assert(err, gocheck.IsNil) defer srvc.Delete() srvc2 := service.Service{Name: "mongodb"} err = srvc2.Create() c.Assert(err, gocheck.IsNil) defer srvc2.Delete() sInstance := service.ServiceInstance{Name: "foo", ServiceName: "mysql"} err = sInstance.Create() c.Assert(err, gocheck.IsNil) defer service.DeleteInstance(&sInstance) sInstance2 := service.ServiceInstance{Name: "bar", ServiceName: "mongodb"} err = sInstance2.Create() defer service.DeleteInstance(&sInstance2) results := servicesAndInstancesByOwner(s.user) expected := []service.ServiceModel{ {Service: "mysql", Instances: []string{"foo"}}, } c.Assert(results, gocheck.DeepEquals, expected) }
func (s *ConsumptionSuite) TestServiceInfoHandlerShouldReturnOnlyInstancesOfTheSameTeamOfTheUser(c *gocheck.C) { srv := service.Service{Name: "mongodb", Teams: []string{s.team.Name}} err := srv.Create() c.Assert(err, gocheck.IsNil) defer srv.Delete() si1 := service.ServiceInstance{ Name: "my_nosql", ServiceName: srv.Name, Apps: []string{}, Teams: []string{s.team.Name}, } err = si1.Create() c.Assert(err, gocheck.IsNil) defer service.DeleteInstance(&si1) si2 := service.ServiceInstance{ Name: "your_nosql", ServiceName: srv.Name, Apps: []string{"wordpress"}, Teams: []string{}, } err = si2.Create() c.Assert(err, gocheck.IsNil) defer service.DeleteInstance(&si2) request, err := http.NewRequest("GET", fmt.Sprintf("/services/%s?:name=%s", "mongodb", "mongodb"), nil) c.Assert(err, gocheck.IsNil) recorder := httptest.NewRecorder() err = ServiceInfoHandler(recorder, request, s.user) c.Assert(err, gocheck.IsNil) body, err := ioutil.ReadAll(recorder.Body) c.Assert(err, gocheck.IsNil) var instances []service.ServiceInstance err = json.Unmarshal(body, &instances) c.Assert(err, gocheck.IsNil) expected := []service.ServiceInstance{si1} c.Assert(instances, gocheck.DeepEquals, expected) }