diff --git a/pkg/api/team_members_test.go b/pkg/api/team_members_test.go index 7a6f44d4cf9..8c54bb68805 100644 --- a/pkg/api/team_members_test.go +++ b/pkg/api/team_members_test.go @@ -65,7 +65,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) diff --git a/pkg/api/user_test.go b/pkg/api/user_test.go index 40c03d0315b..2124a3ef9c0 100644 --- a/pkg/api/user_test.go +++ b/pkg/api/user_test.go @@ -78,7 +78,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 diff --git a/pkg/cmd/grafana-cli/commands/conflict_user_command_test.go b/pkg/cmd/grafana-cli/commands/conflict_user_command_test.go index 05e5b343f6e..d15eb50f495 100644 --- a/pkg/cmd/grafana-cli/commands/conflict_user_command_test.go +++ b/pkg/cmd/grafana-cli/commands/conflict_user_command_test.go @@ -6,6 +6,7 @@ import ( "os" "sort" "testing" + "time" "github.com/stretchr/testify/require" "github.com/urfave/cli/v2" @@ -107,17 +108,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) @@ -212,16 +216,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) } @@ -390,17 +397,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) @@ -499,16 +509,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) @@ -756,16 +769,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 diff --git a/pkg/services/dashboards/database/acl_test.go b/pkg/services/dashboards/database/acl_test.go index 6ed81faf772..92043205c61 100644 --- a/pkg/services/dashboards/database/acl_test.go +++ b/pkg/services/dashboards/database/acl_test.go @@ -2,7 +2,9 @@ package database import ( "context" + "fmt" "testing" + "time" "github.com/stretchr/testify/require" @@ -29,7 +31,7 @@ func TestIntegrationDashboardACLDataAccess(t *testing.T) { var savedFolder, childDash *dashboards.Dashboard var dashboardStore dashboards.Store - setup := func(t *testing.T) { + setup := func(t *testing.T) int64 { sqlStore = db.InitTestDB(t) quotaService := quotatest.New(false, nil) var err error @@ -38,12 +40,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, }) @@ -51,8 +54,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) @@ -68,8 +71,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) @@ -85,11 +88,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) @@ -98,9 +101,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, @@ -108,7 +111,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) @@ -119,7 +122,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, @@ -127,7 +130,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) @@ -142,7 +145,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, @@ -151,7 +154,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) @@ -169,7 +172,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, @@ -178,7 +181,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) @@ -192,14 +195,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) @@ -212,7 +215,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) @@ -221,7 +224,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) @@ -233,7 +236,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)) @@ -244,11 +247,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) @@ -282,11 +285,15 @@ func createUser(t *testing.T, sqlStore *sqlstore.SQLStore, name string, role str usrSvc, err := userimpl.ProvideService(sqlStore, orgService, sqlStore.Cfg, nil, nil, qs, supportbundlestest.NewFakeBundleService()) 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 } diff --git a/pkg/services/org/orgimpl/store_test.go b/pkg/services/org/orgimpl/store_test.go index 5a72372b30a..3262b7c24b5 100644 --- a/pkg/services/org/orgimpl/store_test.go +++ b/pkg/services/org/orgimpl/store_test.go @@ -309,9 +309,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, @@ -419,7 +419,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{ @@ -427,10 +427,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) @@ -479,16 +483,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, @@ -534,49 +542,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{ @@ -589,7 +554,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) { @@ -606,26 +621,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) } } @@ -663,7 +679,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, @@ -708,6 +724,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 @@ -716,9 +747,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}}}, }, }, @@ -727,9 +758,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: {""}}}, }, }, @@ -738,9 +769,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", @@ -752,15 +783,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) @@ -786,11 +808,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) diff --git a/pkg/services/querylibrary/tests/common.go b/pkg/services/querylibrary/tests/common.go index fddced65a0b..77cb7ea47df 100644 --- a/pkg/services/querylibrary/tests/common.go +++ b/pkg/services/querylibrary/tests/common.go @@ -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) diff --git a/pkg/services/serviceaccounts/tests/common.go b/pkg/services/serviceaccounts/tests/common.go index 1e12c690543..c12e49a0075 100644 --- a/pkg/services/serviceaccounts/tests/common.go +++ b/pkg/services/serviceaccounts/tests/common.go @@ -26,7 +26,6 @@ type TestUser struct { Role string Login string IsServiceAccount bool - OrgID int64 } type TestApiKey struct { @@ -50,12 +49,17 @@ func SetupUserServiceAccount(t *testing.T, sqlStore *sqlstore.SQLStore, testUser usrSvc, err := userimpl.ProvideService(sqlStore, orgService, sqlStore.Cfg, nil, nil, quotaService, supportbundlestest.NewFakeBundleService()) 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 diff --git a/pkg/services/stats/statsimpl/stats_test.go b/pkg/services/stats/statsimpl/stats_test.go index 97ad6bd0648..c9787590a1c 100644 --- a/pkg/services/stats/statsimpl/stats_test.go +++ b/pkg/services/stats/statsimpl/stats_test.go @@ -84,7 +84,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 } diff --git a/pkg/services/store/entity/tests/common.go b/pkg/services/store/entity/tests/common.go index f481365ddf5..c45da12d6ce 100644 --- a/pkg/services/store/entity/tests/common.go +++ b/pkg/services/store/entity/tests/common.go @@ -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) diff --git a/pkg/services/team/teamimpl/store_test.go b/pkg/services/team/teamimpl/store_test.go index e5dea577616..c185a371f72 100644 --- a/pkg/services/team/teamimpl/store_test.go +++ b/pkg/services/team/teamimpl/store_test.go @@ -396,7 +396,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 diff --git a/pkg/services/user/user.go b/pkg/services/user/user.go index 2852ef4afd4..7c2a4a05b05 100644 --- a/pkg/services/user/user.go +++ b/pkg/services/user/user.go @@ -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) } diff --git a/pkg/services/user/userimpl/store_test.go b/pkg/services/user/userimpl/store_test.go index bcf07e56d47..e3b43a801b0 100644 --- a/pkg/services/user/userimpl/store_test.go +++ b/pkg/services/user/userimpl/store_test.go @@ -809,7 +809,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 } diff --git a/pkg/services/user/userimpl/user.go b/pkg/services/user/userimpl/user.go index b51116fb21d..1ec9e76e5a0 100644 --- a/pkg/services/user/userimpl/user.go +++ b/pkg/services/user/userimpl/user.go @@ -6,7 +6,6 @@ import ( "crypto/sha256" "encoding/hex" "encoding/json" - "errors" "fmt" "strings" "time" @@ -113,9 +112,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, } @@ -362,115 +361,6 @@ 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 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) { cmd.Email = cmd.Login diff --git a/pkg/services/user/usertest/fake.go b/pkg/services/user/usertest/fake.go index 0e2aee7df32..984230916bd 100644 --- a/pkg/services/user/usertest/fake.go +++ b/pkg/services/user/usertest/fake.go @@ -34,10 +34,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 } diff --git a/pkg/tests/api/alerting/api_alertmanager_test.go b/pkg/tests/api/alerting/api_alertmanager_test.go index 5dcaf53249f..cb01fc9b87b 100644 --- a/pkg/tests/api/alerting/api_alertmanager_test.go +++ b/pkg/tests/api/alerting/api_alertmanager_test.go @@ -2569,7 +2569,7 @@ func createUser(t *testing.T, store *sqlstore.SQLStore, cmd user.CreateUserComma usrSvc, err := userimpl.ProvideService(store, orgService, store.Cfg, nil, nil, quotaService, supportbundlestest.NewFakeBundleService()) 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 } diff --git a/pkg/tests/api/azuremonitor/azuremonitor_test.go b/pkg/tests/api/azuremonitor/azuremonitor_test.go index b750e852e2f..bd40a4eb605 100644 --- a/pkg/tests/api/azuremonitor/azuremonitor_test.go +++ b/pkg/tests/api/azuremonitor/azuremonitor_test.go @@ -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, diff --git a/pkg/tests/api/correlations/common_test.go b/pkg/tests/api/correlations/common_test.go index c216d16ebfd..7def77cf0cb 100644 --- a/pkg/tests/api/correlations/common_test.go +++ b/pkg/tests/api/correlations/common_test.go @@ -145,7 +145,7 @@ func (c TestContext) createUser(cmd user.CreateUserCommand) User { usrSvc, err := userimpl.ProvideService(store, orgService, store.Cfg, nil, nil, quotaService, supportbundlestest.NewFakeBundleService()) 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{ diff --git a/pkg/tests/api/dashboards/api_dashboards_test.go b/pkg/tests/api/dashboards/api_dashboards_test.go index af49bf79c41..78d89989fa1 100644 --- a/pkg/tests/api/dashboards/api_dashboards_test.go +++ b/pkg/tests/api/dashboards/api_dashboards_test.go @@ -114,7 +114,7 @@ func createUser(t *testing.T, store *sqlstore.SQLStore, cmd user.CreateUserComma usrSvc, err := userimpl.ProvideService(store, orgService, store.Cfg, nil, nil, quotaService, supportbundlestest.NewFakeBundleService()) 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 } diff --git a/pkg/tests/api/elasticsearch/elasticsearch_test.go b/pkg/tests/api/elasticsearch/elasticsearch_test.go index ba6d70a11ca..e017d2247a4 100644 --- a/pkg/tests/api/elasticsearch/elasticsearch_test.go +++ b/pkg/tests/api/elasticsearch/elasticsearch_test.go @@ -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, diff --git a/pkg/tests/api/graphite/graphite_test.go b/pkg/tests/api/graphite/graphite_test.go index ec2a9454a08..97f44d3dd40 100644 --- a/pkg/tests/api/graphite/graphite_test.go +++ b/pkg/tests/api/graphite/graphite_test.go @@ -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, diff --git a/pkg/tests/api/influxdb/influxdb_test.go b/pkg/tests/api/influxdb/influxdb_test.go index 95fc4ab7a26..3e6d933466e 100644 --- a/pkg/tests/api/influxdb/influxdb_test.go +++ b/pkg/tests/api/influxdb/influxdb_test.go @@ -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, diff --git a/pkg/tests/api/loki/loki_test.go b/pkg/tests/api/loki/loki_test.go index 14b61154885..8264c093670 100644 --- a/pkg/tests/api/loki/loki_test.go +++ b/pkg/tests/api/loki/loki_test.go @@ -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, diff --git a/pkg/tests/api/opentdsb/opentdsb_test.go b/pkg/tests/api/opentdsb/opentdsb_test.go index 4211e85c4c6..2a1e9df1299 100644 --- a/pkg/tests/api/opentdsb/opentdsb_test.go +++ b/pkg/tests/api/opentdsb/opentdsb_test.go @@ -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, diff --git a/pkg/tests/api/plugins/api_plugins_test.go b/pkg/tests/api/plugins/api_plugins_test.go index af924c70b6b..cb71529d396 100644 --- a/pkg/tests/api/plugins/api_plugins_test.go +++ b/pkg/tests/api/plugins/api_plugins_test.go @@ -129,7 +129,7 @@ func createUser(t *testing.T, store *sqlstore.SQLStore, cmd user.CreateUserComma usrSvc, err := userimpl.ProvideService(store, orgService, store.Cfg, nil, nil, quotaService, supportbundlestest.NewFakeBundleService()) require.NoError(t, err) - _, err = usrSvc.CreateUserForTests(context.Background(), &cmd) + _, err = usrSvc.Create(context.Background(), &cmd) require.NoError(t, err) } diff --git a/pkg/tests/api/plugins/backendplugin/backendplugin_test.go b/pkg/tests/api/plugins/backendplugin/backendplugin_test.go index 2d825905e32..18a6f8399a3 100644 --- a/pkg/tests/api/plugins/backendplugin/backendplugin_test.go +++ b/pkg/tests/api/plugins/backendplugin/backendplugin_test.go @@ -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, } dataSource, err := testEnv.Server.HTTPServer.DataSourcesService.GetDataSource(ctx, getDataSourceQuery) diff --git a/pkg/tests/api/prometheus/prometheus_test.go b/pkg/tests/api/prometheus/prometheus_test.go index 52bc67d1652..0192df41c26 100644 --- a/pkg/tests/api/prometheus/prometheus_test.go +++ b/pkg/tests/api/prometheus/prometheus_test.go @@ -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, diff --git a/pkg/tests/testinfra/testinfra.go b/pkg/tests/testinfra/testinfra.go index 9f4bc60aab9..e6a8e6c1d96 100644 --- a/pkg/tests/testinfra/testinfra.go +++ b/pkg/tests/testinfra/testinfra.go @@ -376,7 +376,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 @@ -389,7 +389,12 @@ func CreateUser(t *testing.T, store *sqlstore.SQLStore, cmd user.CreateUserComma usrSvc, err := userimpl.ProvideService(store, orgService, store.Cfg, nil, nil, quotaService, supportbundlestest.NewFakeBundleService()) 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 }