Chore: Reduce the usage of sqlstore.createorg and use orgservice instead (#59356)

* remove legacy createorg from org service

* remove another createorg from orgimpl

* remove createorg from api pref tests

* remove createorg from api org tests

* fix tests

* remove createorg from annotations test

* remove createorg from team tests

* remove createorg from service accounts

* remove createorg from accesscontrol tests

* remove createorg from provisioning

* Use quotaservice from sc.hs
This commit is contained in:
Serge Zaitsev
2022-11-28 12:05:46 +01:00
committed by GitHub
parent 92e3ee7d89
commit 5b861faec3
11 changed files with 112 additions and 114 deletions
@@ -10,8 +10,10 @@ import (
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/accesscontrol"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/org/orgimpl"
"github.com/grafana/grafana/pkg/services/quota/quotatest"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/user"
)
@@ -435,8 +437,10 @@ func TestIntegrationStore_GetResourcePermissions(t *testing.T) {
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
store, sql := setupTestEnv(t)
orgService, err := orgimpl.ProvideService(sql, sql.Cfg, quotatest.New(false, nil))
require.NoError(t, err)
err := sql.WithDbSession(context.Background(), func(sess *db.Session) error {
err = sql.WithDbSession(context.Background(), func(sess *db.Session) error {
role := &accesscontrol.Role{
OrgID: tt.user.OrgID,
UID: "seeded",
@@ -471,7 +475,7 @@ func TestIntegrationStore_GetResourcePermissions(t *testing.T) {
})
require.NoError(t, err)
seedResourcePermissions(t, store, sql, tt.query.Actions, tt.query.Resource, tt.query.ResourceID, tt.query.ResourceAttribute, tt.numUsers)
seedResourcePermissions(t, store, sql, orgService, tt.query.Actions, tt.query.Resource, tt.query.ResourceID, tt.query.ResourceAttribute, tt.numUsers)
tt.query.User = tt.user
permissions, err := store.GetResourcePermissions(context.Background(), tt.user.OrgID, tt.query)
@@ -481,21 +485,20 @@ func TestIntegrationStore_GetResourcePermissions(t *testing.T) {
}
}
func seedResourcePermissions(t *testing.T, store *store, sql *sqlstore.SQLStore, actions []string, resource, resourceID, resourceAttribute string, numUsers int) {
func seedResourcePermissions(t *testing.T, store *store, sql *sqlstore.SQLStore, orgService org.Service, actions []string, resource, resourceID, resourceAttribute string, numUsers int) {
t.Helper()
var org *models.Org
var orgModel *org.Org
for i := 0; i < numUsers; i++ {
if org == nil {
cmd := &models.CreateOrgCommand{Name: "test", UserId: int64(i)}
err := sql.CreateOrg(context.Background(), cmd)
if orgModel == nil {
cmd := &org.CreateOrgCommand{Name: "test", UserID: int64(i)}
addedOrg, err := orgService.CreateWithMember(context.Background(), cmd)
require.NoError(t, err)
addedOrg := cmd.Result
org = &addedOrg
orgModel = addedOrg
}
u, err := sql.CreateUser(context.Background(), user.CreateUserCommand{
Login: fmt.Sprintf("user:%s%d", resourceID, i),
OrgID: org.Id,
OrgID: orgModel.ID,
})
require.NoError(t, err)
+30 -39
View File
@@ -173,21 +173,21 @@ func TestIntegrationOrgDataAccess(t *testing.T) {
})
t.Run("Testing Account DB Access", func(t *testing.T) {
sqlStore := db.InitTestDB(t)
ss := db.InitTestDB(t)
orgStore = sqlStore{
db: ss,
dialect: ss.GetDialect(),
}
ids := []int64{}
for i := 1; i < 4; i++ {
cmd := &org.CreateOrgCommand{Name: fmt.Sprint("Org #", i)}
res, err := orgStore.CreateWithMember(context.Background(), cmd)
require.NoError(t, err)
ids = append(ids, res.ID)
}
t.Run("Given we have organizations, we can query them by IDs", func(t *testing.T) {
var err error
var cmd *models.CreateOrgCommand
ids := []int64{}
for i := 1; i < 4; i++ {
cmd = &models.CreateOrgCommand{Name: fmt.Sprint("Org #", i)}
err = sqlStore.CreateOrg(context.Background(), cmd)
require.NoError(t, err)
ids = append(ids, cmd.Result.Id)
}
query := &org.SearchOrgsQuery{IDs: ids}
queryResult, err := orgStore.Search(context.Background(), query)
@@ -195,37 +195,28 @@ func TestIntegrationOrgDataAccess(t *testing.T) {
require.Equal(t, len(queryResult), 3)
})
t.Run("Given we have organizations, we can limit and paginate search", func(t *testing.T) {
sqlStore = db.InitTestDB(t)
for i := 1; i < 4; i++ {
cmd := &models.CreateOrgCommand{Name: fmt.Sprint("Org #", i)}
err := sqlStore.CreateOrg(context.Background(), cmd)
require.NoError(t, err)
}
t.Run("Should be able to search with defaults", func(t *testing.T) {
query := &org.SearchOrgsQuery{}
queryResult, err := orgStore.Search(context.Background(), query)
t.Run("Should be able to search with defaults", func(t *testing.T) {
query := &org.SearchOrgsQuery{}
queryResult, err := orgStore.Search(context.Background(), query)
require.NoError(t, err)
require.Equal(t, len(queryResult), 3)
})
require.NoError(t, err)
require.Equal(t, len(queryResult), 3)
})
t.Run("Should be able to limit search", func(t *testing.T) {
query := &org.SearchOrgsQuery{Limit: 1}
queryResult, err := orgStore.Search(context.Background(), query)
t.Run("Should be able to limit search", func(t *testing.T) {
query := &org.SearchOrgsQuery{Limit: 1}
queryResult, err := orgStore.Search(context.Background(), query)
require.NoError(t, err)
require.Equal(t, len(queryResult), 1)
})
require.NoError(t, err)
require.Equal(t, len(queryResult), 1)
})
t.Run("Should be able to limit and paginate search", func(t *testing.T) {
query := &org.SearchOrgsQuery{Limit: 2, Page: 1}
queryResult, err := orgStore.Search(context.Background(), query)
t.Run("Should be able to limit and paginate search", func(t *testing.T) {
query := &org.SearchOrgsQuery{Limit: 2, Page: 1}
queryResult, err := orgStore.Search(context.Background(), query)
require.NoError(t, err)
require.Equal(t, len(queryResult), 1)
})
require.NoError(t, err)
require.Equal(t, len(queryResult), 1)
})
})
}
@@ -3,14 +3,12 @@ package dashboards
import (
"context"
"errors"
"fmt"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/org/orgtest"
@@ -26,7 +24,7 @@ var (
func TestDashboardsAsConfig(t *testing.T) {
t.Run("Dashboards as configuration", func(t *testing.T) {
logger := log.New("test-logger")
store := db.InitTestDB(t)
// store := db.InitTestDB(t)
orgFake := orgtest.NewOrgServiceFake()
t.Run("Should fail if orgs don't exist in the database", func(t *testing.T) {
@@ -38,12 +36,6 @@ func TestDashboardsAsConfig(t *testing.T) {
orgFake.ExpectedError = nil
})
for i := 1; i <= 2; i++ {
orgCommand := models.CreateOrgCommand{Name: fmt.Sprintf("Main Org. %v", i)}
err := store.CreateOrg(context.Background(), &orgCommand)
require.NoError(t, err)
}
t.Run("default values should be applied", func(t *testing.T) {
cfgProvider := configReader{path: appliedDefaults, log: logger, orgService: orgFake}
cfg, err := cfgProvider.readConfig(context.Background())
@@ -14,7 +14,8 @@ import (
encryptionservice "github.com/grafana/grafana/pkg/services/encryption/service"
"github.com/grafana/grafana/pkg/services/notifications"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/org/orgtest"
"github.com/grafana/grafana/pkg/services/org/orgimpl"
"github.com/grafana/grafana/pkg/services/quota/quotatest"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/stretchr/testify/require"
@@ -35,24 +36,22 @@ var (
func TestNotificationAsConfig(t *testing.T) {
var sqlStore *sqlstore.SQLStore
var orgFake org.Service
var orgService org.Service
var ns *alerting.AlertNotificationService
logger := log.New("fake.log")
orgService := orgtest.NewOrgServiceFake()
orgService.ExpectedOrg = &org.Org{}
encryptionService := encryptionservice.SetupTestService(t)
t.Run("Testing notification as configuration", func(t *testing.T) {
setup := func() {
sqlStore = db.InitTestDB(t)
orgFake = orgtest.NewOrgServiceFake()
orgService, _ = orgimpl.ProvideService(sqlStore, sqlStore.Cfg, quotatest.New(false, nil))
nm := &notifications.NotificationService{}
ns = alerting.ProvideService(sqlStore, encryptionService, nm)
for i := 1; i < 5; i++ {
orgCommand := models.CreateOrgCommand{Name: fmt.Sprintf("Main Org. %v", i)}
err := sqlStore.CreateOrg(context.Background(), &orgCommand)
orgCommand := org.CreateOrgCommand{Name: fmt.Sprintf("Main Org. %v", i)}
_, err := orgService.CreateWithMember(context.Background(), &orgCommand)
require.NoError(t, err)
}
@@ -73,7 +72,7 @@ func TestNotificationAsConfig(t *testing.T) {
setup()
_ = os.Setenv("TEST_VAR", "default")
cfgProvider := &configReader{
orgService: orgFake,
orgService: orgService,
encryptionService: encryptionService,
log: log.New("test logger"),
}
@@ -282,7 +281,7 @@ func TestNotificationAsConfig(t *testing.T) {
t.Run("Broken yaml should return error", func(t *testing.T) {
reader := &configReader{
orgService: orgFake,
orgService: orgService,
encryptionService: encryptionService,
log: log.New("test logger"),
}
@@ -293,7 +292,7 @@ func TestNotificationAsConfig(t *testing.T) {
t.Run("Skip invalid directory", func(t *testing.T) {
cfgProvider := &configReader{
orgService: orgFake,
orgService: orgService,
encryptionService: encryptionService,
log: log.New("test logger"),
}
@@ -307,7 +306,7 @@ func TestNotificationAsConfig(t *testing.T) {
t.Run("Unknown notifier should return error", func(t *testing.T) {
cfgProvider := &configReader{
orgService: orgFake,
orgService: orgService,
encryptionService: encryptionService,
log: log.New("test logger"),
}
@@ -318,7 +317,7 @@ func TestNotificationAsConfig(t *testing.T) {
t.Run("Read incorrect properties", func(t *testing.T) {
cfgProvider := &configReader{
orgService: orgFake,
orgService: orgService,
encryptionService: encryptionService,
log: log.New("test logger"),
}
+2 -2
View File
@@ -61,8 +61,8 @@ func TestServiceAccountsAPI_CreateServiceAccount(t *testing.T) {
store.Cfg.AutoAssignOrg = autoAssignOrg
}()
orgCmd := &models.CreateOrgCommand{Name: "Some Test Org"}
err = store.CreateOrg(context.Background(), orgCmd)
orgCmd := &org.CreateOrgCommand{Name: "Some Test Org"}
_, err = orgService.CreateWithMember(context.Background(), orgCmd)
require.Nil(t, err)
type testCreateSATestCase struct {
@@ -10,7 +10,6 @@ import (
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/kvstore"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/apikey/apikeyimpl"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/org/orgimpl"
@@ -43,13 +42,13 @@ func TestStore_CreateServiceAccountOrgNonExistant(t *testing.T) {
func TestStore_CreateServiceAccount(t *testing.T) {
_, store := setupTestDatabase(t)
orgQuery := &models.CreateOrgCommand{Name: sqlstore.MainOrgName}
err := store.sqlStore.CreateOrg(context.Background(), orgQuery)
orgQuery := &org.CreateOrgCommand{Name: sqlstore.MainOrgName}
orgResult, err := store.orgService.CreateWithMember(context.Background(), orgQuery)
require.NoError(t, err)
t.Run("create service account", func(t *testing.T) {
serviceAccountName := "new Service Account"
serviceAccountOrgId := orgQuery.Result.Id
serviceAccountOrgId := orgResult.ID
serviceAccountRole := org.RoleAdmin
isDisabled := true
saForm := serviceaccounts.CreateServiceAccountForm{
@@ -175,7 +174,7 @@ func TestStore_MigrateApiKeys(t *testing.T) {
store.sqlStore.Cfg.AutoAssignOrg = true
store.sqlStore.Cfg.AutoAssignOrgId = 1
store.sqlStore.Cfg.AutoAssignOrgRole = "Viewer"
err := store.sqlStore.CreateOrg(context.Background(), &models.CreateOrgCommand{Name: "main"})
_, err := store.orgService.CreateWithMember(context.Background(), &org.CreateOrgCommand{Name: "main"})
require.NoError(t, err)
key := tests.SetupApiKey(t, db, c.key)
err = store.MigrateApiKey(context.Background(), key.OrgId, key.Id)
@@ -252,7 +251,7 @@ func TestStore_MigrateAllApiKeys(t *testing.T) {
store.sqlStore.Cfg.AutoAssignOrg = true
store.sqlStore.Cfg.AutoAssignOrgId = 1
store.sqlStore.Cfg.AutoAssignOrgRole = "Viewer"
err := store.sqlStore.CreateOrg(context.Background(), &models.CreateOrgCommand{Name: "main"})
_, err := store.orgService.CreateWithMember(context.Background(), &org.CreateOrgCommand{Name: "main"})
require.NoError(t, err)
for _, key := range c.keys {
@@ -314,7 +313,7 @@ func TestStore_RevertApiKey(t *testing.T) {
store.sqlStore.Cfg.AutoAssignOrg = true
store.sqlStore.Cfg.AutoAssignOrgId = 1
store.sqlStore.Cfg.AutoAssignOrgRole = "Viewer"
err := store.sqlStore.CreateOrg(context.Background(), &models.CreateOrgCommand{Name: "main"})
_, err := store.orgService.CreateWithMember(context.Background(), &org.CreateOrgCommand{Name: "main"})
require.NoError(t, err)
key := tests.SetupApiKey(t, db, c.key)