grafana/pkg/api/team_test.go

72 lines
1.8 KiB
Go
Raw Normal View History

2017-04-09 18:24:16 -05:00
package api
import (
"testing"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
. "github.com/smartystreets/goconvey/convey"
)
2017-12-08 09:25:45 -06:00
func TestTeamApiEndpoint(t *testing.T) {
Convey("Given two teams", t, func() {
mockResult := models.SearchTeamQueryResult{
Teams: []*models.SearchTeamDto{
2017-12-08 09:25:45 -06:00
{Name: "team1"},
{Name: "team2"},
2017-04-09 18:24:16 -05:00
},
TotalCount: 2,
}
Convey("When searching with no parameters", func() {
2017-12-08 09:25:45 -06:00
loggedInUserScenario("When calling GET on", "/api/teams/search", func(sc *scenarioContext) {
2017-04-09 18:24:16 -05:00
var sentLimit int
var sendPage int
2017-12-08 09:25:45 -06:00
bus.AddHandler("test", func(query *models.SearchTeamsQuery) error {
2017-04-09 18:24:16 -05:00
query.Result = mockResult
sentLimit = query.Limit
sendPage = query.Page
return nil
})
2017-12-08 09:25:45 -06:00
sc.handlerFunc = SearchTeams
2017-04-09 18:24:16 -05:00
sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
So(sentLimit, ShouldEqual, 1000)
So(sendPage, ShouldEqual, 1)
respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes())
So(err, ShouldBeNil)
So(respJSON.Get("totalCount").MustInt(), ShouldEqual, 2)
2017-12-08 09:25:45 -06:00
So(len(respJSON.Get("teams").MustArray()), ShouldEqual, 2)
2017-04-09 18:24:16 -05:00
})
})
Convey("When searching with page and perpage parameters", func() {
2017-12-08 09:25:45 -06:00
loggedInUserScenario("When calling GET on", "/api/teams/search", func(sc *scenarioContext) {
2017-04-09 18:24:16 -05:00
var sentLimit int
var sendPage int
2017-12-08 09:25:45 -06:00
bus.AddHandler("test", func(query *models.SearchTeamsQuery) error {
2017-04-09 18:24:16 -05:00
query.Result = mockResult
sentLimit = query.Limit
sendPage = query.Page
return nil
})
2017-12-08 09:25:45 -06:00
sc.handlerFunc = SearchTeams
2017-04-09 18:24:16 -05:00
sc.fakeReqWithParams("GET", sc.url, map[string]string{"perpage": "10", "page": "2"}).exec()
So(sentLimit, ShouldEqual, 10)
So(sendPage, ShouldEqual, 2)
})
})
})
}