2021-09-01 09:38:56 -05:00
|
|
|
//go:build integration
|
2020-10-16 02:46:14 -05:00
|
|
|
// +build integration
|
|
|
|
|
2014-12-19 02:43:16 -06:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
2018-06-15 14:23:57 -05:00
|
|
|
"context"
|
2019-09-03 12:34:44 -05:00
|
|
|
"fmt"
|
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
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2021-09-27 09:43:16 -05:00
|
|
|
"github.com/stretchr/testify/require"
|
2014-12-19 02:43:16 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccountDataAccess(t *testing.T) {
|
2021-09-27 09:43:16 -05:00
|
|
|
t.Run("Testing Account DB Access", func(t *testing.T) {
|
2021-03-17 10:06:10 -05:00
|
|
|
sqlStore := InitTestDB(t)
|
2014-12-19 02:43:16 -06:00
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
t.Run("Given we have organizations, we can query them by IDs", func(t *testing.T) {
|
2019-09-03 12:34:44 -05:00
|
|
|
var err error
|
2020-02-29 06:35:15 -06:00
|
|
|
var cmd *models.CreateOrgCommand
|
2019-09-03 12:34:44 -05:00
|
|
|
ids := []int64{}
|
|
|
|
|
|
|
|
for i := 1; i < 4; i++ {
|
2020-02-29 06:35:15 -06:00
|
|
|
cmd = &models.CreateOrgCommand{Name: fmt.Sprint("Org #", i)}
|
2021-11-03 05:31:56 -05:00
|
|
|
err = CreateOrg(context.Background(), cmd)
|
2021-09-27 09:43:16 -05:00
|
|
|
require.NoError(t, err)
|
2019-09-03 12:34:44 -05:00
|
|
|
|
|
|
|
ids = append(ids, cmd.Result.Id)
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
query := &models.SearchOrgsQuery{Ids: ids}
|
2021-11-03 05:31:56 -05:00
|
|
|
err = SearchOrgs(context.Background(), query)
|
2019-09-03 12:34:44 -05:00
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, len(query.Result), 3)
|
2019-09-03 12:34:44 -05:00
|
|
|
})
|
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
t.Run("Given we have organizations, we can limit and paginate search", func(t *testing.T) {
|
|
|
|
sqlStore = InitTestDB(t)
|
2020-08-12 02:59:07 -05:00
|
|
|
for i := 1; i < 4; i++ {
|
|
|
|
cmd := &models.CreateOrgCommand{Name: fmt.Sprint("Org #", i)}
|
2021-11-03 05:31:56 -05:00
|
|
|
err := CreateOrg(context.Background(), cmd)
|
2021-09-27 09:43:16 -05:00
|
|
|
require.NoError(t, err)
|
2020-08-12 02:59:07 -05:00
|
|
|
}
|
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
t.Run("Should be able to search with defaults", func(t *testing.T) {
|
2020-08-12 02:59:07 -05:00
|
|
|
query := &models.SearchOrgsQuery{}
|
2021-11-03 05:31:56 -05:00
|
|
|
err := SearchOrgs(context.Background(), query)
|
2020-08-12 02:59:07 -05:00
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, len(query.Result), 3)
|
2020-08-12 02:59:07 -05:00
|
|
|
})
|
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
t.Run("Should be able to limit search", func(t *testing.T) {
|
2020-08-12 02:59:07 -05:00
|
|
|
query := &models.SearchOrgsQuery{Limit: 1}
|
2021-11-03 05:31:56 -05:00
|
|
|
err := SearchOrgs(context.Background(), query)
|
2020-08-12 02:59:07 -05:00
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, len(query.Result), 1)
|
2020-08-12 02:59:07 -05:00
|
|
|
})
|
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
t.Run("Should be able to limit and paginate search", func(t *testing.T) {
|
2020-08-12 02:59:07 -05:00
|
|
|
query := &models.SearchOrgsQuery{Limit: 2, Page: 1}
|
2021-11-03 05:31:56 -05:00
|
|
|
err := SearchOrgs(context.Background(), query)
|
2020-08-12 02:59:07 -05:00
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, len(query.Result), 1)
|
2020-08-12 02:59:07 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
t.Run("Given single org mode", func(t *testing.T) {
|
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
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
t.Run("Users should be added to default organization", func(t *testing.T) {
|
2020-02-29 06:35:15 -06:00
|
|
|
ac1cmd := models.CreateUserCommand{Login: "ac1", Email: "ac1@test.com", Name: "ac1 name"}
|
|
|
|
ac2cmd := models.CreateUserCommand{Login: "ac2", Email: "ac2@test.com", Name: "ac2 name"}
|
2015-01-27 08:14:53 -06:00
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
ac1, err := sqlStore.CreateUser(context.Background(), ac1cmd)
|
2021-09-27 09:43:16 -05:00
|
|
|
require.NoError(t, err)
|
2021-03-17 10:06:10 -05:00
|
|
|
ac2, err := sqlStore.CreateUser(context.Background(), ac2cmd)
|
2021-09-27 09:43:16 -05:00
|
|
|
require.NoError(t, err)
|
2015-01-27 08:14:53 -06:00
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
q1 := models.GetUserOrgListQuery{UserId: ac1.Id}
|
|
|
|
q2 := models.GetUserOrgListQuery{UserId: ac2.Id}
|
2021-10-04 08:46:09 -05:00
|
|
|
err = GetUserOrgList(context.Background(), &q1)
|
2021-09-27 09:43:16 -05:00
|
|
|
require.NoError(t, err)
|
2021-10-04 08:46:09 -05:00
|
|
|
err = GetUserOrgList(context.Background(), &q2)
|
2021-09-27 09:43:16 -05:00
|
|
|
require.NoError(t, err)
|
2015-01-27 08:14:53 -06:00
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
require.Equal(t, q1.Result[0].OrgId, q2.Result[0].OrgId)
|
|
|
|
require.Equal(t, string(q1.Result[0].Role), "Viewer")
|
2015-01-27 08:14:53 -06:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
t.Run("Given single org and 2 users inserted", func(t *testing.T) {
|
|
|
|
sqlStore = InitTestDB(t)
|
2021-05-12 07:10:35 -05:00
|
|
|
setting.AutoAssignOrg = true
|
|
|
|
setting.AutoAssignOrgId = 1
|
|
|
|
setting.AutoAssignOrgRole = "Viewer"
|
|
|
|
|
|
|
|
ac1cmd := models.CreateUserCommand{Login: "ac1", Email: "ac1@test.com", Name: "ac1 name"}
|
|
|
|
ac2cmd := models.CreateUserCommand{Login: "ac2", Email: "ac2@test.com", Name: "ac2 name"}
|
|
|
|
|
|
|
|
ac1, err := sqlStore.CreateUser(context.Background(), ac1cmd)
|
2021-09-27 09:43:16 -05:00
|
|
|
require.NoError(t, err)
|
2021-05-12 07:10:35 -05:00
|
|
|
_, err = sqlStore.CreateUser(context.Background(), ac2cmd)
|
2021-09-27 09:43:16 -05:00
|
|
|
require.NoError(t, err)
|
2021-05-12 07:10:35 -05:00
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
t.Run("Can get organization users paginated with query", func(t *testing.T) {
|
2021-05-12 07:10:35 -05:00
|
|
|
query := models.SearchOrgUsersQuery{
|
|
|
|
OrgID: ac1.OrgId,
|
|
|
|
Page: 1,
|
|
|
|
}
|
2021-09-27 09:43:16 -05:00
|
|
|
err = sqlStore.SearchOrgUsers(context.Background(), &query)
|
2021-05-12 07:10:35 -05:00
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, len(query.Result.OrgUsers), 2)
|
2021-05-12 07:10:35 -05:00
|
|
|
})
|
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
t.Run("Can get organization users paginated and limited", func(t *testing.T) {
|
2021-05-12 07:10:35 -05:00
|
|
|
query := models.SearchOrgUsersQuery{
|
|
|
|
OrgID: ac1.OrgId,
|
|
|
|
Limit: 1,
|
|
|
|
Page: 1,
|
|
|
|
}
|
2021-09-27 09:43:16 -05:00
|
|
|
err = sqlStore.SearchOrgUsers(context.Background(), &query)
|
2021-05-12 07:10:35 -05:00
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, len(query.Result.OrgUsers), 1)
|
2021-05-12 07:10:35 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
t.Run("Given two saved users", func(t *testing.T) {
|
|
|
|
sqlStore = InitTestDB(t)
|
2015-03-11 10:19:29 -05:00
|
|
|
setting.AutoAssignOrg = false
|
2015-01-27 08:14:53 -06:00
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
ac1cmd := models.CreateUserCommand{Login: "ac1", Email: "ac1@test.com", Name: "ac1 name"}
|
|
|
|
ac2cmd := models.CreateUserCommand{Login: "ac2", Email: "ac2@test.com", Name: "ac2 name", IsAdmin: true}
|
2021-11-23 17:06:40 -06:00
|
|
|
serviceaccountcmd := models.CreateUserCommand{Login: "serviceaccount", Email: "service@test.com", Name: "serviceaccount name", IsAdmin: true, IsServiceAccount: true}
|
2014-12-19 04:53:27 -06:00
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
ac1, err := sqlStore.CreateUser(context.Background(), ac1cmd)
|
2021-09-27 09:43:16 -05:00
|
|
|
require.NoError(t, err)
|
2021-03-17 10:06:10 -05:00
|
|
|
ac2, err := sqlStore.CreateUser(context.Background(), ac2cmd)
|
2021-09-27 09:43:16 -05:00
|
|
|
require.NoError(t, err)
|
2021-11-23 17:06:40 -06:00
|
|
|
// user only used for making sure we filter out the service accounts
|
|
|
|
_, err = sqlStore.CreateUser(context.Background(), serviceaccountcmd)
|
|
|
|
require.NoError(t, err)
|
2014-12-19 02:43:16 -06:00
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
t.Run("Should be able to read user info projection", func(t *testing.T) {
|
2020-02-29 06:35:15 -06:00
|
|
|
query := models.GetUserProfileQuery{UserId: ac1.Id}
|
2021-10-04 08:46:09 -05:00
|
|
|
err = sqlStore.GetUserProfile(context.Background(), &query)
|
2014-12-19 02:43:16 -06:00
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, query.Result.Email, "ac1@test.com")
|
|
|
|
require.Equal(t, query.Result.Login, "ac1")
|
2015-01-16 00:45:37 -06:00
|
|
|
})
|
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
t.Run("Can search users", func(t *testing.T) {
|
2020-02-29 06:35:15 -06:00
|
|
|
query := models.SearchUsersQuery{Query: ""}
|
2021-10-04 08:46:09 -05:00
|
|
|
err := SearchUsers(context.Background(), &query)
|
2015-01-16 00:45:37 -06:00
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, query.Result.Users[0].Email, "ac1@test.com")
|
|
|
|
require.Equal(t, query.Result.Users[1].Email, "ac2@test.com")
|
2014-12-19 03:45:22 -06:00
|
|
|
})
|
2014-12-19 02:43:16 -06:00
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
t.Run("Given an added org user", func(t *testing.T) {
|
2020-02-29 06:35:15 -06:00
|
|
|
cmd := models.AddOrgUserCommand{
|
2015-02-23 13:48:43 -06:00
|
|
|
OrgId: ac1.OrgId,
|
|
|
|
UserId: ac2.Id,
|
2020-02-29 06:35:15 -06:00
|
|
|
Role: models.ROLE_VIEWER,
|
2014-12-19 03:45:22 -06:00
|
|
|
}
|
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
err := sqlStore.AddOrgUser(context.Background(), &cmd)
|
|
|
|
t.Run("Should have been saved without error", func(t *testing.T) {
|
|
|
|
require.NoError(t, err)
|
2014-12-19 03:45:22 -06:00
|
|
|
})
|
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
t.Run("Can update org user role", func(t *testing.T) {
|
2020-02-29 06:35:15 -06:00
|
|
|
updateCmd := models.UpdateOrgUserCommand{OrgId: ac1.OrgId, UserId: ac2.Id, Role: models.ROLE_ADMIN}
|
2021-09-27 09:43:16 -05:00
|
|
|
err = sqlStore.UpdateOrgUser(context.Background(), &updateCmd)
|
|
|
|
require.NoError(t, err)
|
2015-05-01 02:48:07 -05:00
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
orgUsersQuery := models.GetOrgUsersQuery{OrgId: ac1.OrgId}
|
2021-09-27 09:43:16 -05:00
|
|
|
err = sqlStore.GetOrgUsers(context.Background(), &orgUsersQuery)
|
|
|
|
require.NoError(t, err)
|
2015-05-01 02:48:07 -05:00
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
require.EqualValues(t, orgUsersQuery.Result[1].Role, models.ROLE_ADMIN)
|
2015-05-01 02:48:07 -05:00
|
|
|
})
|
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
t.Run("Can get logged in user projection", func(t *testing.T) {
|
2020-02-29 06:35:15 -06:00
|
|
|
query := models.GetSignedInUserQuery{UserId: ac2.Id}
|
2021-05-18 11:24:42 -05:00
|
|
|
err := GetSignedInUser(context.Background(), &query)
|
2015-01-16 07:32:18 -06:00
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, query.Result.Email, "ac2@test.com")
|
|
|
|
require.Equal(t, query.Result.OrgId, ac2.OrgId)
|
|
|
|
require.Equal(t, query.Result.Name, "ac2 name")
|
|
|
|
require.Equal(t, query.Result.Login, "ac2")
|
|
|
|
require.EqualValues(t, query.Result.OrgRole, "Admin")
|
|
|
|
require.Equal(t, query.Result.OrgName, "ac2@test.com")
|
|
|
|
require.Equal(t, query.Result.IsGrafanaAdmin, true)
|
2015-01-16 07:32:18 -06:00
|
|
|
})
|
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
t.Run("Can get user organizations", func(t *testing.T) {
|
2020-02-29 06:35:15 -06:00
|
|
|
query := models.GetUserOrgListQuery{UserId: ac2.Id}
|
2021-10-04 08:46:09 -05:00
|
|
|
err := GetUserOrgList(context.Background(), &query)
|
2014-12-19 04:08:49 -06:00
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, len(query.Result), 2)
|
2014-12-19 04:08:49 -06:00
|
|
|
})
|
2014-12-19 06:12:47 -06:00
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
t.Run("Can get organization users", func(t *testing.T) {
|
2020-02-29 06:35:15 -06:00
|
|
|
query := models.GetOrgUsersQuery{OrgId: ac1.OrgId}
|
2021-09-27 09:43:16 -05:00
|
|
|
err := sqlStore.GetOrgUsers(context.Background(), &query)
|
2015-01-20 08:23:14 -06:00
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, len(query.Result), 2)
|
|
|
|
require.Equal(t, query.Result[0].Role, "Admin")
|
2015-01-20 08:23:14 -06:00
|
|
|
})
|
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
t.Run("Can get organization users with query", func(t *testing.T) {
|
2020-02-29 06:35:15 -06:00
|
|
|
query := models.GetOrgUsersQuery{
|
2018-02-09 03:42:37 -06:00
|
|
|
OrgId: ac1.OrgId,
|
|
|
|
Query: "ac1",
|
|
|
|
}
|
2021-09-27 09:43:16 -05:00
|
|
|
err := sqlStore.GetOrgUsers(context.Background(), &query)
|
2018-02-09 03:42:37 -06:00
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, len(query.Result), 1)
|
|
|
|
require.Equal(t, query.Result[0].Email, ac1.Email)
|
2018-02-09 03:42:37 -06:00
|
|
|
})
|
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
t.Run("Can get organization users with query and limit", func(t *testing.T) {
|
2020-02-29 06:35:15 -06:00
|
|
|
query := models.GetOrgUsersQuery{
|
2018-02-09 03:42:37 -06:00
|
|
|
OrgId: ac1.OrgId,
|
|
|
|
Query: "ac",
|
|
|
|
Limit: 1,
|
|
|
|
}
|
2021-09-27 09:43:16 -05:00
|
|
|
err := sqlStore.GetOrgUsers(context.Background(), &query)
|
2018-02-09 03:42:37 -06:00
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, len(query.Result), 1)
|
|
|
|
require.Equal(t, query.Result[0].Email, ac1.Email)
|
2018-02-09 03:42:37 -06:00
|
|
|
})
|
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
t.Run("Can set using org", func(t *testing.T) {
|
2020-02-29 06:35:15 -06:00
|
|
|
cmd := models.SetUsingOrgCommand{UserId: ac2.Id, OrgId: ac1.OrgId}
|
2021-10-04 08:46:09 -05:00
|
|
|
err := SetUsingOrg(context.Background(), &cmd)
|
2021-09-27 09:43:16 -05:00
|
|
|
require.NoError(t, err)
|
2015-01-16 07:32:18 -06:00
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
t.Run("SignedInUserQuery with a different org", func(t *testing.T) {
|
2020-02-29 06:35:15 -06:00
|
|
|
query := models.GetSignedInUserQuery{UserId: ac2.Id}
|
2021-05-18 11:24:42 -05:00
|
|
|
err := GetSignedInUser(context.Background(), &query)
|
2015-01-16 07:32:18 -06:00
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, query.Result.OrgId, ac1.OrgId)
|
|
|
|
require.Equal(t, query.Result.Email, "ac2@test.com")
|
|
|
|
require.Equal(t, query.Result.Name, "ac2 name")
|
|
|
|
require.Equal(t, query.Result.Login, "ac2")
|
|
|
|
require.Equal(t, query.Result.OrgName, "ac1@test.com")
|
|
|
|
// require.Equal(t, query.Result.OrgRole, "Viewer")
|
2015-01-16 07:32:18 -06:00
|
|
|
})
|
2018-06-13 11:01:50 -05:00
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
t.Run("Should set last org as current when removing user from current", func(t *testing.T) {
|
2020-02-29 06:35:15 -06:00
|
|
|
remCmd := models.RemoveOrgUserCommand{OrgId: ac1.OrgId, UserId: ac2.Id}
|
2021-09-27 09:43:16 -05:00
|
|
|
err := sqlStore.RemoveOrgUser(context.Background(), &remCmd)
|
|
|
|
require.NoError(t, err)
|
2018-06-13 11:01:50 -05:00
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
query := models.GetSignedInUserQuery{UserId: ac2.Id}
|
2021-05-18 11:24:42 -05:00
|
|
|
err = GetSignedInUser(context.Background(), &query)
|
2018-06-13 11:01:50 -05:00
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, query.Result.OrgId, ac2.OrgId)
|
2018-06-13 11:01:50 -05:00
|
|
|
})
|
2014-12-19 06:12:47 -06:00
|
|
|
})
|
2015-01-20 08:48:19 -06:00
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
t.Run("Removing user from org should delete user completely if in no other org", func(t *testing.T) {
|
2018-10-11 00:48:35 -05:00
|
|
|
// make sure ac2 has no org
|
2021-11-03 05:31:56 -05:00
|
|
|
err := DeleteOrg(context.Background(), &models.DeleteOrgCommand{Id: ac2.OrgId})
|
2021-09-27 09:43:16 -05:00
|
|
|
require.NoError(t, err)
|
2018-10-11 00:48:35 -05:00
|
|
|
|
2018-12-04 16:29:34 -06:00
|
|
|
// remove ac2 user from ac1 org
|
2020-02-29 06:35:15 -06:00
|
|
|
remCmd := models.RemoveOrgUserCommand{OrgId: ac1.OrgId, UserId: ac2.Id, ShouldDeleteOrphanedUser: true}
|
2021-09-27 09:43:16 -05:00
|
|
|
err = sqlStore.RemoveOrgUser(context.Background(), &remCmd)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, remCmd.UserWasDeleted)
|
2018-10-11 00:48:35 -05:00
|
|
|
|
2021-05-18 11:24:42 -05:00
|
|
|
err = GetSignedInUser(context.Background(), &models.GetSignedInUserQuery{UserId: ac2.Id})
|
2021-09-27 09:43:16 -05:00
|
|
|
require.Equal(t, err, models.ErrUserNotFound)
|
2018-10-11 00:48:35 -05:00
|
|
|
})
|
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
t.Run("Cannot delete last admin org user", func(t *testing.T) {
|
2020-02-29 06:35:15 -06:00
|
|
|
cmd := models.RemoveOrgUserCommand{OrgId: ac1.OrgId, UserId: ac1.Id}
|
2021-09-27 09:43:16 -05:00
|
|
|
err := sqlStore.RemoveOrgUser(context.Background(), &cmd)
|
|
|
|
require.Equal(t, err, models.ErrLastOrgAdmin)
|
2015-01-20 08:48:19 -06:00
|
|
|
})
|
2015-05-19 04:47:14 -05:00
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
t.Run("Cannot update role so no one is admin user", func(t *testing.T) {
|
2020-02-29 06:35:15 -06:00
|
|
|
cmd := models.UpdateOrgUserCommand{OrgId: ac1.OrgId, UserId: ac1.Id, Role: models.ROLE_VIEWER}
|
2021-09-27 09:43:16 -05:00
|
|
|
err := sqlStore.UpdateOrgUser(context.Background(), &cmd)
|
|
|
|
require.Equal(t, err, models.ErrLastOrgAdmin)
|
2015-05-19 04:47:14 -05:00
|
|
|
})
|
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
t.Run("Given an org user with dashboard permissions", func(t *testing.T) {
|
2020-02-29 06:35:15 -06:00
|
|
|
ac3cmd := models.CreateUserCommand{Login: "ac3", Email: "ac3@test.com", Name: "ac3 name", IsAdmin: false}
|
2021-03-17 10:06:10 -05:00
|
|
|
ac3, err := sqlStore.CreateUser(context.Background(), ac3cmd)
|
2021-09-27 09:43:16 -05:00
|
|
|
require.NoError(t, err)
|
2017-06-15 13:19:54 -05:00
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
orgUserCmd := models.AddOrgUserCommand{
|
2017-06-15 13:19:54 -05:00
|
|
|
OrgId: ac1.OrgId,
|
|
|
|
UserId: ac3.Id,
|
2020-02-29 06:35:15 -06:00
|
|
|
Role: models.ROLE_VIEWER,
|
2017-06-15 13:19:54 -05:00
|
|
|
}
|
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
err = sqlStore.AddOrgUser(context.Background(), &orgUserCmd)
|
|
|
|
require.NoError(t, err)
|
2017-06-15 13:19:54 -05:00
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
query := models.GetOrgUsersQuery{OrgId: ac1.OrgId}
|
2021-09-27 09:43:16 -05:00
|
|
|
err = sqlStore.GetOrgUsers(context.Background(), &query)
|
|
|
|
require.NoError(t, err)
|
|
|
|
// require.Equal(t, len(query.Result), 3)
|
2017-06-15 13:19:54 -05:00
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
dash1 := insertTestDashboard(t, sqlStore, "1 test dash", ac1.OrgId, 0, false, "prod", "webapp")
|
|
|
|
dash2 := insertTestDashboard(t, sqlStore, "2 test dash", ac3.OrgId, 0, false, "prod", "webapp")
|
2018-02-14 08:04:26 -06:00
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
err = testHelperUpdateDashboardAcl(t, sqlStore, dash1.Id, models.DashboardAcl{
|
|
|
|
DashboardID: dash1.Id, OrgID: ac1.OrgId, UserID: ac3.Id, Permission: models.PERMISSION_EDIT,
|
|
|
|
})
|
2021-09-27 09:43:16 -05:00
|
|
|
require.NoError(t, err)
|
2017-06-15 13:19:54 -05:00
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
err = testHelperUpdateDashboardAcl(t, sqlStore, dash2.Id, models.DashboardAcl{
|
|
|
|
DashboardID: dash2.Id, OrgID: ac3.OrgId, UserID: ac3.Id, Permission: models.PERMISSION_EDIT,
|
|
|
|
})
|
2021-09-27 09:43:16 -05:00
|
|
|
require.NoError(t, err)
|
2017-06-15 13:19:54 -05:00
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
t.Run("When org user is deleted", func(t *testing.T) {
|
2020-02-29 06:35:15 -06:00
|
|
|
cmdRemove := models.RemoveOrgUserCommand{OrgId: ac1.OrgId, UserId: ac3.Id}
|
2021-09-27 09:43:16 -05:00
|
|
|
err := sqlStore.RemoveOrgUser(context.Background(), &cmdRemove)
|
|
|
|
require.NoError(t, err)
|
2017-06-15 13:19:54 -05:00
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
t.Run("Should remove dependent permissions for deleted org user", func(t *testing.T) {
|
2021-04-21 03:22:46 -05:00
|
|
|
permQuery := &models.GetDashboardAclInfoListQuery{DashboardID: dash1.Id, OrgID: ac1.OrgId}
|
2021-09-27 09:43:16 -05:00
|
|
|
|
2021-09-23 10:43:32 -05:00
|
|
|
err = sqlStore.GetDashboardAclInfoList(context.Background(), permQuery)
|
2021-09-27 09:43:16 -05:00
|
|
|
require.NoError(t, err)
|
2017-06-15 13:19:54 -05:00
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
require.Equal(t, len(permQuery.Result), 0)
|
2017-06-15 13:19:54 -05:00
|
|
|
})
|
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
t.Run("Should not remove dashboard permissions for same user in another org", func(t *testing.T) {
|
2021-04-21 03:22:46 -05:00
|
|
|
permQuery := &models.GetDashboardAclInfoListQuery{DashboardID: dash2.Id, OrgID: ac3.OrgId}
|
2021-09-27 09:43:16 -05:00
|
|
|
|
2021-09-23 10:43:32 -05:00
|
|
|
err = sqlStore.GetDashboardAclInfoList(context.Background(), permQuery)
|
2021-09-27 09:43:16 -05:00
|
|
|
require.NoError(t, err)
|
2017-06-15 13:19:54 -05:00
|
|
|
|
2021-09-27 09:43:16 -05:00
|
|
|
require.Equal(t, len(permQuery.Result), 1)
|
|
|
|
require.Equal(t, permQuery.Result[0].OrgId, ac3.OrgId)
|
|
|
|
require.Equal(t, permQuery.Result[0].UserId, ac3.Id)
|
2017-06-15 13:19:54 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-12-19 03:45:22 -06:00
|
|
|
})
|
2014-12-19 02:43:16 -06:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2018-02-14 08:04:26 -06:00
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
func testHelperUpdateDashboardAcl(t *testing.T, sqlStore *SQLStore, dashboardID int64,
|
|
|
|
items ...models.DashboardAcl) error {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
var itemPtrs []*models.DashboardAcl
|
|
|
|
for _, it := range items {
|
|
|
|
item := it
|
2018-03-15 18:08:25 -05:00
|
|
|
item.Created = time.Now()
|
|
|
|
item.Updated = time.Now()
|
2021-03-17 10:06:10 -05:00
|
|
|
itemPtrs = append(itemPtrs, &item)
|
2018-02-14 08:04:26 -06:00
|
|
|
}
|
2021-12-22 04:02:42 -06:00
|
|
|
return sqlStore.UpdateDashboardACL(context.Background(), dashboardID, itemPtrs)
|
2018-02-14 08:04:26 -06:00
|
|
|
}
|