|
@@ -9,9 +9,24 @@ import (
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
"github.com/grafana/grafana/pkg/models"
|
|
|
|
|
|
|
|
|
|
+ "github.com/grafana/grafana/pkg/infra/log"
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
|
+ "github.com/stretchr/testify/assert"
|
|
|
|
|
+ macaron "gopkg.in/macaron.v1"
|
|
|
|
|
+ "net/http"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
+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) {
|
|
func TestTeamApiEndpoint(t *testing.T) {
|
|
|
Convey("Given two teams", t, func() {
|
|
Convey("Given two teams", t, func() {
|
|
|
mockResult := models.SearchTeamQueryResult{
|
|
mockResult := models.SearchTeamQueryResult{
|
|
@@ -74,4 +89,67 @@ func TestTeamApiEndpoint(t *testing.T) {
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
|
|
+
|
|
|
|
|
+ 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)
|
|
|
|
|
+ })
|
|
|
|
|
+ })
|
|
|
}
|
|
}
|