* Revert "chore: add replDB to team service (#91799)" This reverts commitc6ae2d7999. * Revert "experiment: use read replica for Get and Find Dashboards (#91706)" This reverts commit54177ca619. * Revert "QuotaService: refactor to use ReplDB for Get queries (#91333)" This reverts commit299c142f6a. * Revert "refactor replCfg to look more like plugins/plugin config (#91142)" This reverts commitac0b4bb34d. * Revert "chore (replstore): fix registration with multiple sql drivers, again (#90990)" This reverts commitdaedb358dd. * Revert "Chore (sqlstore): add validation and testing for repl config (#90683)" This reverts commitaf19f039b6. * Revert "ReplStore: Add support for round robin load balancing between multiple read replicas (#90530)" This reverts commit27b52b1507. * Revert "DashboardStore: Use ReplDB and get dashboard quotas from the ReadReplica (#90235)" This reverts commit8a6107cd35. * Revert "accesscontrol service read replica (#89963)" This reverts commit77a4869fca. * Revert "Fix: add mapping for the new mysqlRepl driver (#89551)" This reverts commitab5a079bcc. * Revert "fix: sql instrumentation dual registration error (#89508)" This reverts commitd988f5c3b0. * Revert "Experimental Feature Toggle: databaseReadReplica (#89232)" This reverts commit50244ed4a1.
99 lines
2.6 KiB
Go
99 lines
2.6 KiB
Go
package testutil
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/db"
|
|
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
|
dashboardstore "github.com/grafana/grafana/pkg/services/dashboards/database"
|
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
|
"github.com/grafana/grafana/pkg/services/quota/quotatest"
|
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
|
"github.com/grafana/grafana/pkg/services/tag/tagimpl"
|
|
"github.com/grafana/grafana/pkg/services/user"
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func SetupRBACRole(t *testing.T, db db.DB, user *user.SignedInUser) *accesscontrol.Role {
|
|
t.Helper()
|
|
|
|
var role *accesscontrol.Role
|
|
err := db.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
|
|
role = &accesscontrol.Role{
|
|
OrgID: user.OrgID,
|
|
UID: "test_role",
|
|
Name: "test:role",
|
|
Updated: time.Now(),
|
|
Created: time.Now(),
|
|
}
|
|
_, err := sess.Insert(role)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = sess.Insert(accesscontrol.UserRole{
|
|
OrgID: role.OrgID,
|
|
RoleID: role.ID,
|
|
UserID: user.UserID,
|
|
Created: time.Now(),
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
return role
|
|
}
|
|
|
|
func SetupRBACPermission(t *testing.T, db db.DB, role *accesscontrol.Role, user *user.SignedInUser) {
|
|
t.Helper()
|
|
|
|
err := db.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
|
|
if _, err := sess.Exec("DELETE FROM permission WHERE role_id = ?", role.ID); err != nil {
|
|
return err
|
|
}
|
|
|
|
var acPermission []accesscontrol.Permission
|
|
for action, scopes := range user.Permissions[user.OrgID] {
|
|
for _, scope := range scopes {
|
|
p := accesscontrol.Permission{RoleID: role.ID, Action: action, Scope: scope, Created: time.Now(), Updated: time.Now()}
|
|
p.Kind, p.Attribute, p.Identifier = p.SplitScope()
|
|
acPermission = append(acPermission, p)
|
|
}
|
|
}
|
|
|
|
if _, err := sess.InsertMulti(&acPermission); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
func CreateDashboard(t *testing.T, db db.DB, cfg *setting.Cfg, features featuremgmt.FeatureToggles, cmd dashboards.SaveDashboardCommand) *dashboards.Dashboard {
|
|
t.Helper()
|
|
|
|
dashboardStore, err := dashboardstore.ProvideDashboardStore(
|
|
db,
|
|
cfg,
|
|
features,
|
|
tagimpl.ProvideService(db),
|
|
quotatest.New(false, nil),
|
|
)
|
|
require.NoError(t, err)
|
|
|
|
dash, err := dashboardStore.SaveDashboard(context.Background(), cmd)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, dash)
|
|
|
|
return dash
|
|
}
|