grafana/pkg/services/sqlstore/team_test.go

141 lines
5.0 KiB
Go
Raw Normal View History

package sqlstore
import (
2018-06-15 14:23:57 -05:00
"context"
"fmt"
"testing"
. "github.com/smartystreets/goconvey/convey"
m "github.com/grafana/grafana/pkg/models"
)
2017-12-08 09:25:45 -06:00
func TestTeamCommandsAndQueries(t *testing.T) {
2017-12-08 09:25:45 -06:00
Convey("Testing Team commands & queries", t, func() {
InitTestDB(t)
2017-12-08 09:25:45 -06:00
Convey("Given saved users and two teams", func() {
var userIds []int64
for i := 0; i < 5; i++ {
userCmd := &m.CreateUserCommand{
Email: fmt.Sprint("user", i, "@test.com"),
Name: fmt.Sprint("user", i),
Login: fmt.Sprint("loginuser", i),
}
2018-06-15 14:23:57 -05:00
err := CreateUser(context.Background(), userCmd)
So(err, ShouldBeNil)
userIds = append(userIds, userCmd.Result.Id)
}
var testOrgId int64 = 1
group1 := m.CreateTeamCommand{OrgId: testOrgId, Name: "group1 name", Email: "test1@test.com"}
group2 := m.CreateTeamCommand{OrgId: testOrgId, Name: "group2 name", Email: "test2@test.com"}
2017-12-08 09:25:45 -06:00
err := CreateTeam(&group1)
So(err, ShouldBeNil)
2017-12-08 09:25:45 -06:00
err = CreateTeam(&group2)
So(err, ShouldBeNil)
2017-12-08 09:25:45 -06:00
Convey("Should be able to create teams and add users", func() {
query := &m.SearchTeamsQuery{OrgId: testOrgId, Name: "group1 name", Page: 1, Limit: 10}
2017-12-08 09:25:45 -06:00
err = SearchTeams(query)
So(err, ShouldBeNil)
2017-04-09 18:24:16 -05:00
So(query.Page, ShouldEqual, 1)
2017-12-08 09:25:45 -06:00
team1 := query.Result.Teams[0]
So(team1.Name, ShouldEqual, "group1 name")
So(team1.Email, ShouldEqual, "test1@test.com")
So(team1.OrgId, ShouldEqual, testOrgId)
err = AddTeamMember(&m.AddTeamMemberCommand{OrgId: testOrgId, TeamId: team1.Id, UserId: userIds[0]})
So(err, ShouldBeNil)
2018-09-14 10:27:36 -05:00
err = AddTeamMember(&m.AddTeamMemberCommand{OrgId: testOrgId, TeamId: team1.Id, UserId: userIds[1], External: true})
So(err, ShouldBeNil)
q1 := &m.GetTeamMembersQuery{OrgId: testOrgId, TeamId: team1.Id}
2017-12-08 09:25:45 -06:00
err = GetTeamMembers(q1)
So(err, ShouldBeNil)
2018-09-14 10:27:36 -05:00
So(q1.Result, ShouldHaveLength, 2)
2017-12-08 09:25:45 -06:00
So(q1.Result[0].TeamId, ShouldEqual, team1.Id)
So(q1.Result[0].Login, ShouldEqual, "loginuser0")
So(q1.Result[0].OrgId, ShouldEqual, testOrgId)
2018-09-14 10:27:36 -05:00
So(q1.Result[1].TeamId, ShouldEqual, team1.Id)
So(q1.Result[1].Login, ShouldEqual, "loginuser1")
So(q1.Result[1].OrgId, ShouldEqual, testOrgId)
So(q1.Result[1].External, ShouldEqual, true)
q2 := &m.GetTeamMembersQuery{OrgId: testOrgId, TeamId: team1.Id, External: true}
err = GetTeamMembers(q2)
So(err, ShouldBeNil)
So(q2.Result, ShouldHaveLength, 1)
So(q2.Result[0].TeamId, ShouldEqual, team1.Id)
So(q2.Result[0].Login, ShouldEqual, "loginuser1")
So(q2.Result[0].OrgId, ShouldEqual, testOrgId)
So(q2.Result[0].External, ShouldEqual, true)
})
2017-12-08 09:25:45 -06:00
Convey("Should be able to search for teams", func() {
query := &m.SearchTeamsQuery{OrgId: testOrgId, Query: "group", Page: 1}
2017-12-08 09:25:45 -06:00
err = SearchTeams(query)
So(err, ShouldBeNil)
2017-12-08 09:25:45 -06:00
So(len(query.Result.Teams), ShouldEqual, 2)
2017-04-09 18:24:16 -05:00
So(query.Result.TotalCount, ShouldEqual, 2)
query2 := &m.SearchTeamsQuery{OrgId: testOrgId, Query: ""}
2017-12-08 09:25:45 -06:00
err = SearchTeams(query2)
2017-04-09 18:24:16 -05:00
So(err, ShouldBeNil)
2017-12-08 09:25:45 -06:00
So(len(query2.Result.Teams), ShouldEqual, 2)
})
2017-12-08 09:25:45 -06:00
Convey("Should be able to return all teams a user is member of", func() {
2017-05-22 03:33:17 -05:00
groupId := group2.Result.Id
err := AddTeamMember(&m.AddTeamMemberCommand{OrgId: testOrgId, TeamId: groupId, UserId: userIds[0]})
2018-04-23 12:28:54 -05:00
So(err, ShouldBeNil)
2017-05-22 03:33:17 -05:00
query := &m.GetTeamsByUserQuery{OrgId: testOrgId, UserId: userIds[0]}
2017-12-08 09:25:45 -06:00
err = GetTeamsByUser(query)
2017-05-22 03:33:17 -05:00
So(err, ShouldBeNil)
So(len(query.Result), ShouldEqual, 1)
So(query.Result[0].Name, ShouldEqual, "group2 name")
So(query.Result[0].Email, ShouldEqual, "test2@test.com")
2017-05-22 03:33:17 -05:00
})
Convey("Should be able to remove users from a group", func() {
2018-02-16 04:45:53 -06:00
err = AddTeamMember(&m.AddTeamMemberCommand{OrgId: testOrgId, TeamId: group1.Result.Id, UserId: userIds[0]})
So(err, ShouldBeNil)
err = RemoveTeamMember(&m.RemoveTeamMemberCommand{OrgId: testOrgId, TeamId: group1.Result.Id, UserId: userIds[0]})
So(err, ShouldBeNil)
2018-02-16 04:45:53 -06:00
q2 := &m.GetTeamMembersQuery{OrgId: testOrgId, TeamId: group1.Result.Id}
err = GetTeamMembers(q2)
So(err, ShouldBeNil)
2018-02-16 04:45:53 -06:00
So(len(q2.Result), ShouldEqual, 0)
})
Convey("Should be able to remove a group with users and permissions", func() {
groupId := group2.Result.Id
err := AddTeamMember(&m.AddTeamMemberCommand{OrgId: testOrgId, TeamId: groupId, UserId: userIds[1]})
So(err, ShouldBeNil)
err = AddTeamMember(&m.AddTeamMemberCommand{OrgId: testOrgId, TeamId: groupId, UserId: userIds[2]})
So(err, ShouldBeNil)
err = testHelperUpdateDashboardAcl(1, m.DashboardAcl{DashboardId: 1, OrgId: testOrgId, Permission: m.PERMISSION_EDIT, TeamId: groupId})
2018-04-23 12:28:54 -05:00
So(err, ShouldBeNil)
err = DeleteTeam(&m.DeleteTeamCommand{OrgId: testOrgId, Id: groupId})
So(err, ShouldBeNil)
query := &m.GetTeamByIdQuery{OrgId: testOrgId, Id: groupId}
2017-12-08 09:25:45 -06:00
err = GetTeamById(query)
So(err, ShouldEqual, m.ErrTeamNotFound)
permQuery := &m.GetDashboardAclInfoListQuery{DashboardId: 1, OrgId: testOrgId}
2017-06-19 16:30:54 -05:00
err = GetDashboardAclInfoList(permQuery)
So(err, ShouldBeNil)
So(len(permQuery.Result), ShouldEqual, 0)
})
})
})
}