2017-04-08 17:55:07 -05:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
2018-06-15 14:23:57 -05:00
|
|
|
"context"
|
2017-04-08 17:55:07 -05:00
|
|
|
"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-04-08 17:55:07 -05:00
|
|
|
|
2017-12-08 09:25:45 -06:00
|
|
|
Convey("Testing Team commands & queries", t, func() {
|
2017-04-08 17:55:07 -05:00
|
|
|
InitTestDB(t)
|
|
|
|
|
2017-12-08 09:25:45 -06:00
|
|
|
Convey("Given saved users and two teams", func() {
|
2017-04-08 17:55:07 -05:00
|
|
|
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)
|
2017-04-08 17:55:07 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
userIds = append(userIds, userCmd.Result.Id)
|
|
|
|
}
|
|
|
|
|
2018-02-09 10:26:15 -06:00
|
|
|
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-04-08 17:55:07 -05:00
|
|
|
|
2017-12-08 09:25:45 -06:00
|
|
|
err := CreateTeam(&group1)
|
2017-04-08 17:55:07 -05:00
|
|
|
So(err, ShouldBeNil)
|
2017-12-08 09:25:45 -06:00
|
|
|
err = CreateTeam(&group2)
|
2017-04-08 17:55:07 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2017-12-08 09:25:45 -06:00
|
|
|
Convey("Should be able to create teams and add users", func() {
|
2018-02-09 10:26:15 -06:00
|
|
|
query := &m.SearchTeamsQuery{OrgId: testOrgId, Name: "group1 name", Page: 1, Limit: 10}
|
2017-12-08 09:25:45 -06:00
|
|
|
err = SearchTeams(query)
|
2017-04-08 17:55:07 -05:00
|
|
|
So(err, ShouldBeNil)
|
2017-04-09 18:24:16 -05:00
|
|
|
So(query.Page, ShouldEqual, 1)
|
2017-04-08 17:55:07 -05:00
|
|
|
|
2017-12-08 09:25:45 -06:00
|
|
|
team1 := query.Result.Teams[0]
|
|
|
|
So(team1.Name, ShouldEqual, "group1 name")
|
2017-12-20 14:20:12 -06:00
|
|
|
So(team1.Email, ShouldEqual, "test1@test.com")
|
2018-02-09 10:26:15 -06:00
|
|
|
So(team1.OrgId, ShouldEqual, testOrgId)
|
2017-04-08 17:55:07 -05:00
|
|
|
|
2018-02-09 10:26:15 -06:00
|
|
|
err = AddTeamMember(&m.AddTeamMemberCommand{OrgId: testOrgId, TeamId: team1.Id, UserId: userIds[0]})
|
2017-04-08 17:55:07 -05:00
|
|
|
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)
|
2017-04-08 17:55:07 -05:00
|
|
|
|
2018-02-09 10:26:15 -06:00
|
|
|
q1 := &m.GetTeamMembersQuery{OrgId: testOrgId, TeamId: team1.Id}
|
2017-12-08 09:25:45 -06:00
|
|
|
err = GetTeamMembers(q1)
|
2017-04-08 17:55:07 -05:00
|
|
|
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)
|
2017-04-08 17:55:07 -05:00
|
|
|
So(q1.Result[0].Login, ShouldEqual, "loginuser0")
|
2018-02-09 10:26:15 -06:00
|
|
|
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-04-08 17:55:07 -05:00
|
|
|
})
|
|
|
|
|
2017-12-08 09:25:45 -06:00
|
|
|
Convey("Should be able to search for teams", func() {
|
2018-02-09 10:26:15 -06:00
|
|
|
query := &m.SearchTeamsQuery{OrgId: testOrgId, Query: "group", Page: 1}
|
2017-12-08 09:25:45 -06:00
|
|
|
err = SearchTeams(query)
|
2017-04-08 17:55:07 -05:00
|
|
|
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)
|
|
|
|
|
2018-02-09 10:26:15 -06:00
|
|
|
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-04-08 17:55:07 -05:00
|
|
|
})
|
|
|
|
|
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
|
2018-02-09 10:26:15 -06:00
|
|
|
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
|
|
|
|
2018-02-09 10:26:15 -06: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")
|
2017-12-20 14:20:12 -06:00
|
|
|
So(query.Result[0].Email, ShouldEqual, "test2@test.com")
|
2017-05-22 03:33:17 -05:00
|
|
|
})
|
|
|
|
|
2017-04-08 17:55:07 -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)
|
|
|
|
|
2018-02-09 10:26:15 -06:00
|
|
|
err = RemoveTeamMember(&m.RemoveTeamMemberCommand{OrgId: testOrgId, TeamId: group1.Result.Id, UserId: userIds[0]})
|
2017-04-08 17:55:07 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2018-02-16 04:45:53 -06:00
|
|
|
q2 := &m.GetTeamMembersQuery{OrgId: testOrgId, TeamId: group1.Result.Id}
|
|
|
|
err = GetTeamMembers(q2)
|
2017-04-08 17:55:07 -05:00
|
|
|
So(err, ShouldBeNil)
|
2018-02-16 04:45:53 -06:00
|
|
|
So(len(q2.Result), ShouldEqual, 0)
|
2017-04-08 17:55:07 -05:00
|
|
|
})
|
|
|
|
|
2017-06-14 11:40:08 -05:00
|
|
|
Convey("Should be able to remove a group with users and permissions", func() {
|
2017-04-08 17:55:07 -05:00
|
|
|
groupId := group2.Result.Id
|
2018-02-09 10:26:15 -06:00
|
|
|
err := AddTeamMember(&m.AddTeamMemberCommand{OrgId: testOrgId, TeamId: groupId, UserId: userIds[1]})
|
2017-04-08 17:55:07 -05:00
|
|
|
So(err, ShouldBeNil)
|
2018-02-09 10:26:15 -06:00
|
|
|
err = AddTeamMember(&m.AddTeamMemberCommand{OrgId: testOrgId, TeamId: groupId, UserId: userIds[2]})
|
2017-04-08 17:55:07 -05:00
|
|
|
So(err, ShouldBeNil)
|
2018-02-14 08:04:26 -06:00
|
|
|
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)
|
2018-02-09 10:26:15 -06:00
|
|
|
err = DeleteTeam(&m.DeleteTeamCommand{OrgId: testOrgId, Id: groupId})
|
2017-04-08 17:55:07 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2018-02-09 10:26:15 -06:00
|
|
|
query := &m.GetTeamByIdQuery{OrgId: testOrgId, Id: groupId}
|
2017-12-08 09:25:45 -06:00
|
|
|
err = GetTeamById(query)
|
|
|
|
So(err, ShouldEqual, m.ErrTeamNotFound)
|
2017-06-14 11:40:08 -05:00
|
|
|
|
2018-02-09 10:26:15 -06:00
|
|
|
permQuery := &m.GetDashboardAclInfoListQuery{DashboardId: 1, OrgId: testOrgId}
|
2017-06-19 16:30:54 -05:00
|
|
|
err = GetDashboardAclInfoList(permQuery)
|
2017-06-14 11:40:08 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(len(permQuery.Result), ShouldEqual, 0)
|
2017-04-08 17:55:07 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|