chore: remove CreateUser from sqlstore & replace with userService.CreateUserForTests (#59910)

This commit is contained in:
Kristin Laemmert
2022-12-07 11:03:22 -05:00
committed by GitHub
parent d036225f7b
commit 70fbf47022
44 changed files with 943 additions and 758 deletions
+38 -19
View File
@@ -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
}