mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Replace AddHandler with AddHandlerCtx in tests (#42585)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
@@ -40,7 +41,7 @@ func TestMiddlewareAuth(t *testing.T) {
|
||||
|
||||
middlewareScenario(t, "ReqSignIn true and NoAnonynmous true", func(
|
||||
t *testing.T, sc *scenarioContext) {
|
||||
bus.AddHandler("test", func(query *models.GetOrgByNameQuery) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, query *models.GetOrgByNameQuery) error {
|
||||
query.Result = &models.Org{Id: orgID, Name: "test"}
|
||||
return nil
|
||||
})
|
||||
@@ -53,7 +54,7 @@ func TestMiddlewareAuth(t *testing.T) {
|
||||
|
||||
middlewareScenario(t, "ReqSignIn true and request with forceLogin in query string", func(
|
||||
t *testing.T, sc *scenarioContext) {
|
||||
bus.AddHandler("test", func(query *models.GetOrgByNameQuery) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, query *models.GetOrgByNameQuery) error {
|
||||
query.Result = &models.Org{Id: orgID, Name: "test"}
|
||||
return nil
|
||||
})
|
||||
@@ -82,7 +83,7 @@ func TestMiddlewareAuth(t *testing.T) {
|
||||
|
||||
middlewareScenario(t, "ReqSignIn true and request with different org provided in query string", func(
|
||||
t *testing.T, sc *scenarioContext) {
|
||||
bus.AddHandler("test", func(query *models.GetOrgByNameQuery) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, query *models.GetOrgByNameQuery) error {
|
||||
query.Result = &models.Org{Id: orgID, Name: "test"}
|
||||
return nil
|
||||
})
|
||||
|
@@ -28,7 +28,7 @@ func TestMiddlewareBasicAuth(t *testing.T) {
|
||||
keyhash, err := util.EncodePassword("v5nAwpMafFP6znaS4urhdWDLS5511M42", "asd")
|
||||
require.NoError(t, err)
|
||||
|
||||
bus.AddHandler("test", func(query *models.GetApiKeyByNameQuery) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, query *models.GetApiKeyByNameQuery) error {
|
||||
query.Result = &models.ApiKey{OrgId: orgID, Role: models.ROLE_EDITOR, Key: keyhash}
|
||||
return nil
|
||||
})
|
||||
@@ -47,7 +47,7 @@ func TestMiddlewareBasicAuth(t *testing.T) {
|
||||
const salt = "Salt"
|
||||
const orgID int64 = 2
|
||||
|
||||
bus.AddHandler("grafana-auth", func(query *models.LoginUserQuery) error {
|
||||
bus.AddHandlerCtx("grafana-auth", func(ctx context.Context, query *models.LoginUserQuery) error {
|
||||
t.Log("Handling LoginUserQuery")
|
||||
encoded, err := util.EncodePassword(password, salt)
|
||||
if err != nil {
|
||||
@@ -80,7 +80,7 @@ func TestMiddlewareBasicAuth(t *testing.T) {
|
||||
|
||||
login.Init()
|
||||
|
||||
bus.AddHandler("user-query", func(query *models.GetUserByLoginQuery) error {
|
||||
bus.AddHandlerCtx("user-query", func(ctx context.Context, query *models.GetUserByLoginQuery) error {
|
||||
encoded, err := util.EncodePassword(password, salt)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -119,7 +119,7 @@ func TestMiddlewareBasicAuth(t *testing.T) {
|
||||
}, configure)
|
||||
|
||||
middlewareScenario(t, "Should return error if user & password do not match", func(t *testing.T, sc *scenarioContext) {
|
||||
bus.AddHandler("user-query", func(loginUserQuery *models.GetUserByLoginQuery) error {
|
||||
bus.AddHandlerCtx("user-query", func(ctx context.Context, loginUserQuery *models.GetUserByLoginQuery) error {
|
||||
return nil
|
||||
})
|
||||
|
||||
|
@@ -149,7 +149,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
keyhash, err := util.EncodePassword("v5nAwpMafFP6znaS4urhdWDLS5511M42", "asd")
|
||||
require.NoError(t, err)
|
||||
|
||||
bus.AddHandler("test", func(query *models.GetApiKeyByNameQuery) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, query *models.GetApiKeyByNameQuery) error {
|
||||
query.Result = &models.ApiKey{OrgId: orgID, Role: models.ROLE_EDITOR, Key: keyhash}
|
||||
return nil
|
||||
})
|
||||
@@ -166,7 +166,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
middlewareScenario(t, "Valid API key, but does not match DB hash", func(t *testing.T, sc *scenarioContext) {
|
||||
const keyhash = "Something_not_matching"
|
||||
|
||||
bus.AddHandler("test", func(query *models.GetApiKeyByNameQuery) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, query *models.GetApiKeyByNameQuery) error {
|
||||
query.Result = &models.ApiKey{OrgId: 12, Role: models.ROLE_EDITOR, Key: keyhash}
|
||||
return nil
|
||||
})
|
||||
@@ -183,7 +183,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
keyhash, err := util.EncodePassword("v5nAwpMafFP6znaS4urhdWDLS5511M42", "asd")
|
||||
require.NoError(t, err)
|
||||
|
||||
bus.AddHandler("test", func(query *models.GetApiKeyByNameQuery) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, query *models.GetApiKeyByNameQuery) error {
|
||||
// api key expired one second before
|
||||
expires := sc.contextHandler.GetTime().Add(-1 * time.Second).Unix()
|
||||
query.Result = &models.ApiKey{OrgId: 12, Role: models.ROLE_EDITOR, Key: keyhash,
|
||||
@@ -389,7 +389,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
middlewareScenario(t, "Should respect auto signup option", func(t *testing.T, sc *scenarioContext) {
|
||||
var actualAuthProxyAutoSignUp *bool = nil
|
||||
|
||||
bus.AddHandler("test", func(cmd *models.UpsertUserCommand) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, cmd *models.UpsertUserCommand) error {
|
||||
actualAuthProxyAutoSignUp = &cmd.SignupAllowed
|
||||
return login.ErrInvalidCredentials
|
||||
})
|
||||
@@ -416,7 +416,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
return models.ErrUserNotFound
|
||||
})
|
||||
|
||||
bus.AddHandler("test", func(cmd *models.UpsertUserCommand) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, cmd *models.UpsertUserCommand) error {
|
||||
cmd.Result = &models.User{Id: userID}
|
||||
return nil
|
||||
})
|
||||
@@ -444,7 +444,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
return models.ErrUserNotFound
|
||||
})
|
||||
|
||||
bus.AddHandler("test", func(cmd *models.UpsertUserCommand) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, cmd *models.UpsertUserCommand) error {
|
||||
cmd.Result = &models.User{Id: userID}
|
||||
storedRoleInfo = cmd.ExternalUser.OrgRoles
|
||||
return nil
|
||||
@@ -475,7 +475,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
return models.ErrUserNotFound
|
||||
})
|
||||
|
||||
bus.AddHandler("test", func(cmd *models.UpsertUserCommand) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, cmd *models.UpsertUserCommand) error {
|
||||
cmd.Result = &models.User{Id: userID}
|
||||
storedRoleInfo = cmd.ExternalUser.OrgRoles
|
||||
return nil
|
||||
@@ -508,7 +508,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
return models.ErrUserNotFound
|
||||
})
|
||||
|
||||
bus.AddHandler("test", func(cmd *models.UpsertUserCommand) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, cmd *models.UpsertUserCommand) error {
|
||||
cmd.Result = &models.User{Id: userID}
|
||||
return nil
|
||||
})
|
||||
@@ -560,7 +560,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
return nil
|
||||
})
|
||||
|
||||
bus.AddHandler("test", func(cmd *models.UpsertUserCommand) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, cmd *models.UpsertUserCommand) error {
|
||||
cmd.Result = &models.User{Id: userID}
|
||||
return nil
|
||||
})
|
||||
@@ -583,7 +583,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
return nil
|
||||
})
|
||||
|
||||
bus.AddHandler("test", func(cmd *models.UpsertUserCommand) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, cmd *models.UpsertUserCommand) error {
|
||||
cmd.Result = &models.User{Id: userID}
|
||||
return nil
|
||||
})
|
||||
@@ -608,7 +608,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
return nil
|
||||
})
|
||||
|
||||
bus.AddHandler("test", func(cmd *models.UpsertUserCommand) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, cmd *models.UpsertUserCommand) error {
|
||||
cmd.Result = &models.User{Id: userID}
|
||||
return nil
|
||||
})
|
||||
@@ -627,7 +627,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
})
|
||||
|
||||
middlewareScenario(t, "Should return 407 status code if LDAP says no", func(t *testing.T, sc *scenarioContext) {
|
||||
bus.AddHandler("LDAP", func(cmd *models.UpsertUserCommand) error {
|
||||
bus.AddHandlerCtx("LDAP", func(ctx context.Context, cmd *models.UpsertUserCommand) error {
|
||||
return errors.New("Do not add user")
|
||||
})
|
||||
|
||||
|
@@ -13,7 +13,7 @@ import (
|
||||
func TestOrgRedirectMiddleware(t *testing.T) {
|
||||
middlewareScenario(t, "when setting a correct org for the user", func(t *testing.T, sc *scenarioContext) {
|
||||
sc.withTokenSessionCookie("token")
|
||||
bus.AddHandler("test", func(query *models.SetUsingOrgCommand) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, query *models.SetUsingOrgCommand) error {
|
||||
return nil
|
||||
})
|
||||
|
||||
@@ -37,7 +37,7 @@ func TestOrgRedirectMiddleware(t *testing.T) {
|
||||
|
||||
middlewareScenario(t, "when setting an invalid org for user", func(t *testing.T, sc *scenarioContext) {
|
||||
sc.withTokenSessionCookie("token")
|
||||
bus.AddHandler("test", func(query *models.SetUsingOrgCommand) error {
|
||||
bus.AddHandlerCtx("test", func(ctx context.Context, query *models.SetUsingOrgCommand) error {
|
||||
return fmt.Errorf("")
|
||||
})
|
||||
|
||||
|
@@ -110,7 +110,7 @@ func TestMiddlewareQuota(t *testing.T) {
|
||||
return nil
|
||||
})
|
||||
|
||||
bus.AddHandler("userQuota", func(query *models.GetUserQuotaByTargetQuery) error {
|
||||
bus.AddHandlerCtx("userQuota", func(_ context.Context, query *models.GetUserQuotaByTargetQuery) error {
|
||||
query.Result = &models.UserQuotaDTO{
|
||||
Target: query.Target,
|
||||
Limit: query.Default,
|
||||
@@ -119,7 +119,7 @@ func TestMiddlewareQuota(t *testing.T) {
|
||||
return nil
|
||||
})
|
||||
|
||||
bus.AddHandler("orgQuota", func(query *models.GetOrgQuotaByTargetQuery) error {
|
||||
bus.AddHandlerCtx("orgQuota", func(_ context.Context, query *models.GetOrgQuotaByTargetQuery) error {
|
||||
query.Result = &models.OrgQuotaDTO{
|
||||
Target: query.Target,
|
||||
Limit: query.Default,
|
||||
|
Reference in New Issue
Block a user