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:
@@ -12,9 +12,13 @@ import (
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/db"
|
||||
"github.com/grafana/grafana/pkg/services/accesscontrol/actest"
|
||||
"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/sqlstore/migrator"
|
||||
"github.com/grafana/grafana/pkg/services/team/teamimpl"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/services/user/userimpl"
|
||||
"github.com/grafana/grafana/pkg/services/user/usertest"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
@@ -102,7 +106,7 @@ func TestBuildConflictBlock(t *testing.T) {
|
||||
t.Run(tc.desc, func(t *testing.T) {
|
||||
// Restore after destructive operation
|
||||
sqlStore := db.InitTestDB(t)
|
||||
|
||||
usrSvc := setupTestUserService(t, sqlStore)
|
||||
if sqlStore.GetDialect().DriverName() != ignoredDatabase {
|
||||
for _, u := range tc.users {
|
||||
cmd := user.CreateUserCommand{
|
||||
@@ -111,7 +115,7 @@ func TestBuildConflictBlock(t *testing.T) {
|
||||
Login: u.Login,
|
||||
OrgID: int64(testOrgID),
|
||||
}
|
||||
_, err := sqlStore.CreateUser(context.Background(), cmd)
|
||||
_, err := usrSvc.CreateUserForTests(context.Background(), &cmd)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
m, err := GetUsersWithConflictingEmailsOrLogins(&cli.Context{Context: context.Background()}, sqlStore)
|
||||
@@ -207,7 +211,7 @@ conflict: test2
|
||||
t.Run(tc.desc, func(t *testing.T) {
|
||||
// Restore after destructive operation
|
||||
sqlStore := db.InitTestDB(t)
|
||||
|
||||
usrSvc := setupTestUserService(t, sqlStore)
|
||||
if sqlStore.GetDialect().DriverName() != ignoredDatabase {
|
||||
for _, u := range tc.users {
|
||||
cmd := user.CreateUserCommand{
|
||||
@@ -216,7 +220,7 @@ conflict: test2
|
||||
Login: u.Login,
|
||||
OrgID: int64(testOrgID),
|
||||
}
|
||||
_, err := sqlStore.CreateUser(context.Background(), cmd)
|
||||
_, err := usrSvc.CreateUserForTests(context.Background(), &cmd)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
@@ -385,6 +389,7 @@ func TestGetConflictingUsers(t *testing.T) {
|
||||
t.Run(tc.desc, func(t *testing.T) {
|
||||
// Restore after destructive operation
|
||||
sqlStore := db.InitTestDB(t)
|
||||
usrSvc := setupTestUserService(t, sqlStore)
|
||||
if sqlStore.GetDialect().DriverName() != ignoredDatabase {
|
||||
for _, u := range tc.users {
|
||||
cmd := user.CreateUserCommand{
|
||||
@@ -394,7 +399,7 @@ func TestGetConflictingUsers(t *testing.T) {
|
||||
OrgID: int64(testOrgID),
|
||||
IsServiceAccount: u.IsServiceAccount,
|
||||
}
|
||||
_, err := sqlStore.CreateUser(context.Background(), cmd)
|
||||
_, err := usrSvc.CreateUserForTests(context.Background(), &cmd)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
m, err := GetUsersWithConflictingEmailsOrLogins(&cli.Context{Context: context.Background()}, sqlStore)
|
||||
@@ -493,6 +498,7 @@ func TestGenerateConflictingUsersFile(t *testing.T) {
|
||||
t.Run(tc.desc, func(t *testing.T) {
|
||||
// Restore after destructive operation
|
||||
sqlStore := db.InitTestDB(t)
|
||||
usrSvc := setupTestUserService(t, sqlStore)
|
||||
if sqlStore.GetDialect().DriverName() != ignoredDatabase {
|
||||
for _, u := range tc.users {
|
||||
cmd := user.CreateUserCommand{
|
||||
@@ -501,7 +507,7 @@ func TestGenerateConflictingUsersFile(t *testing.T) {
|
||||
Login: u.Login,
|
||||
OrgID: int64(testOrgID),
|
||||
}
|
||||
_, err := sqlStore.CreateUser(context.Background(), cmd)
|
||||
_, err := usrSvc.CreateUserForTests(context.Background(), &cmd)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
m, err := GetUsersWithConflictingEmailsOrLogins(&cli.Context{Context: context.Background()}, sqlStore)
|
||||
@@ -543,6 +549,8 @@ func TestRunValidateConflictUserFile(t *testing.T) {
|
||||
t.Run("should validate file thats gets created", func(t *testing.T) {
|
||||
// Restore after destructive operation
|
||||
sqlStore := db.InitTestDB(t)
|
||||
usrSvc := setupTestUserService(t, sqlStore)
|
||||
|
||||
const testOrgID int64 = 1
|
||||
if sqlStore.GetDialect().DriverName() != ignoredDatabase {
|
||||
// add additional user with conflicting login where DOMAIN is upper case
|
||||
@@ -551,14 +559,14 @@ func TestRunValidateConflictUserFile(t *testing.T) {
|
||||
Login: "user_duplicate_test_1_login",
|
||||
OrgID: testOrgID,
|
||||
}
|
||||
_, err := sqlStore.CreateUser(context.Background(), dupUserLogincmd)
|
||||
_, err := usrSvc.Create(context.Background(), &dupUserLogincmd)
|
||||
require.NoError(t, err)
|
||||
dupUserEmailcmd := user.CreateUserCommand{
|
||||
Email: "USERDUPLICATETEST1@TEST.COM",
|
||||
Login: "USER_DUPLICATE_TEST_1_LOGIN",
|
||||
OrgID: testOrgID,
|
||||
}
|
||||
_, err = sqlStore.CreateUser(context.Background(), dupUserEmailcmd)
|
||||
_, err = usrSvc.Create(context.Background(), &dupUserEmailcmd)
|
||||
require.NoError(t, err)
|
||||
|
||||
// get users
|
||||
@@ -589,6 +597,7 @@ func TestIntegrationMergeUser(t *testing.T) {
|
||||
teamSvc := teamimpl.ProvideService(sqlStore, setting.NewCfg())
|
||||
team1, err := teamSvc.CreateTeam("team1 name", "", 1)
|
||||
require.Nil(t, err)
|
||||
usrSvc := setupTestUserService(t, sqlStore)
|
||||
const testOrgID int64 = 1
|
||||
|
||||
if sqlStore.GetDialect().DriverName() != ignoredDatabase {
|
||||
@@ -601,7 +610,7 @@ func TestIntegrationMergeUser(t *testing.T) {
|
||||
Login: "user_duplicate_test_1_login",
|
||||
OrgID: testOrgID,
|
||||
}
|
||||
_, err := sqlStore.CreateUser(context.Background(), dupUserLogincmd)
|
||||
_, err := usrSvc.Create(context.Background(), &dupUserLogincmd)
|
||||
require.NoError(t, err)
|
||||
dupUserEmailcmd := user.CreateUserCommand{
|
||||
Email: "USERDUPLICATETEST1@TEST.COM",
|
||||
@@ -609,7 +618,7 @@ func TestIntegrationMergeUser(t *testing.T) {
|
||||
Login: "USER_DUPLICATE_TEST_1_LOGIN",
|
||||
OrgID: testOrgID,
|
||||
}
|
||||
userWithUpperCase, err := sqlStore.CreateUser(context.Background(), dupUserEmailcmd)
|
||||
userWithUpperCase, err := usrSvc.Create(context.Background(), &dupUserEmailcmd)
|
||||
require.NoError(t, err)
|
||||
// this is the user we want to update to another team
|
||||
err = teamSvc.AddTeamMember(userWithUpperCase.ID, testOrgID, team1.Id, false, 0)
|
||||
@@ -746,6 +755,7 @@ conflict: test2
|
||||
for _, tc := range testCases {
|
||||
// Restore after destructive operation
|
||||
sqlStore := db.InitTestDB(t)
|
||||
usrSvc := setupTestUserService(t, sqlStore)
|
||||
if sqlStore.GetDialect().DriverName() != ignoredDatabase {
|
||||
for _, u := range tc.users {
|
||||
cmd := user.CreateUserCommand{
|
||||
@@ -754,7 +764,7 @@ conflict: test2
|
||||
Login: u.Login,
|
||||
OrgID: int64(testOrgID),
|
||||
}
|
||||
_, err := sqlStore.CreateUser(context.Background(), cmd)
|
||||
_, err := usrSvc.CreateUserForTests(context.Background(), &cmd)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
// add additional user with conflicting login where DOMAIN is upper case
|
||||
@@ -840,3 +850,13 @@ func TestMarshalConflictUser(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func setupTestUserService(t *testing.T, sqlStore *sqlstore.SQLStore) user.Service {
|
||||
t.Helper()
|
||||
orgSvc, err := orgimpl.ProvideService(sqlStore, sqlStore.Cfg, "atest.FakeQuotaService{})
|
||||
require.NoError(t, err)
|
||||
usrSvc, err := userimpl.ProvideService(sqlStore, orgSvc, sqlStore.Cfg, nil, nil, "atest.FakeQuotaService{})
|
||||
require.NoError(t, err)
|
||||
|
||||
return usrSvc
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user