mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
chore: remove CreateUser from sqlstore & replace with userService.CreateUserForTests (#59910)
This commit is contained in:
@@ -2,6 +2,7 @@ package orgimpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
@@ -65,10 +66,12 @@ func (s *Service) GetIDForNewUser(ctx context.Context, cmd org.GetOrgIDForNewUse
|
||||
return cmd.OrgID, nil
|
||||
}
|
||||
|
||||
orgName := cmd.OrgName
|
||||
var orgName string
|
||||
orgName = cmd.OrgName
|
||||
if len(orgName) == 0 {
|
||||
orgName = util.StringsFallback2(cmd.Email, cmd.Login)
|
||||
}
|
||||
orga.Name = orgName
|
||||
|
||||
if setting.AutoAssignOrg {
|
||||
orga, err := s.store.Get(ctx, int64(s.cfg.AutoAssignOrgId))
|
||||
@@ -142,12 +145,14 @@ func (s *Service) Delete(ctx context.Context, cmd *org.DeleteOrgCommand) error {
|
||||
}
|
||||
|
||||
func (s *Service) GetOrCreate(ctx context.Context, orgName string) (int64, error) {
|
||||
var orga *org.Org
|
||||
var orga = &org.Org{}
|
||||
var err error
|
||||
if s.cfg.AutoAssignOrg {
|
||||
orga, err = s.store.Get(ctx, int64(s.cfg.AutoAssignOrgId))
|
||||
if err != nil {
|
||||
got, err := s.store.Get(ctx, int64(s.cfg.AutoAssignOrgId))
|
||||
if err != nil && !errors.Is(err, org.ErrOrgNotFound) {
|
||||
return 0, err
|
||||
} else if err == nil {
|
||||
return got.ID, nil
|
||||
}
|
||||
|
||||
if s.cfg.AutoAssignOrgId != 1 {
|
||||
@@ -156,11 +161,9 @@ func (s *Service) GetOrCreate(ctx context.Context, orgName string) (int64, error
|
||||
return 0, fmt.Errorf("could not create user: organization ID %d does not exist",
|
||||
s.cfg.AutoAssignOrgId)
|
||||
}
|
||||
|
||||
orga.Name = MainOrgName
|
||||
orga.ID = int64(s.cfg.AutoAssignOrgId)
|
||||
} else {
|
||||
orga = &org.Org{}
|
||||
orga.Name = orgName
|
||||
}
|
||||
|
||||
|
||||
@@ -14,8 +14,10 @@ import (
|
||||
"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/quota/quotaimpl"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/services/user/userimpl"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
@@ -304,11 +306,12 @@ func TestIntegrationOrgUserDataAccess(t *testing.T) {
|
||||
})
|
||||
t.Run("GetOrgUsers and UpdateOrgUsers", func(t *testing.T) {
|
||||
ss := db.InitTestDB(t)
|
||||
ac1cmd := user.CreateUserCommand{Login: "ac1", Email: "ac1@test.com", Name: "ac1 name"}
|
||||
ac2cmd := user.CreateUserCommand{Login: "ac2", Email: "ac2@test.com", Name: "ac2 name", IsAdmin: true}
|
||||
ac1, err := ss.CreateUser(context.Background(), ac1cmd)
|
||||
_, usrSvc := createOrgAndUserSvc(t, ss, ss.Cfg)
|
||||
ac1cmd := &user.CreateUserCommand{Login: "ac1", Email: "ac1@test.com", Name: "ac1 name"}
|
||||
ac2cmd := &user.CreateUserCommand{Login: "ac2", Email: "ac2@test.com", Name: "ac2 name", IsAdmin: true}
|
||||
ac1, err := usrSvc.CreateUserForTests(context.Background(), ac1cmd)
|
||||
require.NoError(t, err)
|
||||
ac2, err := ss.CreateUser(context.Background(), ac2cmd)
|
||||
ac2, err := usrSvc.CreateUserForTests(context.Background(), ac2cmd)
|
||||
require.NoError(t, err)
|
||||
cmd := org.AddOrgUserCommand{
|
||||
OrgID: ac1.OrgID,
|
||||
@@ -412,6 +415,8 @@ func TestIntegrationOrgUserDataAccess(t *testing.T) {
|
||||
|
||||
t.Run("Given single org and 2 users inserted", func(t *testing.T) {
|
||||
ss = db.InitTestDB(t)
|
||||
_, usrSvc := createOrgAndUserSvc(t, ss, ss.Cfg)
|
||||
|
||||
testUser := &user.SignedInUser{
|
||||
Permissions: map[int64]map[string][]string{
|
||||
1: {accesscontrol.ActionOrgUsersRead: []string{accesscontrol.ScopeUsersAll}},
|
||||
@@ -421,13 +426,13 @@ func TestIntegrationOrgUserDataAccess(t *testing.T) {
|
||||
ss.Cfg.AutoAssignOrgId = 1
|
||||
ss.Cfg.AutoAssignOrgRole = "Viewer"
|
||||
|
||||
ac1cmd := user.CreateUserCommand{Login: "ac1", Email: "ac1@test.com", Name: "ac1 name"}
|
||||
ac2cmd := user.CreateUserCommand{Login: "ac2", Email: "ac2@test.com", Name: "ac2 name"}
|
||||
ac1cmd := &user.CreateUserCommand{Login: "ac1", Email: "ac1@test.com", Name: "ac1 name"}
|
||||
ac2cmd := &user.CreateUserCommand{Login: "ac2", Email: "ac2@test.com", Name: "ac2 name"}
|
||||
|
||||
ac1, err := ss.CreateUser(context.Background(), ac1cmd)
|
||||
ac1, err := usrSvc.CreateUserForTests(context.Background(), ac1cmd)
|
||||
testUser.OrgID = ac1.OrgID
|
||||
require.NoError(t, err)
|
||||
_, err = ss.CreateUser(context.Background(), ac2cmd)
|
||||
_, err = usrSvc.Create(context.Background(), ac2cmd)
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Run("Can get organization users paginated with query", func(t *testing.T) {
|
||||
@@ -470,20 +475,20 @@ func TestIntegrationSQLStore_AddOrgUser(t *testing.T) {
|
||||
dialect: store.GetDialect(),
|
||||
cfg: setting.NewCfg(),
|
||||
}
|
||||
_, usrSvc := createOrgAndUserSvc(t, store, store.Cfg)
|
||||
|
||||
// create org and admin
|
||||
u, err := store.CreateUser(context.Background(), user.CreateUserCommand{
|
||||
u, err := usrSvc.CreateUserForTests(context.Background(), &user.CreateUserCommand{
|
||||
Login: "admin",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// create a service account with no org
|
||||
sa, err := store.CreateUser(context.Background(), user.CreateUserCommand{
|
||||
sa, err := usrSvc.CreateUserForTests(context.Background(), &user.CreateUserCommand{
|
||||
Login: "sa-no-org",
|
||||
IsServiceAccount: true,
|
||||
SkipOrgSetup: true,
|
||||
})
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, int64(-1), sa.OrgID)
|
||||
|
||||
@@ -599,9 +604,11 @@ func TestIntegration_SQLStore_GetOrgUsers(t *testing.T) {
|
||||
|
||||
func seedOrgUsers(t *testing.T, orgUserStore store, store *sqlstore.SQLStore, numUsers int) {
|
||||
t.Helper()
|
||||
_, usrSvc := createOrgAndUserSvc(t, store, store.Cfg)
|
||||
|
||||
// Seed users
|
||||
for i := 1; i <= numUsers; i++ {
|
||||
user, err := store.CreateUser(context.Background(), user.CreateUserCommand{
|
||||
user, err := usrSvc.CreateUserForTests(context.Background(), &user.CreateUserCommand{
|
||||
Login: fmt.Sprintf("user-%d", i),
|
||||
OrgID: 1,
|
||||
})
|
||||
@@ -633,8 +640,8 @@ func TestIntegration_SQLStore_GetOrgUsers_PopulatesCorrectly(t *testing.T) {
|
||||
}
|
||||
// The millisecond part is not stored in the DB
|
||||
constNow := time.Date(2022, 8, 17, 20, 34, 58, 0, time.UTC)
|
||||
sqlstore.MockTimeNow(constNow)
|
||||
defer sqlstore.ResetTimeNow()
|
||||
userimpl.MockTimeNow(constNow)
|
||||
defer userimpl.ResetTimeNow()
|
||||
|
||||
store := db.InitTestDB(t, sqlstore.InitTestDBOpt{})
|
||||
orgUserStore := sqlStore{
|
||||
@@ -642,6 +649,7 @@ func TestIntegration_SQLStore_GetOrgUsers_PopulatesCorrectly(t *testing.T) {
|
||||
dialect: store.GetDialect(),
|
||||
cfg: setting.NewCfg(),
|
||||
}
|
||||
_, usrSvc := createOrgAndUserSvc(t, store, store.Cfg)
|
||||
|
||||
id, err := orgUserStore.Insert(context.Background(),
|
||||
&org.Org{
|
||||
@@ -651,7 +659,7 @@ func TestIntegration_SQLStore_GetOrgUsers_PopulatesCorrectly(t *testing.T) {
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
newUser, err := store.CreateUser(context.Background(), user.CreateUserCommand{
|
||||
newUser, err := usrSvc.CreateUserForTests(context.Background(), &user.CreateUserCommand{
|
||||
Login: "Viewer",
|
||||
Email: "viewer@localhost",
|
||||
OrgID: id,
|
||||
@@ -752,8 +760,6 @@ func TestIntegration_SQLStore_SearchOrgUsers(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.desc, func(t *testing.T) {
|
||||
result, err := orgUserStore.SearchOrgUsers(context.Background(), tt.query)
|
||||
fmt.Println("users:", result)
|
||||
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, result.OrgUsers, tt.expectedNumUsers)
|
||||
|
||||
@@ -776,15 +782,16 @@ func TestIntegration_SQLStore_RemoveOrgUser(t *testing.T) {
|
||||
dialect: store.GetDialect(),
|
||||
cfg: setting.NewCfg(),
|
||||
}
|
||||
_, usrSvc := createOrgAndUserSvc(t, store, store.Cfg)
|
||||
// create org and admin
|
||||
_, err := store.CreateUser(context.Background(), user.CreateUserCommand{
|
||||
_, err := usrSvc.Create(context.Background(), &user.CreateUserCommand{
|
||||
Login: "admin",
|
||||
OrgID: 1,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// create a user with no org
|
||||
_, err = store.CreateUser(context.Background(), user.CreateUserCommand{
|
||||
_, err = usrSvc.Create(context.Background(), &user.CreateUserCommand{
|
||||
Login: "user",
|
||||
OrgID: 1,
|
||||
SkipOrgSetup: true,
|
||||
@@ -807,3 +814,15 @@ func TestIntegration_SQLStore_RemoveOrgUser(t *testing.T) {
|
||||
})
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func createOrgAndUserSvc(t *testing.T, store db.DB, cfg *setting.Cfg) (org.Service, user.Service) {
|
||||
t.Helper()
|
||||
|
||||
quotaService := quotaimpl.ProvideService(store, cfg)
|
||||
orgService, err := ProvideService(store, cfg, quotaService)
|
||||
require.NoError(t, err)
|
||||
usrSvc, err := userimpl.ProvideService(store, orgService, cfg, nil, nil, quotaService)
|
||||
require.NoError(t, err)
|
||||
|
||||
return orgService, usrSvc
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ func (f *FakeOrgService) Delete(ctx context.Context, cmd *org.DeleteOrgCommand)
|
||||
}
|
||||
|
||||
func (f *FakeOrgService) GetOrCreate(ctx context.Context, orgName string) (int64, error) {
|
||||
return 0, f.ExpectedError
|
||||
return f.ExpectedOrg.ID, f.ExpectedError
|
||||
}
|
||||
|
||||
func (f *FakeOrgService) AddOrgUser(ctx context.Context, cmd *org.AddOrgUserCommand) error {
|
||||
|
||||
Reference in New Issue
Block a user