Chore: Refactor api handlers to use web.Bind (#42199)

* Chore: Refactor api handlers to use web.Bind

* fix comments

* fix comment

* trying to fix most of the tests and force routing.Wrap type check

* fix library panels tests

* fix frontend logging tests

* allow passing nil as a response to skip writing

* return nil instead of the response

* rewrite login handler function types

* remove handlerFuncCtx

* make linter happy

* remove old bindings from the libraryelements

* restore comments
This commit is contained in:
Serge Zaitsev
2021-11-29 10:18:01 +01:00
committed by GitHub
parent 9cbc872f22
commit d9cdcb550e
54 changed files with 739 additions and 299 deletions

View File

@@ -118,8 +118,9 @@ func TestLoginErrorCookieAPIEndpoint(t *testing.T) {
SecretsService: secretsService,
}
sc.defaultHandler = routing.Wrap(func(w http.ResponseWriter, c *models.ReqContext) {
sc.defaultHandler = routing.Wrap(func(c *models.ReqContext) response.Response {
hs.LoginView(c)
return response.Empty(http.StatusOK)
})
cfg.LoginCookieName = "grafana_session"
@@ -166,12 +167,13 @@ func TestLoginViewRedirect(t *testing.T) {
}
hs.Cfg.CookieSecure = true
sc.defaultHandler = routing.Wrap(func(w http.ResponseWriter, c *models.ReqContext) {
sc.defaultHandler = routing.Wrap(func(c *models.ReqContext) response.Response {
c.IsSignedIn = true
c.SignedInUser = &models.SignedInUser{
UserId: 10,
}
hs.LoginView(c)
return response.Empty(http.StatusOK)
})
redirectCases := []redirectCase{
@@ -340,7 +342,7 @@ func TestLoginPostRedirect(t *testing.T) {
}
hs.Cfg.CookieSecure = true
sc.defaultHandler = routing.Wrap(func(w http.ResponseWriter, c *models.ReqContext) response.Response {
sc.defaultHandler = routing.Wrap(func(c *models.ReqContext) response.Response {
c.Req.Header.Set("Content-Type", "application/json")
c.Req.Body = io.NopCloser(bytes.NewBufferString(`{"user":"admin","password":"admin"}`))
return hs.LoginPost(c)
@@ -504,8 +506,9 @@ func TestLoginOAuthRedirect(t *testing.T) {
SocialService: mock,
}
sc.defaultHandler = routing.Wrap(func(c *models.ReqContext) {
sc.defaultHandler = routing.Wrap(func(c *models.ReqContext) response.Response {
hs.LoginView(c)
return response.Empty(http.StatusOK)
})
setting.OAuthAutoLogin = true
@@ -529,9 +532,10 @@ func TestLoginInternal(t *testing.T) {
log: log.New("test"),
}
sc.defaultHandler = routing.Wrap(func(c *models.ReqContext) {
sc.defaultHandler = routing.Wrap(func(c *models.ReqContext) response.Response {
c.Req.URL.RawQuery = "disableAutoLogin=true"
hs.LoginView(c)
return response.Empty(http.StatusOK)
})
setting.OAuthAutoLogin = true
@@ -580,12 +584,13 @@ func setupAuthProxyLoginTest(t *testing.T, enableLoginToken bool) *scenarioConte
SocialService: &mockSocialService{},
}
sc.defaultHandler = routing.Wrap(func(c *models.ReqContext) {
sc.defaultHandler = routing.Wrap(func(c *models.ReqContext) response.Response {
c.IsSignedIn = true
c.SignedInUser = &models.SignedInUser{
UserId: 10,
}
hs.LoginView(c)
return response.Empty(http.StatusOK)
})
sc.cfg.AuthProxyEnabled = true
@@ -616,7 +621,7 @@ func TestLoginPostRunLokingHook(t *testing.T) {
HooksService: hookService,
}
sc.defaultHandler = routing.Wrap(func(w http.ResponseWriter, c *models.ReqContext) response.Response {
sc.defaultHandler = routing.Wrap(func(c *models.ReqContext) response.Response {
c.Req.Header.Set("Content-Type", "application/json")
c.Req.Body = io.NopCloser(bytes.NewBufferString(`{"user":"admin","password":"admin"}`))
x := hs.LoginPost(c)