Chore: Switch over to team.Service instead of sqlstore (#55497)

* switch to using team service

* trying to fix tests

* more tests to fix

* add missing teamtest package
This commit is contained in:
Serge Zaitsev
2022-09-20 18:58:04 +02:00
committed by GitHub
parent 40bc140a9a
commit 305d494902
25 changed files with 192 additions and 94 deletions

View File

@@ -9,6 +9,7 @@ import (
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/team"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/setting"
)
@@ -48,6 +49,7 @@ type dashboardGuardianImpl struct {
ctx context.Context
store sqlstore.Store
dashboardService dashboards.DashboardService
teamService team.Service
}
// New factory for creating a new dashboard guardian instance
@@ -56,7 +58,7 @@ var New = func(ctx context.Context, dashId int64, orgId int64, user *user.Signed
panic("no guardian factory implementation provided")
}
func newDashboardGuardian(ctx context.Context, dashId int64, orgId int64, user *user.SignedInUser, store sqlstore.Store, dashSvc dashboards.DashboardService) *dashboardGuardianImpl {
func newDashboardGuardian(ctx context.Context, dashId int64, orgId int64, user *user.SignedInUser, store sqlstore.Store, dashSvc dashboards.DashboardService, teamSvc team.Service) *dashboardGuardianImpl {
return &dashboardGuardianImpl{
user: user,
dashId: dashId,
@@ -65,6 +67,7 @@ func newDashboardGuardian(ctx context.Context, dashId int64, orgId int64, user *
ctx: ctx,
store: store,
dashboardService: dashSvc,
teamService: teamSvc,
}
}
@@ -276,7 +279,7 @@ func (g *dashboardGuardianImpl) getTeams() ([]*models.TeamDTO, error) {
}
query := models.GetTeamsByUserQuery{OrgId: g.orgId, UserId: g.user.UserID, SignedInUser: g.user}
err := g.store.GetTeamsByUser(g.ctx, &query)
err := g.teamService.GetTeamsByUser(g.ctx, &query)
g.teams = query.Result
return query.Result, err

View File

@@ -14,6 +14,7 @@ import (
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/sqlstore/mockstore"
"github.com/grafana/grafana/pkg/services/team/teamtest"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/setting"
)
@@ -706,7 +707,7 @@ func TestGuardianGetHiddenACL(t *testing.T) {
UserID: 1,
Login: "user1",
}
g := newDashboardGuardian(context.Background(), dashboardID, orgID, user, store, dashSvc)
g := newDashboardGuardian(context.Background(), dashboardID, orgID, user, store, dashSvc, &teamtest.FakeService{})
hiddenACL, err := g.GetHiddenACL(cfg)
require.NoError(t, err)
@@ -722,7 +723,7 @@ func TestGuardianGetHiddenACL(t *testing.T) {
Login: "user1",
IsGrafanaAdmin: true,
}
g := newDashboardGuardian(context.Background(), dashboardID, orgID, user, store, &dashboards.FakeDashboardService{})
g := newDashboardGuardian(context.Background(), dashboardID, orgID, user, store, &dashboards.FakeDashboardService{}, &teamtest.FakeService{})
hiddenACL, err := g.GetHiddenACL(cfg)
require.NoError(t, err)
@@ -756,7 +757,7 @@ func TestGuardianGetACLWithoutDuplicates(t *testing.T) {
UserID: 1,
Login: "user1",
}
g := newDashboardGuardian(context.Background(), dashboardID, orgID, user, store, dashSvc)
g := newDashboardGuardian(context.Background(), dashboardID, orgID, user, store, dashSvc, &teamtest.FakeService{})
acl, err := g.GetACLWithoutDuplicates()
require.NoError(t, err)

View File

@@ -14,6 +14,7 @@ import (
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/sqlstore/mockstore"
"github.com/grafana/grafana/pkg/services/team/teamtest"
"github.com/grafana/grafana/pkg/services/user"
)
@@ -42,7 +43,7 @@ func orgRoleScenario(desc string, t *testing.T, role org.RoleType, fn scenarioFu
OrgRole: role,
}
store := mockstore.NewSQLStoreMock()
guard := newDashboardGuardian(context.Background(), dashboardID, orgID, user, store, &dashboards.FakeDashboardService{})
guard := newDashboardGuardian(context.Background(), dashboardID, orgID, user, store, &dashboards.FakeDashboardService{}, &teamtest.FakeService{})
sc := &scenarioContext{
t: t,
@@ -64,7 +65,7 @@ func apiKeyScenario(desc string, t *testing.T, role org.RoleType, fn scenarioFun
ApiKeyID: 10,
}
store := mockstore.NewSQLStoreMock()
guard := newDashboardGuardian(context.Background(), dashboardID, orgID, user, store, &dashboards.FakeDashboardService{})
guard := newDashboardGuardian(context.Background(), dashboardID, orgID, user, store, &dashboards.FakeDashboardService{}, &teamtest.FakeService{})
sc := &scenarioContext{
t: t,
orgRoleScenario: desc,
@@ -88,7 +89,7 @@ func permissionScenario(desc string, dashboardID int64, sc *scenarioContext,
teams = append(teams, &models.TeamDTO{Id: p.TeamId})
}
}
store.ExpectedTeamsByUser = teams
teamSvc := &teamtest.FakeService{ExpectedTeamsByUser: teams}
dashSvc := dashboards.NewFakeDashboardService(t)
dashSvc.On("GetDashboardACLInfoList", mock.Anything, mock.AnythingOfType("*models.GetDashboardACLInfoListQuery")).Run(func(args mock.Arguments) {
@@ -97,7 +98,7 @@ func permissionScenario(desc string, dashboardID int64, sc *scenarioContext,
}).Return(nil)
sc.permissionScenario = desc
sc.g = newDashboardGuardian(context.Background(), dashboardID, sc.givenUser.OrgID, sc.givenUser, store, dashSvc)
sc.g = newDashboardGuardian(context.Background(), dashboardID, sc.givenUser.OrgID, sc.givenUser, store, dashSvc, teamSvc)
sc.givenDashboardID = dashboardID
sc.givenPermissions = permissions
sc.givenTeams = teams

View File

@@ -6,6 +6,7 @@ import (
"github.com/grafana/grafana/pkg/services/accesscontrol"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/team"
"github.com/grafana/grafana/pkg/services/user"
)
@@ -14,20 +15,20 @@ type Provider struct{}
func ProvideService(
store *sqlstore.SQLStore, ac accesscontrol.AccessControl,
folderPermissionsService accesscontrol.FolderPermissionsService, dashboardPermissionsService accesscontrol.DashboardPermissionsService,
dashboardService dashboards.DashboardService,
dashboardService dashboards.DashboardService, teamService team.Service,
) *Provider {
if !ac.IsDisabled() {
// TODO: Fix this hack, see https://github.com/grafana/grafana-enterprise/issues/2935
InitAccessControlGuardian(store, ac, folderPermissionsService, dashboardPermissionsService, dashboardService)
} else {
InitLegacyGuardian(store, dashboardService)
InitLegacyGuardian(store, dashboardService, teamService)
}
return &Provider{}
}
func InitLegacyGuardian(store sqlstore.Store, dashSvc dashboards.DashboardService) {
func InitLegacyGuardian(store sqlstore.Store, dashSvc dashboards.DashboardService, teamSvc team.Service) {
New = func(ctx context.Context, dashId int64, orgId int64, user *user.SignedInUser) DashboardGuardian {
return newDashboardGuardian(ctx, dashId, orgId, user, store, dashSvc)
return newDashboardGuardian(ctx, dashId, orgId, user, store, dashSvc, teamSvc)
}
}