func (s *ConsumptionSuite) TestGrantRevokeServiceToTeamWithManyInstanceName(c *check.C) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("{'AA': 2}"))
	}))
	defer ts.Close()
	se := []service.Service{
		{Name: "go", Endpoint: map[string]string{"production": ts.URL}},
		{Name: "go2", Endpoint: map[string]string{"production": ts.URL}},
	}
	for _, service := range se {
		err := service.Create()
		c.Assert(err, check.IsNil)
		defer s.conn.Services().Remove(bson.M{"_id": service.Name})
	}
	si := service.ServiceInstance{Name: "si-test", ServiceName: se[0].Name, Teams: []string{s.team.Name}}
	err := si.Create()
	c.Assert(err, check.IsNil)
	defer service.DeleteInstance(&si)
	si2 := service.ServiceInstance{Name: "si-test", ServiceName: se[1].Name, Teams: []string{s.team.Name}}
	err = si2.Create()
	c.Assert(err, check.IsNil)
	defer service.DeleteInstance(&si2)
	team := auth.Team{Name: "test"}
	s.conn.Teams().Insert(team)
	defer s.conn.Teams().Remove(bson.M{"name": team.Name})
	url := fmt.Sprintf("/services/%s/instances/permission/%s/%s?:instance=%s&:team=%s&:service=%s", si2.ServiceName, si2.Name,
		team.Name, si2.Name, team.Name, si2.ServiceName)
	request, err := http.NewRequest("PUT", url, nil)
	c.Assert(err, check.IsNil)
	recorder := httptest.NewRecorder()
	err = serviceInstanceGrantTeam(recorder, request, s.token)
	c.Assert(err, check.IsNil)
	sinst, err := service.GetServiceInstance(si2.ServiceName, si2.Name)
	c.Assert(err, check.IsNil)
	c.Assert(sinst.Teams, check.DeepEquals, []string{s.team.Name, team.Name})
	sinst, err = service.GetServiceInstance(si.ServiceName, si.Name)
	c.Assert(err, check.IsNil)
	c.Assert(sinst.Teams, check.DeepEquals, []string{s.team.Name})
	request, err = http.NewRequest("DELETE", url, nil)
	c.Assert(err, check.IsNil)
	err = serviceInstanceRevokeTeam(recorder, request, s.token)
	c.Assert(err, check.IsNil)
	sinst, err = service.GetServiceInstance(si2.ServiceName, si2.Name)
	c.Assert(err, check.IsNil)
	c.Assert(sinst.Teams, check.DeepEquals, []string{s.team.Name})
}
func (s *ConsumptionSuite) TestRemoveServiceInstanceWithSameInstaceName(c *check.C) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
	}))
	defer ts.Close()
	services := []service.Service{
		{Name: "foo", Endpoint: map[string]string{"production": ts.URL}},
		{Name: "foo2", Endpoint: map[string]string{"production": ts.URL}},
	}
	for _, service := range services {
		err := service.Create()
		c.Assert(err, check.IsNil)
		defer s.conn.Services().Remove(bson.M{"_id": service.Name})
	}
	p := app.Platform{Name: "zend"}
	s.conn.Platforms().Insert(p)
	s.pool = "test1"
	opts := provision.AddPoolOptions{Name: "test1", Default: true}
	err := provision.AddPool(opts)
	c.Assert(err, check.IsNil)
	a := app.App{
		Name:      "app-instance",
		Platform:  "zend",
		TeamOwner: s.team.Name,
	}
	err = app.CreateApp(&a, s.user)
	c.Assert(err, check.IsNil)
	units, _ := s.provisioner.AddUnits(&a, 1, "web", nil)
	si := []service.ServiceInstance{
		{
			Name:        "foo-instance",
			ServiceName: "foo",
			Teams:       []string{s.team.Name},
			Apps:        []string{"app-instance"},
			Units:       []string{units[0].ID},
		},
		{
			Name:        "foo-instance",
			ServiceName: "foo2",
			Teams:       []string{s.team.Name},
			Apps:        []string{},
		},
	}
	for _, instance := range si {
		err = instance.Create()
		c.Assert(err, check.IsNil)
	}
	recorder, request := makeRequestToRemoveInstanceHandler("foo2", "foo-instance", c)
	err = removeServiceInstance(recorder, request, s.token)
	c.Assert(err, check.IsNil)
	b, err := ioutil.ReadAll(recorder.Body)
	c.Assert(err, check.IsNil)
	expected := ""
	expected += `{"Message":"service instance successfuly removed"}` + "\n"
	c.Assert(string(b), check.Equals, expected)
	var result []service.ServiceInstance
	n, err := s.conn.ServiceInstances().Find(bson.M{"name": "foo-instance", "service_name": "foo2"}).Count()
	c.Assert(err, check.IsNil)
	c.Assert(n, check.Equals, 0)
	err = s.conn.ServiceInstances().Find(bson.M{"name": "foo-instance", "service_name": "foo"}).All(&result)
	c.Assert(err, check.IsNil)
	c.Assert(len(result), check.Equals, 1)
	c.Assert(result[0].Apps, check.DeepEquals, []string{"app-instance"})
	recorder, request = makeRequestToRemoveInstanceHandlerWithUnbind("foo", "foo-instance", c)
	err = removeServiceInstance(recorder, request, s.token)
	c.Assert(err, check.IsNil)
	b, err = ioutil.ReadAll(recorder.Body)
	c.Assert(err, check.IsNil)
	expected = ""
	expected += `{"Message":"Unbind app \"app-instance\" ...\n"}` + "\n"
	expected += `{"Message":"\nInstance \"foo-instance\" is not bound to the app \"app-instance\" anymore.\n"}` + "\n"
	expected += `{"Message":"service instance successfuly removed"}` + "\n"
	c.Assert(string(b), check.Equals, expected)
	n, err = s.conn.ServiceInstances().Find(bson.M{"name": "foo-instance", "service_name": "foo"}).Count()
	c.Assert(err, check.IsNil)
	c.Assert(n, check.Equals, 0)
}