Chore: Remove CreateUserForTests() (#64125)
* Chore: Remove CreateUserForTests
* Apply suggestion from code review
(cherry picked from commit e6e8351ee9)
This commit is contained in:
@@ -64,7 +64,7 @@ func setUpGetTeamMembersHandler(t *testing.T, sqlStore *sqlstore.SQLStore) {
|
||||
Login: fmt.Sprint("loginuser", i),
|
||||
}
|
||||
// user
|
||||
user, err := usrSvc.CreateUserForTests(context.Background(), &userCmd)
|
||||
user, err := usrSvc.Create(context.Background(), &userCmd)
|
||||
require.NoError(t, err)
|
||||
err = teamSvc.AddTeamMember(user.ID, testOrgID, team.ID, false, 1)
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -77,7 +77,7 @@ func TestUserAPIEndpoint_userLoggedIn(t *testing.T) {
|
||||
Login: "loginuser",
|
||||
IsAdmin: true,
|
||||
}
|
||||
usr, err := userSvc.CreateUserForTests(context.Background(), &createUserCmd)
|
||||
usr, err := userSvc.Create(context.Background(), &createUserCmd)
|
||||
require.NoError(t, err)
|
||||
|
||||
sc.handlerFunc = hs.GetUserByID
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"os"
|
||||
"sort"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/urfave/cli/v2"
|
||||
@@ -106,17 +107,20 @@ 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 {
|
||||
userStore := userimpl.ProvideStore(sqlStore, sqlStore.Cfg)
|
||||
for _, u := range tc.users {
|
||||
cmd := user.CreateUserCommand{
|
||||
Email: u.Email,
|
||||
Name: u.Name,
|
||||
Login: u.Login,
|
||||
OrgID: int64(testOrgID),
|
||||
u := user.User{
|
||||
Email: u.Email,
|
||||
Name: u.Name,
|
||||
Login: u.Login,
|
||||
OrgID: int64(testOrgID),
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
}
|
||||
_, err := usrSvc.CreateUserForTests(context.Background(), &cmd)
|
||||
require.NoError(t, err)
|
||||
// call user store instead of user service so as not to prevent conflicting users
|
||||
_, err := userStore.Insert(context.Background(), &u)
|
||||
require.NoError(t, err, u)
|
||||
}
|
||||
m, err := GetUsersWithConflictingEmailsOrLogins(&cli.Context{Context: context.Background()}, sqlStore)
|
||||
require.NoError(t, err)
|
||||
@@ -211,16 +215,19 @@ 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 {
|
||||
userStore := userimpl.ProvideStore(sqlStore, sqlStore.Cfg)
|
||||
for _, u := range tc.users {
|
||||
cmd := user.CreateUserCommand{
|
||||
Email: u.Email,
|
||||
Name: u.Name,
|
||||
Login: u.Login,
|
||||
OrgID: int64(testOrgID),
|
||||
u := user.User{
|
||||
Email: u.Email,
|
||||
Name: u.Name,
|
||||
Login: u.Login,
|
||||
OrgID: int64(testOrgID),
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
}
|
||||
_, err := usrSvc.CreateUserForTests(context.Background(), &cmd)
|
||||
// call user store instead of user service so as not to prevent conflicting users
|
||||
_, err := userStore.Insert(context.Background(), &u)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
@@ -389,17 +396,20 @@ 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 {
|
||||
userStore := userimpl.ProvideStore(sqlStore, sqlStore.Cfg)
|
||||
for _, u := range tc.users {
|
||||
cmd := user.CreateUserCommand{
|
||||
u := user.User{
|
||||
Email: u.Email,
|
||||
Name: u.Name,
|
||||
Login: u.Login,
|
||||
OrgID: int64(testOrgID),
|
||||
IsServiceAccount: u.IsServiceAccount,
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
}
|
||||
_, err := usrSvc.CreateUserForTests(context.Background(), &cmd)
|
||||
// call user store instead of user service so as not to prevent conflicting users
|
||||
_, err := userStore.Insert(context.Background(), &u)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
m, err := GetUsersWithConflictingEmailsOrLogins(&cli.Context{Context: context.Background()}, sqlStore)
|
||||
@@ -498,16 +508,19 @@ 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 {
|
||||
userStore := userimpl.ProvideStore(sqlStore, sqlStore.Cfg)
|
||||
for _, u := range tc.users {
|
||||
cmd := user.CreateUserCommand{
|
||||
Email: u.Email,
|
||||
Name: u.Name,
|
||||
Login: u.Login,
|
||||
OrgID: int64(testOrgID),
|
||||
cmd := user.User{
|
||||
Email: u.Email,
|
||||
Name: u.Name,
|
||||
Login: u.Login,
|
||||
OrgID: int64(testOrgID),
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
}
|
||||
_, err := usrSvc.CreateUserForTests(context.Background(), &cmd)
|
||||
// call user store instead of user service so as not to prevent conflicting users
|
||||
_, err := userStore.Insert(context.Background(), &cmd)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
m, err := GetUsersWithConflictingEmailsOrLogins(&cli.Context{Context: context.Background()}, sqlStore)
|
||||
@@ -755,16 +768,19 @@ conflict: test2
|
||||
for _, tc := range testCases {
|
||||
// Restore after destructive operation
|
||||
sqlStore := db.InitTestDB(t)
|
||||
usrSvc := setupTestUserService(t, sqlStore)
|
||||
if sqlStore.GetDialect().DriverName() != ignoredDatabase {
|
||||
userStore := userimpl.ProvideStore(sqlStore, sqlStore.Cfg)
|
||||
for _, u := range tc.users {
|
||||
cmd := user.CreateUserCommand{
|
||||
Email: u.Email,
|
||||
Name: u.Name,
|
||||
Login: u.Login,
|
||||
OrgID: int64(testOrgID),
|
||||
cmd := user.User{
|
||||
Email: u.Email,
|
||||
Name: u.Name,
|
||||
Login: u.Login,
|
||||
OrgID: int64(testOrgID),
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
}
|
||||
_, err := usrSvc.CreateUserForTests(context.Background(), &cmd)
|
||||
// call user store instead of user service so as not to prevent conflicting users
|
||||
_, err := userStore.Insert(context.Background(), &cmd)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
// add additional user with conflicting login where DOMAIN is upper case
|
||||
|
||||
@@ -2,7 +2,9 @@ package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
@@ -28,7 +30,7 @@ func TestIntegrationDashboardACLDataAccess(t *testing.T) {
|
||||
var savedFolder, childDash *dashboards.Dashboard
|
||||
var dashboardStore *DashboardStore
|
||||
|
||||
setup := func(t *testing.T) {
|
||||
setup := func(t *testing.T) int64 {
|
||||
sqlStore = db.InitTestDB(t)
|
||||
quotaService := quotatest.New(false, nil)
|
||||
var err error
|
||||
@@ -37,12 +39,13 @@ func TestIntegrationDashboardACLDataAccess(t *testing.T) {
|
||||
currentUser = createUser(t, sqlStore, "viewer", "Viewer", false)
|
||||
savedFolder = insertTestDashboard(t, dashboardStore, "1 test dash folder", 1, 0, true, "prod", "webapp")
|
||||
childDash = insertTestDashboard(t, dashboardStore, "2 test dash", 1, savedFolder.ID, false, "prod", "webapp")
|
||||
return currentUser.OrgID
|
||||
}
|
||||
|
||||
t.Run("Dashboard permission with userId and teamId set to 0", func(t *testing.T) {
|
||||
setup(t)
|
||||
orgID := setup(t)
|
||||
err := updateDashboardACL(t, dashboardStore, savedFolder.ID, dashboards.DashboardACL{
|
||||
OrgID: 1,
|
||||
OrgID: orgID,
|
||||
DashboardID: savedFolder.ID,
|
||||
Permission: dashboards.PERMISSION_EDIT,
|
||||
})
|
||||
@@ -50,8 +53,8 @@ func TestIntegrationDashboardACLDataAccess(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Folder acl should include default acl", func(t *testing.T) {
|
||||
setup(t)
|
||||
query := dashboards.GetDashboardACLInfoListQuery{DashboardID: savedFolder.ID, OrgID: 1}
|
||||
orgID := setup(t)
|
||||
query := dashboards.GetDashboardACLInfoListQuery{DashboardID: savedFolder.ID, OrgID: orgID}
|
||||
|
||||
queryResult, err := dashboardStore.GetDashboardACLInfoList(context.Background(), &query)
|
||||
require.Nil(t, err)
|
||||
@@ -67,8 +70,8 @@ func TestIntegrationDashboardACLDataAccess(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Dashboard acl should include acl for parent folder", func(t *testing.T) {
|
||||
setup(t)
|
||||
query := dashboards.GetDashboardACLInfoListQuery{DashboardID: childDash.ID, OrgID: 1}
|
||||
orgID := setup(t)
|
||||
query := dashboards.GetDashboardACLInfoListQuery{DashboardID: childDash.ID, OrgID: orgID}
|
||||
|
||||
queryResult, err := dashboardStore.GetDashboardACLInfoList(context.Background(), &query)
|
||||
require.Nil(t, err)
|
||||
@@ -84,11 +87,11 @@ func TestIntegrationDashboardACLDataAccess(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Folder with removed default permissions returns no acl items", func(t *testing.T) {
|
||||
setup(t)
|
||||
orgID := setup(t)
|
||||
err := dashboardStore.UpdateDashboardACL(context.Background(), savedFolder.ID, nil)
|
||||
require.Nil(t, err)
|
||||
|
||||
query := dashboards.GetDashboardACLInfoListQuery{DashboardID: childDash.ID, OrgID: 1}
|
||||
query := dashboards.GetDashboardACLInfoListQuery{DashboardID: childDash.ID, OrgID: orgID}
|
||||
queryResult, err := dashboardStore.GetDashboardACLInfoList(context.Background(), &query)
|
||||
require.Nil(t, err)
|
||||
|
||||
@@ -97,9 +100,9 @@ func TestIntegrationDashboardACLDataAccess(t *testing.T) {
|
||||
|
||||
t.Run("Given a dashboard folder and a user", func(t *testing.T) {
|
||||
t.Run("Given dashboard folder permission", func(t *testing.T) {
|
||||
setup(t)
|
||||
orgID := setup(t)
|
||||
err := updateDashboardACL(t, dashboardStore, savedFolder.ID, dashboards.DashboardACL{
|
||||
OrgID: 1,
|
||||
OrgID: orgID,
|
||||
UserID: currentUser.ID,
|
||||
DashboardID: savedFolder.ID,
|
||||
Permission: dashboards.PERMISSION_EDIT,
|
||||
@@ -107,7 +110,7 @@ func TestIntegrationDashboardACLDataAccess(t *testing.T) {
|
||||
require.Nil(t, err)
|
||||
|
||||
t.Run("When reading dashboard acl should include acl for parent folder", func(t *testing.T) {
|
||||
query := dashboards.GetDashboardACLInfoListQuery{DashboardID: childDash.ID, OrgID: 1}
|
||||
query := dashboards.GetDashboardACLInfoListQuery{DashboardID: childDash.ID, OrgID: orgID}
|
||||
|
||||
queryResult, err := dashboardStore.GetDashboardACLInfoList(context.Background(), &query)
|
||||
require.Nil(t, err)
|
||||
@@ -118,7 +121,7 @@ func TestIntegrationDashboardACLDataAccess(t *testing.T) {
|
||||
|
||||
t.Run("Given child dashboard permission", func(t *testing.T) {
|
||||
err := updateDashboardACL(t, dashboardStore, childDash.ID, dashboards.DashboardACL{
|
||||
OrgID: 1,
|
||||
OrgID: orgID,
|
||||
UserID: currentUser.ID,
|
||||
DashboardID: childDash.ID,
|
||||
Permission: dashboards.PERMISSION_EDIT,
|
||||
@@ -126,7 +129,7 @@ func TestIntegrationDashboardACLDataAccess(t *testing.T) {
|
||||
require.Nil(t, err)
|
||||
|
||||
t.Run("When reading dashboard acl should include acl for parent folder and child", func(t *testing.T) {
|
||||
query := dashboards.GetDashboardACLInfoListQuery{OrgID: 1, DashboardID: childDash.ID}
|
||||
query := dashboards.GetDashboardACLInfoListQuery{OrgID: orgID, DashboardID: childDash.ID}
|
||||
|
||||
queryResult, err := dashboardStore.GetDashboardACLInfoList(context.Background(), &query)
|
||||
require.Nil(t, err)
|
||||
@@ -141,7 +144,7 @@ func TestIntegrationDashboardACLDataAccess(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Reading dashboard acl should include default acl for parent folder and the child acl", func(t *testing.T) {
|
||||
setup(t)
|
||||
orgID := setup(t)
|
||||
err := updateDashboardACL(t, dashboardStore, childDash.ID, dashboards.DashboardACL{
|
||||
OrgID: 1,
|
||||
UserID: currentUser.ID,
|
||||
@@ -150,7 +153,7 @@ func TestIntegrationDashboardACLDataAccess(t *testing.T) {
|
||||
})
|
||||
require.Nil(t, err)
|
||||
|
||||
query := dashboards.GetDashboardACLInfoListQuery{OrgID: 1, DashboardID: childDash.ID}
|
||||
query := dashboards.GetDashboardACLInfoListQuery{OrgID: orgID, DashboardID: childDash.ID}
|
||||
|
||||
queryResult, err := dashboardStore.GetDashboardACLInfoList(context.Background(), &query)
|
||||
require.Nil(t, err)
|
||||
@@ -168,7 +171,7 @@ func TestIntegrationDashboardACLDataAccess(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Add and delete dashboard permission", func(t *testing.T) {
|
||||
setup(t)
|
||||
orgID := setup(t)
|
||||
err := updateDashboardACL(t, dashboardStore, savedFolder.ID, dashboards.DashboardACL{
|
||||
OrgID: 1,
|
||||
UserID: currentUser.ID,
|
||||
@@ -177,7 +180,7 @@ func TestIntegrationDashboardACLDataAccess(t *testing.T) {
|
||||
})
|
||||
require.Nil(t, err)
|
||||
|
||||
q1 := &dashboards.GetDashboardACLInfoListQuery{DashboardID: savedFolder.ID, OrgID: 1}
|
||||
q1 := &dashboards.GetDashboardACLInfoListQuery{DashboardID: savedFolder.ID, OrgID: orgID}
|
||||
q1Result, err := dashboardStore.GetDashboardACLInfoList(context.Background(), q1)
|
||||
require.Nil(t, err)
|
||||
|
||||
@@ -191,14 +194,14 @@ func TestIntegrationDashboardACLDataAccess(t *testing.T) {
|
||||
err = updateDashboardACL(t, dashboardStore, savedFolder.ID)
|
||||
require.Nil(t, err)
|
||||
|
||||
q3 := &dashboards.GetDashboardACLInfoListQuery{DashboardID: savedFolder.ID, OrgID: 1}
|
||||
q3 := &dashboards.GetDashboardACLInfoListQuery{DashboardID: savedFolder.ID, OrgID: orgID}
|
||||
q3Result, err := dashboardStore.GetDashboardACLInfoList(context.Background(), q3)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, 0, len(q3Result))
|
||||
})
|
||||
|
||||
t.Run("Should be able to add a user permission for a team", func(t *testing.T) {
|
||||
setup(t)
|
||||
orgID := setup(t)
|
||||
teamSvc := teamimpl.ProvideService(sqlStore, sqlStore.Cfg)
|
||||
team1, err := teamSvc.CreateTeam("group1 name", "", 1)
|
||||
require.Nil(t, err)
|
||||
@@ -211,7 +214,7 @@ func TestIntegrationDashboardACLDataAccess(t *testing.T) {
|
||||
})
|
||||
require.Nil(t, err)
|
||||
|
||||
q1 := &dashboards.GetDashboardACLInfoListQuery{DashboardID: savedFolder.ID, OrgID: 1}
|
||||
q1 := &dashboards.GetDashboardACLInfoListQuery{DashboardID: savedFolder.ID, OrgID: orgID}
|
||||
q1Result, err := dashboardStore.GetDashboardACLInfoList(context.Background(), q1)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, savedFolder.ID, q1Result[0].DashboardID)
|
||||
@@ -220,7 +223,7 @@ func TestIntegrationDashboardACLDataAccess(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Should be able to update an existing permission for a team", func(t *testing.T) {
|
||||
setup(t)
|
||||
orgID := setup(t)
|
||||
teamSvc := teamimpl.ProvideService(sqlStore, sqlStore.Cfg)
|
||||
team1, err := teamSvc.CreateTeam("group1 name", "", 1)
|
||||
require.Nil(t, err)
|
||||
@@ -232,7 +235,7 @@ func TestIntegrationDashboardACLDataAccess(t *testing.T) {
|
||||
})
|
||||
require.Nil(t, err)
|
||||
|
||||
q3 := &dashboards.GetDashboardACLInfoListQuery{DashboardID: savedFolder.ID, OrgID: 1}
|
||||
q3 := &dashboards.GetDashboardACLInfoListQuery{DashboardID: savedFolder.ID, OrgID: orgID}
|
||||
q3Result, err := dashboardStore.GetDashboardACLInfoList(context.Background(), q3)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, 1, len(q3Result))
|
||||
@@ -243,11 +246,11 @@ func TestIntegrationDashboardACLDataAccess(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Default permissions for root folder dashboards", func(t *testing.T) {
|
||||
setup(t)
|
||||
orgID := setup(t)
|
||||
var rootFolderId int64 = 0
|
||||
//sqlStore := db.InitTestDB(t)
|
||||
|
||||
query := dashboards.GetDashboardACLInfoListQuery{DashboardID: rootFolderId, OrgID: 1}
|
||||
query := dashboards.GetDashboardACLInfoListQuery{DashboardID: rootFolderId, OrgID: orgID}
|
||||
|
||||
queryResult, err := dashboardStore.GetDashboardACLInfoList(context.Background(), &query)
|
||||
require.Nil(t, err)
|
||||
@@ -281,11 +284,15 @@ func createUser(t *testing.T, sqlStore *sqlstore.SQLStore, name string, role str
|
||||
usrSvc, err := userimpl.ProvideService(sqlStore, orgService, sqlStore.Cfg, nil, nil, qs)
|
||||
require.NoError(t, err)
|
||||
|
||||
currentUserCmd := user.CreateUserCommand{Login: name, Email: name + "@test.com", Name: "a " + name, IsAdmin: isAdmin}
|
||||
currentUser, err := usrSvc.CreateUserForTests(context.Background(), ¤tUserCmd)
|
||||
o, err := orgService.CreateWithMember(context.Background(), &org.CreateOrgCommand{Name: fmt.Sprintf("test org %d", time.Now().UnixNano())})
|
||||
require.NoError(t, err)
|
||||
|
||||
currentUserCmd := user.CreateUserCommand{Login: name, Email: name + "@test.com", Name: "a " + name, IsAdmin: isAdmin, OrgID: o.ID}
|
||||
currentUser, err := usrSvc.Create(context.Background(), ¤tUserCmd)
|
||||
require.NoError(t, err)
|
||||
orgs, err := orgService.GetUserOrgList(context.Background(), &org.GetUserOrgListQuery{UserID: currentUser.ID})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, org.RoleType(role), orgs[0].Role)
|
||||
require.Equal(t, o.ID, orgs[0].OrgID)
|
||||
return *currentUser
|
||||
}
|
||||
|
||||
@@ -308,9 +308,9 @@ func TestIntegrationOrgUserDataAccess(t *testing.T) {
|
||||
_, 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)
|
||||
ac1, err := usrSvc.Create(context.Background(), ac1cmd)
|
||||
require.NoError(t, err)
|
||||
ac2, err := usrSvc.CreateUserForTests(context.Background(), ac2cmd)
|
||||
ac2, err := usrSvc.Create(context.Background(), ac2cmd)
|
||||
require.NoError(t, err)
|
||||
cmd := org.AddOrgUserCommand{
|
||||
OrgID: ac1.OrgID,
|
||||
@@ -418,7 +418,7 @@ func TestIntegrationOrgUserDataAccess(t *testing.T) {
|
||||
ss.Cfg.AutoAssignOrgId = 1
|
||||
ss.Cfg.AutoAssignOrgRole = "Viewer"
|
||||
|
||||
_, usrSvc := createOrgAndUserSvc(t, ss, ss.Cfg)
|
||||
orgSvc, usrSvc := createOrgAndUserSvc(t, ss, ss.Cfg)
|
||||
|
||||
testUser := &user.SignedInUser{
|
||||
Permissions: map[int64]map[string][]string{
|
||||
@@ -426,10 +426,14 @@ func TestIntegrationOrgUserDataAccess(t *testing.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"}
|
||||
o, err := orgSvc.CreateWithMember(context.Background(), &org.CreateOrgCommand{Name: "test org"})
|
||||
require.NoError(t, err)
|
||||
|
||||
ac1, err := usrSvc.CreateUserForTests(context.Background(), ac1cmd)
|
||||
ac1cmd := &user.CreateUserCommand{Login: "ac1", Email: "ac1@test.com", Name: "ac1 name", OrgID: o.ID}
|
||||
ac2cmd := &user.CreateUserCommand{Login: "ac2", Email: "ac2@test.com", Name: "ac2 name", OrgID: o.ID}
|
||||
|
||||
ac1, err := usrSvc.Create(context.Background(), ac1cmd)
|
||||
require.NoError(t, err)
|
||||
testUser.OrgID = ac1.OrgID
|
||||
require.Equal(t, int64(1), ac1.OrgID)
|
||||
require.NoError(t, err)
|
||||
@@ -478,16 +482,20 @@ func TestIntegrationSQLStore_AddOrgUser(t *testing.T) {
|
||||
dialect: store.GetDialect(),
|
||||
cfg: setting.NewCfg(),
|
||||
}
|
||||
_, usrSvc := createOrgAndUserSvc(t, store, store.Cfg)
|
||||
orgSvc, usrSvc := createOrgAndUserSvc(t, store, store.Cfg)
|
||||
|
||||
o, err := orgSvc.CreateWithMember(context.Background(), &org.CreateOrgCommand{Name: "test org"})
|
||||
require.NoError(t, err)
|
||||
|
||||
// create org and admin
|
||||
u, err := usrSvc.CreateUserForTests(context.Background(), &user.CreateUserCommand{
|
||||
u, err := usrSvc.Create(context.Background(), &user.CreateUserCommand{
|
||||
Login: "admin",
|
||||
OrgID: o.ID,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// create a service account with no org
|
||||
sa, err := usrSvc.CreateUserForTests(context.Background(), &user.CreateUserCommand{
|
||||
sa, err := usrSvc.Create(context.Background(), &user.CreateUserCommand{
|
||||
Login: "sa-no-org",
|
||||
IsServiceAccount: true,
|
||||
SkipOrgSetup: true,
|
||||
@@ -533,49 +541,6 @@ func TestIntegration_SQLStore_GetOrgUsers(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test")
|
||||
}
|
||||
tests := []struct {
|
||||
desc string
|
||||
query *org.SearchOrgUsersQuery
|
||||
expectedNumUsers int
|
||||
}{
|
||||
{
|
||||
desc: "should return all users",
|
||||
query: &org.SearchOrgUsersQuery{
|
||||
OrgID: 1,
|
||||
User: &user.SignedInUser{
|
||||
OrgID: 1,
|
||||
Permissions: map[int64]map[string][]string{1: {accesscontrol.ActionOrgUsersRead: {accesscontrol.ScopeUsersAll}}},
|
||||
},
|
||||
},
|
||||
expectedNumUsers: 10,
|
||||
},
|
||||
{
|
||||
desc: "should return no users",
|
||||
query: &org.SearchOrgUsersQuery{
|
||||
OrgID: 1,
|
||||
User: &user.SignedInUser{
|
||||
OrgID: 1,
|
||||
Permissions: map[int64]map[string][]string{1: {accesscontrol.ActionOrgUsersRead: {""}}},
|
||||
},
|
||||
},
|
||||
expectedNumUsers: 0,
|
||||
},
|
||||
{
|
||||
desc: "should return some users",
|
||||
query: &org.SearchOrgUsersQuery{
|
||||
OrgID: 1,
|
||||
User: &user.SignedInUser{
|
||||
OrgID: 1,
|
||||
Permissions: map[int64]map[string][]string{1: {accesscontrol.ActionOrgUsersRead: {
|
||||
"users:id:1",
|
||||
"users:id:5",
|
||||
"users:id:9",
|
||||
}}},
|
||||
},
|
||||
},
|
||||
expectedNumUsers: 3,
|
||||
},
|
||||
}
|
||||
|
||||
store := db.InitTestDB(t)
|
||||
orgUserStore := sqlStore{
|
||||
@@ -588,7 +553,57 @@ func TestIntegration_SQLStore_GetOrgUsers(t *testing.T) {
|
||||
orgUserStore.cfg.IsEnterprise = false
|
||||
}()
|
||||
store.Cfg = setting.NewCfg()
|
||||
seedOrgUsers(t, &orgUserStore, store, 10)
|
||||
|
||||
orgSvc, userSvc := createOrgAndUserSvc(t, store, store.Cfg)
|
||||
|
||||
o, err := orgSvc.CreateWithMember(context.Background(), &org.CreateOrgCommand{Name: "test org"})
|
||||
require.NoError(t, err)
|
||||
|
||||
seedOrgUsers(t, &orgUserStore, store, 10, userSvc, o.ID)
|
||||
|
||||
tests := []struct {
|
||||
desc string
|
||||
query *org.SearchOrgUsersQuery
|
||||
expectedNumUsers int
|
||||
}{
|
||||
{
|
||||
desc: "should return all users",
|
||||
query: &org.SearchOrgUsersQuery{
|
||||
OrgID: o.ID,
|
||||
User: &user.SignedInUser{
|
||||
OrgID: o.ID,
|
||||
Permissions: map[int64]map[string][]string{1: {accesscontrol.ActionOrgUsersRead: {accesscontrol.ScopeUsersAll}}},
|
||||
},
|
||||
},
|
||||
expectedNumUsers: 10,
|
||||
},
|
||||
{
|
||||
desc: "should return no users",
|
||||
query: &org.SearchOrgUsersQuery{
|
||||
OrgID: o.ID,
|
||||
User: &user.SignedInUser{
|
||||
OrgID: o.ID,
|
||||
Permissions: map[int64]map[string][]string{1: {accesscontrol.ActionOrgUsersRead: {""}}},
|
||||
},
|
||||
},
|
||||
expectedNumUsers: 0,
|
||||
},
|
||||
{
|
||||
desc: "should return some users",
|
||||
query: &org.SearchOrgUsersQuery{
|
||||
OrgID: o.ID,
|
||||
User: &user.SignedInUser{
|
||||
OrgID: o.ID,
|
||||
Permissions: map[int64]map[string][]string{1: {accesscontrol.ActionOrgUsersRead: {
|
||||
"users:id:1",
|
||||
"users:id:5",
|
||||
"users:id:9",
|
||||
}}},
|
||||
},
|
||||
},
|
||||
expectedNumUsers: 3,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.desc, func(t *testing.T) {
|
||||
@@ -605,26 +620,27 @@ func TestIntegration_SQLStore_GetOrgUsers(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func seedOrgUsers(t *testing.T, orgUserStore store, store *sqlstore.SQLStore, numUsers int) {
|
||||
func seedOrgUsers(t *testing.T, orgUserStore store, store *sqlstore.SQLStore, numUsers int, usrSvc user.Service, orgID int64) {
|
||||
t.Helper()
|
||||
_, usrSvc := createOrgAndUserSvc(t, store, store.Cfg)
|
||||
|
||||
// Seed users
|
||||
for i := 1; i <= numUsers; i++ {
|
||||
user, err := usrSvc.CreateUserForTests(context.Background(), &user.CreateUserCommand{
|
||||
user, err := usrSvc.Create(context.Background(), &user.CreateUserCommand{
|
||||
Login: fmt.Sprintf("user-%d", i),
|
||||
OrgID: 1,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
if i != 1 {
|
||||
err = orgUserStore.AddOrgUser(context.Background(), &org.AddOrgUserCommand{
|
||||
Role: "Viewer",
|
||||
OrgID: 1,
|
||||
UserID: user.ID,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
role := org.RoleViewer
|
||||
if i == 1 {
|
||||
role = org.RoleAdmin
|
||||
}
|
||||
|
||||
err = orgUserStore.AddOrgUser(context.Background(), &org.AddOrgUserCommand{
|
||||
Role: role,
|
||||
OrgID: orgID,
|
||||
UserID: user.ID,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -662,7 +678,7 @@ func TestIntegration_SQLStore_GetOrgUsers_PopulatesCorrectly(t *testing.T) {
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
newUser, err := usrSvc.CreateUserForTests(context.Background(), &user.CreateUserCommand{
|
||||
newUser, err := usrSvc.Create(context.Background(), &user.CreateUserCommand{
|
||||
Login: "Viewer",
|
||||
Email: "viewer@localhost",
|
||||
OrgID: id,
|
||||
@@ -707,6 +723,21 @@ func TestIntegration_SQLStore_SearchOrgUsers(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test")
|
||||
}
|
||||
|
||||
store := db.InitTestDB(t, sqlstore.InitTestDBOpt{})
|
||||
orgUserStore := sqlStore{
|
||||
db: store,
|
||||
dialect: store.GetDialect(),
|
||||
cfg: setting.NewCfg(),
|
||||
}
|
||||
// orgUserStore.cfg.Skip
|
||||
orgSvc, userSvc := createOrgAndUserSvc(t, store, store.Cfg)
|
||||
|
||||
o, err := orgSvc.CreateWithMember(context.Background(), &org.CreateOrgCommand{Name: "test org"})
|
||||
require.NoError(t, err)
|
||||
|
||||
seedOrgUsers(t, &orgUserStore, store, 10, userSvc, o.ID)
|
||||
|
||||
tests := []struct {
|
||||
desc string
|
||||
query *org.SearchOrgUsersQuery
|
||||
@@ -715,9 +746,9 @@ func TestIntegration_SQLStore_SearchOrgUsers(t *testing.T) {
|
||||
{
|
||||
desc: "should return all users",
|
||||
query: &org.SearchOrgUsersQuery{
|
||||
OrgID: 1,
|
||||
OrgID: o.ID,
|
||||
User: &user.SignedInUser{
|
||||
OrgID: 1,
|
||||
OrgID: o.ID,
|
||||
Permissions: map[int64]map[string][]string{1: {accesscontrol.ActionOrgUsersRead: {accesscontrol.ScopeUsersAll}}},
|
||||
},
|
||||
},
|
||||
@@ -726,9 +757,9 @@ func TestIntegration_SQLStore_SearchOrgUsers(t *testing.T) {
|
||||
{
|
||||
desc: "should return no users",
|
||||
query: &org.SearchOrgUsersQuery{
|
||||
OrgID: 1,
|
||||
OrgID: o.ID,
|
||||
User: &user.SignedInUser{
|
||||
OrgID: 1,
|
||||
OrgID: o.ID,
|
||||
Permissions: map[int64]map[string][]string{1: {accesscontrol.ActionOrgUsersRead: {""}}},
|
||||
},
|
||||
},
|
||||
@@ -737,9 +768,9 @@ func TestIntegration_SQLStore_SearchOrgUsers(t *testing.T) {
|
||||
{
|
||||
desc: "should return some users",
|
||||
query: &org.SearchOrgUsersQuery{
|
||||
OrgID: 1,
|
||||
OrgID: o.ID,
|
||||
User: &user.SignedInUser{
|
||||
OrgID: 1,
|
||||
OrgID: o.ID,
|
||||
Permissions: map[int64]map[string][]string{1: {accesscontrol.ActionOrgUsersRead: {
|
||||
"users:id:1",
|
||||
"users:id:5",
|
||||
@@ -751,15 +782,6 @@ func TestIntegration_SQLStore_SearchOrgUsers(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
store := db.InitTestDB(t, sqlstore.InitTestDBOpt{})
|
||||
orgUserStore := sqlStore{
|
||||
db: store,
|
||||
dialect: store.GetDialect(),
|
||||
cfg: setting.NewCfg(),
|
||||
}
|
||||
// orgUserStore.cfg.Skip
|
||||
seedOrgUsers(t, &orgUserStore, store, 10)
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.desc, func(t *testing.T) {
|
||||
result, err := orgUserStore.SearchOrgUsers(context.Background(), tt.query)
|
||||
@@ -785,11 +807,15 @@ func TestIntegration_SQLStore_RemoveOrgUser(t *testing.T) {
|
||||
dialect: store.GetDialect(),
|
||||
cfg: setting.NewCfg(),
|
||||
}
|
||||
_, usrSvc := createOrgAndUserSvc(t, store, store.Cfg)
|
||||
orgSvc, usrSvc := createOrgAndUserSvc(t, store, store.Cfg)
|
||||
|
||||
o, err := orgSvc.CreateWithMember(context.Background(), &org.CreateOrgCommand{Name: MainOrgName})
|
||||
require.NoError(t, err)
|
||||
|
||||
// create org and admin
|
||||
_, err := usrSvc.Create(context.Background(), &user.CreateUserCommand{
|
||||
_, err = usrSvc.Create(context.Background(), &user.CreateUserCommand{
|
||||
Login: "admin",
|
||||
OrgID: 1,
|
||||
OrgID: o.ID,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
|
||||
@@ -24,7 +24,6 @@ func createServiceAccountAdminToken(t *testing.T, name string, env *server.TestE
|
||||
Role: string(org.RoleAdmin),
|
||||
Login: name,
|
||||
IsServiceAccount: true,
|
||||
OrgID: 1,
|
||||
})
|
||||
|
||||
keyGen, err := apikeygenprefix.New(saAPI.ServiceID)
|
||||
|
||||
@@ -25,7 +25,6 @@ type TestUser struct {
|
||||
Role string
|
||||
Login string
|
||||
IsServiceAccount bool
|
||||
OrgID int64
|
||||
}
|
||||
|
||||
type TestApiKey struct {
|
||||
@@ -49,12 +48,17 @@ func SetupUserServiceAccount(t *testing.T, sqlStore *sqlstore.SQLStore, testUser
|
||||
usrSvc, err := userimpl.ProvideService(sqlStore, orgService, sqlStore.Cfg, nil, nil, quotaService)
|
||||
require.NoError(t, err)
|
||||
|
||||
u1, err := usrSvc.CreateUserForTests(context.Background(), &user.CreateUserCommand{
|
||||
org, err := orgService.CreateWithMember(context.Background(), &org.CreateOrgCommand{
|
||||
Name: "test org",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
u1, err := usrSvc.Create(context.Background(), &user.CreateUserCommand{
|
||||
Login: testUser.Login,
|
||||
IsServiceAccount: testUser.IsServiceAccount,
|
||||
DefaultOrgRole: role,
|
||||
Name: testUser.Name,
|
||||
OrgID: testUser.OrgID,
|
||||
OrgID: org.ID,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
return u1
|
||||
|
||||
@@ -83,7 +83,7 @@ func populateDB(t *testing.T, sqlStore *sqlstore.SQLStore) {
|
||||
Login: fmt.Sprintf("user_test_%v_login", i),
|
||||
OrgName: fmt.Sprintf("Org #%v", i),
|
||||
}
|
||||
user, err := userSvc.CreateUserForTests(context.Background(), &cmd)
|
||||
user, err := userSvc.Create(context.Background(), &cmd)
|
||||
require.NoError(t, err)
|
||||
users[i] = *user
|
||||
}
|
||||
|
||||
@@ -28,7 +28,6 @@ func createServiceAccountAdminToken(t *testing.T, env *server.TestEnv) (string,
|
||||
Role: string(org.RoleAdmin),
|
||||
Login: "grpc-server-sa",
|
||||
IsServiceAccount: true,
|
||||
OrgID: 1,
|
||||
})
|
||||
|
||||
keyGen, err := apikeygenprefix.New(saAPI.ServiceID)
|
||||
|
||||
@@ -394,7 +394,7 @@ func TestIntegrationTeamCommandsAndQueries(t *testing.T) {
|
||||
Login: fmt.Sprint("login-sa", 1),
|
||||
IsServiceAccount: true,
|
||||
}
|
||||
serviceAccount, err := userSvc.CreateUserForTests(context.Background(), &userCmd)
|
||||
serviceAccount, err := userSvc.Create(context.Background(), &userCmd)
|
||||
require.NoError(t, err)
|
||||
|
||||
groupId := team2.ID
|
||||
|
||||
@@ -24,7 +24,4 @@ type Service interface {
|
||||
UpdatePermissions(context.Context, int64, bool) error
|
||||
SetUserHelpFlag(context.Context, *SetUserHelpFlagCommand) error
|
||||
GetProfile(context.Context, *GetUserProfileQuery) (*UserProfileDTO, error)
|
||||
|
||||
// TEST ONLY METHOD
|
||||
CreateUserForTests(context.Context, *CreateUserCommand) (*User, error)
|
||||
}
|
||||
|
||||
@@ -808,7 +808,7 @@ func createFiveTestUsers(t *testing.T, svc user.Service, fn func(i int) *user.Cr
|
||||
users := make([]user.User, 5)
|
||||
for i := 0; i < 5; i++ {
|
||||
cmd := fn(i)
|
||||
user, err := svc.CreateUserForTests(context.Background(), cmd)
|
||||
user, err := svc.Create(context.Background(), cmd)
|
||||
require.Nil(t, err)
|
||||
users[i] = *user
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -107,9 +106,9 @@ func (s *Service) Create(ctx context.Context, cmd *user.CreateUserCommand) (*use
|
||||
IsDisabled: cmd.IsDisabled,
|
||||
OrgID: orgID,
|
||||
EmailVerified: cmd.EmailVerified,
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
LastSeenAt: time.Now().AddDate(-10, 0, 0),
|
||||
Created: timeNow(),
|
||||
Updated: timeNow(),
|
||||
LastSeenAt: timeNow().AddDate(-10, 0, 0),
|
||||
IsServiceAccount: cmd.IsServiceAccount,
|
||||
}
|
||||
|
||||
@@ -356,116 +355,7 @@ func readQuotaConfig(cfg *setting.Cfg) (*quota.Map, error) {
|
||||
return limits, nil
|
||||
}
|
||||
|
||||
// CreateUserForTests creates a test user and optionally an organization. Unlike
|
||||
// Create, `cmd.SkipOrgSetup` toggles whether or not to create an org for the
|
||||
// test user if there isn't already an existing org. This must only be used in tests.
|
||||
func (s *Service) CreateUserForTests(ctx context.Context, cmd *user.CreateUserCommand) (*user.User, error) {
|
||||
var orgID int64 = -1
|
||||
var err error
|
||||
if !cmd.SkipOrgSetup {
|
||||
orgID, err = s.getOrgIDForNewUser(ctx, cmd)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if cmd.Email == "" {
|
||||
cmd.Email = cmd.Login
|
||||
}
|
||||
|
||||
usr, err := s.GetByLogin(ctx, &user.GetUserByLoginQuery{LoginOrEmail: cmd.Login})
|
||||
if err != nil && !errors.Is(err, user.ErrUserNotFound) {
|
||||
return usr, err
|
||||
} else if err == nil { // user exists
|
||||
return usr, err
|
||||
}
|
||||
|
||||
// create user
|
||||
usr = &user.User{
|
||||
Email: cmd.Email,
|
||||
Name: cmd.Name,
|
||||
Login: cmd.Login,
|
||||
Company: cmd.Company,
|
||||
IsAdmin: cmd.IsAdmin,
|
||||
IsDisabled: cmd.IsDisabled,
|
||||
OrgID: orgID,
|
||||
EmailVerified: cmd.EmailVerified,
|
||||
Created: timeNow(),
|
||||
Updated: timeNow(),
|
||||
LastSeenAt: timeNow().AddDate(-10, 0, 0),
|
||||
IsServiceAccount: cmd.IsServiceAccount,
|
||||
}
|
||||
|
||||
salt, err := util.GetRandomString(10)
|
||||
if err != nil {
|
||||
return usr, err
|
||||
}
|
||||
usr.Salt = salt
|
||||
rands, err := util.GetRandomString(10)
|
||||
if err != nil {
|
||||
return usr, err
|
||||
}
|
||||
usr.Rands = rands
|
||||
|
||||
if len(cmd.Password) > 0 {
|
||||
encodedPassword, err := util.EncodePassword(cmd.Password, usr.Salt)
|
||||
if err != nil {
|
||||
return usr, err
|
||||
}
|
||||
usr.Password = encodedPassword
|
||||
}
|
||||
|
||||
_, err = s.store.Insert(ctx, usr)
|
||||
if err != nil {
|
||||
return usr, err
|
||||
}
|
||||
|
||||
// create org user link
|
||||
if !cmd.SkipOrgSetup {
|
||||
orgCmd := &org.AddOrgUserCommand{
|
||||
OrgID: orgID,
|
||||
UserID: usr.ID,
|
||||
Role: org.RoleAdmin,
|
||||
AllowAddingServiceAccount: true,
|
||||
}
|
||||
|
||||
if s.cfg.AutoAssignOrg && !usr.IsAdmin {
|
||||
if len(cmd.DefaultOrgRole) > 0 {
|
||||
orgCmd.Role = org.RoleType(cmd.DefaultOrgRole)
|
||||
} else {
|
||||
orgCmd.Role = org.RoleType(s.cfg.AutoAssignOrgRole)
|
||||
}
|
||||
}
|
||||
|
||||
if err = s.orgService.AddOrgUser(ctx, orgCmd); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return usr, nil
|
||||
}
|
||||
|
||||
func (s *Service) getOrgIDForNewUser(ctx context.Context, cmd *user.CreateUserCommand) (int64, error) {
|
||||
if s.cfg.AutoAssignOrg && cmd.OrgID != 0 {
|
||||
if _, err := s.orgService.GetByID(ctx, &org.GetOrgByIDQuery{ID: cmd.OrgID}); err != nil {
|
||||
return -1, err
|
||||
}
|
||||
return cmd.OrgID, nil
|
||||
}
|
||||
|
||||
orgName := cmd.OrgName
|
||||
if orgName == "" {
|
||||
orgName = util.StringsFallback2(cmd.Email, cmd.Login)
|
||||
}
|
||||
|
||||
orgID, err := s.orgService.GetOrCreate(ctx, orgName)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return orgID, err
|
||||
}
|
||||
|
||||
// CreateServiceAccount is a copy of Create with a single difference; it will create the OrgUser service account.
|
||||
// CreateServiceAccount creates a service account in the user table and adds service account to an organisation in the org_user table
|
||||
func (s *Service) CreateServiceAccount(ctx context.Context, cmd *user.CreateUserCommand) (*user.User, error) {
|
||||
cmdOrg := org.GetOrgIDForNewUserCommand{
|
||||
Email: cmd.Email,
|
||||
|
||||
@@ -33,10 +33,6 @@ func (f *FakeUserService) Create(ctx context.Context, cmd *user.CreateUserComman
|
||||
return f.ExpectedUser, f.ExpectedError
|
||||
}
|
||||
|
||||
func (f *FakeUserService) CreateUserForTests(ctx context.Context, cmd *user.CreateUserCommand) (*user.User, error) {
|
||||
return f.ExpectedUser, f.ExpectedError
|
||||
}
|
||||
|
||||
func (f *FakeUserService) CreateServiceAccount(ctx context.Context, cmd *user.CreateUserCommand) (*user.User, error) {
|
||||
return f.ExpectedUser, f.ExpectedError
|
||||
}
|
||||
|
||||
@@ -2567,7 +2567,7 @@ func createUser(t *testing.T, store *sqlstore.SQLStore, cmd user.CreateUserComma
|
||||
usrSvc, err := userimpl.ProvideService(store, orgService, store.Cfg, nil, nil, quotaService)
|
||||
require.NoError(t, err)
|
||||
|
||||
u, err := usrSvc.CreateUserForTests(context.Background(), &cmd)
|
||||
u, err := usrSvc.Create(context.Background(), &cmd)
|
||||
require.NoError(t, err)
|
||||
return u.ID
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ func TestIntegrationAzureMonitor(t *testing.T) {
|
||||
grafanaListeningAddr, testEnv := testinfra.StartGrafanaEnv(t, dir, path)
|
||||
ctx := context.Background()
|
||||
|
||||
testinfra.CreateUser(t, testEnv.SQLStore, user.CreateUserCommand{
|
||||
u := testinfra.CreateUser(t, testEnv.SQLStore, user.CreateUserCommand{
|
||||
DefaultOrgRole: string(org.RoleAdmin),
|
||||
Password: "admin",
|
||||
Login: "admin",
|
||||
@@ -65,7 +65,7 @@ func TestIntegrationAzureMonitor(t *testing.T) {
|
||||
|
||||
uid := "azuremonitor"
|
||||
err := testEnv.Server.HTTPServer.DataSourcesService.AddDataSource(ctx, &datasources.AddDataSourceCommand{
|
||||
OrgId: 1,
|
||||
OrgId: u.OrgID,
|
||||
Access: datasources.DS_ACCESS_PROXY,
|
||||
Name: "Azure Monitor",
|
||||
Type: datasources.DS_AZURE_MONITOR,
|
||||
|
||||
@@ -144,7 +144,7 @@ func (c TestContext) createUser(cmd user.CreateUserCommand) User {
|
||||
usrSvc, err := userimpl.ProvideService(store, orgService, store.Cfg, nil, nil, quotaService)
|
||||
require.NoError(c.t, err)
|
||||
|
||||
user, err := usrSvc.CreateUserForTests(context.Background(), &cmd)
|
||||
user, err := usrSvc.Create(context.Background(), &cmd)
|
||||
require.NoError(c.t, err)
|
||||
|
||||
return User{
|
||||
|
||||
@@ -113,7 +113,7 @@ func createUser(t *testing.T, store *sqlstore.SQLStore, cmd user.CreateUserComma
|
||||
usrSvc, err := userimpl.ProvideService(store, orgService, store.Cfg, nil, nil, quotaService)
|
||||
require.NoError(t, err)
|
||||
|
||||
u, err := usrSvc.CreateUserForTests(context.Background(), &cmd)
|
||||
u, err := usrSvc.Create(context.Background(), &cmd)
|
||||
require.NoError(t, err)
|
||||
return u.ID
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ func TestIntegrationElasticsearch(t *testing.T) {
|
||||
grafanaListeningAddr, testEnv := testinfra.StartGrafanaEnv(t, dir, path)
|
||||
ctx := context.Background()
|
||||
|
||||
testinfra.CreateUser(t, testEnv.SQLStore, user.CreateUserCommand{
|
||||
u := testinfra.CreateUser(t, testEnv.SQLStore, user.CreateUserCommand{
|
||||
DefaultOrgRole: string(org.RoleAdmin),
|
||||
Password: "admin",
|
||||
Login: "admin",
|
||||
@@ -57,7 +57,7 @@ func TestIntegrationElasticsearch(t *testing.T) {
|
||||
|
||||
uid := "es"
|
||||
err := testEnv.Server.HTTPServer.DataSourcesService.AddDataSource(ctx, &datasources.AddDataSourceCommand{
|
||||
OrgId: 1,
|
||||
OrgId: u.OrgID,
|
||||
Access: datasources.DS_ACCESS_PROXY,
|
||||
Name: "Elasticsearch",
|
||||
Type: datasources.DS_ES,
|
||||
|
||||
@@ -31,7 +31,7 @@ func TestIntegrationGraphite(t *testing.T) {
|
||||
grafanaListeningAddr, testEnv := testinfra.StartGrafanaEnv(t, dir, path)
|
||||
ctx := context.Background()
|
||||
|
||||
testinfra.CreateUser(t, testEnv.SQLStore, user.CreateUserCommand{
|
||||
u := testinfra.CreateUser(t, testEnv.SQLStore, user.CreateUserCommand{
|
||||
DefaultOrgRole: string(org.RoleAdmin),
|
||||
Password: "admin",
|
||||
Login: "admin",
|
||||
@@ -55,7 +55,7 @@ func TestIntegrationGraphite(t *testing.T) {
|
||||
|
||||
uid := "graphite"
|
||||
err := testEnv.Server.HTTPServer.DataSourcesService.AddDataSource(ctx, &datasources.AddDataSourceCommand{
|
||||
OrgId: 1,
|
||||
OrgId: u.OrgID,
|
||||
Access: datasources.DS_ACCESS_PROXY,
|
||||
Name: "graphite",
|
||||
Type: datasources.DS_GRAPHITE,
|
||||
|
||||
@@ -31,7 +31,7 @@ func TestIntegrationInflux(t *testing.T) {
|
||||
grafanaListeningAddr, testEnv := testinfra.StartGrafanaEnv(t, dir, path)
|
||||
ctx := context.Background()
|
||||
|
||||
testinfra.CreateUser(t, testEnv.SQLStore, user.CreateUserCommand{
|
||||
u := testinfra.CreateUser(t, testEnv.SQLStore, user.CreateUserCommand{
|
||||
DefaultOrgRole: string(org.RoleAdmin),
|
||||
Password: "admin",
|
||||
Login: "admin",
|
||||
@@ -55,7 +55,7 @@ func TestIntegrationInflux(t *testing.T) {
|
||||
|
||||
uid := "influxdb"
|
||||
err := testEnv.Server.HTTPServer.DataSourcesService.AddDataSource(ctx, &datasources.AddDataSourceCommand{
|
||||
OrgId: 1,
|
||||
OrgId: u.OrgID,
|
||||
Access: datasources.DS_ACCESS_PROXY,
|
||||
Name: "InfluxDB",
|
||||
Type: datasources.DS_INFLUXDB,
|
||||
|
||||
@@ -31,7 +31,7 @@ func TestIntegrationLoki(t *testing.T) {
|
||||
grafanaListeningAddr, testEnv := testinfra.StartGrafanaEnv(t, dir, path)
|
||||
ctx := context.Background()
|
||||
|
||||
testinfra.CreateUser(t, testEnv.SQLStore, user.CreateUserCommand{
|
||||
u := testinfra.CreateUser(t, testEnv.SQLStore, user.CreateUserCommand{
|
||||
DefaultOrgRole: string(org.RoleAdmin),
|
||||
Password: "admin",
|
||||
Login: "admin",
|
||||
@@ -55,7 +55,7 @@ func TestIntegrationLoki(t *testing.T) {
|
||||
|
||||
uid := "loki"
|
||||
err := testEnv.Server.HTTPServer.DataSourcesService.AddDataSource(ctx, &datasources.AddDataSourceCommand{
|
||||
OrgId: 1,
|
||||
OrgId: u.OrgID,
|
||||
Access: datasources.DS_ACCESS_PROXY,
|
||||
Name: "Loki",
|
||||
Type: datasources.DS_LOKI,
|
||||
|
||||
@@ -31,7 +31,7 @@ func TestIntegrationOpenTSDB(t *testing.T) {
|
||||
grafanaListeningAddr, testEnv := testinfra.StartGrafanaEnv(t, dir, path)
|
||||
ctx := context.Background()
|
||||
|
||||
testinfra.CreateUser(t, testEnv.SQLStore, user.CreateUserCommand{
|
||||
u := testinfra.CreateUser(t, testEnv.SQLStore, user.CreateUserCommand{
|
||||
DefaultOrgRole: string(org.RoleAdmin),
|
||||
Password: "admin",
|
||||
Login: "admin",
|
||||
@@ -55,7 +55,7 @@ func TestIntegrationOpenTSDB(t *testing.T) {
|
||||
|
||||
uid := "influxdb"
|
||||
err := testEnv.Server.HTTPServer.DataSourcesService.AddDataSource(ctx, &datasources.AddDataSourceCommand{
|
||||
OrgId: 1,
|
||||
OrgId: u.OrgID,
|
||||
Access: datasources.DS_ACCESS_PROXY,
|
||||
Name: "opentsdb",
|
||||
Type: datasources.DS_OPENTSDB,
|
||||
|
||||
@@ -128,7 +128,7 @@ func createUser(t *testing.T, store *sqlstore.SQLStore, cmd user.CreateUserComma
|
||||
usrSvc, err := userimpl.ProvideService(store, orgService, store.Cfg, nil, nil, quotaService)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = usrSvc.CreateUserForTests(context.Background(), &cmd)
|
||||
_, err = usrSvc.Create(context.Background(), &cmd)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
|
||||
@@ -262,7 +262,7 @@ func newTestScenario(t *testing.T, name string, opts []testScenarioOption, callb
|
||||
tsCtx.testEnv = testEnv
|
||||
ctx := context.Background()
|
||||
|
||||
testinfra.CreateUser(t, testEnv.SQLStore, user.CreateUserCommand{
|
||||
u := testinfra.CreateUser(t, testEnv.SQLStore, user.CreateUserCommand{
|
||||
DefaultOrgRole: string(org.RoleAdmin),
|
||||
Password: "admin",
|
||||
Login: "admin",
|
||||
@@ -284,7 +284,7 @@ func newTestScenario(t *testing.T, name string, opts []testScenarioOption, callb
|
||||
|
||||
tsCtx.uid = "test-plugin"
|
||||
cmd := &datasources.AddDataSourceCommand{
|
||||
OrgId: 1,
|
||||
OrgId: u.OrgID,
|
||||
Access: datasources.DS_ACCESS_PROXY,
|
||||
Name: "TestPlugin",
|
||||
Type: tsCtx.testPluginID,
|
||||
@@ -306,7 +306,7 @@ func newTestScenario(t *testing.T, name string, opts []testScenarioOption, callb
|
||||
require.NoError(t, err)
|
||||
|
||||
getDataSourceQuery := &datasources.GetDataSourceQuery{
|
||||
OrgId: 1,
|
||||
OrgId: u.OrgID,
|
||||
Uid: tsCtx.uid,
|
||||
}
|
||||
err = testEnv.Server.HTTPServer.DataSourcesService.GetDataSource(ctx, getDataSourceQuery)
|
||||
|
||||
@@ -31,7 +31,7 @@ func TestIntegrationPrometheus(t *testing.T) {
|
||||
grafanaListeningAddr, testEnv := testinfra.StartGrafanaEnv(t, dir, path)
|
||||
ctx := context.Background()
|
||||
|
||||
testinfra.CreateUser(t, testEnv.SQLStore, user.CreateUserCommand{
|
||||
u := testinfra.CreateUser(t, testEnv.SQLStore, user.CreateUserCommand{
|
||||
DefaultOrgRole: string(org.RoleAdmin),
|
||||
Password: "admin",
|
||||
Login: "admin",
|
||||
@@ -56,7 +56,7 @@ func TestIntegrationPrometheus(t *testing.T) {
|
||||
|
||||
uid := "prometheus"
|
||||
err := testEnv.Server.HTTPServer.DataSourcesService.AddDataSource(ctx, &datasources.AddDataSourceCommand{
|
||||
OrgId: 1,
|
||||
OrgId: u.OrgID,
|
||||
Access: datasources.DS_ACCESS_PROXY,
|
||||
Name: "Prometheus",
|
||||
Type: datasources.DS_PROMETHEUS,
|
||||
|
||||
@@ -375,7 +375,7 @@ type GrafanaOpts struct {
|
||||
QueryRetries int64
|
||||
}
|
||||
|
||||
func CreateUser(t *testing.T, store *sqlstore.SQLStore, cmd user.CreateUserCommand) int64 {
|
||||
func CreateUser(t *testing.T, store *sqlstore.SQLStore, cmd user.CreateUserCommand) *user.User {
|
||||
t.Helper()
|
||||
|
||||
store.Cfg.AutoAssignOrg = true
|
||||
@@ -388,7 +388,12 @@ func CreateUser(t *testing.T, store *sqlstore.SQLStore, cmd user.CreateUserComma
|
||||
usrSvc, err := userimpl.ProvideService(store, orgService, store.Cfg, nil, nil, quotaService)
|
||||
require.NoError(t, err)
|
||||
|
||||
u, err := usrSvc.CreateUserForTests(context.Background(), &cmd)
|
||||
o, err := orgService.CreateWithMember(context.Background(), &org.CreateOrgCommand{Name: fmt.Sprintf("test org %d", time.Now().UnixNano())})
|
||||
require.NoError(t, err)
|
||||
return u.ID
|
||||
|
||||
cmd.OrgID = o.ID
|
||||
|
||||
u, err := usrSvc.Create(context.Background(), &cmd)
|
||||
require.NoError(t, err)
|
||||
return u
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user