Commit d668550a by Leonard Gram

teams: added feature toggle and refactor tests

parent 0d61f895
......@@ -38,11 +38,11 @@ func (hs *HTTPServer) CreateTeam(c *m.ReqContext, cmd m.CreateTeamCommand) Respo
}
// PUT /api/teams/:teamId
func UpdateTeam(c *m.ReqContext, cmd m.UpdateTeamCommand) Response {
func (hs *HTTPServer) UpdateTeam(c *m.ReqContext, cmd m.UpdateTeamCommand) Response {
cmd.OrgId = c.OrgId
cmd.Id = c.ParamsInt64(":teamId")
if err := teams.CanUpdateTeam(cmd.OrgId, cmd.Id, c.SignedInUser); err != nil {
if err := teams.CanUpdateTeam(cmd.OrgId, cmd.Id, c.SignedInUser, hs.Cfg.EditorsCanOwn); err != nil {
return Error(403, "User not allowed to update team", err)
}
......
......@@ -5,11 +5,15 @@ import (
m "github.com/grafana/grafana/pkg/models"
)
func CanUpdateTeam(orgId int64, teamId int64, user *m.SignedInUser) error {
func CanUpdateTeam(orgId int64, teamId int64, user *m.SignedInUser, editorCanOwn bool) error {
if user.OrgRole == m.ROLE_ADMIN {
return nil
}
if !editorCanOwn {
return m.ErrNotAllowedToUpdateTeam
}
if user.OrgId != orgId {
return m.ErrNotAllowedToUpdateTeamInDifferentOrg
}
......
......@@ -3,7 +3,6 @@ package teams
import (
"github.com/grafana/grafana/pkg/bus"
m "github.com/grafana/grafana/pkg/models"
"github.com/pkg/errors"
. "github.com/smartystreets/goconvey/convey"
"testing"
)
......@@ -27,28 +26,20 @@ func TestUpdateTeam(t *testing.T) {
OrgId: 1,
}
updateTeamCmd := m.UpdateTeamCommand{
Id: testTeam.Id,
OrgId: testTeam.OrgId,
}
Convey("Given an editor and a team he isn't a member of", func() {
Convey("Should not be able to update the team", func() {
shouldNotUpdateTeam()
bus.AddHandler("test", func(cmd *m.GetTeamMembersQuery) error {
cmd.Result = []*m.TeamMemberDTO{}
return nil
})
err := CanUpdateTeam(&editor, &updateTeamCmd)
err := CanUpdateTeam(testTeam.OrgId, testTeam.Id, &editor, true)
So(err, ShouldEqual, m.ErrNotAllowedToUpdateTeam)
})
})
Convey("Given an editor and a team he is an admin in", func() {
Convey("Should be able to update the team", func() {
teamUpdatedCallback := updateTeamCalled()
bus.AddHandler("test", func(cmd *m.GetTeamMembersQuery) error {
cmd.Result = []*m.TeamMemberDTO{{
OrgId: testTeam.OrgId,
......@@ -59,8 +50,7 @@ func TestUpdateTeam(t *testing.T) {
return nil
})
err := CanUpdateTeam(&editor, &updateTeamCmd)
So(teamUpdatedCallback(), ShouldBeTrue)
err := CanUpdateTeam(testTeam.OrgId, testTeam.Id, &editor, true)
So(err, ShouldBeNil)
})
})
......@@ -72,12 +62,6 @@ func TestUpdateTeam(t *testing.T) {
}
Convey("Shouldn't be able to update the team", func() {
cmd := m.UpdateTeamCommand{
Id: testTeamOtherOrg.Id,
OrgId: testTeamOtherOrg.OrgId,
}
shouldNotUpdateTeam()
bus.AddHandler("test", func(cmd *m.GetTeamMembersQuery) error {
cmd.Result = []*m.TeamMemberDTO{{
OrgId: testTeamOtherOrg.OrgId,
......@@ -88,42 +72,24 @@ func TestUpdateTeam(t *testing.T) {
return nil
})
err := CanUpdateTeam(&editor, &cmd)
err := CanUpdateTeam(testTeamOtherOrg.OrgId, testTeamOtherOrg.Id, &editor, true)
So(err, ShouldEqual, m.ErrNotAllowedToUpdateTeamInDifferentOrg)
})
})
Convey("Given an org admin and a team", func() {
Convey("Should be able to update the team", func() {
teamUpdatedCallback := updateTeamCalled()
err := CanUpdateTeam(&admin, &updateTeamCmd)
So(teamUpdatedCallback(), ShouldBeTrue)
err := CanUpdateTeam(testTeam.OrgId, testTeam.Id, &admin, true)
So(err, ShouldBeNil)
})
})
Convey("Given that the editorsCanOwn feature toggle is disabled", func() {
Convey("Given an editor and a team he is an admin", func() {
Convey("Given that the editorsCanOwn feature toggle is disabled", func() {
Convey("Editors should not be able to update teams", func() {
err := CanUpdateTeam(testTeam.OrgId, testTeam.Id, &editor, false)
So(err, ShouldEqual, m.ErrNotAllowedToUpdateTeam)
})
})
})
}
func updateTeamCalled() func() bool {
wasCalled := false
bus.AddHandler("test", func(cmd *m.UpdateTeamCommand) error {
wasCalled = true
return nil
})
return func() bool { return wasCalled }
}
func shouldNotUpdateTeam() {
bus.AddHandler("test", func(cmd *m.UpdateTeamCommand) error {
return errors.New("UpdateTeamCommand not expected.")
})
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment