Replace AddHandler with AddHandlerCtx in tests (#42585)
This commit is contained in:
@@ -18,7 +18,7 @@ func TestAlertingUsageStats(t *testing.T) {
|
||||
Bus: bus.New(),
|
||||
}
|
||||
|
||||
ae.Bus.AddHandler(func(query *models.GetAllAlertsQuery) error {
|
||||
ae.Bus.AddHandlerCtx(func(ctx context.Context, query *models.GetAllAlertsQuery) error {
|
||||
var createFake = func(file string) *simplejson.Json {
|
||||
// Ignore gosec warning G304 since it's a test
|
||||
// nolint:gosec
|
||||
|
||||
@@ -53,7 +53,7 @@ func TestEngineProcessJob(t *testing.T) {
|
||||
job := &Job{running: true, Rule: &Rule{}}
|
||||
|
||||
t.Run("Should register usage metrics func", func(t *testing.T) {
|
||||
bus.AddHandler(func(q *models.GetAllAlertsQuery) error {
|
||||
bus.AddHandlerCtx(func(ctx context.Context, q *models.GetAllAlertsQuery) error {
|
||||
settings, err := simplejson.NewJson([]byte(`{"conditions": [{"query": { "datasourceId": 1}}]}`))
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -62,7 +62,7 @@ func TestEngineProcessJob(t *testing.T) {
|
||||
return nil
|
||||
})
|
||||
|
||||
bus.AddHandler(func(q *models.GetDataSourceQuery) error {
|
||||
bus.AddHandlerCtx(func(ctx context.Context, q *models.GetDataSourceQuery) error {
|
||||
q.Result = &models.DataSource{Id: 1, Type: models.DS_PROMETHEUS}
|
||||
return nil
|
||||
})
|
||||
|
||||
@@ -24,12 +24,12 @@ func TestAlertRuleExtraction(t *testing.T) {
|
||||
influxDBDs := &models.DataSource{Id: 16, OrgId: 1, Name: "InfluxDB", Uid: "InfluxDB-uid"}
|
||||
prom := &models.DataSource{Id: 17, OrgId: 1, Name: "Prometheus", Uid: "Prometheus-uid"}
|
||||
|
||||
bus.AddHandler("test", func(query *models.GetDefaultDataSourceQuery) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, query *models.GetDefaultDataSourceQuery) error {
|
||||
query.Result = defaultDs
|
||||
return nil
|
||||
})
|
||||
|
||||
bus.AddHandler("test", func(query *models.GetDataSourceQuery) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, query *models.GetDataSourceQuery) error {
|
||||
if query.Name == defaultDs.Name || query.Uid == defaultDs.Uid {
|
||||
query.Result = defaultDs
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ func TestInitContextWithAuthProxy_CachedInvalidUserID(t *testing.T) {
|
||||
|
||||
// XXX: These handlers have to be injected AFTER calling getContextHandler, since the latter
|
||||
// creates a SQLStore which installs its own handlers.
|
||||
upsertHandler := func(cmd *models.UpsertUserCommand) error {
|
||||
upsertHandler := func(ctx context.Context, cmd *models.UpsertUserCommand) error {
|
||||
require.Equal(t, name, cmd.ExternalUser.Login)
|
||||
cmd.Result = &models.User{Id: userID}
|
||||
return nil
|
||||
@@ -49,7 +49,7 @@ func TestInitContextWithAuthProxy_CachedInvalidUserID(t *testing.T) {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
bus.AddHandler("", upsertHandler)
|
||||
bus.AddHandlerCtx("", upsertHandler)
|
||||
bus.AddHandlerCtx("", getUserHandler)
|
||||
t.Cleanup(func() {
|
||||
bus.ClearBusHandlers()
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package authproxy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
@@ -130,7 +131,7 @@ func TestMiddlewareContext_ldap(t *testing.T) {
|
||||
t.Run("Logs in via LDAP", func(t *testing.T) {
|
||||
const id int64 = 42
|
||||
|
||||
bus.AddHandler("test", func(cmd *models.UpsertUserCommand) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, cmd *models.UpsertUserCommand) error {
|
||||
cmd.Result = &models.User{
|
||||
Id: id,
|
||||
}
|
||||
|
||||
@@ -283,7 +283,7 @@ func setupDeleteHandlers(t *testing.T, fakeStore *fakeDashboardStore, provisione
|
||||
}
|
||||
|
||||
result := &Result{}
|
||||
bus.AddHandler("test", func(cmd *models.DeleteDashboardCommand) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, cmd *models.DeleteDashboardCommand) error {
|
||||
require.Equal(t, cmd.Id, int64(1))
|
||||
require.Equal(t, cmd.OrgId, int64(1))
|
||||
result.deleteWasCalled = true
|
||||
|
||||
@@ -25,7 +25,7 @@ func TestFolderService(t *testing.T) {
|
||||
origNewGuardian := guardian.New
|
||||
guardian.MockDashboardGuardian(&guardian.FakeDashboardGuardian{})
|
||||
|
||||
bus.AddHandler("test", func(query *models.GetDashboardQuery) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, query *models.GetDashboardQuery) error {
|
||||
query.Result = models.NewDashboardFolder("Folder")
|
||||
return nil
|
||||
})
|
||||
@@ -85,7 +85,7 @@ func TestFolderService(t *testing.T) {
|
||||
dash := models.NewDashboardFolder("Folder")
|
||||
dash.Id = 1
|
||||
|
||||
bus.AddHandler("test", func(query *models.GetDashboardQuery) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, query *models.GetDashboardQuery) error {
|
||||
query.Result = dash
|
||||
return nil
|
||||
})
|
||||
@@ -99,12 +99,12 @@ func TestFolderService(t *testing.T) {
|
||||
return nil
|
||||
}
|
||||
|
||||
bus.AddHandler("test", func(cmd *models.SaveDashboardCommand) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, cmd *models.SaveDashboardCommand) error {
|
||||
cmd.Result = dash
|
||||
return nil
|
||||
})
|
||||
|
||||
bus.AddHandler("test", func(cmd *models.DeleteDashboardCommand) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, cmd *models.DeleteDashboardCommand) error {
|
||||
return nil
|
||||
})
|
||||
|
||||
@@ -139,7 +139,7 @@ func TestFolderService(t *testing.T) {
|
||||
dashFolder.Id = 1
|
||||
dashFolder.Uid = "uid-abc"
|
||||
|
||||
bus.AddHandler("test", func(query *models.GetDashboardQuery) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, query *models.GetDashboardQuery) error {
|
||||
query.Result = dashFolder
|
||||
return nil
|
||||
})
|
||||
|
||||
@@ -684,7 +684,7 @@ func TestGuardianGetHiddenACL(t *testing.T) {
|
||||
t.Run("Get hidden ACL tests", func(t *testing.T) {
|
||||
bus.ClearBusHandlers()
|
||||
|
||||
bus.AddHandler("test", func(query *models.GetDashboardAclInfoListQuery) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, query *models.GetDashboardAclInfoListQuery) error {
|
||||
query.Result = []*models.DashboardAclInfoDTO{
|
||||
{Inherited: false, UserId: 1, UserLogin: "user1", Permission: models.PERMISSION_EDIT},
|
||||
{Inherited: false, UserId: 2, UserLogin: "user2", Permission: models.PERMISSION_ADMIN},
|
||||
@@ -732,7 +732,7 @@ func TestGuardianGetAclWithoutDuplicates(t *testing.T) {
|
||||
t.Run("Get hidden ACL tests", func(t *testing.T) {
|
||||
t.Cleanup(bus.ClearBusHandlers)
|
||||
|
||||
bus.AddHandler("test", func(query *models.GetDashboardAclInfoListQuery) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, query *models.GetDashboardAclInfoListQuery) error {
|
||||
query.Result = []*models.DashboardAclInfoDTO{
|
||||
{Inherited: true, UserId: 3, UserLogin: "user3", Permission: models.PERMISSION_EDIT},
|
||||
{Inherited: false, UserId: 3, UserLogin: "user3", Permission: models.PERMISSION_VIEW},
|
||||
|
||||
@@ -75,7 +75,7 @@ func permissionScenario(desc string, dashboardID int64, sc *scenarioContext,
|
||||
sc.t.Run(desc, func(t *testing.T) {
|
||||
bus.ClearBusHandlers()
|
||||
|
||||
bus.AddHandler("test", func(query *models.GetDashboardAclInfoListQuery) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, query *models.GetDashboardAclInfoListQuery) error {
|
||||
if query.OrgID != sc.givenUser.OrgId {
|
||||
sc.reportFailure("Invalid organization id for GetDashboardAclInfoListQuery", sc.givenUser.OrgId, query.OrgID)
|
||||
}
|
||||
@@ -95,7 +95,7 @@ func permissionScenario(desc string, dashboardID int64, sc *scenarioContext,
|
||||
}
|
||||
}
|
||||
|
||||
bus.AddHandler("test", func(query *models.GetTeamsByUserQuery) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, query *models.GetTeamsByUserQuery) error {
|
||||
if query.OrgId != sc.givenUser.OrgId {
|
||||
sc.reportFailure("Invalid organization id for GetTeamsByUserQuery", sc.givenUser.OrgId, query.OrgId)
|
||||
}
|
||||
|
||||
@@ -20,20 +20,20 @@ func Test_syncOrgRoles_doesNotBreakWhenTryingToRemoveLastOrgAdmin(t *testing.T)
|
||||
|
||||
bus.ClearBusHandlers()
|
||||
defer bus.ClearBusHandlers()
|
||||
bus.AddHandler("test", func(q *models.GetUserOrgListQuery) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, q *models.GetUserOrgListQuery) error {
|
||||
q.Result = createUserOrgDTO()
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
bus.AddHandler("test", func(cmd *models.RemoveOrgUserCommand) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, cmd *models.RemoveOrgUserCommand) error {
|
||||
testData := remResp[0]
|
||||
remResp = remResp[1:]
|
||||
|
||||
require.Equal(t, testData.orgId, cmd.OrgId)
|
||||
return testData.response
|
||||
})
|
||||
bus.AddHandler("test", func(cmd *models.SetUsingOrgCommand) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, cmd *models.SetUsingOrgCommand) error {
|
||||
return nil
|
||||
})
|
||||
|
||||
@@ -55,20 +55,20 @@ func Test_syncOrgRoles_whenTryingToRemoveLastOrgLogsError(t *testing.T) {
|
||||
|
||||
bus.ClearBusHandlers()
|
||||
defer bus.ClearBusHandlers()
|
||||
bus.AddHandler("test", func(q *models.GetUserOrgListQuery) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, q *models.GetUserOrgListQuery) error {
|
||||
q.Result = createUserOrgDTO()
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
bus.AddHandler("test", func(cmd *models.RemoveOrgUserCommand) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, cmd *models.RemoveOrgUserCommand) error {
|
||||
testData := remResp[0]
|
||||
remResp = remResp[1:]
|
||||
|
||||
require.Equal(t, testData.orgId, cmd.OrgId)
|
||||
return testData.response
|
||||
})
|
||||
bus.AddHandler("test", func(cmd *models.SetUsingOrgCommand) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, cmd *models.SetUsingOrgCommand) error {
|
||||
return nil
|
||||
})
|
||||
|
||||
|
||||
@@ -106,7 +106,7 @@ func TestDashboardFileReader(t *testing.T) {
|
||||
setup := func() {
|
||||
bus.ClearBusHandlers()
|
||||
fakeService = mockDashboardProvisioningService()
|
||||
bus.AddHandler("test", mockGetDashboardQuery)
|
||||
bus.AddHandlerCtx("test", mockGetDashboardQuery)
|
||||
cfg = &config{
|
||||
Name: "Default",
|
||||
Type: "file",
|
||||
@@ -637,7 +637,7 @@ func (s *fakeDashboardProvisioningService) GetProvisionedDashboardDataByDashboar
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func mockGetDashboardQuery(cmd *models.GetDashboardQuery) error {
|
||||
func mockGetDashboardQuery(ctx context.Context, cmd *models.GetDashboardQuery) error {
|
||||
for _, d := range fakeService.getDashboard {
|
||||
if d.Slug == cmd.Slug {
|
||||
cmd.Result = d
|
||||
|
||||
@@ -20,7 +20,7 @@ func TestDuplicatesValidator(t *testing.T) {
|
||||
bus.ClearBusHandlers()
|
||||
fakeService = mockDashboardProvisioningService()
|
||||
|
||||
bus.AddHandler("test", mockGetDashboardQuery)
|
||||
bus.AddHandlerCtx("test", mockGetDashboardQuery)
|
||||
cfg := &config{
|
||||
Name: "Default",
|
||||
Type: "file",
|
||||
|
||||
@@ -32,11 +32,11 @@ func TestDatasourceAsConfig(t *testing.T) {
|
||||
setup := func() {
|
||||
fakeRepo = &fakeRepository{}
|
||||
bus.ClearBusHandlers()
|
||||
bus.AddHandler("test", mockDelete)
|
||||
bus.AddHandler("test", mockInsert)
|
||||
bus.AddHandler("test", mockUpdate)
|
||||
bus.AddHandler("test", mockGet)
|
||||
bus.AddHandler("test", mockGetOrg)
|
||||
bus.AddHandlerCtx("test", mockDelete)
|
||||
bus.AddHandlerCtx("test", mockInsert)
|
||||
bus.AddHandlerCtx("test", mockUpdate)
|
||||
bus.AddHandlerCtx("test", mockGet)
|
||||
bus.AddHandlerCtx("test", mockGetOrg)
|
||||
}
|
||||
|
||||
t.Run("apply default values when missing", func(t *testing.T) {
|
||||
@@ -280,22 +280,22 @@ type fakeRepository struct {
|
||||
loadAll []*models.DataSource
|
||||
}
|
||||
|
||||
func mockDelete(cmd *models.DeleteDataSourceCommand) error {
|
||||
func mockDelete(ctx context.Context, cmd *models.DeleteDataSourceCommand) error {
|
||||
fakeRepo.deleted = append(fakeRepo.deleted, cmd)
|
||||
return nil
|
||||
}
|
||||
|
||||
func mockUpdate(cmd *models.UpdateDataSourceCommand) error {
|
||||
func mockUpdate(ctx context.Context, cmd *models.UpdateDataSourceCommand) error {
|
||||
fakeRepo.updated = append(fakeRepo.updated, cmd)
|
||||
return nil
|
||||
}
|
||||
|
||||
func mockInsert(cmd *models.AddDataSourceCommand) error {
|
||||
func mockInsert(ctx context.Context, cmd *models.AddDataSourceCommand) error {
|
||||
fakeRepo.inserted = append(fakeRepo.inserted, cmd)
|
||||
return nil
|
||||
}
|
||||
|
||||
func mockGet(cmd *models.GetDataSourceQuery) error {
|
||||
func mockGet(ctx context.Context, cmd *models.GetDataSourceQuery) error {
|
||||
for _, v := range fakeRepo.loadAll {
|
||||
if cmd.Name == v.Name && cmd.OrgId == v.OrgId {
|
||||
cmd.Result = v
|
||||
@@ -306,6 +306,6 @@ func mockGet(cmd *models.GetDataSourceQuery) error {
|
||||
return models.ErrDataSourceNotFound
|
||||
}
|
||||
|
||||
func mockGetOrg(_ *models.GetOrgByIdQuery) error {
|
||||
func mockGetOrg(ctx context.Context, _ *models.GetOrgByIdQuery) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
@@ -20,7 +21,7 @@ func TestPluginProvisioner(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Should apply configurations", func(t *testing.T) {
|
||||
bus.AddHandler("test", func(query *models.GetOrgByNameQuery) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, query *models.GetOrgByNameQuery) error {
|
||||
if query.Name == "Org 4" {
|
||||
query.Result = &models.Org{Id: 4}
|
||||
}
|
||||
@@ -28,7 +29,7 @@ func TestPluginProvisioner(t *testing.T) {
|
||||
return nil
|
||||
})
|
||||
|
||||
bus.AddHandler("test", func(query *models.GetPluginSettingByIdQuery) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, query *models.GetPluginSettingByIdQuery) error {
|
||||
if query.PluginId == "test-plugin" && query.OrgId == 2 {
|
||||
query.Result = &models.PluginSetting{
|
||||
PluginVersion: "2.0.1",
|
||||
@@ -41,7 +42,7 @@ func TestPluginProvisioner(t *testing.T) {
|
||||
|
||||
sentCommands := []*models.UpdatePluginSettingCmd{}
|
||||
|
||||
bus.AddHandler("test", func(cmd *models.UpdatePluginSettingCmd) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, cmd *models.UpdatePluginSettingCmd) error {
|
||||
sentCommands = append(sentCommands, cmd)
|
||||
return nil
|
||||
})
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package teamguardian
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
@@ -30,7 +31,7 @@ func TestUpdateTeam(t *testing.T) {
|
||||
|
||||
t.Run("Given an editor and a team he isn't a member of", func(t *testing.T) {
|
||||
t.Run("Should not be able to update the team", func(t *testing.T) {
|
||||
bus.AddHandler("test", func(cmd *models.GetTeamMembersQuery) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, cmd *models.GetTeamMembersQuery) error {
|
||||
cmd.Result = []*models.TeamMemberDTO{}
|
||||
return nil
|
||||
})
|
||||
@@ -42,7 +43,7 @@ func TestUpdateTeam(t *testing.T) {
|
||||
|
||||
t.Run("Given an editor and a team he is an admin in", func(t *testing.T) {
|
||||
t.Run("Should be able to update the team", func(t *testing.T) {
|
||||
bus.AddHandler("test", func(cmd *models.GetTeamMembersQuery) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, cmd *models.GetTeamMembersQuery) error {
|
||||
cmd.Result = []*models.TeamMemberDTO{{
|
||||
OrgId: testTeam.OrgId,
|
||||
TeamId: testTeam.Id,
|
||||
@@ -64,7 +65,7 @@ func TestUpdateTeam(t *testing.T) {
|
||||
}
|
||||
|
||||
t.Run("Shouldn't be able to update the team", func(t *testing.T) {
|
||||
bus.AddHandler("test", func(cmd *models.GetTeamMembersQuery) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, cmd *models.GetTeamMembersQuery) error {
|
||||
cmd.Result = []*models.TeamMemberDTO{{
|
||||
OrgId: testTeamOtherOrg.OrgId,
|
||||
TeamId: testTeamOtherOrg.Id,
|
||||
|
||||
Reference in New Issue
Block a user