grafana/pkg/api/team_test.go

153 lines
3.8 KiB
Go
Raw Normal View History

2017-04-09 18:24:16 -05:00
package api
import (
"testing"
"github.com/grafana/grafana/pkg/setting"
macaron "gopkg.in/macaron.v1"
2017-04-09 18:24:16 -05:00
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
"net/http"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2017-04-09 18:24:16 -05:00
)
type testLogger struct {
log.Logger
warnCalled bool
warnMessage string
}
func (stub *testLogger) Warn(testMessage string, ctx ...interface{}) {
stub.warnCalled = true
stub.warnMessage = testMessage
}
func TestTeamAPIEndpoint(t *testing.T) {
t.Run("Given two teams", func(t *testing.T) {
2017-12-08 09:25:45 -06:00
mockResult := models.SearchTeamQueryResult{
Teams: []*models.TeamDTO{
2017-12-08 09:25:45 -06:00
{Name: "team1"},
{Name: "team2"},
2017-04-09 18:24:16 -05:00
},
TotalCount: 2,
}
hs := &HTTPServer{
Cfg: setting.NewCfg(),
}
loggedInUserScenario(t, "When calling GET on", "/api/teams/search", func(sc *scenarioContext) {
var sentLimit int
var sendPage int
bus.AddHandler("test", func(query *models.SearchTeamsQuery) error {
query.Result = mockResult
2017-04-09 18:24:16 -05:00
sentLimit = query.Limit
sendPage = query.Page
2017-04-09 18:24:16 -05:00
return nil
})
2017-04-09 18:24:16 -05:00
sc.handlerFunc = hs.SearchTeams
sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
2017-04-09 18:24:16 -05:00
assert.Equal(t, 1000, sentLimit)
assert.Equal(t, 1, sendPage)
2017-04-09 18:24:16 -05:00
respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes())
require.NoError(t, err)
2017-04-09 18:24:16 -05:00
assert.Equal(t, 2, respJSON.Get("totalCount").MustInt())
assert.Equal(t, 2, len(respJSON.Get("teams").MustArray()))
2017-04-09 18:24:16 -05:00
})
loggedInUserScenario(t, "When calling GET on", "/api/teams/search", func(sc *scenarioContext) {
var sentLimit int
var sendPage int
bus.AddHandler("test", func(query *models.SearchTeamsQuery) error {
query.Result = mockResult
2017-04-09 18:24:16 -05:00
sentLimit = query.Limit
sendPage = query.Page
2017-04-09 18:24:16 -05:00
return nil
})
2017-04-09 18:24:16 -05:00
sc.handlerFunc = hs.SearchTeams
sc.fakeReqWithParams("GET", sc.url, map[string]string{"perpage": "10", "page": "2"}).exec()
2017-04-09 18:24:16 -05:00
assert.Equal(t, 10, sentLimit)
assert.Equal(t, 2, sendPage)
2017-04-09 18:24:16 -05:00
})
})
t.Run("When creating team with API key", func(t *testing.T) {
defer bus.ClearBusHandlers()
hs := &HTTPServer{
Cfg: setting.NewCfg(),
Bus: bus.GetBus(),
}
hs.Cfg.EditorsCanAdmin = true
teamName := "team foo"
createTeamCalled := 0
bus.AddHandler("test", func(cmd *models.CreateTeamCommand) error {
createTeamCalled += 1
cmd.Result = models.Team{Name: teamName, Id: 42}
return nil
})
addTeamMemberCalled := 0
bus.AddHandler("test", func(cmd *models.AddTeamMemberCommand) error {
addTeamMemberCalled += 1
return nil
})
req, _ := http.NewRequest("POST", "/api/teams", nil)
t.Run("with no real signed in user", func(t *testing.T) {
stub := &testLogger{}
c := &models.ReqContext{
Context: &macaron.Context{
Req: macaron.Request{Request: req},
},
SignedInUser: &models.SignedInUser{},
Logger: stub,
}
c.OrgRole = models.ROLE_EDITOR
cmd := models.CreateTeamCommand{Name: teamName}
hs.CreateTeam(c, cmd)
assert.Equal(t, createTeamCalled, 1)
assert.Equal(t, addTeamMemberCalled, 0)
assert.True(t, stub.warnCalled)
assert.Equal(t, stub.warnMessage, "Could not add creator to team because is not a real user.")
})
t.Run("with real signed in user", func(t *testing.T) {
stub := &testLogger{}
c := &models.ReqContext{
Context: &macaron.Context{
Req: macaron.Request{Request: req},
},
SignedInUser: &models.SignedInUser{UserId: 42},
Logger: stub,
}
c.OrgRole = models.ROLE_EDITOR
cmd := models.CreateTeamCommand{Name: teamName}
createTeamCalled, addTeamMemberCalled = 0, 0
hs.CreateTeam(c, cmd)
assert.Equal(t, createTeamCalled, 1)
assert.Equal(t, addTeamMemberCalled, 1)
assert.False(t, stub.warnCalled)
})
})
2017-04-09 18:24:16 -05:00
}