2017-02-08 07:20:07 -06:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
2018-06-15 14:23:57 -05:00
|
|
|
"context"
|
2017-02-08 07:20:07 -06:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
|
2017-06-19 16:15:25 -05:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2017-02-08 07:20:07 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestUserDataAccess(t *testing.T) {
|
|
|
|
|
|
|
|
Convey("Testing DB", t, func() {
|
2018-12-18 12:44:17 -06:00
|
|
|
ss := InitTestDB(t)
|
2017-02-08 07:20:07 -06:00
|
|
|
|
2018-08-21 06:30:39 -05:00
|
|
|
Convey("Creating a user", func() {
|
|
|
|
cmd := &m.CreateUserCommand{
|
|
|
|
Email: "usertest@test.com",
|
|
|
|
Name: "user name",
|
|
|
|
Login: "user_test_login",
|
|
|
|
}
|
|
|
|
|
|
|
|
err := CreateUser(context.Background(), cmd)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
Convey("Loading a user", func() {
|
|
|
|
query := m.GetUserByIdQuery{Id: cmd.Result.Id}
|
|
|
|
err := GetUserById(&query)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(query.Result.Email, ShouldEqual, "usertest@test.com")
|
|
|
|
So(query.Result.Password, ShouldEqual, "")
|
|
|
|
So(query.Result.Rands, ShouldHaveLength, 10)
|
|
|
|
So(query.Result.Salt, ShouldHaveLength, 10)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-06-14 16:45:30 -05:00
|
|
|
Convey("Given 5 users", func() {
|
|
|
|
var err error
|
2017-06-19 16:15:25 -05:00
|
|
|
var cmd *m.CreateUserCommand
|
|
|
|
users := []m.User{}
|
2017-06-14 16:45:30 -05:00
|
|
|
for i := 0; i < 5; i++ {
|
2017-06-19 16:15:25 -05:00
|
|
|
cmd = &m.CreateUserCommand{
|
2017-06-14 16:45:30 -05:00
|
|
|
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(), cmd)
|
2017-06-14 16:45:30 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
users = append(users, cmd.Result)
|
|
|
|
}
|
|
|
|
|
|
|
|
Convey("Can return the first page of users and a total count", func() {
|
2017-06-19 16:15:25 -05:00
|
|
|
query := m.SearchUsersQuery{Query: "", Page: 1, Limit: 3}
|
2017-06-14 16:45:30 -05:00
|
|
|
err = SearchUsers(&query)
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(query.Result.Users), ShouldEqual, 3)
|
|
|
|
So(query.Result.TotalCount, ShouldEqual, 5)
|
2017-02-08 07:20:07 -06:00
|
|
|
})
|
|
|
|
|
2017-06-14 16:45:30 -05:00
|
|
|
Convey("Can return the second page of users and a total count", func() {
|
2017-06-19 16:15:25 -05:00
|
|
|
query := m.SearchUsersQuery{Query: "", Page: 2, Limit: 3}
|
2017-06-14 16:45:30 -05:00
|
|
|
err = SearchUsers(&query)
|
2017-02-08 07:20:07 -06:00
|
|
|
|
2017-06-14 16:45:30 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(query.Result.Users), ShouldEqual, 2)
|
|
|
|
So(query.Result.TotalCount, ShouldEqual, 5)
|
|
|
|
})
|
2017-02-08 07:20:07 -06:00
|
|
|
|
2017-06-14 16:45:30 -05:00
|
|
|
Convey("Can return list of users matching query on user name", func() {
|
2017-06-19 16:15:25 -05:00
|
|
|
query := m.SearchUsersQuery{Query: "use", Page: 1, Limit: 3}
|
2017-06-14 16:45:30 -05:00
|
|
|
err = SearchUsers(&query)
|
2017-02-08 07:20:07 -06:00
|
|
|
|
2017-06-14 16:45:30 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(query.Result.Users), ShouldEqual, 3)
|
|
|
|
So(query.Result.TotalCount, ShouldEqual, 5)
|
2017-03-07 09:03:54 -06:00
|
|
|
|
2017-06-19 16:15:25 -05:00
|
|
|
query = m.SearchUsersQuery{Query: "ser1", Page: 1, Limit: 3}
|
2017-06-14 16:45:30 -05:00
|
|
|
err = SearchUsers(&query)
|
2017-03-07 09:03:54 -06:00
|
|
|
|
2017-06-14 16:45:30 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(query.Result.Users), ShouldEqual, 1)
|
|
|
|
So(query.Result.TotalCount, ShouldEqual, 1)
|
2017-03-07 09:03:54 -06:00
|
|
|
|
2017-06-19 16:15:25 -05:00
|
|
|
query = m.SearchUsersQuery{Query: "USER1", Page: 1, Limit: 3}
|
2017-06-14 16:45:30 -05:00
|
|
|
err = SearchUsers(&query)
|
2017-03-07 09:03:54 -06:00
|
|
|
|
2017-06-14 16:45:30 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(query.Result.Users), ShouldEqual, 1)
|
|
|
|
So(query.Result.TotalCount, ShouldEqual, 1)
|
2017-03-07 09:03:54 -06:00
|
|
|
|
2017-06-19 16:15:25 -05:00
|
|
|
query = m.SearchUsersQuery{Query: "idontexist", Page: 1, Limit: 3}
|
2017-06-14 16:45:30 -05:00
|
|
|
err = SearchUsers(&query)
|
2017-03-07 09:03:54 -06:00
|
|
|
|
2017-06-14 16:45:30 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(query.Result.Users), ShouldEqual, 0)
|
|
|
|
So(query.Result.TotalCount, ShouldEqual, 0)
|
|
|
|
})
|
2017-03-07 09:03:54 -06:00
|
|
|
|
2017-06-14 16:45:30 -05:00
|
|
|
Convey("Can return list of users matching query on email", func() {
|
2017-06-19 16:15:25 -05:00
|
|
|
query := m.SearchUsersQuery{Query: "ser1@test.com", Page: 1, Limit: 3}
|
2017-06-14 16:45:30 -05:00
|
|
|
err = SearchUsers(&query)
|
2017-03-07 09:03:54 -06:00
|
|
|
|
2017-06-14 16:45:30 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(query.Result.Users), ShouldEqual, 1)
|
|
|
|
So(query.Result.TotalCount, ShouldEqual, 1)
|
|
|
|
})
|
2017-03-07 09:03:54 -06:00
|
|
|
|
2017-06-14 16:45:30 -05:00
|
|
|
Convey("Can return list of users matching query on login name", func() {
|
2017-06-19 16:15:25 -05:00
|
|
|
query := m.SearchUsersQuery{Query: "loginuser1", Page: 1, Limit: 3}
|
2017-06-14 16:45:30 -05:00
|
|
|
err = SearchUsers(&query)
|
2017-03-07 09:03:54 -06:00
|
|
|
|
2017-06-14 16:45:30 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(query.Result.Users), ShouldEqual, 1)
|
|
|
|
So(query.Result.TotalCount, ShouldEqual, 1)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("when a user is an org member and has been assigned permissions", func() {
|
2018-06-13 11:01:50 -05:00
|
|
|
err = AddOrgUser(&m.AddOrgUserCommand{LoginOrEmail: users[1].Login, Role: m.ROLE_VIEWER, OrgId: users[0].OrgId, UserId: users[1].Id})
|
2017-06-14 16:45:30 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2018-06-13 11:01:50 -05:00
|
|
|
testHelperUpdateDashboardAcl(1, m.DashboardAcl{DashboardId: 1, OrgId: users[0].OrgId, UserId: users[1].Id, Permission: m.PERMISSION_EDIT})
|
2017-06-14 16:45:30 -05:00
|
|
|
So(err, ShouldBeNil)
|
2017-03-07 09:03:54 -06:00
|
|
|
|
2018-06-13 11:01:50 -05:00
|
|
|
err = SavePreferences(&m.SavePreferencesCommand{UserId: users[1].Id, OrgId: users[0].OrgId, HomeDashboardId: 1, Theme: "dark"})
|
2017-06-14 16:45:30 -05:00
|
|
|
So(err, ShouldBeNil)
|
2017-03-07 09:03:54 -06:00
|
|
|
|
2017-06-14 16:45:30 -05:00
|
|
|
Convey("when the user is deleted", func() {
|
2018-06-13 11:01:50 -05:00
|
|
|
err = DeleteUser(&m.DeleteUserCommand{UserId: users[1].Id})
|
2017-06-14 16:45:30 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
Convey("Should delete connected org users and permissions", func() {
|
2018-06-13 11:01:50 -05:00
|
|
|
query := &m.GetOrgUsersQuery{OrgId: users[0].OrgId}
|
2017-06-14 16:45:30 -05:00
|
|
|
err = GetOrgUsersForTest(query)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(len(query.Result), ShouldEqual, 1)
|
|
|
|
|
2018-06-13 11:01:50 -05:00
|
|
|
permQuery := &m.GetDashboardAclInfoListQuery{DashboardId: 1, OrgId: users[0].OrgId}
|
2017-06-19 16:30:54 -05:00
|
|
|
err = GetDashboardAclInfoList(permQuery)
|
2017-06-14 16:45:30 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(len(permQuery.Result), ShouldEqual, 0)
|
|
|
|
|
2018-06-13 11:01:50 -05:00
|
|
|
prefsQuery := &m.GetPreferencesQuery{OrgId: users[0].OrgId, UserId: users[1].Id}
|
2017-06-14 16:45:30 -05:00
|
|
|
err = GetPreferences(prefsQuery)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(prefsQuery.Result.OrgId, ShouldEqual, 0)
|
|
|
|
So(prefsQuery.Result.UserId, ShouldEqual, 0)
|
|
|
|
})
|
|
|
|
})
|
2018-12-18 12:44:17 -06:00
|
|
|
|
|
|
|
Convey("when retreiving signed in user for orgId=0 result should return active org id", func() {
|
|
|
|
ss.CacheService.Flush()
|
|
|
|
|
|
|
|
query := &m.GetSignedInUserQuery{OrgId: users[1].OrgId, UserId: users[1].Id}
|
|
|
|
err := ss.GetSignedInUserWithCache(query)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(query.Result, ShouldNotBeNil)
|
|
|
|
So(query.OrgId, ShouldEqual, users[1].OrgId)
|
|
|
|
err = SetUsingOrg(&m.SetUsingOrgCommand{UserId: users[1].Id, OrgId: users[0].OrgId})
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
query = &m.GetSignedInUserQuery{OrgId: 0, UserId: users[1].Id}
|
|
|
|
err = ss.GetSignedInUserWithCache(query)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(query.Result, ShouldNotBeNil)
|
|
|
|
So(query.Result.OrgId, ShouldEqual, users[0].OrgId)
|
|
|
|
|
|
|
|
cacheKey := newSignedInUserCacheKey(query.Result.OrgId, query.UserId)
|
|
|
|
_, found := ss.CacheService.Get(cacheKey)
|
|
|
|
So(found, ShouldBeTrue)
|
|
|
|
})
|
2017-06-14 16:45:30 -05:00
|
|
|
})
|
2017-03-07 09:03:54 -06:00
|
|
|
})
|
2018-12-01 11:28:10 -06:00
|
|
|
|
|
|
|
Convey("Given one grafana admin user", func() {
|
|
|
|
var err error
|
|
|
|
createUserCmd := &m.CreateUserCommand{
|
|
|
|
Email: fmt.Sprint("admin", "@test.com"),
|
|
|
|
Name: fmt.Sprint("admin"),
|
|
|
|
Login: fmt.Sprint("admin"),
|
|
|
|
IsAdmin: true,
|
|
|
|
}
|
|
|
|
err = CreateUser(context.Background(), createUserCmd)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
Convey("Cannot make themselves a non-admin", func() {
|
|
|
|
updateUserPermsCmd := m.UpdateUserPermissionsCommand{IsGrafanaAdmin: false, UserId: 1}
|
|
|
|
updatePermsError := UpdateUserPermissions(&updateUserPermsCmd)
|
|
|
|
|
|
|
|
So(updatePermsError, ShouldEqual, m.ErrLastGrafanaAdmin)
|
|
|
|
|
|
|
|
query := m.GetUserByIdQuery{Id: createUserCmd.Result.Id}
|
|
|
|
getUserError := GetUserById(&query)
|
|
|
|
|
|
|
|
So(getUserError, ShouldBeNil)
|
|
|
|
|
|
|
|
So(query.Result.IsAdmin, ShouldEqual, true)
|
|
|
|
})
|
|
|
|
})
|
2017-02-08 07:20:07 -06:00
|
|
|
})
|
|
|
|
}
|
2017-06-14 16:45:30 -05:00
|
|
|
|
2017-06-19 16:15:25 -05:00
|
|
|
func GetOrgUsersForTest(query *m.GetOrgUsersQuery) error {
|
|
|
|
query.Result = make([]*m.OrgUserDTO, 0)
|
2017-06-14 16:45:30 -05:00
|
|
|
sess := x.Table("org_user")
|
|
|
|
sess.Join("LEFT ", "user", fmt.Sprintf("org_user.user_id=%s.id", x.Dialect().Quote("user")))
|
|
|
|
sess.Where("org_user.org_id=?", query.OrgId)
|
|
|
|
sess.Cols("org_user.org_id", "org_user.user_id", "user.email", "user.login", "org_user.role")
|
|
|
|
|
|
|
|
err := sess.Find(&query.Result)
|
|
|
|
return err
|
|
|
|
}
|