remove bus from login (#44995)

* remove bus from login

* remove comments and format correctly

* remove the downcast
This commit is contained in:
ying-jeanne
2022-02-10 00:17:09 +08:00
committed by GitHub
parent 071ff0b399
commit 78eb06ec02
6 changed files with 80 additions and 62 deletions

View File

@@ -16,7 +16,6 @@ import (
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/api/routing"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/login"
@@ -155,7 +154,6 @@ func TestLoginErrorCookieAPIEndpoint(t *testing.T) {
func TestLoginViewRedirect(t *testing.T) {
fakeSetIndexViewData(t)
fakeViewIndex(t)
sc := setupScenarioContext(t, "/login")
cfg := setting.NewCfg()
@@ -348,13 +346,13 @@ func TestLoginPostRedirect(t *testing.T) {
return hs.LoginPost(c)
})
bus.AddHandler("grafana-auth", func(ctx context.Context, query *models.LoginUserQuery) error {
query.User = &models.User{
Id: 42,
Email: "",
}
return nil
})
user := &models.User{
Id: 42,
Email: "",
}
mockAuthenticateUserFunc(user, "", nil)
t.Cleanup(resetAuthenticateUserFunc)
redirectCases := []redirectCase{
{
@@ -441,6 +439,7 @@ func TestLoginPostRedirect(t *testing.T) {
for _, c := range redirectCases {
hs.Cfg.AppURL = c.appURL
hs.Cfg.AppSubURL = c.appSubURL
t.Run(c.desc, func(t *testing.T) {
expCookiePath := "/"
if len(hs.Cfg.AppSubURL) > 0 {
@@ -685,12 +684,8 @@ func TestLoginPostRunLokingHook(t *testing.T) {
for _, c := range testCases {
t.Run(c.desc, func(t *testing.T) {
bus.AddHandler("grafana-auth", func(ctx context.Context, query *models.LoginUserQuery) error {
query.User = c.authUser
query.AuthModule = c.authModule
return c.authErr
})
mockAuthenticateUserFunc(c.authUser, c.authModule, c.authErr)
t.Cleanup(resetAuthenticateUserFunc)
sc.m.Post(sc.url, sc.defaultHandler)
sc.fakeReqNoAssertions("POST", sc.url).exec()
@@ -736,3 +731,14 @@ func (m *mockSocialService) GetOAuthHttpClient(name string) (*http.Client, error
func (m *mockSocialService) GetConnector(string) (social.SocialConnector, error) {
return m.socialConnector, m.err
}
func mockAuthenticateUserFunc(user *models.User, authmodule string, err error) {
login.AuthenticateUserFunc = func(ctx context.Context, query *models.LoginUserQuery) error {
query.User = user
query.AuthModule = authmodule
return err
}
}
func resetAuthenticateUserFunc() {
login.AuthenticateUserFunc = login.AuthenticateUser
}