mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Chore: Use AddUserOrg from org service (#55657)
* Chore: Copy methods from sqlstore to org store * Rename method, add test * Add comments of tests * Chore: Add methods from sqlstore to org service interface * Avoiding import cycle * Add and remove some methods * User AddOrgUSer from org service in api * Fix test function calls
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||
"github.com/grafana/grafana/pkg/services/login"
|
||||
"github.com/grafana/grafana/pkg/services/org"
|
||||
"github.com/grafana/grafana/pkg/services/quota"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
@@ -23,6 +24,7 @@ func ProvideService(
|
||||
quotaService quota.Service,
|
||||
authInfoService login.AuthInfoService,
|
||||
accessControl accesscontrol.Service,
|
||||
orgService org.Service,
|
||||
) *Implementation {
|
||||
s := &Implementation{
|
||||
SQLStore: sqlStore,
|
||||
@@ -30,6 +32,7 @@ func ProvideService(
|
||||
QuotaService: quotaService,
|
||||
AuthInfoService: authInfoService,
|
||||
accessControl: accessControl,
|
||||
orgService: orgService,
|
||||
}
|
||||
return s
|
||||
}
|
||||
@@ -41,6 +44,7 @@ type Implementation struct {
|
||||
QuotaService quota.Service
|
||||
TeamSync login.TeamSyncFunc
|
||||
accessControl accesscontrol.Service
|
||||
orgService org.Service
|
||||
}
|
||||
|
||||
// CreateUser creates inserts a new one.
|
||||
@@ -298,8 +302,8 @@ func (ls *Implementation) syncOrgRoles(ctx context.Context, usr *user.User, extU
|
||||
}
|
||||
|
||||
// add role
|
||||
cmd := &models.AddOrgUserCommand{UserId: usr.ID, Role: orgRole, OrgId: orgId}
|
||||
err := ls.SQLStore.AddOrgUser(ctx, cmd)
|
||||
cmd := &org.AddOrgUserCommand{UserID: usr.ID, Role: orgRole, OrgID: orgId}
|
||||
err := ls.orgService.AddOrgUser(ctx, cmd)
|
||||
if err != nil && !errors.Is(err, models.ErrOrgNotFound) {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/contexthandler/ctxkey"
|
||||
"github.com/grafana/grafana/pkg/services/licensing"
|
||||
"github.com/grafana/grafana/pkg/services/org"
|
||||
"github.com/grafana/grafana/pkg/services/org/orgimpl"
|
||||
"github.com/grafana/grafana/pkg/services/serviceaccounts"
|
||||
"github.com/grafana/grafana/pkg/services/serviceaccounts/database"
|
||||
"github.com/grafana/grafana/pkg/services/serviceaccounts/tests"
|
||||
@@ -43,7 +44,8 @@ func TestServiceAccountsAPI_CreateServiceAccount(t *testing.T) {
|
||||
store := sqlstore.InitTestDB(t)
|
||||
apiKeyService := apikeyimpl.ProvideService(store, store.Cfg)
|
||||
kvStore := kvstore.ProvideService(store)
|
||||
saStore := database.ProvideServiceAccountsStore(store, apiKeyService, kvStore)
|
||||
orgService := orgimpl.ProvideService(store, setting.NewCfg())
|
||||
saStore := database.ProvideServiceAccountsStore(store, apiKeyService, kvStore, orgService)
|
||||
svcmock := tests.ServiceAccountMock{}
|
||||
|
||||
autoAssignOrg := store.Cfg.AutoAssignOrg
|
||||
@@ -209,7 +211,7 @@ func TestServiceAccountsAPI_DeleteServiceAccount(t *testing.T) {
|
||||
store := sqlstore.InitTestDB(t)
|
||||
kvStore := kvstore.ProvideService(store)
|
||||
apiKeyService := apikeyimpl.ProvideService(store, store.Cfg)
|
||||
saStore := database.ProvideServiceAccountsStore(store, apiKeyService, kvStore)
|
||||
saStore := database.ProvideServiceAccountsStore(store, apiKeyService, kvStore, nil)
|
||||
svcmock := tests.ServiceAccountMock{}
|
||||
|
||||
var requestResponse = func(server *web.Mux, httpMethod, requestpath string) *httptest.ResponseRecorder {
|
||||
@@ -312,7 +314,7 @@ func TestServiceAccountsAPI_RetrieveServiceAccount(t *testing.T) {
|
||||
store := sqlstore.InitTestDB(t)
|
||||
apiKeyService := apikeyimpl.ProvideService(store, store.Cfg)
|
||||
kvStore := kvstore.ProvideService(store)
|
||||
saStore := database.ProvideServiceAccountsStore(store, apiKeyService, kvStore)
|
||||
saStore := database.ProvideServiceAccountsStore(store, apiKeyService, kvStore, nil)
|
||||
svcmock := tests.ServiceAccountMock{}
|
||||
type testRetrieveSATestCase struct {
|
||||
desc string
|
||||
@@ -404,7 +406,7 @@ func TestServiceAccountsAPI_UpdateServiceAccount(t *testing.T) {
|
||||
store := sqlstore.InitTestDB(t)
|
||||
apiKeyService := apikeyimpl.ProvideService(store, store.Cfg)
|
||||
kvStore := kvstore.ProvideService(store)
|
||||
saStore := database.ProvideServiceAccountsStore(store, apiKeyService, kvStore)
|
||||
saStore := database.ProvideServiceAccountsStore(store, apiKeyService, kvStore, nil)
|
||||
svcmock := tests.ServiceAccountMock{}
|
||||
type testUpdateSATestCase struct {
|
||||
desc string
|
||||
|
||||
@@ -55,7 +55,7 @@ func TestServiceAccountsAPI_CreateToken(t *testing.T) {
|
||||
store := sqlstore.InitTestDB(t)
|
||||
apiKeyService := apikeyimpl.ProvideService(store, store.Cfg)
|
||||
kvStore := kvstore.ProvideService(store)
|
||||
saStore := database.ProvideServiceAccountsStore(store, apiKeyService, kvStore)
|
||||
saStore := database.ProvideServiceAccountsStore(store, apiKeyService, kvStore, nil)
|
||||
svcmock := tests.ServiceAccountMock{}
|
||||
sa := tests.SetupUserServiceAccount(t, store, tests.TestUser{Login: "sa", IsServiceAccount: true})
|
||||
|
||||
@@ -173,7 +173,7 @@ func TestServiceAccountsAPI_DeleteToken(t *testing.T) {
|
||||
apiKeyService := apikeyimpl.ProvideService(store, store.Cfg)
|
||||
kvStore := kvstore.ProvideService(store)
|
||||
svcMock := &tests.ServiceAccountMock{}
|
||||
saStore := database.ProvideServiceAccountsStore(store, apiKeyService, kvStore)
|
||||
saStore := database.ProvideServiceAccountsStore(store, apiKeyService, kvStore, nil)
|
||||
sa := tests.SetupUserServiceAccount(t, store, tests.TestUser{Login: "sa", IsServiceAccount: true})
|
||||
|
||||
type testCreateSAToken struct {
|
||||
|
||||
@@ -25,14 +25,17 @@ type ServiceAccountsStoreImpl struct {
|
||||
kvStore kvstore.KVStore
|
||||
log log.Logger
|
||||
userService user.Service
|
||||
orgService org.Service
|
||||
}
|
||||
|
||||
func ProvideServiceAccountsStore(store *sqlstore.SQLStore, apiKeyService apikey.Service, kvStore kvstore.KVStore) *ServiceAccountsStoreImpl {
|
||||
func ProvideServiceAccountsStore(store *sqlstore.SQLStore, apiKeyService apikey.Service,
|
||||
kvStore kvstore.KVStore, orgService org.Service) *ServiceAccountsStoreImpl {
|
||||
return &ServiceAccountsStoreImpl{
|
||||
sqlStore: store,
|
||||
apiKeyService: apiKeyService,
|
||||
kvStore: kvStore,
|
||||
log: log.New("serviceaccounts.store"),
|
||||
orgService: orgService,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,10 +66,10 @@ func (s *ServiceAccountsStoreImpl) CreateServiceAccount(ctx context.Context, org
|
||||
return errUser
|
||||
}
|
||||
|
||||
errAddOrgUser := s.sqlStore.AddOrgUser(ctx, &models.AddOrgUserCommand{
|
||||
errAddOrgUser := s.orgService.AddOrgUser(ctx, &org.AddOrgUserCommand{
|
||||
Role: role,
|
||||
OrgId: orgId,
|
||||
UserId: newSA.ID,
|
||||
OrgID: orgId,
|
||||
UserID: newSA.ID,
|
||||
AllowAddingServiceAccount: true,
|
||||
})
|
||||
if errAddOrgUser != nil {
|
||||
|
||||
@@ -9,10 +9,12 @@ import (
|
||||
"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"
|
||||
"github.com/grafana/grafana/pkg/services/serviceaccounts"
|
||||
"github.com/grafana/grafana/pkg/services/serviceaccounts/tests"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -110,7 +112,8 @@ func setupTestDatabase(t *testing.T) (*sqlstore.SQLStore, *ServiceAccountsStoreI
|
||||
db := sqlstore.InitTestDB(t)
|
||||
apiKeyService := apikeyimpl.ProvideService(db, db.Cfg)
|
||||
kvStore := kvstore.ProvideService(db)
|
||||
return db, ProvideServiceAccountsStore(db, apiKeyService, kvStore)
|
||||
orgService := orgimpl.ProvideService(db, setting.NewCfg())
|
||||
return db, ProvideServiceAccountsStore(db, apiKeyService, kvStore, orgService)
|
||||
}
|
||||
|
||||
func TestStore_RetrieveServiceAccount(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user