2014-12-19 02:43:16 -06:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
2018-06-15 14:23:57 -05:00
|
|
|
"context"
|
2014-12-19 02:43:16 -06:00
|
|
|
"testing"
|
2018-03-15 18:08:25 -05:00
|
|
|
"time"
|
2014-12-19 02:43:16 -06:00
|
|
|
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
|
2015-02-05 03:37:13 -06:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2014-12-19 02:43:16 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccountDataAccess(t *testing.T) {
|
|
|
|
Convey("Testing Account DB Access", t, func() {
|
|
|
|
InitTestDB(t)
|
|
|
|
|
2015-02-23 13:48:43 -06:00
|
|
|
Convey("Given single org mode", func() {
|
2015-03-11 10:19:29 -05:00
|
|
|
setting.AutoAssignOrg = true
|
2018-07-13 14:14:40 -05:00
|
|
|
setting.AutoAssignOrgId = 1
|
2015-03-11 10:19:29 -05:00
|
|
|
setting.AutoAssignOrgRole = "Viewer"
|
2015-01-27 08:14:53 -06:00
|
|
|
|
2015-02-23 13:07:49 -06:00
|
|
|
Convey("Users should be added to default organization", func() {
|
2015-01-27 08:14:53 -06:00
|
|
|
ac1cmd := m.CreateUserCommand{Login: "ac1", Email: "ac1@test.com", Name: "ac1 name"}
|
|
|
|
ac2cmd := m.CreateUserCommand{Login: "ac2", Email: "ac2@test.com", Name: "ac2 name"}
|
|
|
|
|
2018-06-15 14:23:57 -05:00
|
|
|
err := CreateUser(context.Background(), &ac1cmd)
|
2015-01-27 08:14:53 -06:00
|
|
|
So(err, ShouldBeNil)
|
2018-06-15 14:23:57 -05:00
|
|
|
err = CreateUser(context.Background(), &ac2cmd)
|
2015-01-27 08:14:53 -06:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2015-02-23 13:48:43 -06:00
|
|
|
q1 := m.GetUserOrgListQuery{UserId: ac1cmd.Result.Id}
|
|
|
|
q2 := m.GetUserOrgListQuery{UserId: ac2cmd.Result.Id}
|
|
|
|
GetUserOrgList(&q1)
|
|
|
|
GetUserOrgList(&q2)
|
2015-01-27 08:14:53 -06:00
|
|
|
|
2015-02-23 13:07:49 -06:00
|
|
|
So(q1.Result[0].OrgId, ShouldEqual, q2.Result[0].OrgId)
|
2015-01-27 08:14:53 -06:00
|
|
|
So(q1.Result[0].Role, ShouldEqual, "Viewer")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-01-19 11:01:04 -06:00
|
|
|
Convey("Given two saved users", func() {
|
2015-03-11 10:19:29 -05:00
|
|
|
setting.AutoAssignOrg = false
|
2015-01-27 08:14:53 -06:00
|
|
|
|
2015-01-19 11:01:04 -06:00
|
|
|
ac1cmd := m.CreateUserCommand{Login: "ac1", Email: "ac1@test.com", Name: "ac1 name"}
|
|
|
|
ac2cmd := m.CreateUserCommand{Login: "ac2", Email: "ac2@test.com", Name: "ac2 name", IsAdmin: true}
|
2014-12-19 04:53:27 -06:00
|
|
|
|
2018-06-15 14:23:57 -05:00
|
|
|
err := CreateUser(context.Background(), &ac1cmd)
|
|
|
|
err = CreateUser(context.Background(), &ac2cmd)
|
2014-12-19 03:45:22 -06:00
|
|
|
So(err, ShouldBeNil)
|
2014-12-19 02:43:16 -06:00
|
|
|
|
2014-12-19 04:53:27 -06:00
|
|
|
ac1 := ac1cmd.Result
|
|
|
|
ac2 := ac2cmd.Result
|
|
|
|
|
2015-01-19 11:01:04 -06:00
|
|
|
Convey("Should be able to read user info projection", func() {
|
2015-02-28 07:30:08 -06:00
|
|
|
query := m.GetUserProfileQuery{UserId: ac1.Id}
|
|
|
|
err = GetUserProfile(&query)
|
2014-12-19 02:43:16 -06:00
|
|
|
|
2014-12-19 03:45:22 -06:00
|
|
|
So(err, ShouldBeNil)
|
2014-12-19 04:53:27 -06:00
|
|
|
So(query.Result.Email, ShouldEqual, "ac1@test.com")
|
2015-01-16 00:45:37 -06:00
|
|
|
So(query.Result.Login, ShouldEqual, "ac1")
|
|
|
|
})
|
|
|
|
|
2015-01-19 11:01:04 -06:00
|
|
|
Convey("Can search users", func() {
|
|
|
|
query := m.SearchUsersQuery{Query: ""}
|
|
|
|
err := SearchUsers(&query)
|
2015-01-16 00:45:37 -06:00
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
2017-02-08 07:20:07 -06:00
|
|
|
So(query.Result.Users[0].Email, ShouldEqual, "ac1@test.com")
|
|
|
|
So(query.Result.Users[1].Email, ShouldEqual, "ac2@test.com")
|
2014-12-19 03:45:22 -06:00
|
|
|
})
|
2014-12-19 02:43:16 -06:00
|
|
|
|
2015-02-23 13:48:43 -06:00
|
|
|
Convey("Given an added org user", func() {
|
|
|
|
cmd := m.AddOrgUserCommand{
|
|
|
|
OrgId: ac1.OrgId,
|
|
|
|
UserId: ac2.Id,
|
|
|
|
Role: m.ROLE_VIEWER,
|
2014-12-19 03:45:22 -06:00
|
|
|
}
|
|
|
|
|
2015-02-23 13:48:43 -06:00
|
|
|
err := AddOrgUser(&cmd)
|
2015-01-16 07:32:18 -06:00
|
|
|
Convey("Should have been saved without error", func() {
|
2014-12-19 03:45:22 -06:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
|
2015-05-01 02:48:07 -05:00
|
|
|
Convey("Can update org user role", func() {
|
|
|
|
updateCmd := m.UpdateOrgUserCommand{OrgId: ac1.OrgId, UserId: ac2.Id, Role: m.ROLE_ADMIN}
|
|
|
|
err = UpdateOrgUser(&updateCmd)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
orgUsersQuery := m.GetOrgUsersQuery{OrgId: ac1.OrgId}
|
|
|
|
err = GetOrgUsers(&orgUsersQuery)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(orgUsersQuery.Result[1].Role, ShouldEqual, m.ROLE_ADMIN)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2015-01-16 07:32:18 -06:00
|
|
|
Convey("Can get logged in user projection", func() {
|
2015-01-19 11:01:04 -06:00
|
|
|
query := m.GetSignedInUserQuery{UserId: ac2.Id}
|
2015-01-16 07:32:18 -06:00
|
|
|
err := GetSignedInUser(&query)
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
2015-01-19 11:01:04 -06:00
|
|
|
So(query.Result.Email, ShouldEqual, "ac2@test.com")
|
2015-02-23 13:48:43 -06:00
|
|
|
So(query.Result.OrgId, ShouldEqual, ac2.OrgId)
|
2015-01-19 11:01:04 -06:00
|
|
|
So(query.Result.Name, ShouldEqual, "ac2 name")
|
|
|
|
So(query.Result.Login, ShouldEqual, "ac2")
|
2015-02-23 13:48:43 -06:00
|
|
|
So(query.Result.OrgRole, ShouldEqual, "Admin")
|
|
|
|
So(query.Result.OrgName, ShouldEqual, "ac2@test.com")
|
2015-01-16 07:32:18 -06:00
|
|
|
So(query.Result.IsGrafanaAdmin, ShouldBeTrue)
|
|
|
|
})
|
|
|
|
|
2015-02-23 13:48:43 -06:00
|
|
|
Convey("Can get user organizations", func() {
|
|
|
|
query := m.GetUserOrgListQuery{UserId: ac2.Id}
|
|
|
|
err := GetUserOrgList(&query)
|
2014-12-19 04:08:49 -06:00
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
2015-01-19 11:01:04 -06:00
|
|
|
So(len(query.Result), ShouldEqual, 2)
|
2014-12-19 04:08:49 -06:00
|
|
|
})
|
2014-12-19 06:12:47 -06:00
|
|
|
|
2015-02-23 13:48:43 -06:00
|
|
|
Convey("Can get organization users", func() {
|
|
|
|
query := m.GetOrgUsersQuery{OrgId: ac1.OrgId}
|
|
|
|
err := GetOrgUsers(&query)
|
2015-01-20 08:23:14 -06:00
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(query.Result), ShouldEqual, 2)
|
|
|
|
So(query.Result[0].Role, ShouldEqual, "Admin")
|
|
|
|
})
|
|
|
|
|
2018-02-09 03:42:37 -06:00
|
|
|
Convey("Can get organization users with query", func() {
|
|
|
|
query := m.GetOrgUsersQuery{
|
|
|
|
OrgId: ac1.OrgId,
|
|
|
|
Query: "ac1",
|
|
|
|
}
|
|
|
|
err := GetOrgUsers(&query)
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(query.Result), ShouldEqual, 1)
|
|
|
|
So(query.Result[0].Email, ShouldEqual, ac1.Email)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Can get organization users with query and limit", func() {
|
|
|
|
query := m.GetOrgUsersQuery{
|
|
|
|
OrgId: ac1.OrgId,
|
|
|
|
Query: "ac",
|
|
|
|
Limit: 1,
|
|
|
|
}
|
|
|
|
err := GetOrgUsers(&query)
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(query.Result), ShouldEqual, 1)
|
|
|
|
So(query.Result[0].Email, ShouldEqual, ac1.Email)
|
|
|
|
})
|
|
|
|
|
2015-02-23 13:48:43 -06:00
|
|
|
Convey("Can set using org", func() {
|
2018-06-13 11:01:50 -05:00
|
|
|
cmd := m.SetUsingOrgCommand{UserId: ac2.Id, OrgId: ac1.OrgId}
|
2015-02-23 13:48:43 -06:00
|
|
|
err := SetUsingOrg(&cmd)
|
2014-12-19 06:12:47 -06:00
|
|
|
So(err, ShouldBeNil)
|
2015-01-16 07:32:18 -06:00
|
|
|
|
2015-02-23 13:48:43 -06:00
|
|
|
Convey("SignedInUserQuery with a different org", func() {
|
2015-01-19 11:01:04 -06:00
|
|
|
query := m.GetSignedInUserQuery{UserId: ac2.Id}
|
2015-01-16 07:32:18 -06:00
|
|
|
err := GetSignedInUser(&query)
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
2018-06-13 11:01:50 -05:00
|
|
|
So(query.Result.OrgId, ShouldEqual, ac1.OrgId)
|
2015-01-19 11:01:04 -06:00
|
|
|
So(query.Result.Email, ShouldEqual, "ac2@test.com")
|
|
|
|
So(query.Result.Name, ShouldEqual, "ac2 name")
|
|
|
|
So(query.Result.Login, ShouldEqual, "ac2")
|
2015-02-23 13:48:43 -06:00
|
|
|
So(query.Result.OrgName, ShouldEqual, "ac1@test.com")
|
|
|
|
So(query.Result.OrgRole, ShouldEqual, "Viewer")
|
2015-01-16 07:32:18 -06:00
|
|
|
})
|
2018-06-13 11:01:50 -05:00
|
|
|
|
|
|
|
Convey("Should set last org as current when removing user from current", func() {
|
|
|
|
remCmd := m.RemoveOrgUserCommand{OrgId: ac1.OrgId, UserId: ac2.Id}
|
|
|
|
err := RemoveOrgUser(&remCmd)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
query := m.GetSignedInUserQuery{UserId: ac2.Id}
|
|
|
|
err = GetSignedInUser(&query)
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(query.Result.OrgId, ShouldEqual, ac2.OrgId)
|
|
|
|
})
|
2014-12-19 06:12:47 -06:00
|
|
|
})
|
2015-01-20 08:48:19 -06:00
|
|
|
|
2018-10-11 00:48:35 -05:00
|
|
|
Convey("Removing user from org should delete user completely if in no other org", func() {
|
|
|
|
// make sure ac2 has no org
|
|
|
|
err := DeleteOrg(&m.DeleteOrgCommand{Id: ac2.OrgId})
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2018-12-04 16:29:34 -06:00
|
|
|
// remove ac2 user from ac1 org
|
2018-10-11 00:48:35 -05:00
|
|
|
remCmd := m.RemoveOrgUserCommand{OrgId: ac1.OrgId, UserId: ac2.Id, ShouldDeleteOrphanedUser: true}
|
|
|
|
err = RemoveOrgUser(&remCmd)
|
|
|
|
So(err, ShouldBeNil)
|
2018-10-11 14:20:53 -05:00
|
|
|
So(remCmd.UserWasDeleted, ShouldBeTrue)
|
2018-10-11 00:48:35 -05:00
|
|
|
|
|
|
|
err = GetSignedInUser(&m.GetSignedInUserQuery{UserId: ac2.Id})
|
|
|
|
So(err, ShouldEqual, m.ErrUserNotFound)
|
|
|
|
})
|
|
|
|
|
2015-05-19 04:47:14 -05:00
|
|
|
Convey("Cannot delete last admin org user", func() {
|
2015-02-23 13:48:43 -06:00
|
|
|
cmd := m.RemoveOrgUserCommand{OrgId: ac1.OrgId, UserId: ac1.Id}
|
|
|
|
err := RemoveOrgUser(&cmd)
|
|
|
|
So(err, ShouldEqual, m.ErrLastOrgAdmin)
|
2015-01-20 08:48:19 -06:00
|
|
|
})
|
2015-05-19 04:47:14 -05:00
|
|
|
|
|
|
|
Convey("Cannot update role so no one is admin user", func() {
|
|
|
|
cmd := m.UpdateOrgUserCommand{OrgId: ac1.OrgId, UserId: ac1.Id, Role: m.ROLE_VIEWER}
|
|
|
|
err := UpdateOrgUser(&cmd)
|
|
|
|
So(err, ShouldEqual, m.ErrLastOrgAdmin)
|
|
|
|
})
|
|
|
|
|
2017-06-15 13:19:54 -05:00
|
|
|
Convey("Given an org user with dashboard permissions", func() {
|
|
|
|
ac3cmd := m.CreateUserCommand{Login: "ac3", Email: "ac3@test.com", Name: "ac3 name", IsAdmin: false}
|
2018-06-15 14:23:57 -05:00
|
|
|
err := CreateUser(context.Background(), &ac3cmd)
|
2017-06-15 13:19:54 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
ac3 := ac3cmd.Result
|
|
|
|
|
|
|
|
orgUserCmd := m.AddOrgUserCommand{
|
|
|
|
OrgId: ac1.OrgId,
|
|
|
|
UserId: ac3.Id,
|
|
|
|
Role: m.ROLE_VIEWER,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = AddOrgUser(&orgUserCmd)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2017-06-22 16:10:43 -05:00
|
|
|
query := m.GetOrgUsersQuery{OrgId: ac1.OrgId}
|
2017-06-15 13:19:54 -05:00
|
|
|
err = GetOrgUsers(&query)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(query.Result), ShouldEqual, 3)
|
|
|
|
|
2018-02-14 08:04:26 -06:00
|
|
|
dash1 := insertTestDashboard("1 test dash", ac1.OrgId, 0, false, "prod", "webapp")
|
|
|
|
dash2 := insertTestDashboard("2 test dash", ac3.OrgId, 0, false, "prod", "webapp")
|
|
|
|
|
|
|
|
err = testHelperUpdateDashboardAcl(dash1.Id, m.DashboardAcl{DashboardId: dash1.Id, OrgId: ac1.OrgId, UserId: ac3.Id, Permission: m.PERMISSION_EDIT})
|
2017-06-15 13:19:54 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2018-02-14 08:04:26 -06:00
|
|
|
err = testHelperUpdateDashboardAcl(dash2.Id, m.DashboardAcl{DashboardId: dash2.Id, OrgId: ac3.OrgId, UserId: ac3.Id, Permission: m.PERMISSION_EDIT})
|
2017-06-15 13:19:54 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
Convey("When org user is deleted", func() {
|
2017-06-22 16:10:43 -05:00
|
|
|
cmdRemove := m.RemoveOrgUserCommand{OrgId: ac1.OrgId, UserId: ac3.Id}
|
2017-06-15 13:19:54 -05:00
|
|
|
err := RemoveOrgUser(&cmdRemove)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
Convey("Should remove dependent permissions for deleted org user", func() {
|
2017-06-22 16:10:43 -05:00
|
|
|
permQuery := &m.GetDashboardAclInfoListQuery{DashboardId: 1, OrgId: ac1.OrgId}
|
2017-06-19 16:30:54 -05:00
|
|
|
err = GetDashboardAclInfoList(permQuery)
|
2017-06-15 13:19:54 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(len(permQuery.Result), ShouldEqual, 0)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Should not remove dashboard permissions for same user in another org", func() {
|
2017-06-22 16:10:43 -05:00
|
|
|
permQuery := &m.GetDashboardAclInfoListQuery{DashboardId: 2, OrgId: ac3.OrgId}
|
2017-06-19 16:30:54 -05:00
|
|
|
err = GetDashboardAclInfoList(permQuery)
|
2017-06-15 13:19:54 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2017-06-22 16:10:43 -05:00
|
|
|
So(len(permQuery.Result), ShouldEqual, 1)
|
2017-06-15 13:19:54 -05:00
|
|
|
So(permQuery.Result[0].OrgId, ShouldEqual, ac3.OrgId)
|
|
|
|
So(permQuery.Result[0].UserId, ShouldEqual, ac3.Id)
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
2014-12-19 03:45:22 -06:00
|
|
|
})
|
2014-12-19 02:43:16 -06:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2018-02-14 08:04:26 -06:00
|
|
|
|
|
|
|
func testHelperUpdateDashboardAcl(dashboardId int64, items ...m.DashboardAcl) error {
|
|
|
|
cmd := m.UpdateDashboardAclCommand{DashboardId: dashboardId}
|
|
|
|
for _, item := range items {
|
2018-03-15 18:08:25 -05:00
|
|
|
item.Created = time.Now()
|
|
|
|
item.Updated = time.Now()
|
2018-02-14 08:04:26 -06:00
|
|
|
cmd.Items = append(cmd.Items, &item)
|
|
|
|
}
|
|
|
|
return UpdateDashboardAcl(&cmd)
|
|
|
|
}
|