Chore: Remove CreateUserForTests() (#64125)
* Chore: Remove CreateUserForTests * Apply suggestion from code review
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user